Search in sources :

Example 21 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.

the class VolcanoPlanner method changeTraitsUsingConverters.

private RelNode changeTraitsUsingConverters(RelNode rel, RelTraitSet toTraits, boolean allowAbstractConverters) {
    final RelTraitSet fromTraits = rel.getTraitSet();
    assert fromTraits.size() >= toTraits.size();
    final boolean allowInfiniteCostConverters = SaffronProperties.INSTANCE.allowInfiniteCostConverters().get();
    // Traits may build on top of another...for example a collation trait
    // would typically come after a distribution trait since distribution
    // destroys collation; so when doing the conversion below we use
    // fromTraits as the trait of the just previously converted RelNode.
    // Also, toTraits may have fewer traits than fromTraits, excess traits
    // will be left as is.  Finally, any null entries in toTraits are
    // ignored.
    RelNode converted = rel;
    for (int i = 0; (converted != null) && (i < toTraits.size()); i++) {
        RelTrait fromTrait = converted.getTraitSet().getTrait(i);
        final RelTraitDef traitDef = fromTrait.getTraitDef();
        RelTrait toTrait = toTraits.getTrait(i);
        if (toTrait == null) {
            continue;
        }
        assert traitDef == toTrait.getTraitDef();
        // if (fromTrait.subsumes(toTrait)) {
        if (fromTrait.equals(toTrait)) {
            // No need to convert; it's already correct.
            continue;
        }
        rel = traitDef.convert(this, converted, toTrait, allowInfiniteCostConverters);
        if (rel != null) {
            assert rel.getTraitSet().getTrait(traitDef).satisfies(toTrait);
            rel = completeConversion(rel, allowInfiniteCostConverters, toTraits, Expressions.list(traitDef));
            if (rel != null) {
                register(rel, converted);
            }
        }
        if ((rel == null) && allowAbstractConverters) {
            RelTraitSet stepTraits = converted.getTraitSet().replace(toTrait);
            rel = getSubset(converted, stepTraits);
        }
        converted = rel;
    }
    // make sure final converted traitset subsumes what was required
    if (converted != null) {
        assert converted.getTraitSet().satisfies(toTraits);
    }
    return converted;
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait) RelNode(org.apache.calcite.rel.RelNode) RelTraitDef(org.apache.calcite.plan.RelTraitDef) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 22 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.

the class VolcanoRuleCall method transformTo.

// ~ Methods ----------------------------------------------------------------
// implement RelOptRuleCall
public void transformTo(RelNode rel, Map<RelNode, RelNode> equiv) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Transform to: rel#{} via {}{}", rel.getId(), getRule(), equiv.isEmpty() ? "" : " with equivalences " + equiv);
        if (generatedRelList != null) {
            generatedRelList.add(rel);
        }
    }
    try {
        // It's possible that rel is a subset or is already registered.
        // Is there still a point in continuing? Yes, because we might
        // discover that two sets of expressions are actually equivalent.
        // Make sure traits that the new rel doesn't know about are
        // propagated.
        RelTraitSet rels0Traits = rels[0].getTraitSet();
        new RelTraitPropagationVisitor(getPlanner(), rels0Traits).go(rel);
        if (LOGGER.isTraceEnabled()) {
            // Cannot call RelNode.toString() yet, because rel has not
            // been registered. For now, let's make up something similar.
            String relDesc = "rel#" + rel.getId() + ":" + rel.getRelTypeName();
            LOGGER.trace("call#{}: Rule {} arguments {} created {}", id, getRule(), Arrays.toString(rels), relDesc);
        }
        if (volcanoPlanner.listener != null) {
            RelOptListener.RuleProductionEvent event = new RelOptListener.RuleProductionEvent(volcanoPlanner, rel, this, true);
            volcanoPlanner.listener.ruleProductionSucceeded(event);
        }
        // don't register twice and cause churn.
        for (Map.Entry<RelNode, RelNode> entry : equiv.entrySet()) {
            volcanoPlanner.ensureRegistered(entry.getKey(), entry.getValue(), this);
        }
        volcanoPlanner.ensureRegistered(rel, rels[0], this);
        rels[0].getCluster().invalidateMetadataQuery();
        if (volcanoPlanner.listener != null) {
            RelOptListener.RuleProductionEvent event = new RelOptListener.RuleProductionEvent(volcanoPlanner, rel, this, false);
            volcanoPlanner.listener.ruleProductionSucceeded(event);
        }
    } catch (Exception e) {
        throw new RuntimeException("Error occurred while applying rule " + getRule(), e);
    }
}
Also used : RelOptListener(org.apache.calcite.plan.RelOptListener) RelTraitPropagationVisitor(org.apache.calcite.plan.RelTraitPropagationVisitor) RelNode(org.apache.calcite.rel.RelNode) RelTraitSet(org.apache.calcite.plan.RelTraitSet) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 23 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.

the class RelSet method add.

/**
 * Adds a relational expression to a set, with its results available under a
 * particular calling convention. An expression may be in the set several
 * times with different calling conventions (and hence different costs).
 */
public RelSubset add(RelNode rel) {
    assert equivalentSet == null : "adding to a dead set";
    final RelTraitSet traitSet = rel.getTraitSet().simplify();
    final RelSubset subset = getOrCreateSubset(rel.getCluster(), traitSet);
    subset.add(rel);
    return subset;
}
Also used : RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 24 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.

the class RelCollationTraitDef method convert.

public RelNode convert(RelOptPlanner planner, RelNode rel, RelCollation toCollation, boolean allowInfiniteCostConverters) {
    if (toCollation.getFieldCollations().isEmpty()) {
        // An empty sort doesn't make sense.
        return null;
    }
    // Create a logical sort, then ask the planner to convert its remaining
    // traits (e.g. convert it to an EnumerableSortRel if rel is enumerable
    // convention)
    final Sort sort = LogicalSort.create(rel, toCollation, null, null);
    RelNode newRel = planner.register(sort, rel);
    final RelTraitSet newTraitSet = rel.getTraitSet().replace(toCollation);
    if (!newRel.getTraitSet().equals(newTraitSet)) {
        newRel = planner.changeTraits(newRel, newTraitSet);
    }
    return newRel;
}
Also used : LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 25 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project calcite by apache.

the class FrameworksTest method testOptimize.

@Test
public void testOptimize() {
    RelNode x = Frameworks.withPlanner(new Frameworks.PlannerAction<RelNode>() {

        public RelNode apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
            final Table table = new AbstractTable() {

                public RelDataType getRowType(RelDataTypeFactory typeFactory) {
                    final RelDataType stringType = typeFactory.createJavaType(String.class);
                    final RelDataType integerType = typeFactory.createJavaType(Integer.class);
                    return typeFactory.builder().add("s", stringType).add("i", integerType).build();
                }
            };
            // "SELECT * FROM myTable"
            final RelOptAbstractTable relOptTable = new RelOptAbstractTable(relOptSchema, "myTable", table.getRowType(typeFactory)) {
            };
            final EnumerableTableScan tableRel = EnumerableTableScan.create(cluster, relOptTable);
            // "WHERE i > 1"
            final RexBuilder rexBuilder = cluster.getRexBuilder();
            final RexNode condition = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(tableRel), "i", true), rexBuilder.makeExactLiteral(BigDecimal.ONE));
            final LogicalFilter filter = LogicalFilter.create(tableRel, condition);
            // Specify that the result should be in Enumerable convention.
            final RelNode rootRel = filter;
            final RelOptPlanner planner = cluster.getPlanner();
            RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
            final RelNode rootRel2 = planner.changeTraits(rootRel, desiredTraits);
            planner.setRoot(rootRel2);
            // Now, plan.
            return planner.findBestExp();
        }
    });
    String s = RelOptUtil.dumpPlan("", x, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES);
    assertThat(Util.toLinux(s), equalTo("EnumerableFilter(condition=[>($1, 1)])\n" + "  EnumerableTableScan(table=[[myTable]])\n"));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelOptSchema(org.apache.calcite.plan.RelOptSchema) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) Table(org.apache.calcite.schema.Table) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) RelNode(org.apache.calcite.rel.RelNode) EnumerableTableScan(org.apache.calcite.adapter.enumerable.EnumerableTableScan) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Aggregations

RelTraitSet (org.apache.calcite.plan.RelTraitSet)190 RelNode (org.apache.calcite.rel.RelNode)111 RelOptCluster (org.apache.calcite.plan.RelOptCluster)38 RelCollation (org.apache.calcite.rel.RelCollation)35 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)26 RexNode (org.apache.calcite.rex.RexNode)24 ArrayList (java.util.ArrayList)20 InvalidRelException (org.apache.calcite.rel.InvalidRelException)19 RelDataType (org.apache.calcite.rel.type.RelDataType)14 SqlNode (org.apache.calcite.sql.SqlNode)14 List (java.util.List)13 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)13 RelOptTable (org.apache.calcite.plan.RelOptTable)11 Sort (org.apache.calcite.rel.core.Sort)11 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)11 ImmutableList (com.google.common.collect.ImmutableList)10 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)10 Test (org.junit.Test)9 Table (org.apache.calcite.schema.Table)8 RexInputRef (org.apache.calcite.rex.RexInputRef)7