Skip to content

[CALCITE-7822] AssertionError "type mismatch" in SqlToRelConverter for OVER () in a GROUP BY query - #5293

Open
kalayciburak wants to merge 1 commit into
apache:mainfrom
kalayciburak:calcite-7822-empty-over-groupby
Open

kalayciburak wants to merge 1 commit into
apache:mainfrom
kalayciburak:calcite-7822-empty-over-groupby

Conversation

@kalayciburak

Copy link
Copy Markdown

Jira Link

CALCITE-7822

Changes Proposed

A RANGE window without ORDER BY takes an implicit sort key from the FROM item's monotonic column. After GROUP BY that column is no longer an input, but its original ordinal was still turned into a RexInputRef. Project.isValid then failed, for example SMALLINT NOT NULL (EMPNO) versus TINYINT (DEPTNO).

The converted key is kept when its input refs match the current input. Otherwise a simple column is rebound by name, or dropped if GROUP BY removed it. Unbounded RANGE already allows an empty order list.

select deptno, count(*) over () as c from emp group by deptno on the scott schema now returns DEPTNO=10/20/30 with C=3. The same shape with an explicit RANGE frame and max(max(sal)) over () grouped by job also converts. ROWS framing, an explicit ORDER BY, and GROUP BY of the monotonic column itself are unchanged.

Tests

  • JAVA_HOME=21 ./gradlew :core:test --tests org.apache.calcite.test.JdbcTest.testEmptyOverInAggregateQuery — failed before the fix with AssertionError: type mismatch: ref: SMALLINT NOT NULL input: TINYINT; passed after
  • ./gradlew :core:test --tests org.apache.calcite.test.JdbcTest.testWin* --tests org.apache.calcite.test.JdbcTest.testOver* --tests org.apache.calcite.test.SqlToRelConverterTest.testWindow* --tests org.apache.calcite.test.SqlToRelConverterTest.testOver* — 39 completed, 0 failed
  • ./gradlew :core:checkstyleMain :core:checkstyleTest — passed

…r OVER () in a GROUP BY query

A RANGE window without ORDER BY used the FROM item's monotonic
column as an implicit sort key. After GROUP BY that column is no
longer an input, but its original ordinal still became a RexInputRef,
so Project.isValid failed (SMALLINT NOT NULL vs TINYINT).

Keep the converted key when its input refs match the current input.
Otherwise rebind a simple column by name, or drop the key if GROUP BY
removed it. Unbounded RANGE already allows an empty order list.
@sonarqubecloud

Copy link
Copy Markdown

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