Skip to content

Commit

Permalink
Abstract identifier injection for different models
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Dec 28, 2024
1 parent 6f3c1c9 commit 95eede7
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 367 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.SingleAlg;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.catalog.logistic.DataModel;
import org.polypheny.db.plan.AlgCluster;
import org.polypheny.db.plan.AlgTraitSet;

@Getter
public abstract class Identifier extends SingleAlg {

protected final Entity entity;

protected Identifier( AlgCluster cluster, AlgTraitSet traits, Entity entity, AlgNode input ) {
super( cluster, traits, input );
this.entity = entity;
}


public String algCompareString() {
return this.getClass().getSimpleName() + "$" +
input.algCompareString() + "$" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2019-2024 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.algebra.enumerable;

import java.util.List;
import org.apache.calcite.linq4j.Enumerable;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.linq4j.function.Function2;
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.AlgNode;
import org.polypheny.db.algebra.core.common.Identifier;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
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.plan.AlgCluster;
import org.polypheny.db.plan.AlgOptCost;
import org.polypheny.db.plan.AlgPlanner;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.transaction.locking.IdentifierUtils;
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.type.entity.document.PolyDocument;
import org.polypheny.db.type.entity.graph.GraphPropertyHolder;
import org.polypheny.db.type.entity.numerical.PolyLong;
import org.polypheny.db.util.BuiltInMethod;

public class EnumerableIdentifier extends Identifier implements EnumerableAlg {

protected EnumerableIdentifier( AlgCluster cluster, AlgTraitSet traits, Entity entity, AlgNode input ) {
super( cluster, traits, entity, input );
assert getConvention() instanceof EnumerableConvention;
}


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


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


@Override
public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
final BlockBuilder builder = new BlockBuilder();
final EnumerableAlg input = (EnumerableAlg) getInput();
final Result result = implementor.visitChild( this, 0, input, Prefer.ANY );
final PhysType physType = result.physType();

Expression input_ = builder.append( "input", result.block() );
Expression entityId_ = Expressions.constant( entity.getId() );
Expression identification_ = null;
switch ( input.getModel() ) {
case RELATIONAL -> {
Expression addRelIdentifiers_ = Expressions.call( EnumerableIdentifier.class, "addRelIdentifiers" );
Expression addRelIdentifierWithId_ = Expressions.call( EnumerableIdentifier.class, "bindLogicalId", addRelIdentifiers_, entityId_ );
identification_ = builder.append( "identification", Expressions.call( BuiltInMethod.PROCESS_AND_STREAM_RIGHT.method, input_, addRelIdentifierWithId_ ) );
}
case DOCUMENT -> {
Expression addDocIdentifiers_ = Expressions.call( EnumerableIdentifier.class, "addDocIdentifiers" );
Expression addDocIdentifierWithId_ = Expressions.call( EnumerableIdentifier.class, "bindLogicalId", addDocIdentifiers_, entityId_ );
identification_ = builder.append( "identification", Expressions.call( BuiltInMethod.PROCESS_AND_STREAM_RIGHT.method, input_, addDocIdentifierWithId_ ) );
}
case GRAPH -> {
Expression addLpgIdentifiers_ = Expressions.call( EnumerableIdentifier.class, "addLpgIdentifiers" );
Expression addLpgIdentifiersWithId_ = Expressions.call( EnumerableIdentifier.class, "bindLogicalId", addLpgIdentifiers_, entityId_ );
identification_ = builder.append( "identification", Expressions.call( BuiltInMethod.PROCESS_AND_STREAM_RIGHT.method, input_, addLpgIdentifiersWithId_ ) );
}
}
assert identification_ != null;
builder.add( Expressions.return_( null, identification_ ) );
return implementor.result( physType, builder.toBlock() );

}


public static Function1<Enumerable<PolyValue[]>, Enumerable<PolyValue[]>> bindLogicalId(
Function2<Enumerable<PolyValue[]>, Long, Enumerable<PolyValue[]>> function,
long logicalId ) {
return input -> function.apply( input, logicalId );
}


public static Function2<Enumerable<PolyValue[]>, Long, Enumerable<PolyValue[]>> addRelIdentifiers() {
return ( input, logicalId ) -> {
LogicalEntity entity = Catalog.getInstance().getSnapshot()
.getLogicalEntity( logicalId )
.orElseThrow();
return input.select( row -> {
row[0] = entity.getEntryIdentifiers()
.getNextEntryIdentifier()
.getEntryIdentifierAsPolyLong();
return row;
} );
};
}


public static Function2<Enumerable<PolyValue[]>, Long, Enumerable<PolyValue[]>> addDocIdentifiers() {
return ( input, logicalId ) -> {
LogicalEntity entity = Catalog.getInstance().getSnapshot()
.getLogicalEntity( logicalId )
.orElseThrow();
return input.select( row -> {
for ( PolyValue value : row ) {
PolyLong entryIdentifier = entity.getEntryIdentifiers()
.getNextEntryIdentifier()
.getEntryIdentifierAsPolyLong();
if ( value instanceof PolyDocument ) {
((PolyDocument) value).put( IdentifierUtils.getIdentifierKeyAsPolyString(), entryIdentifier );
}
}
return row;
} );
};
}


public static Function2<Enumerable<PolyValue[]>, Long, Enumerable<PolyValue[]>> addLpgIdentifiers() {
return ( input, logicalId ) -> {
LogicalEntity entity = Catalog.getInstance().getSnapshot()
.getLogicalEntity( logicalId )
.orElseThrow();
return input.select( row -> {
for ( PolyValue value : row ) {
PolyLong entryIdentifier = entity.getEntryIdentifiers()
.getNextEntryIdentifier()
.getEntryIdentifierAsPolyLong();
if ( value instanceof GraphPropertyHolder ) {
((GraphPropertyHolder) value).getProperties()
.put( IdentifierUtils.getIdentifierKeyAsPolyString(), entryIdentifier );
}
}
return row;
} );
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,30 @@

import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.convert.ConverterRule;
import org.polypheny.db.algebra.core.AlgFactories;
import org.polypheny.db.algebra.core.common.Identifier;
import org.polypheny.db.algebra.logical.document.LogicalDocIdentifier;
import org.polypheny.db.algebra.logical.lpg.LogicalLpgIdentifier;
import org.polypheny.db.algebra.logical.relational.LogicalRelIdentifier;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.plan.Convention;

public class EnumerableRelIdentifierRule extends ConverterRule {
EnumerableRelIdentifierRule() {
super( LogicalRelIdentifier.class, Convention.NONE, EnumerableConvention.INSTANCE, "EnumerableRelIdentifierRule" );
}
public class EnumerableIdentifierRule extends ConverterRule {

public static final EnumerableIdentifierRule REL_INSTANCE = new EnumerableIdentifierRule( LogicalRelIdentifier.class );
public static final EnumerableIdentifierRule DOC_INSTANCE = new EnumerableIdentifierRule( LogicalDocIdentifier.class );
public static final EnumerableIdentifierRule GRAPH_INSTANCE = new EnumerableIdentifierRule( LogicalLpgIdentifier.class );


private EnumerableIdentifierRule( Class<? extends Identifier> identifier ) {
super( identifier, Convention.NONE, EnumerableConvention.INSTANCE, "Enumerable" + identifier.getSimpleName() + "Rule" );
}

@Override
public AlgNode convert( AlgNode alg ) {
final LogicalRelIdentifier identifier = (LogicalRelIdentifier) alg;
final Identifier identifier = (Identifier) alg;
final AlgTraitSet traits = identifier.getTraitSet().replace( EnumerableConvention.INSTANCE );
final AlgNode input = convert(identifier.getInput(), identifier.getInput().getTraitSet().replace( EnumerableConvention.INSTANCE ));
return new EnumerableRelIdentifier( identifier.getCluster(), traits, identifier.getEntity(), input );
return new EnumerableIdentifier( identifier.getCluster(), traits, identifier.getEntity(), input );
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@
import org.polypheny.db.algebra.enumerable.common.EnumerableConstraintEnforcerRule;
import org.polypheny.db.algebra.enumerable.common.EnumerableContextSwitcherRule;
import org.polypheny.db.algebra.enumerable.common.EnumerableModifyCollectRule;
import org.polypheny.db.algebra.enumerable.document.EnumerableDocIdentifierRule;
import org.polypheny.db.algebra.enumerable.document.EnumerableDocumentUnwindRule;
import org.polypheny.db.algebra.enumerable.document.EnumerableDocumentValuesRule;
import org.polypheny.db.algebra.enumerable.lpg.EnumerableLpgIdentifierRule;
import org.polypheny.db.algebra.enumerable.lpg.EnumerableLpgMatchRule;
import org.polypheny.db.algebra.enumerable.lpg.EnumerableLpgTransformerRule;
import org.polypheny.db.algebra.enumerable.lpg.EnumerableLpgValuesRule;
Expand Down Expand Up @@ -77,11 +75,11 @@ private EnumerableRules() {
}


public static final EnumerableRelIdentifierRule ENUMERABLE_REL_IDENTIFIER_RULE = new EnumerableRelIdentifierRule();
public static final EnumerableIdentifierRule ENUMERABLE_REL_IDENTIFIER_RULE = EnumerableIdentifierRule.REL_INSTANCE;

public static final EnumerableDocIdentifierRule ENUMERABLE_DOC_IDENTIFIER_RULE = new EnumerableDocIdentifierRule();
public static final EnumerableIdentifierRule ENUMERABLE_DOC_IDENTIFIER_RULE = EnumerableIdentifierRule.DOC_INSTANCE;

public static final EnumerableLpgIdentifierRule ENUMERABLE_LPG_IDENTIFIER_RULE = new EnumerableLpgIdentifierRule();
public static final EnumerableIdentifierRule ENUMERABLE_LPG_IDENTIFIER_RULE = EnumerableIdentifierRule.GRAPH_INSTANCE;

public static final EnumerableConditionalExecuteRule ENUMERABLE_CONDITIONAL_EXECUTE_RULE = new EnumerableConditionalExecuteRule();

Expand Down

This file was deleted.

Loading

0 comments on commit 95eede7

Please sign in to comment.