From 1bd929cc297e28db5b56085ecd1665bf5144d67e Mon Sep 17 00:00:00 2001 From: Tobias Hafner Date: Mon, 23 Dec 2024 12:01:10 +0100 Subject: [PATCH] Fix relational inserts --- .../enumerable/EnumerableRelIdentifier.java | 10 +++++++++- .../enumerable/EnumerableRelIdentifierRule.java | 2 +- .../document/EnumerableDocIdentifier.java | 9 ++++++++- .../document/EnumerableDocIdentifierRule.java | 2 +- .../enumerable/lpg/EnumerableLpgIdentifier.java | 10 +++++++++- .../lpg/EnumerableLpgIdentifierRule.java | 2 +- .../org/polypheny/db/functions/Functions.java | 17 +++++++++-------- .../org/polypheny/db/util/BuiltInMethod.java | 6 +++--- .../db/transaction/TransactionImpl.java | 2 +- 9 files changed, 42 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifier.java b/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifier.java index 54560bb9a4..f526b6efb9 100644 --- a/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifier.java +++ b/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifier.java @@ -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; @@ -44,6 +45,13 @@ public AlgOptCost computeSelfCost( AlgPlanner planner, AlgMetadataQuery mq ) { } + @Override + public AlgNode copy( AlgTraitSet traitSet, List 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(); @@ -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_ ) ); diff --git a/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifierRule.java b/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifierRule.java index 73fcbd95b3..9a420cfaef 100644 --- a/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifierRule.java +++ b/core/src/main/java/org/polypheny/db/algebra/enumerable/EnumerableRelIdentifierRule.java @@ -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 ); } diff --git a/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifier.java b/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifier.java index 91caf3884d..279f4e876c 100644 --- a/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifier.java +++ b/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifier.java @@ -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; @@ -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; @@ -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 inputs ) { + return new EnumerableDocIdentifier( inputs.get(0).getCluster(), traitSet, entity, inputs.get( 0 ) ); + } + @Override public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) { @@ -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_ ) ); diff --git a/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifierRule.java b/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifierRule.java index 034696c67c..6f463aa4e6 100644 --- a/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifierRule.java +++ b/core/src/main/java/org/polypheny/db/algebra/enumerable/document/EnumerableDocIdentifierRule.java @@ -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 ); } diff --git a/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifier.java b/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifier.java index 1a0a54f34f..4cdc414148 100644 --- a/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifier.java +++ b/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifier.java @@ -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; @@ -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; @@ -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 inputs ) { + return new EnumerableLpgIdentifier( inputs.get(0).getCluster(), traitSet, entity, inputs.get( 0 ) ); + } + @Override public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) { @@ -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_ ) ); diff --git a/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifierRule.java b/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifierRule.java index bbb9bf54ea..cfd6108099 100644 --- a/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifierRule.java +++ b/core/src/main/java/org/polypheny/db/algebra/enumerable/lpg/EnumerableLpgIdentifierRule.java @@ -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 ); } diff --git a/core/src/main/java/org/polypheny/db/functions/Functions.java b/core/src/main/java/org/polypheny/db/functions/Functions.java index 03e415e867..a82f30ef74 100644 --- a/core/src/main/java/org/polypheny/db/functions/Functions.java +++ b/core/src/main/java/org/polypheny/db/functions/Functions.java @@ -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; @@ -292,26 +293,26 @@ public static Enumerable batch( final DataContext context, final Enumerable

addRelIdentifiers(final Enumerable 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 addRelIdentifiers(final Enumerable 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 addDocIdentifiers(final Enumerable input, LogicalEntity entity) { + public static Enumerable addDocIdentifiers(final Enumerable 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 addLpgIdentifiers(final Enumerable input, LogicalEntity entity) { + public static Enumerable addLpgIdentifiers(final Enumerable input, long logicalId) { return input.select( oldRow -> { // ToDo: find out what has to be done here return oldRow; diff --git a/core/src/main/java/org/polypheny/db/util/BuiltInMethod.java b/core/src/main/java/org/polypheny/db/util/BuiltInMethod.java index 339e6e7576..49e21a15e3 100644 --- a/core/src/main/java/org/polypheny/db/util/BuiltInMethod.java +++ b/core/src/main/java/org/polypheny/db/util/BuiltInMethod.java @@ -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 ), diff --git a/dbms/src/main/java/org/polypheny/db/transaction/TransactionImpl.java b/dbms/src/main/java/org/polypheny/db/transaction/TransactionImpl.java index a5040d58d2..c487c161ba 100644 --- a/dbms/src/main/java/org/polypheny/db/transaction/TransactionImpl.java +++ b/dbms/src/main/java/org/polypheny/db/transaction/TransactionImpl.java @@ -126,7 +126,7 @@ public class TransactionImpl implements Transaction, Comparable { private Set lockedEntities = new HashSet<>(); @Getter - private Set readSet = new HashSet<>(); + private Set readSet = new HashSet<>(); // This only contains entries if the transaction involves entities in MVCC mode TransactionImpl(