Skip to content

Commit

Permalink
Not much progress on identifiers so far
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Dec 13, 2024
1 parent 6e5a82d commit 1c9ae77
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ public void rewriteAlg( LogicalRelIdentifierInjection alg ) {
LogicalRelIdentifierInjection newAlg =
LogicalRelIdentifierInjection.create(
alg.getEntity(),
getNewForOldRel( alg.getInput() ),
alg.getRowType());
getNewForOldRel( alg.getLeft() ),
getNewForOldRel( alg.getRight() ));
setNewForOldAlg( alg, newAlg );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,48 @@

package org.polypheny.db.algebra.enumerable;

import java.lang.reflect.Modifier;
import java.util.List;
import lombok.Getter;
import org.apache.calcite.linq4j.Enumerable;
import org.apache.calcite.linq4j.function.Function;
import org.apache.calcite.linq4j.function.Function0;
import org.apache.calcite.linq4j.tree.BlockBuilder;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
import org.polypheny.db.algebra.AlgCollationTraitDef;
import org.polypheny.db.algebra.AlgDistributionTraitDef;
import org.apache.calcite.linq4j.tree.FunctionExpression;
import org.apache.calcite.linq4j.tree.MethodCallExpression;
import org.apache.calcite.linq4j.tree.ParameterExpression;
import org.apache.calcite.linq4j.tree.Types;
import org.polypheny.db.adapter.DataContext;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.SingleAlg;
import org.polypheny.db.algebra.metadata.AlgMdCollation;
import org.polypheny.db.algebra.metadata.AlgMdDistribution;
import org.polypheny.db.algebra.core.common.Streamer;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.plan.AlgCluster;
import org.polypheny.db.plan.AlgOptCost;
import org.polypheny.db.plan.AlgPlanner;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.util.BuiltInMethod;

@Getter
public class EnumerableIdentifierInjection extends SingleAlg implements EnumerableAlg{
public class EnumerableIdentifierInjection extends Streamer implements EnumerableAlg {

public final Entity entity;

public EnumerableIdentifierInjection( Entity entity, AlgCluster cluster, AlgTraitSet traitSet, AlgNode input) {
super(cluster, traitSet, input);

public EnumerableIdentifierInjection( Entity entity, AlgCluster cluster, AlgTraitSet traitSet, AlgNode provider, AlgNode collector ) {
super( cluster, traitSet, provider, collector );
this.entity = entity;
this.rowType = input.getTupleType();
assert getConvention() instanceof EnumerableConvention;
assert getConvention() == input.getConvention();
}

public static EnumerableIdentifierInjection create(Entity table, AlgNode input) {
final AlgCluster cluster = input.getCluster();
final AlgMetadataQuery mq = cluster.getMetadataQuery();
final AlgTraitSet traitSet =
cluster.traitSetOf( EnumerableConvention.INSTANCE )
.replaceIfs( AlgCollationTraitDef.INSTANCE, () -> AlgMdCollation.limit( mq, input ) )
.replaceIf( AlgDistributionTraitDef.INSTANCE, () -> AlgMdDistribution.limit( mq, input ) );
return new EnumerableIdentifierInjection( table, cluster, traitSet, input );

public static EnumerableIdentifierInjection create( AlgTraitSet traitSet, Entity table, AlgNode provider, AlgNode collector ) {
final AlgCluster cluster = provider.getCluster();
return new EnumerableIdentifierInjection( table, cluster, traitSet, provider, collector );
}


@Override
public AlgOptCost computeSelfCost( AlgPlanner planner, AlgMetadataQuery mq ) {
double rowCount = mq.getTupleCount( this );
Expand All @@ -64,27 +66,30 @@ public AlgOptCost computeSelfCost( AlgPlanner planner, AlgMetadataQuery mq ) {


@Override
public String algCompareString() {
return this.getClass().getSimpleName() + "$" +
input.algCompareString() + "$" +
entity.hashCode()+ "&";
public EnumerableIdentifierInjection copy( AlgTraitSet traitSet, List<AlgNode> newInputs ) {
AlgCluster cluster = newInputs.get(0).getCluster();
return new EnumerableIdentifierInjection( entity, cluster, traitSet, newInputs.get(0), newInputs.get(1) );
}


@Override
public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
//TODO TH: this is a copy of the enumerable streamer use for testing
final BlockBuilder builder = new BlockBuilder();
final EnumerableAlg input = (EnumerableAlg) getInput();

final Result result = implementor.visitChild( this, 0, input, pref );
final PhysType physicalType = PhysTypeImpl.of( implementor.getTypeFactory(), getTupleType(), result.format() );

Expression expression = builder.append( "input", result.block() );

// ToDo: actually do something here...


builder.add( Expressions.return_( null, expression ) );
return implementor.result( physicalType, builder.toBlock() );
final Result query = implementor.visitChild( this, 0, (EnumerableAlg) getLeft(), pref );
final Result prepared = implementor.visitChild( this, 1, (EnumerableAlg) getRight(), pref );
Expression executor = builder.append( builder.newName( "executor" + System.nanoTime() ), prepared.block() );
ParameterExpression exp = Expressions.parameter( Types.of( Function0.class, Enumerable.class ), builder.newName( "executor" + System.nanoTime() ) );
FunctionExpression<Function<?>> expCall = Expressions.lambda( Expressions.block( Expressions.return_( null, executor ) ) );
builder.add( Expressions.declare( Modifier.FINAL, exp, expCall ) );
MethodCallExpression transformContext = Expressions.call(
BuiltInMethod.STREAM_RIGHT.method,
Expressions.constant( DataContext.ROOT ),
builder.append( builder.newName( "query" + System.nanoTime() ), query.block() ),
exp,
Expressions.constant( getLeft().getTupleType().getFields().stream().map( f -> f.getType().getPolyType() ).toList() ) );
builder.add( Expressions.return_( null, builder.append( "test", transformContext ) ) );
return implementor.result( prepared.physType(), builder.toBlock() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.convert.ConverterRule;
import org.polypheny.db.algebra.logical.relational.LogicalRelIdentifierInjection;
import org.polypheny.db.plan.AlgTraitSet;

/**
* Planner rule that converts a {@link LogicalRelIdentifierInjection} relational expression {@link EnumerableConvention enumerable calling convention}.
Expand All @@ -33,10 +34,9 @@ public class EnumerableIdentifierInjectionRule extends ConverterRule {
@Override
public AlgNode convert( AlgNode alg ) {
LogicalRelIdentifierInjection injection = (LogicalRelIdentifierInjection) alg;
AlgNode input = injection.getInput();
if ( !(input.getConvention() instanceof EnumerableConvention) ) {
input = convert( input, input.getTraitSet().replace( EnumerableConvention.INSTANCE ) );
}
return EnumerableIdentifierInjection.create( input.getEntity(), input );
final AlgNode provider = convert( injection.getLeft(), injection.getLeft().getTraitSet().replace( EnumerableConvention.INSTANCE ) );
final AlgNode collector = convert( injection.getRight(), injection.getRight().getTraitSet().replace( EnumerableConvention.INSTANCE ) );
final AlgTraitSet traits = alg.getTraitSet().replace( EnumerableConvention.INSTANCE );
return EnumerableIdentifierInjection.create( traits, injection.getEntity(), provider, collector );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
import lombok.Getter;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.SingleAlg;
import org.polypheny.db.algebra.core.common.Streamer;
import org.polypheny.db.algebra.core.relational.RelAlg;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.plan.AlgCluster;
import org.polypheny.db.plan.AlgOptCost;
Expand All @@ -34,31 +33,21 @@
* Relational expression that complements the results of its input expression with unique identifiers.
*/
@Getter
public class LogicalRelIdentifierInjection extends SingleAlg implements RelAlg {
public class LogicalRelIdentifierInjection extends Streamer implements RelAlg {

public final Entity entity;


protected LogicalRelIdentifierInjection( Entity entity, AlgCluster cluster, AlgTraitSet traits, AlgNode input, AlgDataType rowType ) {
super( cluster, traits, input );

protected LogicalRelIdentifierInjection( Entity entity, AlgCluster cluster, AlgTraitSet traits, AlgNode provider, AlgNode collector ) {
super( cluster, traits, provider, collector );
this.entity = entity;
this.rowType = rowType;
}


public static LogicalRelIdentifierInjection create( Entity table, final AlgNode input, AlgDataType rowType ) {
final AlgCluster cluster = input.getCluster();
final AlgTraitSet traits = input.getTraitSet();
return new LogicalRelIdentifierInjection( table, cluster, traits, input, rowType );
}


@Override
public String algCompareString() {
return this.getClass().getSimpleName() + "$" +
input.algCompareString() + "$" +
entity.hashCode()+ "&";
public static LogicalRelIdentifierInjection create( Entity entity, final AlgNode provider, AlgNode collector ) {
final AlgCluster cluster = provider.getCluster();
final AlgTraitSet traits = provider.getTraitSet();
return new LogicalRelIdentifierInjection( entity, cluster, traits, provider, collector );
}


Expand All @@ -70,14 +59,14 @@ public AlgNode unfoldView( @Nullable AlgNode parent, int index, AlgCluster clust

@Override
public AlgOptCost computeSelfCost( AlgPlanner planner, AlgMetadataQuery mq ) {
double dRows = mq.getTupleCount( getInput() );
double dRows = mq.getTupleCount( getLeft() );
return planner.getCostFactory().makeCost( dRows, 0, 0 );
}


@Override
public AlgNode copy( AlgTraitSet traitSet, List<AlgNode> inputs ) {
return new LogicalRelIdentifierInjection( entity, getCluster(), traitSet, sole( inputs ), getRowType() );
return new LogicalRelIdentifierInjection( entity, getCluster(), traitSet, getLeft(), getRight() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ public static <T> Enumerable<PolyValue[]> streamRight( final DataContext context
return Linq4j.asEnumerable( results );
}


private static AlgDataType deriveType( AlgDataTypeFactory factory, PolyType type ) {
if ( type == PolyType.CHAR ) {
return factory.createPolyType( PolyType.VARCHAR );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

package org.polypheny.db.transaction.locking;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.AlgRoot;
import org.polypheny.db.algebra.AlgShuttleImpl;
import org.polypheny.db.algebra.core.AlgFactories;
import org.polypheny.db.algebra.core.common.Modify;
import org.polypheny.db.algebra.core.common.Modify.Operation;
import org.polypheny.db.algebra.logical.common.LogicalConditionalExecute;
import org.polypheny.db.algebra.logical.common.LogicalConstraintEnforcer;
Expand Down Expand Up @@ -64,11 +63,10 @@
import org.polypheny.db.algebra.logical.relational.LogicalRelUnion;
import org.polypheny.db.algebra.logical.relational.LogicalRelValues;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.algebra.type.AlgDataTypeField;
import org.polypheny.db.rex.RexBuilder;
import org.polypheny.db.rex.RexIndexRef;
import org.polypheny.db.rex.RexLiteral;
import org.polypheny.db.rex.RexNode;
import org.polypheny.db.rex.RexUtil;
import org.polypheny.db.tools.AlgBuilder;

public class AlgTreeRewriter extends AlgShuttleImpl {

Expand Down Expand Up @@ -116,7 +114,7 @@ public AlgNode visit( LogicalRelFilter filter ) {

@Override
public AlgNode visit( LogicalRelProject project ) {
return visitChild(project, 0, project.getInput());
return visitChild( project, 0, project.getInput() );
}


Expand Down Expand Up @@ -170,30 +168,49 @@ public AlgNode visit( LogicalConditionalExecute lce ) {

@Override
public AlgNode visit( LogicalRelModify modify ) {
if (modify.getOperation() != Operation.INSERT) {
if ( modify.getOperation() != Operation.INSERT ) {
return visitChildren( modify );
}
// modify is an insert: project away current eid and add new one using injector
AlgDataType modifyInputRowType = modify.getExpectedInputRowType( 0 );

AlgBuilder algBuilder = AlgFactories.LOGICAL_BUILDER.create( modify.getCluster(), modify.getCluster().getSnapshot() );
RexBuilder rexBuilder = algBuilder.getRexBuilder();
AlgNode input = modify.getInput();

algBuilder.push( LogicalRelProject.create( LogicalRelValues.createOneRow( input.getCluster() ),
input.getTupleType()
.getFields()
.stream()
.map( f -> rexBuilder.makeDynamicParam( f.getType(), f.getIndex() ) )
.toList(),
input.getTupleType() ) );

Modify<?> prepared = LogicalRelModify.create(
modify.getEntity(),
algBuilder.build(),
modify.getOperation(),
modify.getUpdateColumns(),
null, // ToDo TH: are they always null in our case?
false ).streamed( true );

/*
List<RexNode> projects = getProjects(modifyInputRowType);
AlgDataType projectRowType = RexUtil.createStructType( modify.getCluster().getTypeFactory(), projects);
AlgNode identifierRemovingProject = LogicalRelProject.create(modify.getInput(), projects , projectRowType );
AlgNode identifierInjection = LogicalRelIdentifierInjection.create(modify.getEntity(), identifierRemovingProject, modifyInputRowType );
*/
AlgNode identifierInjection = LogicalRelIdentifierInjection.create(modify.getEntity(), modify.getInput(), modifyInputRowType );
return modify.copy(modify.getTraitSet(), Collections.singletonList( identifierInjection ) );

return LogicalRelIdentifierInjection.create( modify.getEntity(), input, prepared );
}

private List<RexNode> getProjects(AlgDataType rowType) {

private List<RexNode> getProjects( AlgDataType rowType ) {
return rowType.getFields().stream()
.filter( f -> !f.getName().equals( IdentifierUtils.IDENTIFIER_KEY ) )
.map( f -> new RexIndexRef( f.getIndex(), f.getType() ) )
.collect( Collectors.toCollection( LinkedList::new));
.collect( Collectors.toCollection( LinkedList::new ) );
}


@Override
public AlgNode visit( LogicalRelIdentifierInjection idInjection ) {
return visitChildren( idInjection );
Expand Down Expand Up @@ -319,4 +336,5 @@ public AlgNode visit( AlgNode other ) {
return visitChildren( other );

}

}

0 comments on commit 1c9ae77

Please sign in to comment.