Skip to content

Commit

Permalink
fixing aggregates and calc algnodes to always work with typed array e…
Browse files Browse the repository at this point in the history
…numerables
  • Loading branch information
datomo committed Oct 6, 2023
1 parent 806747a commit 65dafc3
Show file tree
Hide file tree
Showing 28 changed files with 190 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public int increaseContext() {
public ClassDeclaration implementRoot( EnumerableAlg rootAlg, EnumerableAlg.Prefer prefer ) {
EnumerableAlg.Result result = rootAlg.implement( this, prefer );
if ( Objects.requireNonNull( prefer ) == Prefer.ARRAY ) {
if ( result.physType.getFormat() == JavaRowFormat.ARRAY && rootAlg.getRowType().getFieldCount() == 1 ) {
if ( false && result.physType.getFormat() == JavaRowFormat.ARRAY /*&& rootAlg.getRowType().getFieldCount() == 1 dl we want this*/ ) {
BlockBuilder bb = new BlockBuilder();
Expression e = null;
for ( Statement statement : result.block.statements ) {
Expand Down Expand Up @@ -423,7 +423,7 @@ public RexToLixTranslator.InputGetter getCorrelVariableGetter( String name ) {


public EnumerableAlg.Result result( PhysType physType, BlockStatement block ) {
return new EnumerableAlg.Result( block, physType, ((PhysTypeImpl) physType).format );
return new EnumerableAlg.Result( block, physType, JavaRowFormat.ARRAY );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {

final Result result = implementor.visitChild( this, 0, child, pref );

final PhysType physType = PhysTypeImpl.of( typeFactory, getRowType(), pref.prefer( result.format ) );
final PhysType physType = PhysTypeImpl.of( typeFactory, getRowType(), JavaRowFormat.ARRAY );

// final Enumerable<Employee> inputEnumerable = <<child adapter>>;
// return new Enumerable<IntString>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ Type javaFieldClass( JavaTypeFactory typeFactory, AlgDataType type, int index )
@Override
public Expression record( Type javaRowClass, List<Expression> expressions ) {
assert expressions.size() == 1;

return Expressions.newArrayInit( PolyValue.class, expressions );
/*if ( !TypeUtils.isAssignable( javaRowClass, PolyValue.class ) ) {
return Expressions.call( PolyLong.class, "of", expressions.get( 0 ) );
}*/

return expressions.get( 0 );
//return expressions.get( 0 );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static PhysType of( JavaTypeFactory typeFactory, AlgDataType rowType, Jav


public static PhysType of( JavaTypeFactory typeFactory, AlgDataType rowType, JavaRowFormat format, boolean optimize ) {
if ( optimize ) {
if ( false && optimize ) { // we always want arrays
format = format.optimize( rowType );
}
final Type javaRowClass = JavaRowFormat.ARRAY.javaRowClass( typeFactory, rowType );//format.javaRowClass( typeFactory, rowType );
Expand Down Expand Up @@ -504,7 +504,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,7 +514,7 @@ 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private static void nodePropertiesTableInsert( DataContext context, List<Functio
if ( !node.properties.isEmpty() ) {
context.addParameterValues( 0, idType, Collections.nCopies( node.properties.size(), node.id ) );
context.addParameterValues( 1, labelType, new ArrayList<>( node.properties.keySet() ) );
context.addParameterValues( 2, valueType, new ArrayList<>( node.properties.values().stream().map( e -> PolyString.of( e.toJsonOrNull() ) ).collect( Collectors.toList() ) ) );
context.addParameterValues( 2, valueType, new ArrayList<>( node.properties.values().stream().map( e -> PolyString.of( e.toTypedJson() ) ).collect( Collectors.toList() ) ) );
drainInserts( enumerables.get( i ), node.properties.size() );
context.resetParameterValues();
}
Expand Down
4 changes: 2 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 @@ -3195,7 +3195,7 @@ public static List<?> reparse( PolyType innerType, Long dimension, String string
if ( stringValue == null ) {
return null;
}
return PolyValue.readJsonOrNull( stringValue, PolyList.class ).asList();
return PolyValue.fromTypedJson( stringValue, PolyList.class ).asList();
}


Expand Down Expand Up @@ -4027,7 +4027,7 @@ public String toString() {

@SuppressWarnings("unused")
public static Enumerable<PolyValue[]> singletonEnumerable( Object value ) {
return Linq4j.singletonEnumerable( new PolyValue[]{ (PolyValue) value } );
return Linq4j.singletonEnumerable( (PolyValue[]) value );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.polypheny.db.type.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonToken;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
Expand All @@ -35,6 +36,7 @@
import org.apache.calcite.linq4j.tree.Expressions;
import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.type.PolySerializable;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.category.PolyNumber;
Expand Down Expand Up @@ -85,6 +87,12 @@ public static PolyBigDecimal ofNullable( Number value, int precision, int scale
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : value.toString();
}


public static PolyBigDecimal convert( Object value ) {
if ( value instanceof PolyNumber ) {
return PolyBigDecimal.of( ((PolyNumber) value).bigDecimalValue() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.polypheny.db.type.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonToken;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
Expand All @@ -33,6 +34,7 @@
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.type.PolySerializable;
import org.polypheny.db.type.PolyType;

Expand Down Expand Up @@ -70,6 +72,12 @@ public static PolyBoolean of( boolean value ) {
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : String.valueOf( value );
}


public static PolyBoolean convert( Object value ) {
if ( value == null ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.type.entity;

import com.fasterxml.jackson.core.JsonToken;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
Expand Down Expand Up @@ -63,6 +64,12 @@ public static PolyDouble ofNullable( Number value ) {
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : String.valueOf( value );
}


@Override
public int compareTo( @NotNull PolyValue o ) {
if ( !o.isNumber() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.type.entity;

import com.fasterxml.jackson.core.JsonToken;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
Expand Down Expand Up @@ -63,6 +64,12 @@ public static PolyFloat ofNullable( Number value ) {
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : String.valueOf( value );
}


@Override
public int compareTo( @NotNull PolyValue o ) {
if ( !o.isNumber() ) {
Expand Down Expand Up @@ -177,7 +184,6 @@ public PolyFloat decode( BinaryInput in ) throws CorruptedDataException {
}



@Override
public String toString() {
return value.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.polypheny.db.type.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonToken;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
Expand All @@ -32,6 +33,7 @@
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.type.PolySerializable;
import org.polypheny.db.type.PolyType;
Expand All @@ -52,6 +54,12 @@ public PolyInteger( @JsonProperty @Deserialize("value") Integer value ) {
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : String.valueOf( value );
}


public static PolyInteger of( byte value ) {
return new PolyInteger( (int) value );
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/polypheny/db/type/entity/PolyList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.polypheny.db.type.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
Expand Down Expand Up @@ -97,6 +98,12 @@ public Expression asExpression() {
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : "[" + value.stream().map( e -> e.isString() ? e.asString().toQuotedJson() : e.toJson() ).collect( Collectors.joining( "," ) ) + "]";
}


@Override
public int compareTo( @NotNull PolyValue o ) {
if ( !isSameType( o ) ) {
Expand Down
45 changes: 44 additions & 1 deletion core/src/main/java/org/polypheny/db/type/entity/PolyLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package org.polypheny.db.type.entity;

import com.fasterxml.jackson.core.JsonToken;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.CompatibilityLevel;
import io.activej.serializer.CorruptedDataException;
import io.activej.serializer.SimpleSerializerDef;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Objects;
Expand All @@ -25,6 +32,7 @@
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.type.PolySerializable;
import org.polypheny.db.type.PolyType;
Expand Down Expand Up @@ -77,6 +85,12 @@ public static PolyLong from( PolyValue value ) {
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : String.valueOf( value );
}


@Override
public int compareTo( @NotNull PolyValue o ) {
if ( !o.isNumber() ) {
Expand Down Expand Up @@ -196,7 +210,6 @@ public static PolyLong convert( PolyValue value ) {
}



@Override
public int hashCode() {
return Objects.hash( super.hashCode(), value );
Expand All @@ -208,4 +221,34 @@ public String toString() {
return value.toString();
}


public static class PolyLongSerializerDef extends SimpleSerializerDef<PolyLong> {

@Override
protected BinarySerializer<PolyLong> createSerializer( int version, CompatibilityLevel compatibilityLevel ) {
return new BinarySerializer<>() {
@Override
public void encode( BinaryOutput out, PolyLong item ) {
if ( item.value == null ) {
out.writeBoolean( true );
} else {
out.writeBoolean( false );
out.writeLong( item.value );
}
}


@Override
public PolyLong decode( BinaryInput in ) throws CorruptedDataException {
boolean isNull = in.readBoolean();
if ( !isNull ) {
return null;
}
return PolyLong.of( in.readLong() );
}
};
}

}

}
7 changes: 7 additions & 0 deletions core/src/main/java/org/polypheny/db/type/entity/PolyNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.type.entity;

import com.fasterxml.jackson.core.JsonToken;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
import io.activej.serializer.BinarySerializer;
Expand Down Expand Up @@ -52,6 +53,12 @@ public PolyNull() {
}


@Override
public @Nullable String toJson() {
return JsonToken.VALUE_NULL.asString();
}


@Override
public int compareTo( @NotNull PolyValue o ) {
return o.isNull() ? 0 : -1;
Expand Down
13 changes: 12 additions & 1 deletion core/src/main/java/org/polypheny/db/type/entity/PolyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.polypheny.db.type.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonToken;
import com.google.common.base.Charsets;
import io.activej.serializer.BinaryInput;
import io.activej.serializer.BinaryOutput;
Expand Down Expand Up @@ -77,6 +78,17 @@ public static PolyString ofNullable( String value ) {
}


@Override
public @Nullable String toJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : value;
}


public @Nullable String toQuotedJson() {
return value == null ? JsonToken.VALUE_NULL.asString() : "\"" + value + "\"";
}


public static PolyString concat( List<PolyString> strings ) {
return PolyString.of( strings.stream().map( s -> s.value ).collect( Collectors.joining() ) );
}
Expand Down Expand Up @@ -184,7 +196,6 @@ public PolyString decode( BinaryInput in ) throws CorruptedDataException {
}



@Override
public String toString() {
return value;
Expand Down
Loading

0 comments on commit 65dafc3

Please sign in to comment.