Justin Swett created CALCITE-6795:
-------------------------------------

             Summary: Correlated sub-query is not resolving correlation 
varables.
                 Key: CALCITE-6795
                 URL: https://issues.apache.org/jira/browse/CALCITE-6795
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.38.0
            Reporter: Justin Swett


The following test will result in a NPE "variable $cor0 is not found".

```

  @Test void testCSS() {
    String query = "SELECT p.\"product_name\", SUM(s.\"store_sales\"), "
        + "(SELECT MAX(s2.\"store_sales\") FROM \"sales_fact_1997\" s2 "
        + "WHERE s2.\"product_id\" = p.\"product_id\") AS max_sold_for_product "
        + "FROM \"product\" p "
        + "JOIN \"sales_fact_1997\" s ON p.\"product_id\" = s.\"product_id\" "
        + "GROUP BY p.\"product_name\", p.\"product_id\"";

    String expected = "SELECT \"product\".\"product_name\", 
SUM(\"sales_fact_1997\".\"store_sales\"), (((SELECT MAX(\"store_sales\")\n"
        + "FROM \"foodmart\".\"sales_fact_1997\"\n"
        + "WHERE \"product_id\" = \"sales_fact_19970\".\"time_id\"))) AS 
\"MAX_SOLD_FOR_PRODUCT\"\n"
        + "FROM \"foodmart\".\"product\"\n"
        + "INNER JOIN \"foodmart\".\"sales_fact_1997\" ON 
\"product\".\"product_id\" = \"sales_fact_1997\".\"product_id\"\n"
        + "GROUP BY \"product\".\"product_name\", \"product\".\"product_id\"";
    sql(query).withConfig(c -> c.withExpand(false)).ok(expected);
  }
```

At first glance, the issue seems to be related to whether or not the 
correlation appears to be in scope. Stepping through 
`SqlToRelConverter.getCorrelationUse` I noticed that correlInCurrentScope is 
false and if I set value to true then the test passes.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to