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

   Jira: https://issues.apache.org/jira/browse/CALCITE-7856
   
   A correlated sub-query in the `UPDATE SET` clause of a `MERGE` that 
references a target column with the same name as a source column silently reads 
the source column:
   
   ```sql
   merge into empnullables e
   using (select * from emp) t
   on e.empno = t.empno
   when matched then update
   set sal = (select d.deptno from dept d where d.deptno = e.deptno)
   ```
   
   `SqlValidatorImpl.rewriteMerge` built the select list of the MERGE source 
select by shallow-cloning the select list of the UPDATE's source select, so the 
sub-query and its identifiers were shared by both selects. The validator keeps 
one scope per node, and the registration of the UPDATE source select (target 
table only) won. `e.deptno` was resolved against that scope, which gave 
`$cor0.DEPTNO` over the target row type instead of `$cor0.DEPTNO0` over the 
join row, and the correlation variable was not set. In addition, `convertMerge` 
rebuilt the top projection without `variablesSet`.
   
   Changes:
   * deep-copy the select list in `rewriteMerge`;
   * keep `variablesSet` of the source projection in `convertMerge`;
   * add `SqlToRelConverterTest.testMergeUpdateCorrelatedSubQuery`.
   
   Not covered: with `expand = true` a correlated sub-query in MERGE still 
fails in `convertMerge` (it fails earlier without this change); it is a 
separate problem.
   


-- 
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