Search in sources :

Example 51 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project beam by apache.

the class ArrayScanColumnRefToUncollect method convert.

@Override
public RelNode convert(ResolvedNodes.ResolvedArrayScan zetaNode, List<RelNode> inputs) {
    assert inputs.size() == 1;
    RelNode input = inputs.get(0);
    RexInputRef columnRef = (RexInputRef) getExpressionConverter().convertRexNodeFromResolvedExpr(getColumnRef(zetaNode.getArrayExpr()), zetaNode.getInputScan().getColumnList(), input.getRowType().getFieldList(), ImmutableMap.of());
    CorrelationId correlationId = new CorrelationId(0);
    RexNode convertedColumnRef = getCluster().getRexBuilder().makeFieldAccess(getCluster().getRexBuilder().makeCorrel(input.getRowType(), correlationId), columnRef.getIndex());
    String fieldName = String.format("%s%s", zetaNode.getElementColumn().getTableName(), zetaNode.getElementColumn().getName());
    RelNode projectNode = LogicalProject.create(createOneRow(getCluster()), ImmutableList.of(), Collections.singletonList(convertArrayExpr(zetaNode.getArrayExpr(), getCluster().getRexBuilder(), convertedColumnRef)), ImmutableList.of(fieldName));
    boolean ordinality = (zetaNode.getArrayOffsetColumn() != null);
    RelNode uncollect = ZetaSqlUnnest.create(projectNode.getTraitSet(), projectNode, ordinality);
    return LogicalCorrelate.create(input, uncollect, correlationId, ImmutableBitSet.of(columnRef.getIndex()), JoinRelType.INNER);
}
Also used : RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) RexInputRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef) CorrelationId(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 52 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project ignite-3 by apache.

the class CorrelatedNestedLoopJoinRule method convert.

@Override
protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalJoin rel) {
    final int leftFieldCount = rel.getLeft().getRowType().getFieldCount();
    final RelOptCluster cluster = rel.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RelBuilder relBuilder = relBuilderFactory.create(rel.getCluster(), null);
    final Set<CorrelationId> correlationIds = new HashSet<>();
    final ArrayList<RexNode> corrVar = new ArrayList<>();
    for (int i = 0; i < batchSize; i++) {
        CorrelationId correlationId = cluster.createCorrel();
        correlationIds.add(correlationId);
        corrVar.add(rexBuilder.makeCorrel(rel.getLeft().getRowType(), correlationId));
    }
    // Generate first condition
    final RexNode condition = rel.getCondition().accept(new RexShuttle() {

        @Override
        public RexNode visitInputRef(RexInputRef input) {
            int field = input.getIndex();
            if (field >= leftFieldCount) {
                return rexBuilder.makeInputRef(input.getType(), input.getIndex() - leftFieldCount);
            }
            return rexBuilder.makeFieldAccess(corrVar.get(0), field);
        }
    });
    List<RexNode> conditionList = new ArrayList<>();
    conditionList.add(condition);
    // Add batchSize-1 other conditions
    for (int i = 1; i < batchSize; i++) {
        final int corrIndex = i;
        final RexNode condition2 = condition.accept(new RexShuttle() {

            @Override
            public RexNode visitCorrelVariable(RexCorrelVariable variable) {
                return corrVar.get(corrIndex);
            }
        });
        conditionList.add(condition2);
    }
    RelTraitSet filterInTraits = rel.getRight().getTraitSet();
    // Push a filter with batchSize disjunctions
    relBuilder.push(rel.getRight().copy(filterInTraits, rel.getRight().getInputs())).filter(relBuilder.or(conditionList));
    RelNode right = relBuilder.build();
    JoinRelType joinType = rel.getJoinType();
    RelTraitSet outTraits = cluster.traitSetOf(IgniteConvention.INSTANCE);
    RelTraitSet leftInTraits = cluster.traitSetOf(IgniteConvention.INSTANCE);
    CorrelationTrait corrTrait = CorrelationTrait.correlations(correlationIds);
    RelTraitSet rightInTraits = cluster.traitSetOf(IgniteConvention.INSTANCE).replace(RewindabilityTrait.REWINDABLE).replace(corrTrait);
    RelNode left = convert(rel.getLeft(), leftInTraits);
    right = convert(right, rightInTraits);
    return new IgniteCorrelatedNestedLoopJoin(cluster, outTraits, left, right, rel.getCondition(), correlationIds, joinType);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) CorrelationTrait(org.apache.ignite.internal.sql.engine.trait.CorrelationTrait) RelBuilder(org.apache.calcite.tools.RelBuilder) RexShuttle(org.apache.calcite.rex.RexShuttle) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) IgniteCorrelatedNestedLoopJoin(org.apache.ignite.internal.sql.engine.rel.IgniteCorrelatedNestedLoopJoin) JoinRelType(org.apache.calcite.rel.core.JoinRelType) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) CorrelationId(org.apache.calcite.rel.core.CorrelationId) HashSet(java.util.HashSet) RexNode(org.apache.calcite.rex.RexNode)

Example 53 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project ignite-3 by apache.

the class IgniteFilter method passThroughCorrelation.

/**
 * PassThroughCorrelation.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 */
@Override
public Pair<RelTraitSet, List<RelTraitSet>> passThroughCorrelation(RelTraitSet nodeTraits, List<RelTraitSet> inTraits) {
    Set<CorrelationId> corrSet = RexUtils.extractCorrelationIds(getCondition());
    CorrelationTrait correlation = TraitUtils.correlation(nodeTraits);
    if (corrSet.isEmpty() || correlation.correlationIds().containsAll(corrSet)) {
        return Pair.of(nodeTraits, List.of(inTraits.get(0).replace(correlation)));
    }
    return null;
}
Also used : CorrelationTrait(org.apache.ignite.internal.sql.engine.trait.CorrelationTrait) CorrelationId(org.apache.calcite.rel.core.CorrelationId)

Example 54 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project ignite-3 by apache.

the class IgniteMdCumulativeCost method getCumulativeCost.

/**
 * GetCumulativeCost.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 */
public RelOptCost getCumulativeCost(IgniteCorrelatedNestedLoopJoin rel, RelMetadataQuery mq) {
    RelOptCost cost = nonCumulativeCost(rel, mq);
    if (cost.isInfinite()) {
        return cost;
    }
    RelNode left = rel.getLeft();
    RelNode right = rel.getRight();
    Set<CorrelationId> corIds = rel.getVariablesSet();
    RelOptCost leftCost = mq.getCumulativeCost(left);
    if (leftCost.isInfinite()) {
        return leftCost;
    }
    RelOptCost rightCost = mq.getCumulativeCost(right);
    if (rightCost.isInfinite()) {
        return rightCost;
    }
    return cost.plus(leftCost).plus(rightCost.multiplyBy(left.estimateRowCount(mq) / corIds.size()));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelOptCost(org.apache.calcite.plan.RelOptCost) CorrelationId(org.apache.calcite.rel.core.CorrelationId)

Example 55 with CorrelationId

use of org.apache.calcite.rel.core.CorrelationId in project ignite-3 by apache.

the class ProjectConverterRule method convert.

/**
 * {@inheritDoc}
 */
@Override
protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalProject rel) {
    RelOptCluster cluster = rel.getCluster();
    RelTraitSet traits = cluster.traitSetOf(IgniteConvention.INSTANCE).replace(IgniteDistributions.single());
    Collection<CorrelationId> corrIds = RexUtils.extractCorrelationIds(rel.getProjects());
    if (!corrIds.isEmpty()) {
        traits = traits.replace(CorrelationTrait.correlations(corrIds)).replace(RewindabilityTrait.REWINDABLE);
    }
    RelNode input = convert(rel.getInput(), traits);
    return new IgniteProject(cluster, traits, input, rel.getProjects(), rel.getRowType());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) IgniteProject(org.apache.ignite.internal.sql.engine.rel.IgniteProject) RelNode(org.apache.calcite.rel.RelNode) RelTraitSet(org.apache.calcite.plan.RelTraitSet) CorrelationId(org.apache.calcite.rel.core.CorrelationId)

Aggregations

CorrelationId (org.apache.calcite.rel.core.CorrelationId)74 RelNode (org.apache.calcite.rel.RelNode)39 RexNode (org.apache.calcite.rex.RexNode)33 RelDataType (org.apache.calcite.rel.type.RelDataType)25 ArrayList (java.util.ArrayList)22 RelOptCluster (org.apache.calcite.plan.RelOptCluster)19 RexBuilder (org.apache.calcite.rex.RexBuilder)18 RelBuilder (org.apache.calcite.tools.RelBuilder)17 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)15 ImmutableList (com.google.common.collect.ImmutableList)12 RelTraitSet (org.apache.calcite.plan.RelTraitSet)12 RexCorrelVariable (org.apache.calcite.rex.RexCorrelVariable)11 RexShuttle (org.apache.calcite.rex.RexShuttle)11 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 List (java.util.List)9 RelOptUtil (org.apache.calcite.plan.RelOptUtil)9 JoinRelType (org.apache.calcite.rel.core.JoinRelType)9 ByteString (org.apache.calcite.avatica.util.ByteString)7 SqlOperator (org.apache.calcite.sql.SqlOperator)7