Search in sources :

Example 1 with HrClusteredSchema

use of org.apache.calcite.schemas.HrClusteredSchema in project calcite by apache.

the class EnumerableLimitRuleTest method enumerableLimitOnEmptySort.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2941">[CALCITE-2941]
 * EnumerableLimitRule on Sort with no collation creates EnumerableLimit with
 * wrong traitSet and cluster</a>.
 */
@Test
void enumerableLimitOnEmptySort() throws Exception {
    RuleSet prepareRules = RuleSets.ofList(EnumerableRules.ENUMERABLE_FILTER_RULE, EnumerableRules.ENUMERABLE_SORT_RULE, EnumerableRules.ENUMERABLE_LIMIT_RULE, EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE);
    SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    SchemaPlus defSchema = rootSchema.add("hr", new HrClusteredSchema());
    FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(defSchema).traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE).programs(Programs.of(prepareRules)).build();
    RelBuilder builder = RelBuilder.create(config);
    RelNode planBefore = builder.scan("hr", "emps").sort(// will produce collation [0] in the plan
    builder.field(0)).filter(builder.notEquals(builder.field(0), builder.literal(100))).limit(1, // force a limit inside an "empty" Sort (with no collation)
    5).build();
    RelTraitSet desiredTraits = planBefore.getTraitSet().replace(EnumerableConvention.INSTANCE);
    Program program = Programs.of(prepareRules);
    RelNode planAfter = program.run(planBefore.getCluster().getPlanner(), planBefore, desiredTraits, ImmutableList.of(), ImmutableList.of());
    // verify that the collation [0] is not lost in the final plan
    final RelCollation collation = planAfter.getTraitSet().getTrait(RelCollationTraitDef.INSTANCE);
    assertThat(collation, notNullValue());
    final List<RelFieldCollation> fieldCollationList = collation.getFieldCollations();
    assertThat(fieldCollationList, notNullValue());
    assertThat(fieldCollationList.size(), is(1));
    assertThat(fieldCollationList.get(0).getFieldIndex(), is(0));
}
Also used : RuleSet(org.apache.calcite.tools.RuleSet) RelCollation(org.apache.calcite.rel.RelCollation) RelBuilder(org.apache.calcite.tools.RelBuilder) Program(org.apache.calcite.tools.Program) RelNode(org.apache.calcite.rel.RelNode) SchemaPlus(org.apache.calcite.schema.SchemaPlus) HrClusteredSchema(org.apache.calcite.schemas.HrClusteredSchema) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) RelTraitSet(org.apache.calcite.plan.RelTraitSet) Test(org.junit.jupiter.api.Test)

Example 2 with HrClusteredSchema

use of org.apache.calcite.schemas.HrClusteredSchema in project calcite by apache.

the class SortRemoveRuleTest method transform.

/**
 * The default schema that is used in these tests provides tables sorted on the primary key. Due
 * to this scan operators always come with a {@link org.apache.calcite.rel.RelCollation} trait.
 */
private RelNode transform(String sql, RuleSet prepareRules) throws Exception {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final SchemaPlus defSchema = rootSchema.add("hr", new HrClusteredSchema());
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(defSchema).traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE).programs(Programs.of(prepareRules), Programs.ofRules(CoreRules.SORT_REMOVE)).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelRoot planRoot = planner.rel(validate);
    RelNode planBefore = planRoot.rel;
    RelTraitSet desiredTraits = planBefore.getTraitSet().replace(EnumerableConvention.INSTANCE);
    RelNode planAfter = planner.transform(0, desiredTraits, planBefore);
    return planner.transform(1, desiredTraits, planAfter);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) SchemaPlus(org.apache.calcite.schema.SchemaPlus) HrClusteredSchema(org.apache.calcite.schemas.HrClusteredSchema) Planner(org.apache.calcite.tools.Planner) RelRoot(org.apache.calcite.rel.RelRoot) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 RelNode (org.apache.calcite.rel.RelNode)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 HrClusteredSchema (org.apache.calcite.schemas.HrClusteredSchema)2 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)2 RelCollation (org.apache.calcite.rel.RelCollation)1 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)1 RelRoot (org.apache.calcite.rel.RelRoot)1 SqlNode (org.apache.calcite.sql.SqlNode)1 Planner (org.apache.calcite.tools.Planner)1 Program (org.apache.calcite.tools.Program)1 RelBuilder (org.apache.calcite.tools.RelBuilder)1 RuleSet (org.apache.calcite.tools.RuleSet)1 Test (org.junit.jupiter.api.Test)1