Search in sources :

Example 21 with LogicalFilter

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

the class FilterToCalcRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final LogicalFilter filter = call.rel(0);
    final RelNode rel = filter.getInput();
    // Create a program containing a filter.
    final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
    final RelDataType inputRowType = rel.getRowType();
    final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    programBuilder.addIdentity();
    programBuilder.addCondition(filter.getCondition());
    final RexProgram program = programBuilder.getProgram();
    final LogicalCalc calc = LogicalCalc.create(rel, program);
    call.transformTo(calc);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RexProgram(org.apache.calcite.rex.RexProgram) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder)

Example 22 with LogicalFilter

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

the class RelWriterTest method testWriter.

/**
 * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonWriter} on
 * a simple tree of relational expressions, consisting of a table, a filter
 * and an aggregate node.
 */
@Test
public void testWriter() {
    String s = Frameworks.withPlanner(new Frameworks.PlannerAction<String>() {

        public String apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
            LogicalTableScan scan = LogicalTableScan.create(cluster, relOptSchema.getTableForMember(Arrays.asList("hr", "emps")));
            final RexBuilder rexBuilder = cluster.getRexBuilder();
            LogicalFilter filter = LogicalFilter.create(scan, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(scan), "deptno", true), rexBuilder.makeExactLiteral(BigDecimal.TEN)));
            final RelJsonWriter writer = new RelJsonWriter();
            final RelDataType bigIntType = cluster.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
            LogicalAggregate aggregate = LogicalAggregate.create(filter, ImmutableBitSet.of(0), null, ImmutableList.of(AggregateCall.create(SqlStdOperatorTable.COUNT, true, false, ImmutableList.of(1), -1, bigIntType, "c"), AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, ImmutableList.<Integer>of(), -1, bigIntType, "d")));
            aggregate.explain(writer);
            return writer.asString();
        }
    });
    assertThat(s, is(XX));
}
Also used : LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) RelDataType(org.apache.calcite.rel.type.RelDataType) RelJsonWriter(org.apache.calcite.rel.externalize.RelJsonWriter) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) JdbcTest(org.apache.calcite.test.JdbcTest) Frameworks(org.apache.calcite.tools.Frameworks) RexBuilder(org.apache.calcite.rex.RexBuilder) JdbcTest(org.apache.calcite.test.JdbcTest) Test(org.junit.Test)

Example 23 with LogicalFilter

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

the class SplunkPushDownRule method onMatch.

// ~ Methods --------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    LOGGER.debug(description);
    int relLength = call.rels.length;
    SplunkTableScan splunkRel = (SplunkTableScan) call.rels[relLength - 1];
    LogicalFilter filter;
    LogicalProject topProj = null;
    LogicalProject bottomProj = null;
    RelDataType topRow = splunkRel.getRowType();
    int filterIdx = 2;
    if (call.rels[relLength - 2] instanceof LogicalProject) {
        bottomProj = (LogicalProject) call.rels[relLength - 2];
        filterIdx = 3;
        // bottom projection will change the field count/order
        topRow = bottomProj.getRowType();
    }
    String filterString;
    if (filterIdx <= relLength && call.rels[relLength - filterIdx] instanceof LogicalFilter) {
        filter = (LogicalFilter) call.rels[relLength - filterIdx];
        int topProjIdx = filterIdx + 1;
        if (topProjIdx <= relLength && call.rels[relLength - topProjIdx] instanceof LogicalProject) {
            topProj = (LogicalProject) call.rels[relLength - topProjIdx];
        }
        RexCall filterCall = (RexCall) filter.getCondition();
        SqlOperator op = filterCall.getOperator();
        List<RexNode> operands = filterCall.getOperands();
        LOGGER.debug("fieldNames: {}", getFieldsString(topRow));
        final StringBuilder buf = new StringBuilder();
        if (getFilter(op, operands, buf, topRow.getFieldNames())) {
            filterString = buf.toString();
        } else {
            // can't handle
            return;
        }
    } else {
        filterString = "";
    }
    // top projection will change the field count/order
    if (topProj != null) {
        topRow = topProj.getRowType();
    }
    LOGGER.debug("pre transformTo fieldNames: {}", getFieldsString(topRow));
    call.transformTo(appendSearchString(filterString, splunkRel, topProj, bottomProj, topRow, null));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) NlsString(org.apache.calcite.util.NlsString) RexNode(org.apache.calcite.rex.RexNode)

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