zstan commented on code in PR #4328:
URL: https://github.com/apache/calcite/pull/4328#discussion_r2060454020


##########
core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java:
##########
@@ -473,6 +478,117 @@ public TrimResult trimFields(
     return result(newCalc, mapping, calc);
   }
 
+  /**
+   * Shuttle that finds all references to a {@link TableScan} within a tree
+   * of {@link RelNode}s.
+   */
+  private static class InputTables extends RelHomogeneousShuttle {
+    private List<TableScan> tables = new ArrayList<>();
+
+    List<TableScan> tables() {
+      return tables;
+    }
+
+    @Override public RelNode visit(TableScan scan) {
+      tables.add(scan);
+      return super.visit(scan);
+    }
+  }
+
+  /**
+   * Shuttle that finds all references to a {@link TableScan} within current 
{@link RexNode}.
+   */
+  private static class InputTablesVisitor extends RexVisitorImpl<Void> {
+    private List<TableScan> tables = new ArrayList<>();
+
+    protected InputTablesVisitor() {
+      super(true);
+    }
+
+    List<TableScan> tables() {
+      return tables;
+    }
+
+    @Override public Void visitSubQuery(RexSubQuery subQuery) {
+      if (subQuery.getKind() == SqlKind.SCALAR_QUERY) {
+        subQuery.rel.accept(new RelHomogeneousShuttle() {
+          @Override public RelNode visit(TableScan scan) {
+            tables.add(scan);
+            return super.visit(scan);
+          }
+        });
+      }
+      return null;
+    }
+  }
+
+  /** Extension of {@link RelOptUtil.InputFinder} with subquery lookup. */
+  private static class SubQueryAwareInputFinder extends RelOptUtil.InputFinder 
{
+    boolean visitSubQuery;
+
+    SubQueryAwareInputFinder(@Nullable Set<RelDataTypeField> extraFields, 
boolean visitSubQuery) {
+      super(extraFields, ImmutableBitSet.builder());
+      this.visitSubQuery = visitSubQuery;
+    }
+
+    @Override public Void visitSubQuery(RexSubQuery subQuery) {
+      if (visitSubQuery && subQuery.getKind() == SqlKind.SCALAR_QUERY) {
+        subQuery.rel.accept(new RelHomogeneousShuttle() {
+          @Override public RelNode visit(LogicalProject project) {
+            project.getProjects().forEach(r -> 
r.accept(SubQueryAwareInputFinder.this));
+            return super.visit(project);
+          }
+
+          @Override public RelNode visit(LogicalFilter filter) {
+            filter.getCondition().accept(SubQueryAwareInputFinder.this);
+            return super.visit(filter);
+          }
+        });
+
+        return null;
+      } else {
+        return super.visitSubQuery(subQuery);
+      }
+    }
+  }
+
+  private boolean inputContainSubQueryTables(Project project, RelNode input) {
+    InputTablesVisitor inputSubQueryTablesCollector = new InputTablesVisitor();
+
+    for (RexNode node : project.getProjects()) {
+      node.accept(inputSubQueryTablesCollector);
+    }
+
+    List<TableScan> subQueryTables = inputSubQueryTablesCollector.tables();
+
+    List<String> qName = null;
+    for (TableScan t : subQueryTables) {

Review Comment:
   check current assertion in:
   RelFieldTrimmer#trimFields(org.apache.calcite.rel.core.Project, ...)
   ...
   assert correlationIds.size() == 1;
   so i suppose only one table is expected here, one correlate can`t be applied 
to different tables.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to