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();
}
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);
}
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);
}
Aggregations