Skip to content

Commit

Permalink
Begin implementation of readset and id handling as part of alg nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Dec 5, 2024
1 parent 8d37e34 commit 58da4e2
Show file tree
Hide file tree
Showing 12 changed files with 955 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public abstract class AbstractAlgNode implements AlgNode {
/**
* Cached type of this relational expression.
*/
@Getter
protected AlgDataType rowType;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.polypheny.db.algebra.AlgWriter;
import org.polypheny.db.algebra.constant.Kind;
import org.polypheny.db.algebra.core.common.Modify;
import org.polypheny.db.algebra.logical.relational.LogicalRelModify;
import org.polypheny.db.algebra.logical.relational.LogicalRelValues;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.algebra.type.AlgDataTypeFactory;
Expand All @@ -37,7 +39,10 @@
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.rex.RexNode;
import org.polypheny.db.schema.trait.ModelTrait;
import org.polypheny.db.transaction.locking.IdentifierUtils;
import org.polypheny.db.type.PolyTypeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
Expand All @@ -54,6 +59,7 @@
*/
public abstract class RelModify<E extends Entity> extends Modify<E> implements RelAlg {

private static final Logger LOGGER = LoggerFactory.getLogger( RelModify.class );

/**
* The table definition.
Expand Down Expand Up @@ -99,17 +105,33 @@ protected RelModify(
this.operation = operation;
this.updateColumns = updateColumns;
this.sourceExpressions = sourceExpressions;
if ( operation == Operation.UPDATE ) {
Objects.requireNonNull( updateColumns );
Objects.requireNonNull( sourceExpressions );
Preconditions.checkArgument( sourceExpressions.size() == updateColumns.size() );
} else {
Preconditions.checkArgument( updateColumns == null );
Preconditions.checkArgument( sourceExpressions == null );
switch(operation) {
case UPDATE -> {
Objects.requireNonNull( updateColumns );
Objects.requireNonNull( sourceExpressions );
Preconditions.checkArgument( sourceExpressions.size() == updateColumns.size() );
}
case INSERT -> {
Preconditions.checkArgument( updateColumns == null );
Preconditions.checkArgument( sourceExpressions == null );
addIdentifiers();
}
default -> {
Preconditions.checkArgument( updateColumns == null );
Preconditions.checkArgument( sourceExpressions == null );
}
}
this.flattened = flattened;
}

private void addIdentifiers() {
if (!(input instanceof LogicalRelValues values) ) {
LOGGER.warn("New source type detected: {}", input);
return;
}
input = IdentifierUtils.overwriteIdentifierInInput( values );
}


public boolean isInsert() {
return operation == Operation.INSERT;
Expand Down
24 changes: 22 additions & 2 deletions core/src/main/java/org/polypheny/db/rex/RexLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import lombok.Value;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.constant.Kind;
Expand All @@ -57,6 +58,7 @@
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.type.entity.category.PolyNumber;
import org.polypheny.db.type.entity.numerical.PolyBigDecimal;
import org.polypheny.db.type.entity.numerical.PolyLong;
import org.polypheny.db.util.Collation;
import org.polypheny.db.util.NlsString;
import org.polypheny.db.util.Pair;
Expand Down Expand Up @@ -189,6 +191,21 @@ public RexLiteral( PolyValue value, AlgDataType type, PolyType polyType ) {
this.digest = computeDigest( RexDigestIncludeType.OPTIONAL );
}

public RexLiteral( PolyValue value, AlgDataType type, PolyType polyType, String digest ) {
this.value = value;
this.type = Objects.requireNonNull( type );
this.polyType = Objects.requireNonNull( polyType );
if ( !valueMatchesType( value, polyType, true ) ) {
System.err.println( value );
System.err.println( value.getClass().getCanonicalName() );
System.err.println( type );
System.err.println( polyType );
throw new IllegalArgumentException();
}
Preconditions.checkArgument( (value != null) || type.isNullable() );
Preconditions.checkArgument( polyType != PolyType.ANY );
this.digest = digest;
}

public RexLiteral( PolyValue value, AlgDataType type, PolyType polyType, boolean raw ) {
this.value = value;
Expand Down Expand Up @@ -424,7 +441,11 @@ private static void printAsJava( PolyValue value, PrintWriter pw, PolyType typeN
pw.print( value.asBoolean().value );
break;
case DECIMAL:
assert value.isBigDecimal();
assert value.isBigDecimal() || value.isLong();
if (value.isLong()) {
pw.print( value.asLong().value );
break;
}
pw.print( value.asBigDecimal().value );
break;
case DOUBLE:
Expand Down Expand Up @@ -662,6 +683,5 @@ public PolyValue getValue( AlgDataType type ) {
}
return value;
}

}

2 changes: 2 additions & 0 deletions core/src/main/java/org/polypheny/db/rex/RexNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@


import java.util.Collection;
import lombok.Getter;
import org.polypheny.db.algebra.constant.Kind;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.nodes.Node;
Expand All @@ -54,6 +55,7 @@
public abstract class RexNode implements Wrapper {

// Effectively final. Set in each sub-class constructor, and never re-set.
@Getter
protected String digest;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
public class IdentifierRegistry {

private static final Long MAX_IDENTIFIER_VALUE = Long.MAX_VALUE;
public static final IdentifierRegistry INSTANCE = new IdentifierRegistry(MAX_IDENTIFIER_VALUE);
public static final IdentifierRegistry INSTANCE = new IdentifierRegistry( MAX_IDENTIFIER_VALUE );

private final TreeSet<IdentifierInterval> availableIdentifiers;


IdentifierRegistry( long maxIdentifierValue ) {
this.availableIdentifiers = new TreeSet<>();
this.availableIdentifiers.add( new IdentifierInterval( 0, maxIdentifierValue ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@

package org.polypheny.db.transaction.locking;

import com.google.common.collect.ImmutableList;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.logical.relational.LogicalRelValues;
import org.polypheny.db.catalog.logistic.Collation;
import org.polypheny.db.ddl.DdlManager.ColumnTypeInformation;
import org.polypheny.db.ddl.DdlManager.FieldInformation;
import org.polypheny.db.rex.RexLiteral;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.numerical.PolyBigDecimal;
import org.polypheny.db.type.entity.numerical.PolyLong;

public class IdentifierUtils {

public static final String IDENTIFIER_KEY = "_eid";
public static final long MISSING_IDENTIFIER = 0;

public static final ColumnTypeInformation IDENTIFIER_COLUMN_TYPE = new ColumnTypeInformation(
PolyType.BIGINT, // binary not supported by hsqldb TODO TH: check for other stores, datatypes
Expand All @@ -39,8 +51,66 @@ public class IdentifierUtils {
IDENTIFIER_KEY,
IDENTIFIER_COLUMN_TYPE,
Collation.CASE_INSENSITIVE,
null,
new PolyLong( MISSING_IDENTIFIER ),
1
);

public static PolyLong getIdentifier() {
return new PolyLong( IdentifierRegistry.INSTANCE.getEntryIdentifier() );
}

public static void throwIllegalFieldName() {
throw new IllegalArgumentException( MessageFormat.format(
"The field {0} is reserved for internal use and cannot be used.",
IdentifierUtils.IDENTIFIER_KEY)
);
}

public static AlgNode overwriteIdentifierInInput( LogicalRelValues value ) {
List<List<RexLiteral>> newValues = new ArrayList<>();
value.tuples.forEach(row -> {
List<RexLiteral> newRow = new ArrayList<>(row);
RexLiteral identifierLiteral = newRow.get(0);
newRow.set(0, IdentifierUtils.copyAndUpdateIdentifier(identifierLiteral, getIdentifier()));
newValues.add(newRow);
});

ImmutableList<ImmutableList<RexLiteral>> immutableValues = new ImmutableList.Builder<ImmutableList<RexLiteral>>()
.addAll(newValues.stream()
.map(ImmutableList::copyOf)
.toList())
.build();

return new LogicalRelValues(
value.getCluster(),
value.getTraitSet(),
value.getRowType(),
immutableValues
);
}

private static RexLiteral copyAndUpdateIdentifier( RexLiteral identifierLiteral, PolyLong identifier ) {
return new RexLiteral(identifier, identifierLiteral.getType(), PolyType.DECIMAL);
}


public static List<FieldInformation> addIdentifierFieldIfAbsent( List<FieldInformation> fields ) {
if (fields.get(0).name().equals( IDENTIFIER_KEY )){
return fields;
}
List<FieldInformation> newFields = fields.stream()
.map( f -> new FieldInformation(f.name(), f.typeInformation(), f.collation(), f.defaultValue(), f.position() + 1) )
.collect( Collectors.toCollection( LinkedList::new ) );
newFields.add(0, IDENTIFIER_FIELD_INFORMATION );
return newFields;
}

public static void throwIfContainsIdentifierField(List<FieldInformation> fields) {
if (fields.stream().noneMatch( f -> f.name().equals(IDENTIFIER_FIELD_INFORMATION.name()))) {
return;

}
throwIllegalFieldName();
}

}
7 changes: 5 additions & 2 deletions dbms/src/main/java/org/polypheny/db/ddl/DdlManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@
import org.polypheny.db.routing.RoutingManager;
import org.polypheny.db.transaction.Statement;
import org.polypheny.db.transaction.TransactionException;
import org.polypheny.db.transaction.locking.IdentifierUtils;
import org.polypheny.db.type.ArrayType;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.type.entity.numerical.PolyLong;
import org.polypheny.db.util.Pair;
import org.polypheny.db.view.MaterializedViewManager;

Expand Down Expand Up @@ -2056,8 +2056,11 @@ public void createTable( long namespaceId, String name, List<FieldInformation> f
true );

// addLColumns

Map<String, LogicalColumn> ids = new HashMap<>();

IdentifierUtils.throwIfContainsIdentifierField( fields );
fields = IdentifierUtils.addIdentifierFieldIfAbsent(fields);

for ( FieldInformation information : fields ) {
ids.put( information.name(), addColumn( namespaceId, information.name(), information.typeInformation(), information.collation(), information.defaultValue(), logical.id, information.position() + 1 ) ); // pos + 1 to make space for entry identifier column
}
Expand Down
Loading

0 comments on commit 58da4e2

Please sign in to comment.