Search in sources :

Example 6 with RelVisitor

use of org.apache.calcite.rel.RelVisitor 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 7 with RelVisitor

use of org.apache.calcite.rel.RelVisitor in project calcite by apache.

the class Prepare method optimize.

/**
 * Optimizes a query plan.
 *
 * @param root Root of relational expression tree
 * @param materializations Tables known to be populated with a given query
 * @param lattices Lattices
 * @return an equivalent optimized relational expression
 */
protected RelRoot optimize(RelRoot root, final List<Materialization> materializations, final List<CalciteSchema.LatticeEntry> lattices) {
    final RelOptPlanner planner = root.rel.getCluster().getPlanner();
    final DataContext dataContext = context.getDataContext();
    planner.setExecutor(new RexExecutorImpl(dataContext));
    final List<RelOptMaterialization> materializationList = new ArrayList<>();
    for (Materialization materialization : materializations) {
        List<String> qualifiedTableName = materialization.materializedTable.path();
        materializationList.add(new RelOptMaterialization(materialization.tableRel, materialization.queryRel, materialization.starRelOptTable, qualifiedTableName));
    }
    final List<RelOptLattice> latticeList = new ArrayList<>();
    for (CalciteSchema.LatticeEntry lattice : lattices) {
        final CalciteSchema.TableEntry starTable = lattice.getStarTable();
        final JavaTypeFactory typeFactory = context.getTypeFactory();
        final RelOptTableImpl starRelOptTable = RelOptTableImpl.create(catalogReader, starTable.getTable().getRowType(typeFactory), starTable, null);
        latticeList.add(new RelOptLattice(lattice.getLattice(), starRelOptTable));
    }
    final RelTraitSet desiredTraits = getDesiredRootTraitSet(root);
    // Work around
    // [CALCITE-1774] Allow rules to be registered during planning process
    // by briefly creating each kind of physical table to let it register its
    // rules. The problem occurs when plans are created via RelBuilder, not
    // the usual process (SQL and SqlToRelConverter.Config.isConvertTableAccess
    // = true).
    final RelVisitor visitor = new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof TableScan) {
                final RelOptCluster cluster = node.getCluster();
                final RelOptTable.ToRelContext context = RelOptUtil.getContext(cluster);
                final RelNode r = node.getTable().toRel(context);
                planner.registerClass(r);
            }
            super.visit(node, ordinal, parent);
        }
    };
    visitor.go(root.rel);
    final Program program = getProgram();
    final RelNode rootRel4 = program.run(planner, root.rel, desiredTraits, materializationList, latticeList);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Plan after physical tweaks: {}", RelOptUtil.toString(rootRel4, SqlExplainLevel.ALL_ATTRIBUTES));
    }
    return root.withRel(rootRel4);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) TableScan(org.apache.calcite.rel.core.TableScan) Program(org.apache.calcite.tools.Program) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) LatticeEntry(org.apache.calcite.jdbc.CalciteSchema.LatticeEntry) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) DataContext(org.apache.calcite.DataContext) RelNode(org.apache.calcite.rel.RelNode) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelOptLattice(org.apache.calcite.plan.RelOptLattice) RelVisitor(org.apache.calcite.rel.RelVisitor) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 8 with RelVisitor

use of org.apache.calcite.rel.RelVisitor in project calcite by apache.

the class VolcanoPlanner method setInitialImportance.

private void setInitialImportance() {
    RelVisitor visitor = new RelVisitor() {

        int depth = 0;

        final Set<RelSubset> visitedSubsets = new HashSet<>();

        public void visit(RelNode p, int ordinal, RelNode parent) {
            if (p instanceof RelSubset) {
                RelSubset subset = (RelSubset) p;
                if (visitedSubsets.contains(subset)) {
                    return;
                }
                if (subset != root) {
                    Double importance = Math.pow(0.9, (double) depth);
                    ruleQueue.updateImportance(subset, importance);
                }
                visitedSubsets.add(subset);
                depth++;
                for (RelNode rel : subset.getRels()) {
                    visit(rel, -1, subset);
                }
                depth--;
            } else {
                super.visit(p, ordinal, parent);
            }
        }
    };
    visitor.go(root);
}
Also used : RelTraitSet(org.apache.calcite.plan.RelTraitSet) Set(java.util.Set) HashSet(java.util.HashSet) RelNode(org.apache.calcite.rel.RelNode) RelVisitor(org.apache.calcite.rel.RelVisitor)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)8 RelVisitor (org.apache.calcite.rel.RelVisitor)8 ArrayList (java.util.ArrayList)3 TableScan (org.apache.calcite.rel.core.TableScan)3 HashSet (java.util.HashSet)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 HiveRelNode (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveRelNode)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1 DataContext (org.apache.calcite.DataContext)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 LatticeEntry (org.apache.calcite.jdbc.CalciteSchema.LatticeEntry)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelOptLattice (org.apache.calcite.plan.RelOptLattice)1 RelOptMaterialization (org.apache.calcite.plan.RelOptMaterialization)1 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 RelOptTable (org.apache.calcite.plan.RelOptTable)1