Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.calcite.rel.core.RelFactories;
import org.apache.calcite.rel.core.SemiJoin;
import org.apache.calcite.rel.core.Sort;
import org.apache.calcite.rel.core.TableScan;
import org.apache.calcite.rel.externalize.RelJsonWriter;
import org.apache.calcite.rel.externalize.RelWriterImpl;
import org.apache.calcite.rel.externalize.RelXmlWriter;
Expand Down Expand Up @@ -196,6 +197,30 @@ public static boolean isOrder(RelNode rel) {
return false;
}

/**
* Returns a set of tables used by this expression or its children
*/
public static Set<RelOptTable> findTables(RelNode rel) {
return new LinkedHashSet<RelOptTable>(findAllTables(rel));
}

/**
* Returns a list of all tables used by this expression or its children
*/
public static List<RelOptTable> findAllTables(RelNode rel) {
final List<RelOptTable> usedTables = new ArrayList<>();
new RelVisitor() {
@Override public void visit(RelNode node, int ordinal, RelNode parent) {
if (node instanceof TableScan) {
usedTables.add(node.getTable());
}
super.visit(node, ordinal, parent);
}
// CHECKSTYLE: IGNORE 1
}.go(rel);
return usedTables;
}

/**
* Returns a list of variables set by a relational expression or its
* descendants.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.apache.calcite.rel.RelVisitor;
import org.apache.calcite.rel.convert.Converter;
import org.apache.calcite.rel.convert.ConverterRule;
import org.apache.calcite.rel.core.TableScan;
import org.apache.calcite.rel.metadata.JaninoRelMetadataProvider;
import org.apache.calcite.rel.metadata.RelMetadataProvider;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
Expand Down Expand Up @@ -103,7 +102,6 @@
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -460,7 +458,7 @@ private void useApplicableMaterializations() {
final List<RelOptMaterialization> applicableMaterializations =
getApplicableMaterializations(originalRoot, materializations);
useMaterializations(originalRoot, applicableMaterializations);
final Set<RelOptTable> queryTables = findTables(originalRoot);
final Set<RelOptTable> queryTables = RelOptUtil.findTables(originalRoot);

// Use a lattice if the query uses at least the central (fact) table of the
// lattice.
Expand Down Expand Up @@ -503,7 +501,7 @@ public static List<RelOptMaterialization> getApplicableMaterializations(RelNode
final List<String> qname = materialization.table.getQualifiedName();
qnameMap.put(qname, materialization);
for (RelOptTable usedTable
: findTables(materialization.queryRel)) {
: RelOptUtil.findTables(materialization.queryRel)) {
usesGraph.addVertex(qname);
usesGraph.addVertex(usedTable.getQualifiedName());
usesGraph.addEdge(usedTable.getQualifiedName(), qname);
Expand All @@ -516,7 +514,7 @@ public static List<RelOptMaterialization> getApplicableMaterializations(RelNode
// actually use.)
final Graphs.FrozenGraph<List<String>, DefaultEdge> frozenGraph =
Graphs.makeImmutable(usesGraph);
final Set<RelOptTable> queryTablesUsed = findTables(root);
final Set<RelOptTable> queryTablesUsed = RelOptUtil.findTables(root);
final List<RelOptMaterialization> applicableMaterializations = Lists.newArrayList();
for (List<String> qname : TopologicalOrderIterator.of(usesGraph)) {
RelOptMaterialization materialization = qnameMap.get(qname);
Expand Down Expand Up @@ -546,20 +544,6 @@ private static boolean usesTable(
return false;
}

private static Set<RelOptTable> findTables(RelNode rel) {
final Set<RelOptTable> usedTables = new LinkedHashSet<>();
new RelVisitor() {
@Override public void visit(RelNode node, int ordinal, RelNode parent) {
if (node instanceof TableScan) {
usedTables.add(node.getTable());
}
super.visit(node, ordinal, parent);
}
// CHECKSTYLE: IGNORE 1
}.go(rel);
return usedTables;
}

/**
* Finds an expression's equivalence set. If the expression is not
* registered, returns null.
Expand Down
Loading