jinfengni commented on code in PR #3640:
URL: https://github.com/apache/calcite/pull/3640#discussion_r1499590342


##########
core/src/test/java/org/apache/calcite/test/JdbcTest.java:
##########
@@ -8270,6 +8270,75 @@ private void checkGetTimestamp(Connection con) throws 
SQLException {
         .returns("EXPR$0=[1, 1.1]\n");
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6032";>[CALCITE-6032]
+   * NullPointerException in Reldecorrelator for a Multi level correlated 
subquery</a>. */
+  @Test void testMultiLevelDecorrelation() throws Exception {
+    String hsqldbMemUrl = "jdbc:hsqldb:mem:.";
+    Connection baseConnection = DriverManager.getConnection(hsqldbMemUrl);
+    Statement baseStmt = baseConnection.createStatement();
+    baseStmt.execute("create table invoice (inv_id integer, col1\n"
+        + "integer, inv_amt integer)");
+    baseStmt.execute("create table item(item_id integer, item_amt\n"
+        + "integer, item_col1 integer, item_col2 integer, item_col3\n"
+        + "integer,item_col4 integer )");
+    baseStmt.execute("INSERT INTO invoice VALUES (1, 1, 1)");
+    baseStmt.execute("INSERT INTO invoice VALUES (2, 2, 2)");
+    baseStmt.execute("INSERT INTO invoice VALUES (3, 3, 3)");
+    baseStmt.execute("INSERT INTO item values (1, 1, 1, 1, 1, 1)");
+    baseStmt.execute("INSERT INTO item values (2, 2, 2, 2, 2, 2)");
+    baseStmt.close();
+    baseConnection.commit();
+
+    Properties info = new Properties();
+    info.put("model",
+        "inline:"
+            + "{\n"
+            + "  version: '1.0',\n"
+            + "  defaultSchema: 'BASEJDBC',\n"
+            + "  schemas: [\n"
+            + "     {\n"
+            + "       type: 'jdbc',\n"
+            + "       name: 'BASEJDBC',\n"
+            + "       jdbcDriver: '" + jdbcDriver.class.getName() + "',\n"
+            + "       jdbcUrl: '" + hsqldbMemUrl + "',\n"
+            + "       jdbcCatalog: null,\n"
+            + "       jdbcSchema: null\n"
+            + "     }\n"
+            + "  ]\n"
+            + "}");
+
+    Connection calciteConnection =
+        DriverManager.getConnection("jdbc:calcite:", info);
+
+    String statement = "SELECT Sum(invoice.inv_amt * (\n"

Review Comment:
   Can we simply the query to reproduce the issue?  Remove the unnecessary part 
that is not related to de-correlation logic.  
   
   I tried the following query, using `foodmart`, and seems it hit the  similar 
stack trace.
   
   ```
   SELECT SUM(p."min_scale" * (
           select max(e."salary" + e."salary")
           from FOODMART."employee" e
           where e."position_id" = p."position_id"
           AND     e."supervisor_id" = (select MAX(e2."supervisor_id")
                                    from FOODMART."employee" e2
                                    WHERE e."position_id" <= p."position_id"
                                    AND   e."last_name" = e2."last_name")
   )), count(*) as cnt
   from FOODMART."position" p;
   ```
   
   ```
   Error: Error while executing SQL "SELECT SUM(p."min_scale" * (
           select max(e."salary" + e."salary")
           from FOODMART."employee" e
           where e."position_id" = p."position_id"
           AND     e."supervisor_id" = (select MAX(e2."supervisor_id")
                                    from FOODMART."employee" e2
                                    WHERE e."position_id" <= p."position_id"
                                    AND   e."last_name" = e2."last_name")
   )), count(*) as cnt
   from FOODMART."position" p": cm.mapCorToCorRel.get($cor2) (state=,code=0)
   ```



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