[ 
https://issues.apache.org/jira/browse/CALCITE-7343?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18048960#comment-18048960
 ] 

krooswu edited comment on CALCITE-7343 at 1/6/26 6:15 AM:
----------------------------------------------------------

*Progress Update*

I have identified the root cause and started implementing a fix.

*Root Cause*
Correlated scalar subqueries require outer table aliases to be preserved, but 
{{RelToSqlConverter}} does not currently detect or propagate this requirement. 
As a result, the generated SQL may miss necessary outer aliases.

*Solution Approach*
I am introducing a context-flag–based mechanism to propagate “outer alias 
required” information from parent relational nodes down to {{{}TableScan{}}}.
This allows {{TableScan}} to decide whether an explicit alias must be emitted 
during SQL generation.

*Current Status*
 * ✅ Root cause identified

 * ✅ {{Project}} (SELECT list) case implemented and verified (this is the 
primary scenario reported in this issue)

*Additional Findings*
During implementation, I found that the same issue also occurs with scalar 
correlated subqueries under other operators, such as {{{}Filter{}}}. This 
indicates that the problem is broader than initially scoped.

*Next Steps*
I will extend the same mechanism incrementally to other operators (e.g. 
{{{}Filter{}}}, and potentially others), ensuring correctness while keeping the 
change set manageable.

This approach provides a general solution rather than a one-off fix for 
{{{}Project{}}}.


was (Author: JIRAUSER308217):
*Progress Update:*

Working on this and fix the Project part

*Root Cause:* Correlated subqueries require outer table aliases, but 
{{RelToSqlConverter}} doesn't detect this requirement.

*Solution:* Implementing a context flag pattern to propagate alias requirements 
from parent nodes (Project/Filter) to TableScan.

*Status:*
 * ✅ Root cause identified
 * 🔄 Implementation in progress
 * Starting with Project (SELECT list) - the primary reported scenario
 * Will extend to other operators incrementally

This is a broader fix than initially scoped. Taking an incremental approach.

> RelToSqlConverter generate wrong sql when scalar correlated sub-query in 
> Project
> --------------------------------------------------------------------------------
>
>                 Key: CALCITE-7343
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7343
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: xiong duan
>            Assignee: krooswu
>            Priority: Major
>
> The unit test in RelToSqlConverterTest:
> {code:java}
> @Test void testProjectScalarSubquery() {
>   final String sql = "SELECT \"EMPNO\", (SELECT COUNT(*) AS \"c\"\n" +
>       "FROM \"EMP\"\n" +
>       "WHERE \"MGR\" < \"t\".\"MGR\") AS \"$f1\"\n" +
>       "FROM \"EMP\" as \"t\"\n" +
>       "WHERE \"SAL\" > 10";
>   final String expected = "SELECT \"EMPNO\", (SELECT COUNT(*) AS \"c\"\n" +
>       "FROM \"SCOTT\".\"EMP\"\n" +
>       "WHERE \"MGR\" < \"t\".\"MGR\") AS \"$f1\"\n" +
>       "FROM \"SCOTT\".\"EMP\"\n" +
>       "WHERE CAST(\"SAL\" AS DECIMAL(12, 2)) > 10.00";
>   sql(sql)
>       .schema(CalciteAssert.SchemaSpec.JDBC_SCOTT)
>       .withCalcite()
>       .ok(expected);
> } {code}
> The expected Sql should be:
> {code:java}
> final String expected = "SELECT \"EMPNO\", (SELECT COUNT(*) AS \"c\"\n" +
>     "FROM \"SCOTT\".\"EMP\"\n" +
>     "WHERE \"MGR\" < \"t\".\"MGR\") AS \"$f1\"\n" +
>     "FROM \"SCOTT\".\"EMP\" as \"t\"\n" +
>     "WHERE CAST(\"SAL\" AS DECIMAL(12, 2)) > 10.00"; {code}
> "SCOTT"."EMP" should have the alias t.



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

Reply via email to