Skip to content

Commit

Permalink
fixing jdbc, aggregation, list seralization, doc queries
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Oct 17, 2023
1 parent 2b4a4ad commit c6b0dcc
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.polypheny.db.adapter.java.JavaTypeFactory;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.interpreter.Row;
import org.polypheny.db.runtime.ComparableList;
import org.polypheny.db.runtime.Unit;
import org.polypheny.db.type.entity.PolyList;
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.util.BuiltInMethod;

Expand Down Expand Up @@ -117,7 +117,7 @@ public Expression field( Expression expression, int field, Type fromType, Type f
LIST {
@Override
Type javaRowClass( JavaTypeFactory typeFactory, AlgDataType type ) {
return ComparableList.class;
return PolyList.class;
}


Expand All @@ -133,9 +133,9 @@ public Expression record( Type javaRowClass, List<Expression> expressions ) {
case 0:
return Expressions.field(
null,
ComparableList.class,
"COMPARABLE_EMPTY_LIST" );
case 2:
PolyList.class,
"EMPTY_LIST" );
/*case 2:
return Expressions.convert_(
Expressions.call(
ComparableList.class,
Expand Down Expand Up @@ -174,14 +174,14 @@ public Expression record( Type javaRowClass, List<Expression> expressions ) {
null,
BuiltInMethod.LIST6.method,
expressions ),
List.class );
List.class );*/
default:
return Expressions.convert_(
Expressions.call(
ComparableList.class,
PolyList.class,
null,
BuiltInMethod.LIST_N.method,
Expressions.newArrayInit( Comparable.class, expressions ) ),
Expressions.newArrayInit( PolyValue.class, expressions ) ),
List.class );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import org.apache.calcite.linq4j.Ord;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.linq4j.tree.BlockBuilder;
Expand All @@ -42,6 +43,8 @@
import org.polypheny.db.algebra.type.AlgDataTypeField;
import org.polypheny.db.runtime.Utilities;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.PolyList;
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.util.BuiltInMethod;
import org.polypheny.db.util.CoreUtil;
import org.polypheny.db.util.Pair;
Expand Down Expand Up @@ -141,20 +144,20 @@ public Expression generateSelector( ParameterExpression parameter, List<Integer>
public Expression generateSelector( ParameterExpression parameter, List<Integer> fields, JavaRowFormat targetFormat ) {
// Optimize target format
switch ( fields.size() ) {
case 0:
default:
targetFormat = JavaRowFormat.LIST;
break;
case 1:
targetFormat = JavaRowFormat.SCALAR;
break;
}
final PhysType targetPhysType = project( fields, targetFormat );
switch ( format ) {
case SCALAR:
return Expressions.call( BuiltInMethod.IDENTITY_SELECTOR.method );
default:
return Expressions.lambda( Function1.class, targetPhysType.record( fieldReferences( parameter, fields ) ), parameter );
if ( Objects.requireNonNull( format ) == JavaRowFormat.SCALAR ) {
return Expressions.call( BuiltInMethod.IDENTITY_SELECTOR.method );
} else if ( fields.size() == 1 ) {
return Expressions.lambda( Function1.class, Expressions.arrayIndex( targetPhysType.record( fieldReferences( parameter, fields ) ), Expressions.constant( 0 ) ), parameter );
}
return Expressions.lambda( Function1.class, targetPhysType.record( fieldReferences( parameter, fields ) ), parameter );
}


Expand Down Expand Up @@ -182,12 +185,12 @@ public Expression generateSelector( final ParameterExpression parameter, final L
public Pair<Type, List<Expression>> selector( ParameterExpression parameter, List<Integer> fields, JavaRowFormat targetFormat ) {
// Optimize target format
switch ( fields.size() ) {
case 0:
default:
targetFormat = JavaRowFormat.LIST;
break;
case 1:
/*case 1:
targetFormat = JavaRowFormat.SCALAR;
break;
break;*/
}
final PhysType targetPhysType = project( fields, targetFormat );
switch ( format ) {
Expand Down Expand Up @@ -504,7 +507,7 @@ public Expression generateAccessor( List<Integer> fields ) {
Function1.class,
Expressions.field( null, BuiltInMethod.COMPARABLE_EMPTY_LIST.field ),
v1 );
/*case 1:
case 1:
int field0 = fields.get( 0 );

// new Function1<Employee, Res> {
Expand All @@ -514,74 +517,28 @@ public Expression generateAccessor( List<Integer> fields ) {
// }
Class<?> returnType = fieldClasses.get( field0 );
Expression fieldReference = Types.castIfNecessary( returnType, fieldReference( v1, field0 ) );
return Expressions.lambda( Function1.class, fieldReference, v1 );*/
return Expressions.lambda( Function1.class, fieldReference, v1 );
default:
// new Function1<Employee, List> {
// public List apply(Employee v1) {
// return Arrays.asList(
// new Object[] {v1.<fieldN>, v1.<fieldM>});
// }
// }
Expressions.FluentList<Expression> list = Expressions.list();
List<Expression> list = new ArrayList<>();
for ( int field : fields ) {
list.add( fieldReference( v1, field ) );
}
switch ( list.size() ) {
case 2:
return Expressions.lambda(
Function1.class,
Expressions.call(
List.class,
null,
BuiltInMethod.LIST2.method,
list ),
v1 );
case 3:
return Expressions.lambda(
Function1.class,
Expressions.call(
List.class,
null,
BuiltInMethod.LIST3.method,
list ),
v1 );
case 4:
return Expressions.lambda(
Function1.class,
Expressions.call(
List.class,
null,
BuiltInMethod.LIST4.method,
list ),
v1 );
case 5:
return Expressions.lambda(
Function1.class,
Expressions.call(
List.class,
null,
BuiltInMethod.LIST5.method,
list ),
v1 );
case 6:
return Expressions.lambda(
Function1.class,
Expressions.call(
List.class,
null,
BuiltInMethod.LIST6.method,
list ),
v1 );
default:
return Expressions.lambda(
Function1.class,
Expressions.call(
List.class,
null,
BuiltInMethod.LIST_N.method,
Expressions.newArrayInit( Comparable.class, list ) ),
v1 );
}
// todo dl change this later
return Expressions.lambda(
Function1.class,
Expressions.call(
PolyList.class,
null,
BuiltInMethod.LIST_N.method,
Expressions.newArrayInit( PolyValue.class, list ) ),
v1 );
//}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.calcite.linq4j.tree.MethodCallExpression;
import org.apache.calcite.linq4j.tree.ParameterExpression;
import org.apache.calcite.linq4j.tree.Types;
import org.apache.calcite.linq4j.tree.Types.ArrayType;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.core.document.DocumentUnwind;
import org.polypheny.db.algebra.enumerable.EnumUtils;
Expand Down Expand Up @@ -78,7 +79,7 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
final Type enumeratorType = Types.of( Enumerator.class, outputJavaType );
Type inputJavaType = res.physType.getJavaRowType();

ParameterExpression inputEnumerator = Expressions.parameter( Types.of( Enumerator.class, inputJavaType ), "inputEnumerator" );
ParameterExpression inputEnumerator = Expressions.parameter( Types.of( Enumerator.class, PolyValue[].class ), "inputEnumerator" );

Expression inputEnumerable = builder.append( builder.newName( "inputEnumerable" + System.nanoTime() ), res.block, false );

Expand All @@ -91,7 +92,7 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
BlockBuilder unwindBlock = new BlockBuilder();

// docQueryValue( (PolyValue) enumerable.next(), PolyList[]);
Expression fullCurrent = Expressions.convert_( Expressions.call( inputEnumerator, BuiltInMethod.ENUMERATOR_CURRENT.method ), PolyValue.class );
Expression fullCurrent = Expressions.arrayIndex( Expressions.convert_( Expressions.call( inputEnumerator, BuiltInMethod.ENUMERATOR_CURRENT.method ), PolyValue[].class ), Expressions.constant( 0 ) );
value = Expressions.call(
BuiltInMethod.MQL_QUERY_VALUE.method,
Expressions.convert_( doc_, PolyValue.class ),
Expand Down Expand Up @@ -146,7 +147,11 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {

currentBuilder.add( ifNotSet );
// return mergeDocuments( doc_, Pair(path, list.get(i_)));
currentBuilder.add( Expressions.return_( null, Expressions.call( RefactorFunctions.class, "mergeDocuments", doc_, Expressions.call( BuiltInMethod.PAIR_OF.method, Expressions.constant( path ), Expressions.call( list_, "get", i_ ) ) ) ) );
currentBuilder.add(
Expressions.return_( null,
Expressions.newArrayInit( new ArrayType( PolyValue.class ),
Expressions.call( RefactorFunctions.class, "mergeDocuments", doc_,
Expressions.call( BuiltInMethod.PAIR_OF.method, Expressions.constant( path ), Expressions.call( list_, "get", i_ ) ) ) ) ) );

BlockStatement currentBody = currentBuilder.toBlock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public void renameLogicalColumn( long id, String newFieldName ) {
}
for ( PhysicalColumn u : updates ) {
PhysicalTable table = fromAllocation( u.allocId );
List<PhysicalColumn> newColumns = new ArrayList<>( table.columns );
newColumns.remove( u );
List<PhysicalColumn> newColumns = table.columns.stream().filter( c -> c.id != id ).collect( Collectors.toList() );
newColumns.add( u );
physicals.put( table.id, table.toBuilder().columns( ImmutableList.copyOf( newColumns ) ).build() );
columns.put( Pair.of( u.allocId, u.id ), u );
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/org/polypheny/db/functions/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3086,7 +3086,7 @@ public static int currentDate( DataContext root ) {
@NonDeterministic
public static long localTimestamp( DataContext root ) {
// Cast required for JDK 1.6.
return (Long) DataContext.Variable.LOCAL_TIMESTAMP.get( root );
return DataContext.Variable.LOCAL_TIMESTAMP.get( root );
}


Expand All @@ -3101,7 +3101,7 @@ public static int localTime( DataContext root ) {

@NonDeterministic
public static TimeZone timeZone( DataContext root ) {
return (TimeZone) DataContext.Variable.TIME_ZONE.get( root );
return DataContext.Variable.TIME_ZONE.get( root );
}


Expand Down Expand Up @@ -3157,6 +3157,7 @@ public static PolyValue item( PolyValue object, PolyValue index ) {
/**
* As {@link #arrayItem} method, but allows array to be nullable.
*/
@SuppressWarnings("unused")
public static PolyValue arrayItemOptional( List<PolyValue> list, PolyNumber item ) {
if ( list == null ) {
return null;
Expand All @@ -3168,6 +3169,7 @@ public static PolyValue arrayItemOptional( List<PolyValue> list, PolyNumber item
/**
* As {@link #mapItem} method, but allows map to be nullable.
*/
@SuppressWarnings("unused")
public static PolyValue mapItemOptional( Map<PolyValue, PolyValue> map, PolyValue item ) {
if ( map == null ) {
return null;
Expand All @@ -3179,6 +3181,7 @@ public static PolyValue mapItemOptional( Map<PolyValue, PolyValue> map, PolyValu
/**
* As {@link #item} method, but allows object to be nullable.
*/
@SuppressWarnings("unused")
public static PolyValue itemOptional( Map<PolyValue, PolyValue> object, PolyValue index ) {
if ( object == null ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
public class ComparableList<T extends Comparable<T>> implements Comparable<ComparableList<T>>, List<T> {

// used by BuiltInMethod
public static final ComparableList<?> COMPARABLE_EMPTY_LIST = (ComparableList<?>) ComparableList.of();
public static final ComparableList<?> COMPARABLE_EMPTY_LIST = ComparableList.of();

@Delegate
List<T> list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public static PolyBigDecimal ofNullable( Number value, int precision, int scale
}


public static PolyBigDecimal ofNullable( BigDecimal value ) {
return value == null ? null : of( value, value.precision(), value.scale() );
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : value.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
@Value(staticConstructor = "copyOf")
public class PolyList<E extends PolyValue> extends PolyValue implements List<E> {

public static final PolyList<?> EMPTY_LIST = new PolyList<>();


@Delegate
@JsonProperty
Expand All @@ -72,7 +74,7 @@ public PolyList( E... value ) {
@Override
public @Nullable String toTypedJson() {
try {
return JSON_WRAPPER.writerFor( new TypeReference<PolyList>() {
return JSON_WRAPPER.writerFor( new TypeReference<PolyList<PolyValue>>() {
} ).writeValueAsString( this );
} catch ( JsonProcessingException e ) {
log.warn( "Error on serializing typed JSON." );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public static PolyString concat( List<PolyString> strings ) {


public static PolyString convert( Object value ) {
if ( value == null ) {
return null;
}
if ( value instanceof PolyValue ) {
if ( ((PolyValue) value).isString() ) {
return ((PolyValue) value).asString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public enum BuiltInMethod {
MAP_OF_ENTRIES( ImmutableMap.class, "copyOf", List.class ),
ARRAY( Functions.class, "array", Object[].class ),
FLAT_PRODUCT( Functions.class, "flatProduct", int[].class, boolean.class, FlatProductInputType[].class ),
LIST_N( ComparableList.class, "of", Comparable[].class ),
LIST_N( PolyList.class, "of", Comparable[].class ),
LIST2( ComparableList.class, "of", Object.class, Object.class ),
LIST3( ComparableList.class, "of", Object.class, Object.class, Object.class ),
LIST4( ComparableList.class, "of", Object.class, Object.class, Object.class, Object.class ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public List<Integer> toList() {
* breaks even (versus {@link #toList}) if you intend to scan it only once.
*/
public List<Integer> asList() {
return new AbstractList<Integer>() {
return new AbstractList<>() {
@Override
public Integer get( int index ) {
return nth( index );
Expand Down
4 changes: 3 additions & 1 deletion dbms/src/main/java/org/polypheny/db/ddl/DdlManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2845,7 +2845,9 @@ public void dropNamespace( String namespaceName, boolean ifExists, Statement sta
dropTable( table, statement );
}

dropGraph( logicalNamespace.id, ifExists, statement );
if ( catalog.getSnapshot().graph().getGraph( logicalNamespace.id ).isPresent() ) {
dropGraph( logicalNamespace.id, ifExists, statement );
}

// Drop schema
catalog.dropNamespace( logicalNamespace.id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ protected Map<Long, List<AllocationColumn>> selectPlacementOld( LogicalTable tab

public AlgNode recursiveCopy( AlgNode node ) {
List<AlgNode> inputs = new LinkedList<>();
if ( node.getInputs() != null && node.getInputs().size() > 0 ) {
if ( node.getInputs() != null && !node.getInputs().isEmpty() ) {
for ( AlgNode input : node.getInputs() ) {
inputs.add( recursiveCopy( input ) );
}
Expand Down
Loading

0 comments on commit c6b0dcc

Please sign in to comment.