Skip to content

Commit

Permalink
Start implementation of update rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Jan 12, 2025
1 parent 341e5a5 commit 741b543
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.transaction.locking;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -24,6 +25,8 @@
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.AlgRoot;
import org.polypheny.db.algebra.AlgShuttleImpl;
import org.polypheny.db.algebra.core.JoinAlgType;
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;
import org.polypheny.db.algebra.logical.document.LogicalDocIdentifier;
Expand Down Expand Up @@ -62,16 +65,28 @@
import org.polypheny.db.algebra.logical.relational.LogicalRelTableFunctionScan;
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.AlgDataTypeFactoryImpl;
import org.polypheny.db.algebra.type.AlgDataTypeField;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.rex.RexIndexRef;
import org.polypheny.db.rex.RexLiteral;
import org.polypheny.db.transaction.Statement;
import org.polypheny.db.transaction.Transaction;
import org.polypheny.db.transaction.locking.DeferredAlgTreeModification.Modification;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.PolyTypeFactoryImpl;
import org.polypheny.db.type.entity.PolyBoolean;
import org.polypheny.db.type.entity.PolyString;
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.type.entity.graph.PolyEdge;
import org.polypheny.db.type.entity.graph.PolyNode;

public class AlgTreeRewriter extends AlgShuttleImpl {

public static final AlgDataType BOOLEAN_TRUE_ALG_TYPE = ((PolyTypeFactoryImpl) AlgDataTypeFactoryImpl.DEFAULT).createBasicPolyType( PolyType.BOOLEAN, true );
public static final AlgDataType SINGLE_VERSION_ROW_ALG_TYPE = AlgDataTypeFactoryImpl.DEFAULT.createStructType( List.of(0L), List.of(IdentifierUtils.VERSION_ALG_TYPE), List.of("_vid"));

private final Statement statement;
private final Set<DeferredAlgTreeModification> pendingModifications;
private boolean containsIdentifierKey;
Expand Down Expand Up @@ -117,6 +132,7 @@ private <T extends AlgNode> T applyModificationsOrSkip( T node ) {
return node;
}


private Transaction getTransaction() {
return statement.getTransaction();
}
Expand Down Expand Up @@ -303,8 +319,61 @@ public AlgNode visit( LogicalRelModify modify ) {
input.getTupleType()
);
return modify1.copy( modify1.getTraitSet(), List.of( identifier ) );

case UPDATE:
return modify1;
/* Rewrite:
Update <- Filter <- Input
to
v Project <- RelValues (for new _vid)
Insert <- Join
^ Project (to remove old _vid) <- Filter <- Input
*/

PolyValue newVersion = IdentifierUtils.getVersionAsPolyLong( getTransaction().getSequenceNumber(), false );
LogicalRelValues newVersionValues = LogicalRelValues.create(
modify1.getCluster(),
SINGLE_VERSION_ROW_ALG_TYPE,
ImmutableList.of( ImmutableList.of(
new RexLiteral(
newVersion,
IdentifierUtils.VERSION_ALG_TYPE,
PolyType.BIGINT )
) ) );

LogicalRelProject versionProject = LogicalRelProject.create(
newVersionValues,
List.of(new RexIndexRef(0, IdentifierUtils.VERSION_ALG_TYPE) ),
List.of(IdentifierUtils.VERSION_KEY));

List<AlgDataTypeField> inputFields = modify1.getInput().getTupleType().getFields().stream()
.filter( f -> !f.getName().equals( IdentifierUtils.VERSION_KEY ) ).toList();
List<RexIndexRef> inputProjects = inputFields.stream()
.map( f -> new RexIndexRef( f.getIndex(), f.getType()) )
.toList();
LogicalRelProject inputProject = LogicalRelProject.create(
modify1.getInput(),
inputProjects,
inputFields.stream().map( AlgDataTypeField::getName ).toList()
);

LogicalRelJoin versionToInputJoin = LogicalRelJoin.create(
inputProject,
versionProject,
new RexLiteral( PolyBoolean.TRUE, BOOLEAN_TRUE_ALG_TYPE, PolyType.BOOLEAN ),
Set.of(),
JoinAlgType.INNER );

return LogicalRelModify.create(
modify1.getEntity(),
versionToInputJoin,
Operation.INSERT,
null,
null,
false
);

default:
return modify1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.polypheny.db.algebra.logical.lpg.LogicalLpgModify;
import org.polypheny.db.algebra.logical.relational.LogicalRelValues;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.algebra.type.AlgDataTypeFactoryImpl;
import org.polypheny.db.catalog.logistic.Collation;
Expand Down Expand Up @@ -106,7 +107,6 @@ public static PolyLong getVersionAsPolyLong(long version, boolean isComitted) {
return PolyLong.of( isComitted ? version : version * -1);
}


public static List<FieldInformation> addMvccFieldsIfAbsent(List<FieldInformation> fields) {
List<FieldInformation> newFields = new LinkedList<>();

Expand Down

0 comments on commit 741b543

Please sign in to comment.