xiedeyantu commented on code in PR #4840:
URL: https://github.com/apache/calcite/pull/4840#discussion_r3007745355


##########
core/src/test/java/org/apache/calcite/sql2rel/RelDecorrelatorTest.java:
##########
@@ -1830,4 +1830,160 @@ public static Frameworks.ConfigBuilder config() {
         + "                LogicalTableScan(table=[[scott, EMP]])\n";
     assertThat(after, hasTree(planAfter));
   }
+
+  /** Test case for <a 
href="https://issues.apache.org/jira/browse/CALCITE-7442";>[CALCITE-7442]
+  * Getting Wrong index of Correlated variable inside Subquery after 
FilterJoinRule</a>. */
+  @Test void testCorrelatedVariableIndexForInClause() {
+    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 = "select e.empno, d.dname, b.ename\n"
+        + "from emp e\n"
+        + "inner join dept d\n"
+        + "  on d.deptno = e.deptno\n"
+        + "inner join bonus b\n"
+        + "  on e.ename = b.ename\n"
+        + "  and b.job in (\n"
+        + "    select b2.job\n"
+        + "    from bonus b2\n"
+        + "    where b2.ename = b.ename)\n"
+        + "where e.sal > 1000 and d.dname = 'SALES'";
+
+    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(
+                CoreRules.FILTER_INTO_JOIN,
+                CoreRules.FILTER_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(EMPNO=[$0], DNAME=[$9], 
ENAME=[$11])\n"
+        + "  LogicalJoin(condition=[=($1, $11)], joinType=[inner], 
variablesSet=[[$cor0]])\n"

Review Comment:
   It seems this issue was discussed here quite some time ago, but it appears 
to be rather complex. https://issues.apache.org/jira/browse/CALCITE-7340 might 
be helpful.



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