Search in sources :

Example 1 with LogicalFilter

use of org.apache.calcite.rel.logical.LogicalFilter in project storm by apache.

the class TestRelNodeCompiler method testFilter.

@Test
public void testFilter() throws Exception {
    String sql = "SELECT ID + 1 FROM FOO WHERE ID > 3";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverDummyTable(sql);
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    LogicalProject project = (LogicalProject) state.tree();
    LogicalFilter filter = (LogicalFilter) project.getInput();
    try (StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw)) {
        RelNodeCompiler compiler = new RelNodeCompiler(pw, typeFactory);
        // standalone mode doesn't use inputstreams argument
        compiler.visitFilter(filter, Collections.EMPTY_LIST);
        pw.flush();
        Assert.assertThat(sw.toString(), containsString("> 3"));
    }
    try (StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw)) {
        RelNodeCompiler compiler = new RelNodeCompiler(pw, typeFactory);
        // standalone mode doesn't use inputstreams argument
        compiler.visitProject(project, Collections.EMPTY_LIST);
        pw.flush();
        Assert.assertThat(sw.toString(), containsString(" + 1"));
    }
}
Also used : StringWriter(java.io.StringWriter) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with LogicalFilter

use of org.apache.calcite.rel.logical.LogicalFilter in project calcite by apache.

the class FilterCalcMergeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final LogicalFilter filter = call.rel(0);
    final LogicalCalc calc = call.rel(1);
    // We'll have chance to merge later, when the over is expanded.
    if (calc.getProgram().containsAggs()) {
        return;
    }
    // Create a program containing the filter.
    final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
    final RexProgramBuilder progBuilder = new RexProgramBuilder(calc.getRowType(), rexBuilder);
    progBuilder.addIdentity();
    progBuilder.addCondition(filter.getCondition());
    RexProgram topProgram = progBuilder.getProgram();
    RexProgram bottomProgram = calc.getProgram();
    // Merge the programs together.
    RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder);
    final LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), mergedProgram);
    call.transformTo(newCalc);
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RexBuilder(org.apache.calcite.rex.RexBuilder) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder)

Example 3 with LogicalFilter

use of org.apache.calcite.rel.logical.LogicalFilter in project calcite by apache.

the class FilterMultiJoinMergeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    LogicalFilter filter = call.rel(0);
    MultiJoin multiJoin = call.rel(1);
    MultiJoin newMultiJoin = new MultiJoin(multiJoin.getCluster(), multiJoin.getInputs(), multiJoin.getJoinFilter(), multiJoin.getRowType(), multiJoin.isFullOuterJoin(), multiJoin.getOuterJoinConditions(), multiJoin.getJoinTypes(), multiJoin.getProjFields(), multiJoin.getJoinFieldRefCountsMap(), filter.getCondition());
    call.transformTo(newMultiJoin);
}
Also used : LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter)

Example 4 with LogicalFilter

use of org.apache.calcite.rel.logical.LogicalFilter 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)

Example 5 with LogicalFilter

use of org.apache.calcite.rel.logical.LogicalFilter in project calcite by apache.

the class SemiJoinFilterTransposeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    SemiJoin semiJoin = call.rel(0);
    LogicalFilter filter = call.rel(1);
    RelNode newSemiJoin = SemiJoin.create(filter.getInput(), semiJoin.getRight(), semiJoin.getCondition(), semiJoin.getLeftKeys(), semiJoin.getRightKeys());
    final RelFactories.FilterFactory factory = RelFactories.DEFAULT_FILTER_FACTORY;
    RelNode newFilter = factory.createFilter(newSemiJoin, filter.getCondition());
    call.transformTo(newFilter);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SemiJoin(org.apache.calcite.rel.core.SemiJoin) RelFactories(org.apache.calcite.rel.core.RelFactories)

Aggregations

LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)23 RelNode (org.apache.calcite.rel.RelNode)14 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)9 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)7 RexBuilder (org.apache.calcite.rex.RexBuilder)6 Test (org.junit.Test)6 LogicalTableScan (org.apache.calcite.rel.logical.LogicalTableScan)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 RexNode (org.apache.calcite.rex.RexNode)5 RelRoot (org.apache.calcite.rel.RelRoot)3 RelFactories (org.apache.calcite.rel.core.RelFactories)3 TableScan (org.apache.calcite.rel.core.TableScan)3 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)2 RelOptCluster (org.apache.calcite.plan.RelOptCluster)2 LogicalCalc (org.apache.calcite.rel.logical.LogicalCalc)2 RexCall (org.apache.calcite.rex.RexCall)2