Skip to content

Commit

Permalink
Fix relational inserts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Dec 23, 2024
1 parent 1e319d5 commit 1bd929c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.algebra.enumerable;

import java.util.List;
import org.apache.calcite.linq4j.tree.BlockBuilder;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
Expand Down Expand Up @@ -44,6 +45,13 @@ public AlgOptCost computeSelfCost( AlgPlanner planner, AlgMetadataQuery mq ) {
}


@Override
public AlgNode copy( AlgTraitSet traitSet, List<AlgNode> inputs ) {
return new EnumerableRelIdentifier( inputs.get(0).getCluster(), traitSet, entity, inputs.get( 0 ) );
}



@Override
public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
final BlockBuilder builder = new BlockBuilder();
Expand All @@ -52,7 +60,7 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
final PhysType physType = result.physType();

Expression input_ = builder.append( "input", result.block() );
Expression entity_ = Expressions.constant( entity );
Expression entity_ = Expressions.constant(entity.getId());
Expression identification_ = builder.append( "identification", Expressions.call( BuiltInMethod.ADD_REL_IDENTIFIERS.method, input_, entity_ ) );

builder.add( Expressions.return_( null, identification_ ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class EnumerableRelIdentifierRule extends ConverterRule {
public AlgNode convert( AlgNode alg ) {
final LogicalRelIdentifier identifier = (LogicalRelIdentifier) alg;
final AlgTraitSet traits = identifier.getTraitSet().replace( EnumerableConvention.INSTANCE );
final AlgNode input = identifier.getInput();
final AlgNode input = convert(identifier.getInput(), identifier.getInput().getTraitSet().replace( EnumerableConvention.INSTANCE ));
return new EnumerableRelIdentifier( identifier.getCluster(), traits, identifier.getEntity(), input );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.algebra.enumerable.document;

import java.util.List;
import org.apache.calcite.linq4j.tree.BlockBuilder;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
Expand All @@ -24,6 +25,7 @@
import org.polypheny.db.algebra.enumerable.EnumerableAlg;
import org.polypheny.db.algebra.enumerable.EnumerableAlgImplementor;
import org.polypheny.db.algebra.enumerable.EnumerableConvention;
import org.polypheny.db.algebra.enumerable.EnumerableRelIdentifier;
import org.polypheny.db.algebra.enumerable.PhysType;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.catalog.entity.Entity;
Expand All @@ -47,6 +49,11 @@ public AlgOptCost computeSelfCost( AlgPlanner planner, AlgMetadataQuery mq ) {
return planner.getCostFactory().makeCost( dRows, 0, 0 );
}

@Override
public AlgNode copy( AlgTraitSet traitSet, List<AlgNode> inputs ) {
return new EnumerableDocIdentifier( inputs.get(0).getCluster(), traitSet, entity, inputs.get( 0 ) );
}


@Override
public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
Expand All @@ -56,7 +63,7 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
final PhysType physType = result.physType();

Expression input_ = builder.append( "input", result.block() );
Expression entity_ = Expressions.constant( entity );
Expression entity_ = Expressions.constant(entity.getId());
Expression identification_ = builder.append( "identification", Expressions.call( BuiltInMethod.ADD_REL_IDENTIFIERS.method, input_, entity_ ) );

builder.add( Expressions.return_( null, identification_ ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public EnumerableDocIdentifierRule() {
public AlgNode convert( AlgNode alg ) {
final LogicalRelIdentifier identifier = (LogicalRelIdentifier) alg;
final AlgTraitSet traits = identifier.getTraitSet().replace( EnumerableConvention.INSTANCE );
final AlgNode input = identifier.getInput();
final AlgNode input = convert(identifier.getInput(), identifier.getInput().getTraitSet().replace( EnumerableConvention.INSTANCE ));
return new EnumerableDocIdentifier( identifier.getCluster(), traits, identifier.getEntity(), input );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.algebra.enumerable.lpg;

import java.util.List;
import org.apache.calcite.linq4j.tree.BlockBuilder;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
Expand All @@ -24,7 +25,9 @@
import org.polypheny.db.algebra.enumerable.EnumerableAlg;
import org.polypheny.db.algebra.enumerable.EnumerableAlgImplementor;
import org.polypheny.db.algebra.enumerable.EnumerableConvention;
import org.polypheny.db.algebra.enumerable.EnumerableRelIdentifier;
import org.polypheny.db.algebra.enumerable.PhysType;
import org.polypheny.db.algebra.enumerable.document.EnumerableDocIdentifier;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.plan.AlgCluster;
Expand All @@ -47,6 +50,11 @@ public AlgOptCost computeSelfCost( AlgPlanner planner, AlgMetadataQuery mq ) {
return planner.getCostFactory().makeCost( dRows, 0, 0 );
}

@Override
public AlgNode copy( AlgTraitSet traitSet, List<AlgNode> inputs ) {
return new EnumerableLpgIdentifier( inputs.get(0).getCluster(), traitSet, entity, inputs.get( 0 ) );
}


@Override
public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
Expand All @@ -56,7 +64,7 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
final PhysType physType = result.physType();

Expression input_ = builder.append( "input", result.block() );
Expression entity_ = Expressions.constant( entity );
Expression entity_ = Expressions.constant(entity.getId());
Expression identification_ = builder.append( "identification", Expressions.call( BuiltInMethod.ADD_REL_IDENTIFIERS.method, input_, entity_ ) );

builder.add( Expressions.return_( null, identification_ ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public EnumerableLpgIdentifierRule() {
public AlgNode convert( AlgNode alg ) {
final LogicalRelIdentifier identifier = (LogicalRelIdentifier) alg;
final AlgTraitSet traits = identifier.getTraitSet().replace( EnumerableConvention.INSTANCE );
final AlgNode input = identifier.getInput();
final AlgNode input = convert(identifier.getInput(), identifier.getInput().getTraitSet().replace( EnumerableConvention.INSTANCE ));
return new EnumerableLpgIdentifier( identifier.getCluster(), traits, identifier.getEntity(), input );
}

Expand Down
17 changes: 9 additions & 8 deletions core/src/main/java/org/polypheny/db/functions/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.algebra.type.AlgDataTypeFactory;
import org.polypheny.db.algebra.type.AlgDataTypeSystem;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.catalog.entity.logical.LogicalEntity;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
Expand Down Expand Up @@ -292,26 +293,26 @@ public static Enumerable<?> batch( final DataContext context, final Enumerable<P
}

@SuppressWarnings("unused")
public static Enumerable<PolyValue[]> addRelIdentifiers(final Enumerable<PolyValue[]> input, LogicalEntity entity) {
return input.select( oldRow -> {
PolyValue[] newRow = new PolyValue[oldRow.length + 1];
newRow[0] = entity.getEntryIdentifiers().getNextEntryIdentifier().getEntryIdentifierAsPolyLong();
System.arraycopy( oldRow, 0, newRow, 1, oldRow.length );
return newRow;
public static Enumerable<PolyValue[]> addRelIdentifiers(final Enumerable<PolyValue[]> input, long logicalId) {
return input.select( row -> {
LogicalEntity entity = Catalog.getInstance().getSnapshot().getLogicalEntity( logicalId ).orElseThrow();
row[0] = entity.getEntryIdentifiers().getNextEntryIdentifier().getEntryIdentifierAsPolyLong();
return row;
} );
}

@SuppressWarnings("unused")
public static Enumerable<PolyValue[]> addDocIdentifiers(final Enumerable<PolyValue[]> input, LogicalEntity entity) {
public static Enumerable<PolyValue[]> addDocIdentifiers(final Enumerable<PolyValue[]> input, long logicalId) {
return input.select( oldRow -> {
PolyDocument document = (PolyDocument) oldRow[0];
LogicalEntity entity = Catalog.getInstance().getSnapshot().getLogicalEntity( logicalId ).orElseThrow();
document.put( IdentifierUtils.getIdentifierKeyAsPolyString(), entity.getEntryIdentifiers().getNextEntryIdentifier().getEntryIdentifierAsPolyLong());
return new PolyValue[]{document};
} );
}

@SuppressWarnings("unused")
public static Enumerable<PolyValue[]> addLpgIdentifiers(final Enumerable<PolyValue[]> input, LogicalEntity entity) {
public static Enumerable<PolyValue[]> addLpgIdentifiers(final Enumerable<PolyValue[]> input, long logicalId) {
return input.select( oldRow -> {
// ToDo: find out what has to be done here
return oldRow;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/polypheny/db/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ public enum BuiltInMethod {
SWITCH_CONTEXT( DataContext.class, "switchContext" ),
BATCH( Functions.class, "batch", DataContext.class, Enumerable.class ),
STREAM_RIGHT( Functions.class, "streamRight", DataContext.class, Enumerable.class, Function0.class, List.class ),
ADD_REL_IDENTIFIERS(Functions.class, "addRelIdentifiers", Enumerable.class, LogicalEntity.class),
ADD_DOC_IDENTIFIERS(Functions.class, "addDocIdentifiers", Enumerable.class, LogicalEntity.class),
ADD_LPG_IDENTIFIERS(Functions.class, "addLpgIdentifiers", Enumerable.class, LogicalEntity.class),
ADD_REL_IDENTIFIERS(Functions.class, "addRelIdentifiers", Enumerable.class, long.class),
ADD_DOC_IDENTIFIERS(Functions.class, "addDocIdentifiers", Enumerable.class, long.class),
ADD_LPG_IDENTIFIERS(Functions.class, "addLpgIdentifiers", Enumerable.class, long.class),
ENFORCE_CONSTRAINT( Functions.class, "enforceConstraint", DataContext.class, Function0.class, Function0.class, List.class, List.class ),
PARSE_ARRAY_FROM_TEXT( Functions.class, "reparse", String.class ),
QUERYABLE_SELECT( Queryable.class, "select", FunctionExpression.class ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class TransactionImpl implements Transaction, Comparable<Object> {
private Set<Lockable> lockedEntities = new HashSet<>();

@Getter
private Set<VersionedEntryIdentifier> readSet = new HashSet<>();
private Set<VersionedEntryIdentifier> readSet = new HashSet<>(); // This only contains entries if the transaction involves entities in MVCC mode


TransactionImpl(
Expand Down

0 comments on commit 1bd929c

Please sign in to comment.