Skip to content

Commit

Permalink
added estimation for ANY types in jdbc, to handle some ARRAY edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed May 2, 2024
1 parent 21bd24e commit 821fb23
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/src/main/java/org/polypheny/db/type/entity/PolyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,38 @@ public static Function1<PolyValue, Object> getPolyToJava( AlgDataType type, bool
}
case FILE, IMAGE, AUDIO, VIDEO -> o -> o.asBlob().asByteArray();
case DOCUMENT -> o -> o.asDocument().toJson();
case ANY -> o -> getPolyToJavaRuntime( o, arrayAsList );
default -> throw new NotImplementedException( "meta: " + type.getFullTypeString() );
};
}


private static Object getPolyToJavaRuntime( PolyValue value, boolean arrayAsList ) {
if ( value == null || value.isNull() ) {
return null;
}

return switch ( value.type ) {
case VARCHAR, CHAR, TEXT -> value.asString().value;
case INTEGER, TINYINT, SMALLINT -> value.asNumber().IntValue();
case FLOAT, REAL -> value.asNumber().FloatValue();
case DOUBLE -> value.asNumber().DoubleValue();
case BIGINT -> value.asNumber().LongValue();
case DECIMAL -> value.asNumber().BigDecimalValue();
case DATE -> value.asDate().getDaysSinceEpoch();
case TIME -> value.asTime().getMillisOfDay();
case TIMESTAMP -> value.asTimestamp().millisSinceEpoch;
case BOOLEAN -> value.asBoolean().value;
case ARRAY -> arrayAsList
? (value.asList().value.stream().map( e -> getPolyToJavaRuntime( e, true ) ).toList())
: value.asList().value.stream().map( e -> getPolyToJavaRuntime( e, false ) ).toList().toArray();
case FILE, IMAGE, AUDIO, VIDEO -> value.asBlob().asByteArray();
case DOCUMENT -> value.asDocument().toJson();
default -> throw new NotImplementedException( "meta: " + value.type );
};
}


private static AlgDataType getAndDecreaseArrayDimensionIfNecessary( ArrayType type ) {
AlgDataType component = type.getComponentType();
while ( component.getPolyType() == PolyType.ARRAY ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,33 @@ private static Array getArray( PolyValue value, AlgDataType type, ConnectionHand
while ( t.getComponentType().getPolyType() == PolyType.ARRAY ) {
t = t.getComponentType();
}

componentType = SqlType.valueOf( t.getComponentType().getPolyType().getJdbcOrdinal() );
if ( t.getComponentType().getPolyType() == PolyType.ANY ) {
componentType = estimateFittingType( value.asList().value );
}
Object[] array = (Object[]) PolyValue.wrapNullableIfNecessary( PolyValue.getPolyToJava( type, false ), type.isNullable() ).apply( value );
return connectionHandler.createArrayOf( connectionHandler.getDialect().getArrayComponentTypeString( componentType ), array );
}


public static SqlType estimateFittingType( List<PolyValue> value ) {
if ( value.isEmpty() ) {
return SqlType.NULL;
} else if ( value.stream().allMatch( PolyValue::isBoolean ) ) {
return SqlType.BOOLEAN;
} else if ( value.stream().allMatch( PolyValue::isNumber ) ) {
return SqlType.NUMERIC;
} else if ( value.stream().allMatch( PolyValue::isBinary ) ) {
return SqlType.BINARY;
} else if ( value.stream().allMatch( PolyValue::isString ) ) {
return SqlType.VARCHAR;
}

return SqlType.ANY;
}


@Override
public Enumerator<PolyValue[]> enumerator() {
if ( preparedStatementEnricher == null ) {
Expand Down

0 comments on commit 821fb23

Please sign in to comment.