Skip to content

[CALCITE-7439] Qualify GROUP BY keys for DISTINCT over joins#4829

Draft
bvolpato wants to merge 1 commit intoapache:mainfrom
bvolpato:bvolpato/calcite-7439-rel2sql-ambiguous-groupby
Draft

[CALCITE-7439] Qualify GROUP BY keys for DISTINCT over joins#4829
bvolpato wants to merge 1 commit intoapache:mainfrom
bvolpato:bvolpato/calcite-7439-rel2sql-ambiguous-groupby

Conversation

@bvolpato
Copy link

@bvolpato bvolpato commented Mar 9, 2026

Summary

RelToSqlConverter emitted an ambiguous GROUP BY key after semi-join rewrites for a DISTINCT query over LEFT JOIN ... USING.

Reproduction query

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

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

Disclaimer

The changes were done with the assistance of Codex (gpt-5.3-codex) in response to an actual bug when using Calcite in production. The changes were manually verified prior to sending this to review.

@bvolpato bvolpato force-pushed the bvolpato/calcite-7439-rel2sql-ambiguous-groupby branch 3 times, most recently from ae686b6 to be816c1 Compare March 10, 2026 03:58
@bvolpato bvolpato marked this pull request as ready for review March 10, 2026 04:49
@bvolpato bvolpato force-pushed the bvolpato/calcite-7439-rel2sql-ambiguous-groupby branch 2 times, most recently from 8805604 to 0558ef1 Compare March 10, 2026 12:38
@bvolpato bvolpato marked this pull request as draft March 10, 2026 12:39
@bvolpato bvolpato force-pushed the bvolpato/calcite-7439-rel2sql-ambiguous-groupby branch from 0558ef1 to 67bc8f8 Compare March 10, 2026 13:13
@bvolpato bvolpato force-pushed the bvolpato/calcite-7439-rel2sql-ambiguous-groupby branch from 67bc8f8 to b6af6a8 Compare March 10, 2026 15:18
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant