[ 
https://issues.apache.org/jira/browse/CALCITE-6157?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Ulrich Kramer updated CALCITE-6157:
-----------------------------------
    Description: 
When switching from 1.35.0 to 1.36.0, {{SqlValidatorImpl}} breaks the join 
condition on the following statement

{code:SQL}
WITH `it1` AS (
    SELECT
        `company`,
        `area`,
        `revenue`,
        `regionId`
    FROM
        `12345678-1234-1234-1234-000100000001`.`revenues`
)
SELECT
    *
FROM
    (
        SELECT
            `T1`.`company` AS `company`,
            MAX(`T1`.`revenue`) AS `revenue`
        FROM
            `it1` AS `T1`
        GROUP BY
            `company`
    ) AS `T`
    LEFT JOIN `it1` AS `T2` ON `T`.`company` = `T2`.`company`
    AND `T`.`revenue` = `T2`.`revenue`
{code}

It modifies the join condition [at this 
location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java#L3603-L3604]
 to

{code:SQL}
`T`.`company` = `T1`.`company`
    AND `T`.`revenue` = `T1`.`revenue`
{code}

which causes a "{{Table `T1`not found}}" exception.

I already tried to reproduce the problem by adding the following test to 
{{SqlValidatorTest}}, but everything worked fine

{code:java}
  @Test void testWith2() {
    sql("with it1 as (select empno, sal, deptno from emp)\n"
        + "select * from ( select t1.empno as empno, max(t1.deptno) as deptno 
from it1 as t1 group by empno) as t\n"
        + "left outer join it1 as t2 on t.empno = t2.empno and t.deptno = 
t2.deptno")
        .ok();
  }
{code}

During debugging, I recognized that the {{DelegatingScope}} does something 
different in our case [at this 
location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/DelegatingScope.java#L333-L337].
 In our case the {{fromNs}} resolved to {{`it1` AS `T1`}}, which is wrong. 
But I was not able to find the root cause yet.

  was:
When switching from 1.35.0 to 1.36.0, {{SqlValidatorImpl}} breaks the join 
condition on the following statement

{code:SQL}
WITH `it1` AS (
    SELECT
        `company`,
        `area`,
        `revenue`,
        `regionId`
    FROM
        `12345678-1234-1234-1234-000100000001`.`revenues`
)
SELECT
    *
FROM
    (
        SELECT
            `T1`.`company` AS `company`,
            MAX(`T1`.`revenue`) AS `revenue`
        FROM
            `it1` AS `T1`
        GROUP BY
            `company`
    ) AS `T`
    LEFT JOIN `it1` AS `T2` ON `T`.`company` = `T2`.`company`
    AND `T`.`revenue` = `T2`.`revenue`
{code}

It modifies the join condition [at this 
location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java#L3603-L3604]
 to

{code:SQL}
`T`.`company` = `T1`.`company`
    AND `T`.`revenue` = `T1`.`revenue`
{code}

which causes a {{Table `T1`not found}}.

I already tried to reproduce the problem by adding the following test to 
{{SqlValidatorTest}}, but everything worked fine

{code:java}
  @Test void testWith2() {
    sql("with it1 as (select empno, sal, deptno from emp)\n"
        + "select * from ( select t1.empno as empno, max(t1.deptno) as deptno 
from it1 as t1 group by empno) as t\n"
        + "left outer join it1 as t2 on t.empno = t2.empno and t.deptno = 
t2.deptno")
        .ok();
  }
{code}

During debugging, I recognized that the {{DelegatingScope}} does something 
different in our case [at this 
location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/DelegatingScope.java#L333-L337].
 In our case the {{fromNs}} resolved to {{`it1` AS `T1`}}, which is wrong. 
But I was not able to find the root cause yet.


> SqlValidatorImpl breaks join condition
> --------------------------------------
>
>                 Key: CALCITE-6157
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6157
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.36.0
>            Reporter: Ulrich Kramer
>            Priority: Major
>
> When switching from 1.35.0 to 1.36.0, {{SqlValidatorImpl}} breaks the join 
> condition on the following statement
> {code:SQL}
> WITH `it1` AS (
>     SELECT
>         `company`,
>         `area`,
>         `revenue`,
>         `regionId`
>     FROM
>         `12345678-1234-1234-1234-000100000001`.`revenues`
> )
> SELECT
>     *
> FROM
>     (
>         SELECT
>             `T1`.`company` AS `company`,
>             MAX(`T1`.`revenue`) AS `revenue`
>         FROM
>             `it1` AS `T1`
>         GROUP BY
>             `company`
>     ) AS `T`
>     LEFT JOIN `it1` AS `T2` ON `T`.`company` = `T2`.`company`
>     AND `T`.`revenue` = `T2`.`revenue`
> {code}
> It modifies the join condition [at this 
> location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java#L3603-L3604]
>  to
> {code:SQL}
> `T`.`company` = `T1`.`company`
>     AND `T`.`revenue` = `T1`.`revenue`
> {code}
> which causes a "{{Table `T1`not found}}" exception.
> I already tried to reproduce the problem by adding the following test to 
> {{SqlValidatorTest}}, but everything worked fine
> {code:java}
>   @Test void testWith2() {
>     sql("with it1 as (select empno, sal, deptno from emp)\n"
>         + "select * from ( select t1.empno as empno, max(t1.deptno) as deptno 
> from it1 as t1 group by empno) as t\n"
>         + "left outer join it1 as t2 on t.empno = t2.empno and t.deptno = 
> t2.deptno")
>         .ok();
>   }
> {code}
> During debugging, I recognized that the {{DelegatingScope}} does something 
> different in our case [at this 
> location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/DelegatingScope.java#L333-L337].
>  In our case the {{fromNs}} resolved to {{`it1` AS `T1`}}, which is wrong. 
> But I was not able to find the root cause yet.



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

Reply via email to