[
https://issues.apache.org/jira/browse/CALCITE-7508?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18099267#comment-18099267
]
krooswu commented on CALCITE-7508:
----------------------------------
Hi Haris Sattar,
Thanks for the report. I tried a couple of ways to reproduce this but haven't
hit the mismatched-alias behavior yet — could you help me align the repro?
*Attempt 1 — via SQL parser fixture:*
java
{{final String sql = "SELECT \"DEPTNO\", \n"
+ " (SELECT SUM(\"SAL\") FROM \"EMP\" "
+ "WHERE \"EMP\".\"DEPTNO\" = \"DEPT\".\"DEPTNO\")\n"
+ " AS \"TOTAL_SAL\"\n"
+ "FROM \"DEPT\"";
sql(sql)
.schema(CalciteAssert.SchemaSpec.JDBC_SCOTT)
.withPresto()
.ok(...);}}
Output:
sql
{{SELECT "DEPTNO", (SELECT SUM("SAL")
FROM "SCOTT"."EMP"
WHERE "DEPTNO" = "DEPT"."DEPTNO") AS "TOTAL_SAL"
FROM "SCOTT"."DEPT" AS "DEPT"}}
*Attempt 2 — via RelBuilder, bypassing SqlToRelConverter/optimizer entirely
({{{}RexSubQuery.scalar{}}} embedded in a {{{}LogicalProject{}}}, then
{{converter.visitRoot(root)}} directly):*
java
{{final RelNode root = new LogicalProject(
cluster,
cluster.traitSetOf(Convention.NONE),
ImmutableList.of(),
outer,
projects, // [DEPTNO ref, RexSubQuery.scalar(inner)]
rowType,
ImmutableSet.of(v.get().id));
new RelToSqlConverter(PrestoSqlDialect.DEFAULT)
.visitRoot(root).asStatement()
.toSqlString(PrestoSqlDialect.DEFAULT).getSql();}}
Output:
sql
{{SELECT "DEPTNO", (SELECT SUM("SAL") AS "$f0"
FROM "scott"."EMP"
WHERE "DEPTNO" = "DEPT"."DEPTNO") AS "TOTAL_SAL"
FROM "scott"."DEPT" AS "DEPT"}}
Both give consistent aliases ({{{}"DEPT"{}}} on both sides) — no
{{{}t{}}}/{{{}t0{}}} mismatch.
A few questions to help me match your repro:
# Which Calcite version/commit are you on?
# Was your RelNode tree parsed from SQL (with the default planner/optimizer
applied), or built manually like above?
# Could you share the actual RelNode tree ({{{}RelOptUtil.toString(root){}}})
or your construction code?
# Does a single-level correlated scalar subquery trigger it, or is extra
nesting required?
Thanks — happy to keep digging once I can reproduce it locally.
> RelToSqlConverter registers wrong alias for correlation variable in
> visit(Project)
> ----------------------------------------------------------------------------------
>
> Key: CALCITE-7508
> URL: https://issues.apache.org/jira/browse/CALCITE-7508
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.39.0
> Reporter: Haris Sattar
> Assignee: krooswu
> Priority: Major
> Labels: pull-request-available
>
> *Example:*
> Input RelNode tree: a correlated scalar subquery where the outer table is
> table_a and the inner table is table_b, correlated on column "id".
> *Expected SQL output:*
> SELECT "id",
> (SELECT SUM("value") FROM "table_b" AS "t1"
> WHERE "id" = "t"."id") AS "total_value"
> FROM "table_a" AS "t"
> *Actual SQL output:*
> SELECT "id",
> (SELECT SUM("value") FROM "table_b" AS "t1"
> WHERE "id" = "t0"."id") AS "total_value"
> FROM "table_a" AS "t"
> The correlation reference emits "t0"."id" but the outer table's FROM clause
> says AS "t". "t0" does not exist in the query. The SQL is invalid and will be
> rejected by the execution engine.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)