Search in sources :

Example 16 with RelTraitSet

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

the class EnumerableJoinRule method convert.

@Override
public RelNode convert(RelNode rel) {
    LogicalJoin join = (LogicalJoin) rel;
    List<RelNode> newInputs = new ArrayList<>();
    for (RelNode input : join.getInputs()) {
        if (!(input.getConvention() instanceof EnumerableConvention)) {
            input = convert(input, input.getTraitSet().replace(EnumerableConvention.INSTANCE));
        }
        newInputs.add(input);
    }
    final RelOptCluster cluster = join.getCluster();
    final RelTraitSet traitSet = join.getTraitSet().replace(EnumerableConvention.INSTANCE);
    final RelNode left = newInputs.get(0);
    final RelNode right = newInputs.get(1);
    final JoinInfo info = JoinInfo.of(left, right, join.getCondition());
    if (!info.isEqui() && join.getJoinType() != JoinRelType.INNER) {
        // if it is an inner join.
        try {
            return new EnumerableThetaJoin(cluster, traitSet, left, right, join.getCondition(), join.getVariablesSet(), join.getJoinType());
        } catch (InvalidRelException e) {
            EnumerableRules.LOGGER.debug(e.toString());
            return null;
        }
    }
    RelNode newRel;
    try {
        newRel = new EnumerableJoin(cluster, join.getTraitSet().replace(EnumerableConvention.INSTANCE), left, right, info.getEquiCondition(left, right, cluster.getRexBuilder()), info.leftKeys, info.rightKeys, join.getVariablesSet(), join.getJoinType());
    } catch (InvalidRelException e) {
        EnumerableRules.LOGGER.debug(e.toString());
        return null;
    }
    if (!info.isEqui()) {
        newRel = new EnumerableFilter(cluster, newRel.getTraitSet(), newRel, info.getRemaining(cluster.getRexBuilder()));
    }
    return newRel;
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) JoinInfo(org.apache.calcite.rel.core.JoinInfo) InvalidRelException(org.apache.calcite.rel.InvalidRelException) RelNode(org.apache.calcite.rel.RelNode) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 17 with RelTraitSet

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

the class EnumerableLimitRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final Sort sort = call.rel(0);
    if (sort.offset == null && sort.fetch == null) {
        return;
    }
    final RelTraitSet traitSet = sort.getTraitSet().replace(EnumerableConvention.INSTANCE);
    RelNode input = sort.getInput();
    if (!sort.getCollation().getFieldCollations().isEmpty()) {
        // Create a sort with the same sort key, but no offset or fetch.
        input = sort.copy(sort.getTraitSet(), input, sort.getCollation(), null, null);
    }
    RelNode x = convert(input, input.getTraitSet().replace(EnumerableConvention.INSTANCE));
    call.transformTo(new EnumerableLimit(sort.getCluster(), traitSet, x, sort.offset, sort.fetch));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) Sort(org.apache.calcite.rel.core.Sort) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 18 with RelTraitSet

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

the class EnumerableProject method create.

/**
 * Creates an EnumerableProject, specifying row type rather than field
 * names.
 */
public static EnumerableProject create(final RelNode input, final List<? extends RexNode> projects, RelDataType rowType) {
    final RelOptCluster cluster = input.getCluster();
    final RelMetadataQuery mq = cluster.getMetadataQuery();
    final RelTraitSet traitSet = cluster.traitSet().replace(EnumerableConvention.INSTANCE).replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {

        public List<RelCollation> get() {
            return RelMdCollation.project(mq, input, projects);
        }
    });
    return new EnumerableProject(cluster, traitSet, input, projects, rowType);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) List(java.util.List) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 19 with RelTraitSet

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

the class EnumerableCorrelateRule method convert.

public RelNode convert(RelNode rel) {
    final LogicalCorrelate c = (LogicalCorrelate) rel;
    final RelTraitSet traitSet = c.getTraitSet().replace(EnumerableConvention.INSTANCE);
    return new EnumerableCorrelate(rel.getCluster(), traitSet, convert(c.getLeft(), c.getLeft().getTraitSet().replace(EnumerableConvention.INSTANCE)), convert(c.getRight(), c.getRight().getTraitSet().replace(EnumerableConvention.INSTANCE)), c.getCorrelationId(), c.getRequiredColumns(), c.getJoinType());
}
Also used : LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 20 with RelTraitSet

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

the class Programs method heuristicJoinOrder.

/**
 * Creates a program that invokes heuristic join-order optimization
 * (via {@link org.apache.calcite.rel.rules.JoinToMultiJoinRule},
 * {@link org.apache.calcite.rel.rules.MultiJoin} and
 * {@link org.apache.calcite.rel.rules.LoptOptimizeJoinRule})
 * if there are 6 or more joins (7 or more relations).
 */
public static Program heuristicJoinOrder(final Iterable<? extends RelOptRule> rules, final boolean bushy, final int minJoinCount) {
    return new Program() {

        public RelNode run(RelOptPlanner planner, RelNode rel, RelTraitSet requiredOutputTraits, List<RelOptMaterialization> materializations, List<RelOptLattice> lattices) {
            final int joinCount = RelOptUtil.countJoins(rel);
            final Program program;
            if (joinCount < minJoinCount) {
                program = ofRules(rules);
            } else {
                // Create a program that gathers together joins as a MultiJoin.
                final HepProgram hep = new HepProgramBuilder().addRuleInstance(FilterJoinRule.FILTER_ON_JOIN).addMatchOrder(HepMatchOrder.BOTTOM_UP).addRuleInstance(JoinToMultiJoinRule.INSTANCE).build();
                final Program program1 = of(hep, false, DefaultRelMetadataProvider.INSTANCE);
                // Create a program that contains a rule to expand a MultiJoin
                // into heuristically ordered joins.
                // We use the rule set passed in, but remove JoinCommuteRule and
                // JoinPushThroughJoinRule, because they cause exhaustive search.
                final List<RelOptRule> list = Lists.newArrayList(rules);
                list.removeAll(ImmutableList.of(JoinCommuteRule.INSTANCE, JoinAssociateRule.INSTANCE, JoinPushThroughJoinRule.LEFT, JoinPushThroughJoinRule.RIGHT));
                list.add(bushy ? MultiJoinOptimizeBushyRule.INSTANCE : LoptOptimizeJoinRule.INSTANCE);
                final Program program2 = ofRules(list);
                program = sequence(program1, program2);
            }
            return program.run(planner, rel, requiredOutputTraits, materializations, lattices);
        }
    };
}
Also used : HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgram(org.apache.calcite.plan.hep.HepProgram) RelNode(org.apache.calcite.rel.RelNode) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule)

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