Dwrite opened a new pull request, #5036:
URL: https://github.com/apache/calcite/pull/5036

   Jira Link
   [https://issues.apache.org/jira/browse/CALCITE-7505]
   
   RelToSqlConverter generates incorrect SQL for correlated sub-queries
   (EXISTS / IN / scalar) inside a Filter's WHERE clause when the dialect's
   hasImplicitTableAlias() is true.
   
   Root cause: unlike Project, Filter did not carry a variablesSet field, so
   RelToSqlConverter.visit(Filter) had no way to detect that the WHERE
   clause introduced a correlation variable (e.g. $cor0) referencing the
   outer relation. As a result:
   
     - the outer table never received an explicit alias
     - the correlated reference inside the sub-query resolved against the
       wrong (or no) qualified context, producing duplicate/missing FROM
       aliases or, in some plans, completely wrong field references
   
   Repro:
   
     SELECT deptno, SUM(sal) AS total
     FROM emp t
     WHERE EXISTS (
       SELECT * FROM dept t0
       WHERE deptno = t.deptno
     )
     GROUP BY deptno
   
   Fix:
   
     1. Add a variablesSet field to Filter/LogicalFilter, mirroring Project's
        existing constructor (and preserved across copy()), so a Filter that
        introduces a correlation variable can declare it -- the same gap
        CALCITE-5716 fixed for SubQueryRemoveRule-built plans.
   
     2. In RelToSqlConverter.visit(Filter) / visit(Project), only apply
        resetAliasForCorrelation when:
          a) the Filter/Project actually declares the correlation variable
             (variablesSet non-empty), AND
          b) the input's row type matches the correlation variable's row
             type (confirming this node's input is genuinely the relation
             the variable is bound to, not just a pass-through), AND
          c) the input is not a Join/Correlate (BiRel) -- those already
             expose natural per-table qualification and aren't a single
             aliasable FROM item, so forcing an alias would wrap them in
             a derived table and corrupt field resolution.
   
     This ensures an explicit, non-conflicting alias is generated exactly
     when needed, regardless of dialect.hasImplicitTableAlias().


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to