Skip to content

[CALCITE-7655] RelToSqlConverter incorrectly removes a subquery when grouping by a window function result#5101

Open
zzwqqq wants to merge 2 commits into
apache:mainfrom
zzwqqq:fix_groupby_window
Open

[CALCITE-7655] RelToSqlConverter incorrectly removes a subquery when grouping by a window function result#5101
zzwqqq wants to merge 2 commits into
apache:mainfrom
zzwqqq:fix_groupby_window

Conversation

@zzwqqq

@zzwqqq zzwqqq commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7655

Changes Proposed

RelToSqlConverter could incorrectly remove a required subquery when an outer aggregate groups by the result of an inner window function. This caused the window function to appear directly in the generated GROUP BY clause:

GROUP BY EMPNO, ROW_NUMBER() OVER (ORDER BY EMPNO)

Window functions are not allowed in GROUP BY. I verified that this SQL fails on MySQL, PostgreSQL, and Oracle.

The fix detects window expressions referenced by aggregate group keys and preserves the input subquery.

@zzwqqq zzwqqq changed the title [CALCITE-7655] RelToSqlConverter generates invalid SQL when grouping by a window function result [CALCITE-7655] RelToSqlConverter incorrectly removes a subquery when grouping by a window function result Jul 14, 2026
@sonarqubecloud

Copy link
Copy Markdown

* <a href="https://issues.apache.org/jira/browse/CALCITE-7655">[CALCITE-7655]
* RelToSqlConverter incorrectly removes a subquery when grouping by a window function
* result</a>. */
@Test void testGroupByWindowFunction() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you described in Jira, the default generated SQL is not supported by many databases; therefore, the tests here should include the appropriate dialect to verify that the generated SQL is valid for the databases.

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.

2 participants