Search in sources :

Example 11 with LogicalFilter

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

the class FlinkSemiAntiJoinFilterTransposeRule method onMatch.

// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    LogicalJoin join = call.rel(0);
    LogicalFilter filter = call.rel(1);
    RelNode newJoin = LogicalJoin.create(filter.getInput(), join.getRight(), join.getHints(), join.getCondition(), join.getVariablesSet(), join.getJoinType());
    final RelFactories.FilterFactory factory = RelFactories.DEFAULT_FILTER_FACTORY;
    RelNode newFilter = factory.createFilter(newJoin, filter.getCondition());
    call.transformTo(newFilter);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelFactories(org.apache.calcite.rel.core.RelFactories)

Example 12 with LogicalFilter

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

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) RelNodeCompiler(com.hortonworks.streamline.streams.sql.compiler.RelNodeCompiler) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 13 with LogicalFilter

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

the class QueryConverter method hasNestedExists.

private static boolean hasNestedExists(RelNode root) {
    class NestedExistsFinder extends RelVisitor {

        private boolean found;

        private int depth;

        @Override
        public void visit(RelNode node, int ordinal, @Nullable RelNode parent) {
            if (node instanceof LogicalFilter) {
                RexSubQuery exists = getExists((LogicalFilter) node);
                if (exists != null) {
                    found |= depth > 0;
                    depth++;
                    go(exists.rel);
                    depth--;
                }
            }
            super.visit(node, ordinal, parent);
        }

        private boolean find() {
            go(root);
            return found;
        }

        private RexSubQuery getExists(LogicalFilter filter) {
            RexSubQuery[] existsSubQuery = { null };
            filter.getCondition().accept(new RexVisitorImpl<Void>(true) {

                @Override
                public Void visitSubQuery(RexSubQuery subQuery) {
                    if (subQuery.getKind() == SqlKind.EXISTS) {
                        existsSubQuery[0] = subQuery;
                    }
                    return super.visitSubQuery(subQuery);
                }
            });
            return existsSubQuery[0];
        }
    }
    return new NestedExistsFinder().find();
}
Also used : RelNode(org.apache.calcite.rel.RelNode) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelVisitor(org.apache.calcite.rel.RelVisitor) RexSubQuery(org.apache.calcite.rex.RexSubQuery) Nullable(javax.annotation.Nullable)

Example 14 with LogicalFilter

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

the class TestQueryPlanner method testRemoteJoinWithFilterHelper.

void testRemoteJoinWithFilterHelper(boolean enableOptimizer) throws SamzaSqlValidatorException {
    Map<String, String> staticConfigs = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(1);
    String sql = "Insert into testavro.enrichedPageViewTopic " + "select pv.pageKey as __key__, pv.pageKey as pageKey, coalesce(null, 'N/A') as companyName," + "       p.name as profileName, p.address as profileAddress " + "from testavro.PAGEVIEW as pv " + "join testRemoteStore.Profile.`$table` as p " + " on p.__key__ = pv.profileId" + " where p.name = pv.pageKey AND p.name = 'Mike' AND pv.profileId = 1";
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMT, sql);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_ENABLE_PLAN_OPTIMIZER, Boolean.toString(enableOptimizer));
    Config samzaConfig = new MapConfig(staticConfigs);
    DslConverter dslConverter = new SamzaSqlDslConverterFactory().create(samzaConfig);
    Collection<RelRoot> relRoots = dslConverter.convertDsl(sql);
    /*
      Query plan without optimization:
      LogicalProject(__key__=[$1], pageKey=[$1], companyName=['N/A'], profileName=[$5], profileAddress=[$7])
        LogicalFilter(condition=[AND(=($5, $1), =($5, 'Mike'), =($2, 1))])
          LogicalJoin(condition=[=($3, $2)], joinType=[inner])
            LogicalTableScan(table=[[testavro, PAGEVIEW]])
            LogicalTableScan(table=[[testRemoteStore, Profile, $table]])

      Query plan with optimization:
      LogicalProject(__key__=[$1], pageKey=[$1], companyName=['N/A'], profileName=[$5], profileAddress=[$7])
        LogicalFilter(condition=[AND(=($5, $1), =($5, 'Mike'))])
          LogicalJoin(condition=[=($3, $2)], joinType=[inner])
            LogicalFilter(condition=[=($2, 1)])
              LogicalTableScan(table=[[testavro, PAGEVIEW]])
            LogicalTableScan(table=[[testRemoteStore, Profile, $table]])
     */
    assertEquals(1, relRoots.size());
    RelRoot relRoot = relRoots.iterator().next();
    RelNode relNode = relRoot.rel;
    assertTrue(relNode instanceof LogicalProject);
    relNode = relNode.getInput(0);
    assertTrue(relNode instanceof LogicalFilter);
    if (enableOptimizer) {
        assertEquals("AND(=($1, $5), =($5, 'Mike'))", ((LogicalFilter) relNode).getCondition().toString());
    } else {
        assertEquals("AND(=(1, $2), =($1, $5), =($5, 'Mike'))", ((LogicalFilter) relNode).getCondition().toString());
    }
    relNode = relNode.getInput(0);
    assertTrue(relNode instanceof LogicalJoin);
    assertEquals(2, relNode.getInputs().size());
    LogicalJoin join = (LogicalJoin) relNode;
    RelNode left = join.getLeft();
    RelNode right = join.getRight();
    assertTrue(right instanceof LogicalTableScan);
    if (enableOptimizer) {
        assertTrue(left instanceof LogicalFilter);
        assertEquals("=(1, $2)", ((LogicalFilter) left).getCondition().toString());
        assertTrue(left.getInput(0) instanceof LogicalTableScan);
    } else {
        assertTrue(left instanceof LogicalTableScan);
    }
}
Also used : DslConverter(org.apache.samza.sql.interfaces.DslConverter) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SamzaSqlDslConverterFactory(org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory) RelRoot(org.apache.calcite.rel.RelRoot) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) RelNode(org.apache.calcite.rel.RelNode) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) MapConfig(org.apache.samza.config.MapConfig) LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Example 15 with LogicalFilter

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

the class JoinTranslator method isValidRemoteJoinRef.

/**
 * Helper method to check if the join condition can be evaluated by the remote table.
 * It does follow single path  using the index ref path checking if it is a simple reference all the way to table scan.
 * In case any RexCall is encountered will stop an return null as a marker otherwise will return Column Name.
 *
 * @param inputRexIndex rex ref index
 * @param relNode current Rel Node
 * @return false if any Relational Expression is encountered on the path, true if is simple ref to __key__ column.
 */
private static boolean isValidRemoteJoinRef(int inputRexIndex, RelNode relNode) {
    if (relNode instanceof TableScan) {
        return relNode.getRowType().getFieldList().get(inputRexIndex).getName().equals(SamzaSqlRelMessage.KEY_NAME);
    }
    // has to be a single rel kind filter/project/table scan
    Preconditions.checkState(relNode.getInputs().size() == 1, "Has to be single input RelNode and got " + relNode.getDigest());
    if (relNode instanceof LogicalFilter) {
        return isValidRemoteJoinRef(inputRexIndex, relNode.getInput(0));
    }
    RexNode inputRef = ((LogicalProject) relNode).getProjects().get(inputRexIndex);
    if (inputRef instanceof RexCall) {
        // we can not push any expression as of now stop and return null.
        return false;
    }
    return isValidRemoteJoinRef(((RexInputRef) inputRef).getIndex(), relNode.getInput(0));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) TableScan(org.apache.calcite.rel.core.TableScan) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) 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