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

   ## Summary
   RelToSqlConverter emitted an ambiguous GROUP BY key after semi-join rewrites 
for a DISTINCT query over LEFT JOIN ... USING.
   
   ## Reproduction query
   ```sql
   WITH product_keys AS (
     SELECT p."product_id",
            (SELECT MAX(p3."product_id")
             FROM "foodmart"."product" p3
             WHERE p3."product_id" = p."product_id") AS "mx"
     FROM "foodmart"."product" p
   )
   SELECT DISTINCT pk."product_id"
   FROM product_keys pk
   LEFT JOIN "foodmart"."product" p2 USING ("product_id")
   WHERE pk."product_id" IN (
     SELECT p4."product_id"
     FROM "foodmart"."product" p4
   )
   ```
   
   ## Error before this PR
   ```text
   org.apache.calcite.runtime.CalciteContextException:
   From line 12, column 10 to line 12, column 21: Column 'product_id' is 
ambiguous
   ```
   
   ## Fix
   When building GROUP BY keys (and corresponding SELECT keys), qualify 
one-part identifiers against the SQL join side alias when the aggregate source 
is a join-shaped input.
   
   ## Test
   - Added 
`RelToSqlConverterTest.testPostgresqlRoundTripDistinctLeftJoinInSubqueryWithSemiJoinRules`
   - Assert that generated SQL contains `GROUP BY "t2"."product_id"`
   - Ran: `./gradlew :core:test --tests 
org.apache.calcite.rel.rel2sql.RelToSqlConverterTest`
   - Result: 563 passed, 0 failed
   


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