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

zzwqqq updated CALCITE-7378:
----------------------------
    Description: 
In dialects where `{{{}hasImplicitTableAlias{}}}is` true, even if a table is 
explicitly assigned an alias in the original SQL, the `RelToSqlConverter` will 
strip it away during the conversion. In scenarios like self-joins, if no 
explicit alias is provided, column references will be incorrectly attributed to 
the nearest table in the generated SQL structure.
This issue also occurs when handle TPC-H Query 21.

A minimal reproducible example:
{code:java}
select "product_name" 
from "product" t1 
where "product_id" not in (
    select "product_id" 
    from "product" t2 
    where t2."product_id" = t1."product_id" and t1."product_id" = 2 and 
t2."product_id" = 1){code}
after rel-to-sql
{code:java}
SELECT "product_name"
FROM "foodmart"."product"
WHERE "product_id" IN (
    SELECT "product_id"
    FROM "foodmart"."product"
    WHERE "product_id" = "product"."product_id" AND "product"."product_id" = 2 
AND "product_id" = 1){code}
{code:java}
"product_id" = "product"."product_id" AND "product"."product_id" = 2 AND 
"product_id" = 1{code}
*In the new expression, all the "product_id" come from the nearest table, but 
in the original SQL, it comes from t1 and t2 respectively.* And the new 
expression always evaluates to false.

I suggest that when a correlated table alias shares the same name as the 
current table identifier, the table alias should be required.

  was:
In dialects where `{{{}hasImplicitTableAlias{}}}is` true, even if a table is 
explicitly assigned an alias in the original SQL, the `RelToSqlConverter` will 
strip it away during the conversion. In scenarios like self-joins, if no 
explicit alias is provided, column references will be incorrectly attributed to 
the nearest table in the generated SQL structure.
This issue also occurs when handle TPC-H Query 21.

A minimal reproducible example:
{code:java}
select "product_name" 
from "product" t1 
where "product_id" not in (select "product_id" 
from "product" t2 
where t2."product_id" = t1."product_id" and t1."product_id" = 2 and 
t2."product_id" = 1){code}
after rel-to-sql
{code:java}
SELECT "product_name"
FROM "foodmart"."product"
WHERE "product_id" IN (SELECT "product_id"
FROM "foodmart"."product"
WHERE "product_id" = "product"."product_id" AND "product"."product_id" = 2 AND 
"product_id" = 1){code}
{code:java}
"product"."product_id" = 2 AND "product_id" = 1{code}
The expression always evaluates to false.

I suggest that when a correlated table alias shares the same name as the 
current table identifier, the table alias should be required.


> Potential incorrect column attribution in RelToSqlConverter due to implicit 
> table alias handling
> ------------------------------------------------------------------------------------------------
>
>                 Key: CALCITE-7378
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7378
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: zzwqqq
>            Priority: Major
>              Labels: pull-request-available
>
> In dialects where `{{{}hasImplicitTableAlias{}}}is` true, even if a table is 
> explicitly assigned an alias in the original SQL, the `RelToSqlConverter` 
> will strip it away during the conversion. In scenarios like self-joins, if no 
> explicit alias is provided, column references will be incorrectly attributed 
> to the nearest table in the generated SQL structure.
> This issue also occurs when handle TPC-H Query 21.
> A minimal reproducible example:
> {code:java}
> select "product_name" 
> from "product" t1 
> where "product_id" not in (
>     select "product_id" 
>     from "product" t2 
>     where t2."product_id" = t1."product_id" and t1."product_id" = 2 and 
> t2."product_id" = 1){code}
> after rel-to-sql
> {code:java}
> SELECT "product_name"
> FROM "foodmart"."product"
> WHERE "product_id" IN (
>     SELECT "product_id"
>     FROM "foodmart"."product"
>     WHERE "product_id" = "product"."product_id" AND "product"."product_id" = 
> 2 AND "product_id" = 1){code}
> {code:java}
> "product_id" = "product"."product_id" AND "product"."product_id" = 2 AND 
> "product_id" = 1{code}
> *In the new expression, all the "product_id" come from the nearest table, but 
> in the original SQL, it comes from t1 and t2 respectively.* And the new 
> expression always evaluates to false.
> I suggest that when a correlated table alias shares the same name as the 
> current table identifier, the table alias should be required.



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

Reply via email to