Search in sources :

Example 26 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 executeQuery.

private void executeQuery(FrameworkConfig config, @SuppressWarnings("SameParameterValue") String query, boolean debug) throws RelConversionException, SqlParseException, ValidationException {
    Planner planner = Frameworks.getPlanner(config);
    if (debug) {
        System.out.println("Query:" + query);
    }
    SqlNode n = planner.parse(query);
    n = planner.validate(n);
    RelNode root = planner.rel(n).project();
    if (debug) {
        System.out.println(RelOptUtil.dumpPlan("-- Logical Plan", root, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES));
    }
    RelOptCluster cluster = root.getCluster();
    final RelOptPlanner optPlanner = cluster.getPlanner();
    RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
    final RelNode newRoot = optPlanner.changeTraits(root, desiredTraits);
    if (debug) {
        System.out.println(RelOptUtil.dumpPlan("-- Mid Plan", newRoot, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES));
    }
    optPlanner.setRoot(newRoot);
    RelNode bestExp = optPlanner.findBestExp();
    if (debug) {
        System.out.println(RelOptUtil.dumpPlan("-- Best Plan", bestExp, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES));
    }
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelNode(org.apache.calcite.rel.RelNode) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) SqlNode(org.apache.calcite.sql.SqlNode)

Example 27 with RelTraitSet

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

the class PlannerTest method testPlan.

/**
 * Unit test that parses, validates, converts and plans.
 */
@Test
public void testPlan() throws Exception {
    Program program = Programs.ofRules(FilterMergeRule.INSTANCE, EnumerableRules.ENUMERABLE_FILTER_RULE, EnumerableRules.ENUMERABLE_PROJECT_RULE);
    Planner planner = getPlanner(null, program);
    SqlNode parse = planner.parse("select * from \"emps\"");
    SqlNode validate = planner.validate(parse);
    RelNode convert = planner.rel(validate).project();
    RelTraitSet traitSet = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
    RelNode transform = planner.transform(0, traitSet, convert);
    assertThat(toString(transform), equalTo("EnumerableProject(empid=[$0], deptno=[$1], name=[$2], salary=[$3], commission=[$4])\n" + "  EnumerableTableScan(table=[[hr, emps]])\n"));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 28 with RelTraitSet

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

the class PlannerTest method testDuplicateSortPlanWORemoveSortRule.

/**
 * Unit test that parses, validates, converts and
 * plans for query using two duplicate order by.
 */
@Test
public void testDuplicateSortPlanWORemoveSortRule() throws Exception {
    RuleSet ruleSet = RuleSets.ofList(EnumerableRules.ENUMERABLE_PROJECT_RULE, EnumerableRules.ENUMERABLE_SORT_RULE);
    Planner planner = getPlanner(null, Programs.of(ruleSet));
    SqlNode parse = planner.parse("select \"empid\" from ( " + "select * " + "from \"emps\" " + "order by \"emps\".\"deptno\") " + "order by \"deptno\"");
    SqlNode validate = planner.validate(parse);
    RelNode convert = planner.rel(validate).rel;
    RelTraitSet traitSet = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
    RelNode transform = planner.transform(0, traitSet, convert);
    assertThat(toString(transform), equalTo("EnumerableSort(sort0=[$1], dir0=[ASC])\n" + "  EnumerableProject(empid=[$0], deptno=[$1])\n" + "    EnumerableSort(sort0=[$1], dir0=[ASC])\n" + "      EnumerableProject(empid=[$0], deptno=[$1], name=[$2], salary=[$3], commission=[$4])\n" + "        EnumerableTableScan(table=[[hr, emps]])\n"));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 29 with RelTraitSet

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

the class PlannerTest method runDuplicateSortCheck.

// If proper "SqlParseException, ValidationException, RelConversionException"
// is used, then checkstyle fails with
// "Redundant throws: 'ValidationException' listed more then one time"
// "Redundant throws: 'RelConversionException' listed more then one time"
private void runDuplicateSortCheck(String sql, String plan) throws Exception {
    RuleSet ruleSet = RuleSets.ofList(SortRemoveRule.INSTANCE, EnumerableRules.ENUMERABLE_PROJECT_RULE, EnumerableRules.ENUMERABLE_WINDOW_RULE, EnumerableRules.ENUMERABLE_SORT_RULE, ProjectToWindowRule.PROJECT);
    Planner planner = getPlanner(null, SqlParser.configBuilder().setLex(Lex.JAVA).build(), Programs.of(ruleSet));
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelNode convert = planner.rel(validate).rel;
    RelTraitSet traitSet = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
    if (traitSet.getTrait(RelCollationTraitDef.INSTANCE) == null) {
        // SortRemoveRule can only work if collation trait is enabled.
        return;
    }
    RelNode transform = planner.transform(0, traitSet, convert);
    assertThat(toString(transform), equalTo(plan));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 30 with RelTraitSet

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

the class SortRemoveRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    if (!call.getPlanner().getRelTraitDefs().contains(RelCollationTraitDef.INSTANCE)) {
        // Collation is not an active trait.
        return;
    }
    final Sort sort = call.rel(0);
    if (sort.offset != null || sort.fetch != null) {
        // Don't remove sort if would also remove OFFSET or LIMIT.
        return;
    }
    // Express the "sortedness" requirement in terms of a collation trait and
    // we can get rid of the sort. This allows us to use rels that just happen
    // to be sorted but get the same effect.
    final RelCollation collation = sort.getCollation();
    assert collation == sort.getTraitSet().getTrait(RelCollationTraitDef.INSTANCE);
    final RelTraitSet traits = sort.getInput().getTraitSet().replace(collation);
    call.transformTo(convert(sort.getInput(), traits));
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) Sort(org.apache.calcite.rel.core.Sort) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

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)36 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