Search in sources :

Example 16 with Project

use of org.apache.calcite.rel.core.Project in project calcite by apache.

the class RelMetadataTest method testCorrelateUniqueKeys.

@Test
public void testCorrelateUniqueKeys() {
    final String sql = "select *\n" + "from (select distinct deptno from emp) as e,\n" + "  lateral (\n" + "    select * from dept where dept.deptno = e.deptno)";
    final RelNode rel = convertSql(sql);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    assertThat(rel, isA((Class) Project.class));
    final Project project = (Project) rel;
    final Set<ImmutableBitSet> result = mq.getUniqueKeys(project);
    assertThat(result, sortsAs("[{0}]"));
    if (false) {
        assertUniqueConsistent(project);
    }
    assertThat(project.getInput(), isA((Class) Correlate.class));
    final Correlate correlate = (Correlate) project.getInput();
    final Set<ImmutableBitSet> result2 = mq.getUniqueKeys(correlate);
    assertThat(result2, sortsAs("[{0}]"));
    if (false) {
        assertUniqueConsistent(correlate);
    }
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) Correlate(org.apache.calcite.rel.core.Correlate) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Test(org.junit.Test)

Example 17 with Project

use of org.apache.calcite.rel.core.Project in project calcite by apache.

the class RelMetadataTest method testAllPredicates.

@Test
public void testAllPredicates() {
    final Project rel = (Project) convertSql("select * from emp, dept");
    final Join join = (Join) rel.getInput();
    final RelOptTable empTable = join.getInput(0).getTable();
    final RelOptTable deptTable = join.getInput(1).getTable();
    Frameworks.withPlanner(new Frameworks.PlannerAction<Void>() {

        public Void apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            checkAllPredicates(cluster, empTable, deptTable);
            return null;
        }
    });
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RelOptSchema(org.apache.calcite.plan.RelOptSchema) Frameworks(org.apache.calcite.tools.Frameworks) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SemiJoin(org.apache.calcite.rel.core.SemiJoin) Join(org.apache.calcite.rel.core.Join) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) EnumerableMergeJoin(org.apache.calcite.adapter.enumerable.EnumerableMergeJoin) RelOptTable(org.apache.calcite.plan.RelOptTable) Test(org.junit.Test)

Example 18 with Project

use of org.apache.calcite.rel.core.Project in project calcite by apache.

the class RelOptRulesTest method testAggregateExtractProjectRuleWithFilter.

@Test
public void testAggregateExtractProjectRuleWithFilter() {
    final String sql = "select sum(sal) filter (where empno = 40)\n" + "from emp";
    HepProgram pre = new HepProgramBuilder().addRuleInstance(AggregateProjectMergeRule.INSTANCE).build();
    // AggregateProjectMergeRule does not merges Project with Filter.
    // Force match Aggregate on top of Project once explicitly in unit test.
    final AggregateExtractProjectRule rule = new AggregateExtractProjectRule(operand(Aggregate.class, operand(Project.class, null, new PredicateImpl<Project>() {

        int matchCount = 0;

        public boolean test(@Nullable Project project) {
            return matchCount++ == 0;
        }
    }, none())), RelFactories.LOGICAL_BUILDER);
    sql(sql).withPre(pre).withRule(rule).checkUnchanged();
}
Also used : Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) HepProgram(org.apache.calcite.plan.hep.HepProgram) AggregateExtractProjectRule(org.apache.calcite.rel.rules.AggregateExtractProjectRule) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Aggregate(org.apache.calcite.rel.core.Aggregate) Test(org.junit.Test)

Example 19 with Project

use of org.apache.calcite.rel.core.Project in project calcite by apache.

the class ProjectSetOpTransposeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    LogicalProject origProj = call.rel(0);
    SetOp setOp = call.rel(1);
    // cannot push project past a distinct
    if (!setOp.all) {
        return;
    }
    // locate all fields referenced in the projection
    PushProjector pushProject = new PushProjector(origProj, null, setOp, preserveExprCondition, call.builder());
    pushProject.locateAllRefs();
    List<RelNode> newSetOpInputs = new ArrayList<>();
    int[] adjustments = pushProject.getAdjustments();
    // and it is the only operator this rule currently acts on
    for (RelNode input : setOp.getInputs()) {
        // be lazy:  produce two ProjectRels, and let another rule
        // merge them (could probably just clone origProj instead?)
        Project p = pushProject.createProjectRefsAndExprs(input, true, false);
        newSetOpInputs.add(pushProject.createNewProject(p, adjustments));
    }
    // create a new setop whose children are the ProjectRels created above
    SetOp newSetOp = setOp.copy(setOp.getTraitSet(), newSetOpInputs);
    call.transformTo(newSetOp);
}
Also used : Project(org.apache.calcite.rel.core.Project) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) SetOp(org.apache.calcite.rel.core.SetOp) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Example 20 with Project

use of org.apache.calcite.rel.core.Project in project calcite by apache.

the class ProjectSortTransposeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final Project project = call.rel(0);
    final Sort sort = call.rel(1);
    if (sort.getClass() != Sort.class) {
        return;
    }
    RelNode newProject = project.copy(project.getTraitSet(), ImmutableList.of(sort.getInput()));
    final Sort newSort = sort.copy(sort.getTraitSet(), newProject, sort.getCollation(), sort.offset, sort.fetch);
    call.transformTo(newSort);
}
Also used : Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode) Sort(org.apache.calcite.rel.core.Sort)

Aggregations

Project (org.apache.calcite.rel.core.Project)143 RexNode (org.apache.calcite.rex.RexNode)77 RelNode (org.apache.calcite.rel.RelNode)71 ArrayList (java.util.ArrayList)46 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)35 RexBuilder (org.apache.calcite.rex.RexBuilder)28 RelDataType (org.apache.calcite.rel.type.RelDataType)26 Aggregate (org.apache.calcite.rel.core.Aggregate)22 Filter (org.apache.calcite.rel.core.Filter)22 Join (org.apache.calcite.rel.core.Join)22 List (java.util.List)19 RexLiteral (org.apache.calcite.rex.RexLiteral)19 AggregateCall (org.apache.calcite.rel.core.AggregateCall)18 Sort (org.apache.calcite.rel.core.Sort)18 Test (org.junit.Test)18 RelBuilder (org.apache.calcite.tools.RelBuilder)17 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)16 HiveProject (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject)16 Collectors (java.util.stream.Collectors)15 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)15