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


##########
core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java:
##########
@@ -1954,7 +1954,8 @@ private static boolean isWidening(RelDataType type, 
RelDataType type1) {
 
     Frame newLeftFrame = leftFrame;
     boolean joinConditionContainsFieldAccess = 
RexUtil.containsFieldAccess(rel.getCondition());
-    if (joinConditionContainsFieldAccess && isCorVarDefined) {
+    if ((joinConditionContainsFieldAccess || rel.getJoinType() == 
JoinRelType.LEFT)

Review Comment:
   I wonder whether LEFT ASOF is also something that should be checked, but 
that may be harder to write tests for.
   



##########
core/src/test/java/org/apache/calcite/sql2rel/RelDecorrelatorTest.java:
##########
@@ -1272,4 +1272,79 @@ public static Frameworks.ConfigBuilder config() {
         + "              LogicalTableScan(table=[[scott, BONUS]])\n";
     assertThat(after, hasTree(planAfter));
   }
+
+  /** Test case for <a 
href="https://issues.apache.org/jira/browse/CALCITE-7379";>[CALCITE-7379]
+   * LHS correlated variables are shadowed by nullable RHS outputs in LEFT 
JOIN</a>. */
+  @Test void testDecorrelateLeftJoinCorVarShadowing() {
+    final FrameworkConfig frameworkConfig = config().build();
+    final RelBuilder builder = RelBuilder.create(frameworkConfig);
+    final RelOptCluster cluster = builder.getCluster();
+    final Planner planner = Frameworks.getPlanner(frameworkConfig);
+    final String sql = ""
+        + "WITH\n"
+        + "  t1(a, b, c) AS (VALUES (2, 2, 2), (3, 3, 3), (4, 4, 4)),\n"
+        + "  t2(a, b, c) AS (VALUES (1, 1, 1), (3, 3, 3), (4, 4, 4)),\n"
+        + "  t3(a, b, c) AS (VALUES (1, 1, 1), (2, 2, 2), (4, 4, 4))\n"
+        + "SELECT * FROM t1 WHERE EXISTS (\n"
+        + "SELECT * FROM t2\n"
+        + "LEFT JOIN\n"
+        + "(SELECT * FROM t3 WHERE t3.a = t1.a) foo\n"
+        + "ON t2.a = foo.a)";
+    final RelNode originalRel;
+    try {
+      final SqlNode parse = planner.parse(sql);
+      final SqlNode validate = planner.validate(parse);
+      originalRel = planner.rel(validate).rel;
+    } catch (Exception e) {
+      throw TestUtil.rethrow(e);
+    }
+
+    final HepProgram hepProgram = HepProgram.builder()
+        .addRuleCollection(
+            ImmutableList.of(
+                // SubQuery program rules
+                CoreRules.FILTER_SUB_QUERY_TO_CORRELATE,
+                CoreRules.PROJECT_SUB_QUERY_TO_CORRELATE,
+                CoreRules.JOIN_SUB_QUERY_TO_CORRELATE))
+        .build();
+    final Program program =
+        Programs.of(hepProgram, true,
+            requireNonNull(cluster.getMetadataProvider()));
+    final RelNode before =
+        program.run(cluster.getPlanner(), originalRel, cluster.traitSet(),
+            Collections.emptyList(), Collections.emptyList());
+    final String planBefore = ""
+        + "LogicalProject(A=[$0], B=[$1], C=[$2])\n"
+        + "  LogicalProject(EXPR$0=[$0], EXPR$1=[$1], EXPR$2=[$2])\n"
+        + "    LogicalCorrelate(correlation=[$cor0], joinType=[inner], 
requiredColumns=[{0}])\n"
+        + "      LogicalValues(tuples=[[{ 2, 2, 2 }, { 3, 3, 3 }, { 4, 4, 4 
}]])\n"
+        + "      LogicalAggregate(group=[{0}])\n"
+        + "        LogicalProject(i=[true])\n"
+        + "          LogicalJoin(condition=[=($0, $3)], joinType=[left])\n"
+        + "            LogicalValues(tuples=[[{ 1, 1, 1 }, { 3, 3, 3 }, { 4, 
4, 4 }]])\n"
+        + "            LogicalProject(A=[$0], B=[$1], C=[$2])\n"
+        + "              LogicalFilter(condition=[=($0, $cor0.A)])\n"
+        + "                LogicalValues(tuples=[[{ 1, 1, 1 }, { 2, 2, 2 }, { 
4, 4, 4 }]])\n";
+    assertThat(before, hasTree(planBefore));
+
+    // Decorrelate without any rules, just "purely" decorrelation algorithm on 
RelDecorrelator

Review Comment:
   Can you add a comment also explaining where this plan differs from the 
"broken" one?



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