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


##########
core/src/test/java/org/apache/calcite/test/enumerable/EnumerableCorrelateTest.java:
##########
@@ -168,6 +168,24 @@ class EnumerableCorrelateTest {
             "empid=200");
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5638";>[CALCITE-5638]
+   * Complex nested correlated subquery failed</a>.
+   */
+  @Test void complexNestedCorrelatedSubquery() {
+    String sql = "SELECT empid, deptno, (SELECT count(*) FROM emps AS x "
+        + "WHERE x.salary>emps.salary and x.deptno<emps.deptno) FROM emps "
+        + "WHERE empid<salary ORDER BY 1,2,3";
+
+    tester(false, new HrSchema())

Review Comment:
   I assume this has been validated using another database?



##########
core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java:
##########
@@ -473,6 +479,107 @@ public TrimResult trimFields(
     return result(newCalc, mapping, calc);
   }
 
+  /**
+   * Shuttle that finds all {@link TableScan}`s inside a given {@link RelNode}.
+   */
+  private static class TableScanCollector extends RelHomogeneousShuttle {
+    private ImmutableSet.Builder<List<String>> builder = 
ImmutableSet.builder();
+
+    /** Qualified names. */
+    Set<List<String>> tables() {
+      return builder.build();
+    }
+
+    @Override public RelNode visit(TableScan scan) {
+      builder.add(scan.getTable().getQualifiedName());
+      return super.visit(scan);
+    }
+  }
+
+  /**
+   * Shuttle that finds all {@link TableScan}`s inside a given {@link RexNode}.
+   */
+  private static class InputTablesVisitor extends RexVisitorImpl<Void> {
+    private ImmutableSet.Builder<List<String>> builder = 
ImmutableSet.builder();
+
+    protected InputTablesVisitor() {
+      super(true);
+    }
+
+    /** Qualified names. */
+    Set<List<String>> tables() {
+      return builder.build();
+    }
+
+    @Override public Void visitSubQuery(RexSubQuery subQuery) {
+      if (subQuery.getKind() == SqlKind.SCALAR_QUERY) {
+        subQuery.rel.accept(new RelHomogeneousShuttle() {
+          @Override public RelNode visit(TableScan scan) {
+            builder.add(scan.getTable().getQualifiedName());
+            return super.visit(scan);
+          }
+        });
+      }
+      return null;
+    }
+  }
+
+  /** Extension of {@link RelOptUtil.InputFinder} with subquery lookup. */
+  private static class SubQueryAwareInputFinder extends RelOptUtil.InputFinder 
{

Review Comment:
   I believe that @julianhyde suggested moving this class to RelOptUtil.



-- 
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