Skip to content

Commit

Permalink
GIS #462: introduced new PolyGeomotryTypes enum to remove string valu…
Browse files Browse the repository at this point in the history
…es; added test to check serialize/deserialize process
  • Loading branch information
danylokravchenko committed Nov 1, 2023
1 parent 0695fdc commit 018e966
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 123 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/org/polypheny/db/type/PolyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ public enum PolyType {
PrecScale.NO_NO,
true,
// TODO: or should it be Types.VARCHAR because of WKT or Types.JAVA_OBJECT
Types.JAVA_OBJECT, // ExtraPolyTypes.GEOMETRY,
Types.JAVA_OBJECT,
PolyTypeFamily.GEO ),

// TODO: add all other Geo types

FILE(
PrecScale.NO_NO,
true,
Expand Down Expand Up @@ -461,6 +463,8 @@ public enum PolyType {

public static final List<PolyType> BLOB_TYPES = ImmutableList.of( FILE, AUDIO, IMAGE, VIDEO );

// TODO: add Geometry types

public static final Set<PolyType> YEAR_INTERVAL_TYPES =
Sets.immutableEnumSet(
PolyType.INTERVAL_YEAR,
Expand Down
240 changes: 126 additions & 114 deletions core/src/main/java/org/polypheny/db/type/entity/PolyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.polypheny.db.type.entity.PolyBoolean.PolyBooleanSerializerDef;
import org.polypheny.db.type.entity.PolyDouble.PolyDoubleSerializerDef;
import org.polypheny.db.type.entity.PolyFloat.PolyFloatSerializerDef;
//import org.polypheny.db.type.entity.spatial.PolyGeometry.PolyGeometrySerializerDef;
import org.polypheny.db.type.entity.spatial.PolyGeometry;
import org.polypheny.db.type.entity.spatial.PolyGeometry.PolyGeometrySerializerDef;
import org.polypheny.db.type.entity.PolyInteger.PolyIntegerSerializerDef;
Expand Down Expand Up @@ -141,6 +140,13 @@
})
public abstract class PolyValue implements Expressible, Comparable<PolyValue>, PolySerializable {

public static final ObjectMapper JSON_WRAPPER = JsonMapper.builder()
.configure( MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, true )
.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false )
.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false )
.configure( MapperFeature.USE_STATIC_TYPING, true )
.build();

@JsonIgnore
// used internally to serialize into binary format
public static BinarySerializer<PolyValue> serializer = SerializerBuilder.create( CLASS_LOADER )
Expand All @@ -163,15 +169,6 @@ public abstract class PolyValue implements Expressible, Comparable<PolyValue>, P
.build( PolyValue.class );


public static final ObjectMapper JSON_WRAPPER = JsonMapper.builder()

.configure( MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, true )
.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false )
.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false )
.configure( MapperFeature.USE_STATIC_TYPING, true )
.build();


static {
JSON_WRAPPER.setSerializationInclusion( JsonInclude.Include.NON_NULL )
.setVisibility( JSON_WRAPPER.getSerializationConfig().getDefaultVisibilityChecker()
Expand Down Expand Up @@ -235,6 +232,8 @@ public static Function1<PolyValue, Object> getPolyToJava( AlgDataType type, bool
case AUDIO:
case VIDEO:
return o -> o.asBlob().asByteArray();
case GEOMETRY:
return o -> o.asGeometry().getJtsGeometry();
default:
throw new org.apache.commons.lang3.NotImplementedException( "meta" );
}
Expand All @@ -261,17 +260,6 @@ public static Function1<PolyValue, Object> wrapNullableIfNecessary( Function1<Po
}


@Nullable
public String toTypedJson() {
try {
return JSON_WRAPPER.writeValueAsString( this );
} catch ( JsonProcessingException e ) {
log.warn( "Error on serializing typed JSON." );
return null;
}
}


@Nullable
public static <E extends PolyValue> E fromTypedJson( String value, Class<E> clazz ) {
try {
Expand All @@ -283,30 +271,6 @@ public static <E extends PolyValue> E fromTypedJson( String value, Class<E> claz
}


public String toJson() {
// fallback serializer
try {
return JSON_WRAPPER.writeValueAsString( this );
} catch ( JsonProcessingException e ) {
log.warn( "Error on deserialize JSON." );
return null;
}
}


@NotNull
public Optional<Long> getByteSize() {
if ( byteSize == null ) {
byteSize = deriveByteSize();
}
return Optional.ofNullable( byteSize );
}


@Nullable
public abstract Long deriveByteSize();


public static Expression getInitialExpression( Type type ) {
if ( PolyDefaults.DEFAULTS.get( type ) != null ) {
return PolyDefaults.DEFAULTS.get( type ).asExpression();
Expand Down Expand Up @@ -343,17 +307,15 @@ public static Class<? extends PolyValue> classFrom( PolyType polyType ) {
case BOOLEAN:
return PolyBoolean.class;
case TINYINT:
case INTEGER:
return PolyInteger.class;
case SMALLINT:
return PolyInteger.class;
case INTEGER:
return PolyInteger.class;
case BIGINT:
return PolyLong.class;
case DECIMAL:
return PolyBigDecimal.class;
case FLOAT:
return PolyFloat.class;
case REAL:
return PolyFloat.class;
case DOUBLE:
Expand Down Expand Up @@ -439,6 +401,7 @@ public static Class<? extends PolyValue> classFrom( PolyType polyType ) {
case DYNAMIC_STAR:
return PolyValue.class;
case GEOMETRY:
// TODO: should here be all Geometry classes?
return PolyGeometry.class;
case FILE:
case IMAGE:
Expand All @@ -457,6 +420,107 @@ public static PolyValue deserialize( String json ) {
}


public static PolyValue convert( PolyValue value, PolyType type ) {

switch ( type ) {
case INTEGER:
return PolyInteger.from( value );
case DOCUMENT:
// docs accept all
return value;
case BIGINT:
return PolyLong.from( value );
}
if ( type.getFamily() == value.getType().getFamily() ) {
return value;
}

throw new GenericRuntimeException( "%s does not support conversion to %s.", value, type );
}


public static PolyValue fromType( Object object, PolyType type ) {
// TODO: should GIS be here?
switch ( type ) {
case BOOLEAN:
return PolyBoolean.of( (Boolean) object );
case TINYINT:
case SMALLINT:
case INTEGER:
return PolyInteger.of( (Number) object );
case BIGINT:
return PolyLong.of( (Number) object );
case DECIMAL:
return PolyBigDecimal.of( object.toString() );
case FLOAT:
case REAL:
return PolyFloat.of( (Number) object );
case DOUBLE:
return PolyDouble.of( (Number) object );
case DATE:
if ( object instanceof Number ) {
return PolyDate.of( (Number) object );
}
throw new NotImplementedException();

case TIME:
case TIME_WITH_LOCAL_TIME_ZONE:
if ( object instanceof Number ) {
return PolyTime.of( (Number) object );
}
throw new NotImplementedException();
case TIMESTAMP:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
if ( object instanceof Timestamp ) {
return PolyTimeStamp.of( (Timestamp) object );
}
throw new NotImplementedException();
case CHAR:
case VARCHAR:
return PolyString.of( (String) object );
case BINARY:
case VARBINARY:
return PolyBinary.of( (ByteString) object );
}
throw new NotImplementedException();
}


@Nullable
public String toTypedJson() {
try {
return JSON_WRAPPER.writeValueAsString( this );
} catch ( JsonProcessingException e ) {
log.warn( "Error on serializing typed JSON." );
return null;
}
}


public String toJson() {
// fallback serializer
try {
return JSON_WRAPPER.writeValueAsString( this );
} catch ( JsonProcessingException e ) {
log.warn( "Error on deserialize JSON." );
return null;
}
}


@NotNull
public Optional<Long> getByteSize() {
if ( byteSize == null ) {
byteSize = deriveByteSize();
}
return Optional.ofNullable( byteSize );
}


@Nullable
public abstract Long deriveByteSize();


@Override
public String serialize() {
return PolySerializable.serialize( serializer, this );
Expand Down Expand Up @@ -499,7 +563,7 @@ public PolyBoolean asBoolean() {


@NotNull
private GenericRuntimeException cannotParse( PolyValue value, Class<?> clazz ) {
protected GenericRuntimeException cannotParse( PolyValue value, Class<?> clazz ) {
return new GenericRuntimeException( "Cannot parse %s to type %s", value, clazz.getSimpleName() );
}

Expand Down Expand Up @@ -816,83 +880,31 @@ public PolyBlob asBlob() {
throw cannotParse( this, PolyBlob.class );
}


public boolean isUserDefinedValue() {
return PolyType.USER_DEFINED_TYPE == type;
public boolean isGeometry() {
return type == PolyType.GEOMETRY;
}


@NotNull
public PolyUserDefinedValue asUserDefinedValue() {
if ( isUserDefinedValue() ) {
return (PolyUserDefinedValue) this;
public PolyGeometry asGeometry() {
if ( isGeometry() ) {
return (PolyGeometry) this;
}
throw cannotParse( this, PolyUserDefinedValue.class );
throw cannotParse( this, PolyGeometry.class );
}


public static PolyValue convert( PolyValue value, PolyType type ) {

switch ( type ) {
case INTEGER:
return PolyInteger.from( value );
case DOCUMENT:
// docs accept all
return value;
case BIGINT:
return PolyLong.from( value );
}
if ( type.getFamily() == value.getType().getFamily() ) {
return value;
}

throw new GenericRuntimeException( "%s does not support conversion to %s.", value, type );
public boolean isUserDefinedValue() {
return PolyType.USER_DEFINED_TYPE == type;
}


public static PolyValue fromType( Object object, PolyType type ) {
switch ( type ) {
case BOOLEAN:
return PolyBoolean.of( (Boolean) object );
case TINYINT:
case SMALLINT:
case INTEGER:
return PolyInteger.of( (Number) object );
case BIGINT:
return PolyLong.of( (Number) object );
case DECIMAL:
return PolyBigDecimal.of( object.toString() );
case FLOAT:
case REAL:
return PolyFloat.of( (Number) object );
case DOUBLE:
return PolyDouble.of( (Number) object );
case DATE:
if ( object instanceof Number ) {
return PolyDate.of( (Number) object );
}
throw new NotImplementedException();

case TIME:
case TIME_WITH_LOCAL_TIME_ZONE:
if ( object instanceof Number ) {
return PolyTime.of( (Number) object );
}
throw new NotImplementedException();
case TIMESTAMP:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
if ( object instanceof Timestamp ) {
return PolyTimeStamp.of( (Timestamp) object );
}
throw new NotImplementedException();
case CHAR:
case VARCHAR:
return PolyString.of( (String) object );
case BINARY:
case VARBINARY:
return PolyBinary.of( (ByteString) object );
@NotNull
public PolyUserDefinedValue asUserDefinedValue() {
if ( isUserDefinedValue() ) {
return (PolyUserDefinedValue) this;
}
throw new NotImplementedException();
throw cannotParse( this, PolyUserDefinedValue.class );
}


Expand Down
Loading

0 comments on commit 018e966

Please sign in to comment.