Skip to content

Commit

Permalink
Use enumerables for doc identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Dec 18, 2024
1 parent 6002253 commit 8eb6af7
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 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.entity.Entity;
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.util.BuiltInMethod;

public class EnumerableRelIdentifier extends Identifier implements EnumerableAlg {

protected EnumerableRelIdentifier( 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 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 identification_ = builder.append( "identification", Expressions.call( input_, BuiltInMethod.ADD_REL_IDENTIFIERS.method ) );

builder.add( Expressions.return_( null, identification_ ) );
return implementor.result( physType, builder.toBlock() );

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,27 @@
* limitations under the License.
*/

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

import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.convert.ConverterRule;
import org.polypheny.db.algebra.core.common.Identifier;
import org.polypheny.db.algebra.enumerable.EnumerableConvention;
import org.polypheny.db.algebra.logical.relational.LogicalRelIdentifier;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.plan.Convention;

public class EnumerableIdentifierRule extends ConverterRule {

public static final EnumerableIdentifierRule REL_INSTANCE = new EnumerableIdentifierRule( LogicalRelIdentifier.class );

public static final EnumerableIdentifierRule DOC_INSTANCE = new EnumerableIdentifierRule( LogicalRelIdentifier.class );


private EnumerableIdentifierRule( Class<? extends Identifier> identifier ) {
super( identifier, Convention.NONE, EnumerableConvention.INSTANCE, "EnumerableIdentifierRule" );
public class EnumerableRelIdentifierRule extends ConverterRule {

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


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

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
import org.polypheny.db.algebra.enumerable.common.EnumerableConditionalExecuteTrueRule;
import org.polypheny.db.algebra.enumerable.common.EnumerableConstraintEnforcerRule;
import org.polypheny.db.algebra.enumerable.common.EnumerableContextSwitcherRule;
import org.polypheny.db.algebra.enumerable.common.EnumerableIdentifierRule;
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.EnumerableLpgMatchRule;
Expand Down Expand Up @@ -74,9 +74,10 @@ public class EnumerableRules {
private EnumerableRules() {
}

public static final EnumerableIdentifierRule ENUMERABLE_REL_IDENTIFIER_RULE = EnumerableIdentifierRule.REL_INSTANCE;

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

public static final EnumerableDocIdentifierRule ENUMERABLE_DOC_IDENTIFIER_RULE = new EnumerableDocIdentifierRule();

public static final EnumerableConditionalExecuteRule ENUMERABLE_CONDITIONAL_EXECUTE_RULE = new EnumerableConditionalExecuteRule();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

import org.apache.calcite.linq4j.tree.BlockBuilder;
import org.apache.calcite.linq4j.tree.Expression;
Expand All @@ -33,9 +33,9 @@
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.util.BuiltInMethod;

public class EnumerableIdentifier extends Identifier implements EnumerableAlg {
public class EnumerableDocIdentifier extends Identifier implements EnumerableAlg {

protected EnumerableIdentifier( AlgCluster cluster, AlgTraitSet traits, Entity entity, AlgNode input ) {
protected EnumerableDocIdentifier( AlgCluster cluster, AlgTraitSet traits, Entity entity, AlgNode input ) {
super( cluster, traits, entity, input );
assert getConvention() instanceof EnumerableConvention;
}
Expand All @@ -54,11 +54,9 @@ public Result implement( EnumerableAlgImplementor implementor, Prefer pref ) {
final PhysType physType = result.physType();

Expression input_ = builder.append( "input", result.block() );
Expression identification_ = builder.append( "identification", Expressions.call( input_, BuiltInMethod.ADD_IDENTIFIERS.method ) );
Expression identification_ = builder.append( "identification", Expressions.call( input_, BuiltInMethod.ADD_DOC_IDENTIFIERS.method ) );

builder.add( Expressions.return_( null, identification_ ) );
return implementor.result( physType, builder.toBlock() );

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.document;

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.convert.ConverterRule;
import org.polypheny.db.algebra.enumerable.EnumerableAlg;
import org.polypheny.db.algebra.enumerable.EnumerableAlg.Prefer;
import org.polypheny.db.algebra.enumerable.EnumerableAlg.Result;
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.logical.document.LogicalDocIdentifier;
import org.polypheny.db.algebra.logical.relational.LogicalRelIdentifier;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.catalog.entity.Entity;
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.plan.Convention;
import org.polypheny.db.util.BuiltInMethod;



/*
* 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.
*/


public class EnumerableDocIdentifierRule extends ConverterRule {

public EnumerableDocIdentifierRule() {
super( LogicalRelIdentifier.class, Convention.NONE, EnumerableConvention.INSTANCE, "EnumerableDocIdentifierRule" );
}

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

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.logical.document;

import java.util.List;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.core.common.Identifier;
import org.polypheny.db.algebra.core.document.DocumentAlg;
import org.polypheny.db.algebra.metadata.AlgMetadataQuery;
import org.polypheny.db.catalog.entity.Entity;
import org.polypheny.db.plan.AlgCluster;
import org.polypheny.db.plan.AlgOptCost;
import org.polypheny.db.plan.AlgPlanner;
import org.polypheny.db.plan.AlgTraitSet;

public class LogicalDocIdentifier extends Identifier implements DocumentAlg {
protected LogicalDocIdentifier( Entity entitiy, AlgCluster cluster, AlgTraitSet traits, AlgNode input) {
super(cluster, traits, entitiy, input);
}

public static LogicalDocIdentifier create(Entity document, final AlgNode input) {
final AlgCluster cluster = input.getCluster();
final AlgTraitSet traits = input.getTraitSet();
return new LogicalDocIdentifier( document, cluster, traits, input );
}


@Override
public DocType getDocType() {
// ToDo TH: is this correct?
return DocType.VALUES;
}

@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 traitSete, List<AlgNode> inputs) {
return new LogicalDocIdentifier(entity, getCluster(), traitSete, sole(inputs) );
}


}
12 changes: 11 additions & 1 deletion core/src/main/java/org/polypheny/db/functions/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.type.entity.category.PolyNumber;
import org.polypheny.db.type.entity.category.PolyTemporal;
import org.polypheny.db.type.entity.document.PolyDocument;
import org.polypheny.db.type.entity.graph.PolyDictionary;
import org.polypheny.db.type.entity.numerical.PolyBigDecimal;
import org.polypheny.db.type.entity.numerical.PolyDouble;
Expand Down Expand Up @@ -290,7 +291,7 @@ public static Enumerable<?> batch( final DataContext context, final Enumerable<P
}

@SuppressWarnings("unused")
public static Enumerable<PolyValue[]> addIdentifiers(final Enumerable<PolyValue[]> input) {
public static Enumerable<PolyValue[]> addRelIdentifiers(final Enumerable<PolyValue[]> input) {
return input.select( oldRow -> {
PolyValue[] newRow = new PolyValue[oldRow.length + 1];
newRow[0] = IdentifierUtils.getIdentifierAsPolyLong();
Expand All @@ -299,6 +300,15 @@ public static Enumerable<PolyValue[]> addIdentifiers(final Enumerable<PolyValue[
} );
}

@SuppressWarnings("unused")
public static Enumerable<PolyValue[]> addDocIdentifiers(final Enumerable<PolyValue[]> input) {
return input.select( oldRow -> {
PolyDocument document = (PolyDocument) oldRow[0];
document.put( IdentifierUtils.getIdentifierKeyAsPolyString(), IdentifierUtils.getIdentifierAsPolyLong() );
return new PolyValue[]{document};
} );
}


@SuppressWarnings("unused")
public static Enumerable<PolyValue[]> streamRight( final DataContext context, final Enumerable<PolyValue[]> input, final Function0<Enumerable<PolyValue[]>> executorCall, final List<PolyType> polyTypes ) {
Expand Down
Loading

0 comments on commit 8eb6af7

Please sign in to comment.