Skip to content

Commit

Permalink
Update Protobuf files, adapt how PolyDate is serialized, remove unuse…
Browse files Browse the repository at this point in the history
…d code
  • Loading branch information
gartens committed Mar 19, 2024
1 parent 541f49d commit e08d79c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.sql.Types;
import java.util.Map;
import org.polypheny.db.protointerface.proto.ProtoPolyType;
import org.polypheny.db.protointerface.proto.ProtoValue;
import org.polypheny.db.protointerface.proto.ProtoPolyType;
import org.polypheny.db.protointerface.proto.ProtoValue.ValueCase;
import org.polypheny.jdbc.jdbctypes.ExtraPolyTypes;

Expand Down Expand Up @@ -73,47 +71,6 @@ public class ProtoToJdbcTypeMap {
.build();


private static final Map<ValueCase, Integer> PROTO_VALUE_TO_JDBC =
ImmutableMap.<ValueCase, Integer>builder()
.put( ValueCase.BOOLEAN, Types.BOOLEAN )
.put( ValueCase.INTEGER, Types.INTEGER )
.put( ValueCase.LONG, Types.BIGINT )
.put( ValueCase.BIG_DECIMAL, Types.DECIMAL )
.put( ValueCase.FLOAT, Types.FLOAT )
.put( ValueCase.DOUBLE, Types.DOUBLE )
.put( ValueCase.DATE, Types.DATE )
.put( ValueCase.TIME, Types.TIME )
// TODO .put( ProtoPolyType.TIME_WITH_LOCAL_TIME_ZONE, Types.TIMESTAMP_WITH_TIMEZONE )
.put( ValueCase.TIMESTAMP, Types.TIMESTAMP )
// TODO .put( ProtoPolyType.TIMESTAMP_WITH_LOCAL_TIME_ZONE, Types.TIMESTAMP_WITH_TIMEZONE )
.put( ValueCase.INTERVAL, Types.OTHER )
.put( ValueCase.STRING, Types.VARCHAR )
.put( ValueCase.BINARY, Types.BINARY )
.put( ValueCase.NULL, Types.NULL )
.put( ValueCase.LIST, Types.ARRAY )
.put( ValueCase.MAP, Types.OTHER )
.put( ValueCase.DOCUMENT, Types.STRUCT )
.put( ValueCase.GRAPH, Types.JAVA_OBJECT )
.put( ValueCase.NODE, Types.JAVA_OBJECT )
.put( ValueCase.EDGE, Types.JAVA_OBJECT )
.put( ValueCase.PATH, Types.JAVA_OBJECT )
.put( ValueCase.FILE, Types.BINARY )
// TODO .put( ValueCase.DISTINCT, Types.DISTINCT )
// TODO .put( ValueCase.STRUCTURED, Types.STRUCT )
// TODO .put( ValueCase.OTHER, Types.OTHER )
// TODO .put( ProtoPolyType.CURSOR, Types.REF_CURSOR )
// TODO .put( ValueCase.COLUMN_LIST, Types.OTHER + 2 )
// TODO .put( ValueCase.DYNAMIC_STAR, Types.JAVA_OBJECT )
// TODO .put( ProtoPolyType.GEOMETRY, ExtraPolyTypes.GEOMETRY )
// TODO .put( ProtoPolyType.SYMBOL, Types.OTHER )
// TODO .put( ProtoPolyType.JSON, Types.VARCHAR )
// TODO .put( ProtoPolyType.MULTISET, Types.ARRAY )
// TODO .put( ProtoPolyType.ANY, Types.JAVA_OBJECT )
// TODO .put( ProtoPolyType.USER_DEFINED_TYPE, Types.OTHER )
// TODO .put( ProtoPolyType.ROW, Types.ROWID )
.build();


public static int getJdbcTypeFromProto( ProtoPolyType ProtoPolyType ) {
Integer jdbcType = PROTO_TYPE_TO_JDBC.get( ProtoPolyType );
if ( jdbcType == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

public class ProtoValueDeserializer {

private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;


public static TypedValue deserializeToTypedValue( ProtoValue value ) {
// TODO Java 17: Convert to switch expression
try {
Expand All @@ -31,7 +34,7 @@ public static TypedValue deserializeToTypedValue( ProtoValue value ) {
case BINARY:
return TypedValue.fromObject( value.getBinary().getBinary(), Types.BINARY );
case DATE:
return TypedValue.fromDate( new Date( value.getDate().getDate() ) );
return TypedValue.fromDate( new Date( value.getDate().getDate() * MILLISECONDS_PER_DAY ) );
case DOUBLE:
return TypedValue.fromDouble( value.getDouble().getDouble() );
case FLOAT:
Expand All @@ -50,14 +53,8 @@ public static TypedValue deserializeToTypedValue( ProtoValue value ) {
case LIST:
return TypedValue.fromArray( getArray( value ) );
case INTERVAL:
case USER_DEFINED_TYPE:
case MAP:
case DOCUMENT:
case NODE:
case EDGE:
case PATH:
case GRAPH:
case ROW_ID:
default:
throw new RuntimeException( "Cannot deserialize ProtoValue of case " + value.getValueCase() );
}
Expand Down
127 changes: 1 addition & 126 deletions src/main/java/org/polypheny/jdbc/nativetypes/PolyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import java.math.BigInteger;
import java.math.MathContext;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;
import org.polypheny.db.protointerface.proto.ProtoPolyType;
import org.polypheny.jdbc.ProtoInterfaceErrors;
Expand All @@ -32,24 +29,16 @@
import org.polypheny.jdbc.nativetypes.category.PolyNumber;
import org.polypheny.jdbc.nativetypes.category.PolyTemporal;
import org.polypheny.jdbc.nativetypes.document.PolyDocument;
import org.polypheny.jdbc.nativetypes.graph.PolyDictionary;
import org.polypheny.jdbc.nativetypes.graph.PolyEdge;
import org.polypheny.jdbc.nativetypes.graph.PolyEdge.EdgeDirection;
import org.polypheny.jdbc.nativetypes.graph.PolyGraph;
import org.polypheny.jdbc.nativetypes.graph.PolyNode;
import org.polypheny.jdbc.nativetypes.relational.PolyMap;
import org.polypheny.db.protointerface.proto.ProtoBigDecimal;
import org.polypheny.db.protointerface.proto.ProtoDocument;
import org.polypheny.db.protointerface.proto.ProtoEdge;
import org.polypheny.db.protointerface.proto.ProtoEntry;
import org.polypheny.db.protointerface.proto.ProtoGraph;
import org.polypheny.db.protointerface.proto.ProtoGraphPropertyHolder;
import org.polypheny.db.protointerface.proto.ProtoList;
import org.polypheny.db.protointerface.proto.ProtoMap;
import org.polypheny.db.protointerface.proto.ProtoNode;
import org.polypheny.db.protointerface.proto.ProtoUserDefinedType;
import org.polypheny.db.protointerface.proto.ProtoValue;
import org.polypheny.db.protointerface.proto.ProtoPolyType;
import org.polypheny.db.protointerface.proto.ProtoValue.ValueCase;

public abstract class PolyValue implements Comparable<PolyValue> {
Expand Down Expand Up @@ -417,8 +406,6 @@ public PolyUserDefinedValue asUserDefinedValue() throws ProtoInterfaceServiceExc

public static PolyValue fromProto( ProtoValue protoValue ) {
switch ( protoValue.getValueCase() ) {
case ROW_ID:
throw new RuntimeException( "Should never be thrown." );
case BOOLEAN:
return new PolyBoolean( protoValue.getBoolean().getBoolean() );
case INTEGER:
Expand All @@ -436,7 +423,7 @@ public static PolyValue fromProto( ProtoValue protoValue ) {
case TIME:
return new PolyTime( protoValue.getTime().getTime() );
case TIMESTAMP:
return new PolyTimeStamp(protoValue.getTimestamp().getTimestamp());
return new PolyTimeStamp( protoValue.getTimestamp().getTimestamp() );
case INTERVAL:
BigDecimal value = deserializeToBigDecimal( protoValue.getInterval().getValue() );
return new PolyInterval( value, ProtoPolyType.UNSPECIFIED ); // TODO: Fix type
Expand All @@ -452,123 +439,11 @@ public static PolyValue fromProto( ProtoValue protoValue ) {
return deserializeToPolyMap( protoValue.getMap() );
case DOCUMENT:
return deserializeToPolyDocument( protoValue.getDocument() );
case GRAPH:
return deserializeToPolyGraph( protoValue.getGraph() );
case NODE:
return deserializeToPolyNode( protoValue.getNode() );
case EDGE:
return deserializeToPolyEdge( protoValue.getEdge() );
case PATH:
throw new NotImplementedException( "Conversion from path with local timezone not yet implemented." );
case FILE:
return new PolyBinary( protoValue.getBinary().getBinary().toByteArray(), ProtoPolyType.FILE ); // TODO: Fix type
case USER_DEFINED_TYPE:
return deserializeToPolyUserDefinedType( protoValue.getUserDefinedType() );
}
throw new RuntimeException( "Should never be thrown." );
}


private static PolyValue deserializeToPolyUserDefinedType( ProtoUserDefinedType userDefinedType ) {
Map<String, PolyValue> values = userDefinedType.getValueMap().entrySet().stream()
.collect( Collectors.toMap( Entry::getKey, e -> PolyValue.fromProto( e.getValue() ), ( key1, key2 ) -> key1 )
);
return new PolyUserDefinedValue( userDefinedType.getTemplateMap(), values );
}


private static PolyValue deserializeToPolyEdge( ProtoEdge edge ) {
return new PolyEdge(
new PolyString( edge.getGraphPropertyHolder().getId().getString() ),
new PolyDictionary( deserializeToPolyMap( edge.getGraphPropertyHolder().getProperties() ) ),
getLabels( edge.getGraphPropertyHolder() ),
new PolyString( edge.getSource().getString() ),
new PolyString( edge.getTarget().getString() ),
EdgeDirection.valueOf( edge.getEdgeDirection().name() ),
new PolyString( edge.getGraphPropertyHolder().getVariableName().getString() )
);
}


private static List<PolyString> getLabels( ProtoGraphPropertyHolder propertyHolder ) {
return propertyHolder.getLabels().getValuesList().stream()
.map( p -> p.getString().getString() )
.map( PolyString::new )
.collect( Collectors.toList() );
}


private static PolyValue deserializeToPolyNode( ProtoNode node ) {
return new PolyNode(
new PolyString( node.getGraphPropertyHolder().getId().getString() ),
new PolyDictionary( deserializeToPolyMap( node.getGraphPropertyHolder().getProperties() ) ),
getLabels( node.getGraphPropertyHolder() ),
new PolyString( node.getGraphPropertyHolder().getVariableName().getString() )
);
}


private static PolyValue deserializeToPolyGraph( ProtoGraph graph ) {
Map<PolyString, PolyNode> nodes = getNodeMap( graph.getNodes() );
Map<PolyString, PolyEdge> edges = getEdgeMap( graph.getEdges() );
return new PolyGraph(
new PolyString( graph.getId().getString() ),
nodes,
edges
);
}


private static Map<PolyString, PolyEdge> getEdgeMap( ProtoMap edges ) {
return edges.getEntriesList().stream()
.filter( e -> e.getKey().getValueCase() == ValueCase.STRING )
.filter( e -> e.getValue().getValueCase() == ValueCase.EDGE )
.collect( Collectors.toMap(
e -> {
try {
return PolyValue.fromProto( e.getKey() ).asString();
} catch ( ProtoInterfaceServiceException ex ) {
throw new RuntimeException( "Should never be thrown." );
}
},
e -> {
try {
return PolyValue.fromProto( e.getValue() ).asEdge();
} catch ( ProtoInterfaceServiceException ex ) {
throw new RuntimeException( "Should never be thrown." );
}
},
( key1, key2 ) -> key1
)
);
}


private static Map<PolyString, PolyNode> getNodeMap( ProtoMap nodes ) {
return nodes.getEntriesList().stream()
.filter( e -> e.getKey().getValueCase() == ValueCase.STRING )
.filter( e -> e.getValue().getValueCase() == ValueCase.NODE )
.collect( Collectors.toMap(
e -> {
try {
return PolyValue.fromProto( e.getKey() ).asString();
} catch ( ProtoInterfaceServiceException ex ) {
throw new RuntimeException( "Should never be thrown." );
}
},
e -> {
try {
return PolyValue.fromProto( e.getValue() ).asNode();
} catch ( ProtoInterfaceServiceException ex ) {
throw new RuntimeException( "Should never be thrown." );
}
},
( key1, key2 ) -> key1
)
);
}


public static PolyDocument deserializeToPolyDocument( ProtoDocument document ) {
return new PolyDocument( deserializeToPolyMap( document.getEntriesList() ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
import org.polypheny.db.protointerface.proto.ProtoList;
import org.polypheny.db.protointerface.proto.ProtoLong;
import org.polypheny.db.protointerface.proto.ProtoNull;
import org.polypheny.db.protointerface.proto.ProtoRowId;
import org.polypheny.db.protointerface.proto.ProtoString;
import org.polypheny.db.protointerface.proto.ProtoTime;
import org.polypheny.db.protointerface.proto.ProtoTimestamp;
import org.polypheny.db.protointerface.proto.ProtoValue;
import org.polypheny.db.protointerface.proto.ProtoPolyType;
import org.polypheny.jdbc.jdbctypes.TypedValue;
import org.polypheny.jdbc.properties.DriverProperties;

public class ProtoValueSerializer {

private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;


public static Map<String, ProtoValue> serializeParameterMap( Map<String, TypedValue> parameters ) {

return null;
Expand Down Expand Up @@ -140,12 +141,13 @@ public static ProtoValue serialize( TypedValue typedValue ) throws SQLException


private static ProtoValue serializeAsProtoRowId( TypedValue typedValue ) throws SQLException {
ProtoRowId protoRowId = ProtoRowId.newBuilder()
.setRowId( typedValue.asRowId().toString() )
.build();
return ProtoValue.newBuilder()
.setRowId( protoRowId )
.build();
throw new RuntimeException( "RowID serialization is not implemented" );
// ProtoRowId protoRowId = ProtoRowId.newBuilder()
// .setRowId( typedValue.asRowId().toString() )
// .build();
// return ProtoValue.newBuilder()
// .setRowId( protoRowId )
// .build();
}


Expand All @@ -163,11 +165,6 @@ private static ProtoValue serializeAsProtoList( TypedValue typedValue ) throws S
}


private static ProtoPolyType getType( TypedValue typedValue ) {
return JdbcToProtoTypeMap.getTypeOf( typedValue );
}


private static ProtoValue serializeAsProtoDouble( TypedValue typedValue ) throws SQLException {
ProtoDouble protoDouble = ProtoDouble.newBuilder()
.setDouble( typedValue.asDouble() )
Expand Down Expand Up @@ -213,9 +210,9 @@ private static ProtoValue serializeAsProtoBigDecimal( TypedValue typedValue ) th

private static ProtoValue serializeAsProtoDate( TypedValue typedValue ) throws SQLException {
long milliseconds = typedValue.asDate().getTime();
milliseconds += DriverProperties.getDEFAULT_TIMEZONE().getOffset(milliseconds);
milliseconds += DriverProperties.getDEFAULT_TIMEZONE().getOffset( milliseconds );
ProtoDate protoDate = ProtoDate.newBuilder()
.setDate( milliseconds )
.setDate( milliseconds / MILLISECONDS_PER_DAY )
.build();
return ProtoValue.newBuilder()
.setDate( protoDate )
Expand All @@ -235,7 +232,7 @@ private static ProtoValue serializeAsProtoString( TypedValue typedValue ) throws

private static ProtoValue serializeAsProtoTime( TypedValue typedValue ) throws SQLException {
long ofDay = typedValue.asTime().getTime();
ofDay += DriverProperties.getDEFAULT_TIMEZONE().getOffset(ofDay);
ofDay += DriverProperties.getDEFAULT_TIMEZONE().getOffset( ofDay );
ProtoTime protoTime = ProtoTime.newBuilder()
.setTime( (int) ofDay )
.build();
Expand All @@ -247,7 +244,7 @@ private static ProtoValue serializeAsProtoTime( TypedValue typedValue ) throws S

private static ProtoValue serializeAsTimestamp( TypedValue typedValue ) throws SQLException {
long milliseconds = typedValue.asTimestamp().getTime();
milliseconds += DriverProperties.getDEFAULT_TIMEZONE().getOffset(milliseconds);
milliseconds += DriverProperties.getDEFAULT_TIMEZONE().getOffset( milliseconds );
ProtoTimestamp protoTimestamp = ProtoTimestamp.newBuilder()
.setTimestamp( milliseconds )
.build();
Expand Down
Loading

0 comments on commit e08d79c

Please sign in to comment.