Skip to content

Commit

Permalink
added "fix" for complex binary values in monetdb, added log message f…
Browse files Browse the repository at this point in the history
…or errors in resultbuilding
  • Loading branch information
datomo committed Dec 12, 2023
1 parent dfd2ff2 commit c1569db
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 9 deletions.
48 changes: 47 additions & 1 deletion core/src/main/java/org/polypheny/db/type/entity/PolyBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

package org.polypheny.db.type.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.apache.calcite.avatica.util.ByteString;
Expand All @@ -32,10 +44,14 @@
public class PolyBinary extends PolyValue {

public static final PolyBinary EMPTY = new PolyBinary( ByteString.EMPTY );
@JsonProperty()
@JsonSerialize(using = ByteStringSerializer.class)
@JsonDeserialize(using = ByteStringDeserializer.class)
public ByteString value;


public PolyBinary( ByteString value ) {
@JsonCreator
public PolyBinary( @JsonProperty("value") ByteString value ) {
super( PolyType.BINARY );
this.value = value;
}
Expand Down Expand Up @@ -117,4 +133,34 @@ public int getBitCount() {
}


public static class ByteStringSerializer extends StdSerializer<ByteString> {

public ByteStringSerializer() {
super( ByteString.class );
}


@Override
public void serialize( ByteString value, JsonGenerator gen, SerializerProvider provider ) throws IOException {
gen.writeBinary( value.getBytes() );
}

}


public static class ByteStringDeserializer extends StdDeserializer<ByteString> {

public ByteStringDeserializer() {
super( ByteString.class );
}


@Override
public ByteString deserialize( JsonParser p, DeserializationContext ctxt ) throws IOException, JacksonException {
return new ByteString( p.getBinaryValue() );
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
Expand Down Expand Up @@ -61,6 +62,8 @@
import org.polypheny.db.type.PolySerializable;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.PolyBigDecimal.PolyBigDecimalSerializerDef;
import org.polypheny.db.type.entity.PolyBinary.ByteStringDeserializer;
import org.polypheny.db.type.entity.PolyBinary.ByteStringSerializer;
import org.polypheny.db.type.entity.PolyBoolean.PolyBooleanSerializerDef;
import org.polypheny.db.type.entity.PolyDouble.PolyDoubleSerializerDef;
import org.polypheny.db.type.entity.PolyFloat.PolyFloatSerializerDef;
Expand Down Expand Up @@ -158,11 +161,13 @@ public abstract class PolyValue implements Expressible, Comparable<PolyValue>, P


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 )
.addModule( new SimpleModule()
.addSerializer( ByteString.class, new ByteStringSerializer() )
.addDeserializer( ByteString.class, new ByteStringDeserializer() ) )
.build();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private void generateGet(
source = Expressions.call( resultSet_, jdbcGetMethod( primitive ), Expressions.constant( i + 1 ) );

}
final Expression poly = getOfPolyExpression( fieldType, source, resultSet_, i );
final Expression poly = getOfPolyExpression( fieldType, source, resultSet_, i, dialect );

//source is null if an expression was already added to the builder.
if ( poly != null ) {
Expand Down Expand Up @@ -376,7 +376,7 @@ private static Expression getPreprocessArrayExpression( ParameterExpression resu
return Expressions.call(
BuiltInMethod.JDBC_DEEP_ARRAY_TO_POLY_LIST.method,
Expressions.call( resultSet_, "getArray", Expressions.constant( i + 1 ) ),
Expressions.lambda( getOfPolyExpression( componentType, argument, resultSet_, i ), argument ),
Expressions.lambda( getOfPolyExpression( componentType, argument, resultSet_, i, dialect ), argument ),
Expressions.constant( depth )
);
}
Expand All @@ -390,7 +390,7 @@ private static Expression getPreprocessArrayExpression( ParameterExpression resu
}


private static Expression getOfPolyExpression( AlgDataType fieldType, Expression source, ParameterExpression resultSet_, int i ) {
private static Expression getOfPolyExpression( AlgDataType fieldType, Expression source, ParameterExpression resultSet_, int i, SqlDialect dialect ) {
final Expression poly;
switch ( fieldType.getPolyType() ) {
case BIGINT:
Expand Down Expand Up @@ -431,7 +431,11 @@ private static Expression getOfPolyExpression( AlgDataType fieldType, Expression
poly = Expressions.call( PolyList.class, fieldType.isNullable() ? "ofNullable" : "of", source ); // todo might change
break;
case VARBINARY:
poly = Expressions.call( PolyBinary.class, fieldType.isNullable() ? "ofNullable" : "of", Expressions.convert_( source, byte[].class ) );
if ( dialect.supportsComplexBinary() ) {
poly = Expressions.call( PolyBinary.class, fieldType.isNullable() ? "ofNullable" : "of", Expressions.convert_( source, byte[].class ) );
} else {
poly = Expressions.call( PolyBinary.class, "fromTypedJson", Expressions.convert_( source, String.class ), Expressions.constant( PolyBinary.class ) );
}
break;
case FILE:
case AUDIO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@

/**
* Executes a SQL statement and returns the result as an {@link Enumerable}.
*
*/
@Slf4j
public class ResultSetEnumerable extends AbstractEnumerable<PolyValue[]> {
Expand Down Expand Up @@ -290,7 +289,11 @@ private static void setDynamicParam( PreparedStatement preparedStatement, int i,
break;
case VARBINARY:
case BINARY:
preparedStatement.setBytes( i, value.asBinary().value.getBytes() );
if ( !connectionHandler.getDialect().supportsComplexBinary() ) {
preparedStatement.setString( i, value.asBinary().toTypedJson() );
} else {
preparedStatement.setBytes( i, value.asBinary().value.getBytes() );
}
break;
case ARRAY:
if ( connectionHandler.getDialect().supportsNestedArrays() ) {
Expand All @@ -311,6 +314,9 @@ private static void setDynamicParam( PreparedStatement preparedStatement, int i,
preparedStatement.setBytes( i, value.asBlob().asByteArray() );
}
break;
case TEXT:
preparedStatement.setString( i, value.asString().value );
break;
default:
log.warn( "potentially unhandled type" );
preparedStatement.setObject( i, value );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ protected String getTypeString( PolyType type ) {
case BOOLEAN:
return "BOOLEAN";
case VARBINARY:
throw new GenericRuntimeException( "Unsupported datatype: " + type.name() );
return "VARCHAR";//throw new GenericRuntimeException( "Unsupported datatype: " + type.name() );
case TINYINT:
return "TINYINT";
case SMALLINT:
Expand All @@ -334,6 +334,8 @@ protected String getTypeString( PolyType type ) {
return "TIMESTAMP";
case ARRAY:
return "TEXT";
case TEXT:
return "TEXT";
}
throw new GenericRuntimeException( "Unknown type: " + type.name() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,11 @@ public boolean supportsIsBoolean() {
}


public boolean supportsComplexBinary() {
return true;
}


public enum IntervalParameterStrategy {CAST, MULTIPLICATION, NONE}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public SqlNode getCastSpec( AlgDataType type ) {
// We need to flag the type with a underscore to flag the type (the underscore is removed in the unparse method)
castSpec = "_TEXT";
break;
case VARBINARY:
castSpec = "_TEXT";
break;
case FILE:
case IMAGE:
case VIDEO:
Expand Down Expand Up @@ -106,6 +109,12 @@ public boolean supportsIsBoolean() {
}


@Override
public boolean supportsComplexBinary() {
return false;
}


@Override
public void unparseCall( SqlWriter writer, SqlCall call, int leftPrec, int rightPrec ) {
switch ( call.getKind() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public static long getNamespaceIdOrDefault( String namespace ) {

for ( ExecutedContext executedContext : executedContexts ) {
if ( executedContext.getException().isPresent() ) {
log.warn( "Caught exception", executedContext.getException().get() );
return List.of( buildErrorResult( transaction, executedContext, executedContext.getException().get() ).build() );
}

Expand Down

0 comments on commit c1569db

Please sign in to comment.