Skip to content

GEP-26: translate GINQ window functions to SQL OVER - #3167

Closed
daniellansun wants to merge 2 commits into
apache:masterfrom
daniellansun:gep26-sql-window
Closed

daniellansun wants to merge 2 commits into
apache:masterfrom
daniellansun:gep26-sql-window

Conversation

@daniellansun

@daniellansun daniellansun commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Full GINQ → SQL window-function support for GEP-26 (not IR-only), plus corrections so window frames, ranks, and SQL OVER follow one set of rules on the collection provider and on the SQL providers.

Depends on: #3155. Branch is maintained by rebase only (never merge) onto current gep26-sql / #3155 tip. Future #3155 changes are rebased onto this branch the same way.

Related: GEP-26.

What this does

  • Registers portable GINQ window functions on the sealed standard registry as Kind.WINDOW (before seal): rowNumber, rank, denseRank, percentRank, cumeDist, ntile, lead, lag, firstValue, lastValue, nthValue, plus window count / sum / avg / min / max.
  • Pushes fn(...) over(partitionby ... orderby ... rows|range ...) through existing SqlWindow* IR; native + jOOQ renderers emit ANSI OVER.
  • GINQ semantic alignment: 0-based rowNumber / nthValue, ntile formula, single-row percentRank=1, sum null→0, whole partition when no frame.

Window frame / rank / OVER corrections

  • A null partition key is one group.
  • rank, denseRank, percentRank and cumeDist use every orderby key.
  • lead and lag move within the ordered partition. rowNumber and ntile do too, so a frame does not change them. SQL still rejects a frame on those functions, and on rank and its relatives, because SQL defines them without one. In memory, rank and its relatives still honor a frame.
  • An empty avg, and a variance or standard deviation with no samples, is null.
  • A range offset is a value interval in either sort direction. A descending orderby rewrites the SQL RANGE bounds so the interval stays the same. rows bounds are not rewritten.
  • ntile is GINQ's 0-based formula, rendered with integer division (CAST to BIGINT, or INTEGER on SQLite) instead of FLOOR.
  • A numeric RANGE offset is rejected at render time on SQLite, and on MySQL and MariaDB, where null ordering is a second sort key. COUNT(DISTINCT) OVER is rejected on SQLite. Both checks are shared by the native renderer and the jOOQ renderer.
  • A window function nested inside an aggregate is rejected.

Tests

./gradlew :groovy-ginq:test :groovy-ginq-sql:test

677 tests in groovy-ginq, 216 tests in groovy-ginq-sql.

Still deferred / limits

  • GROUPS, EXCLUDE, FILTER, a named WINDOW clause, and the window forms of median, stdev, stdevp, var, varp and agg stay deferred.
  • rowNumber, ntile, lead and lag without orderby follow scan order in SQL and insertion order in memory.
  • A fractional RANGE offset is printed by the native renderer. jOOQ accepts only an integer literal offset.
  • rank with a frame stays frame-sensitive in memory and is rejected in SQL.
  • A non-numeric range with a finite bound is an error. A date or time range is not supported.
  • Dialect frame limits are reported when SQL is rendered. mixedMode does not move those clauses back into memory.

Tip / CM

Auto-merge paused; do not merge until Daniel notifies resume + review.

@daniellansun
daniellansun force-pushed the gep26-sql-window branch 2 times, most recently from a820bef to e5c99c7 Compare November 2, 2026 17:34
@daniellansun daniellansun changed the title GEP-26: prepare SQL IR for window functions GEP-26: translate GINQ window functions to SQL OVER Nov 2, 2026
@paulk-asert

Copy link
Copy Markdown
Contributor

Looked at this for alpha-1 too. The code is confined to groovy-ginq-sql, and I ran a differential check of the window functions against the collection provider on H2: rowNumber/nthValue 0-based, ntile, single-row percentRank, lastValue with the default frame, lead/lag with defaults, running and whole-partition aggregates, and sum with nulls all match. The only differences were numeric types (double vs BigDecimal for avg/cumeDist) and two cases where the collection provider itself throws (pre-existing, raised separately).

Before merging:

  • Rebase once GEP-26: continue the GINQ SQL/jOOQ reference implementation (from #2658) #3155 lands; the Windows red here is the post-build cache scrub (rm: Device or resource busy), fixed on master in 7155266, not a test failure (BUILD SUCCESSFUL in 30m 5s). macOS is the known stall.
  • The user guide still says "The main current restriction in strict mode is window functions" and lists window functions as a mixedMode example; this PR doesn't touch the .adoc. GEP-26's coverage and deferred tables also still list window functions as deferred.
  • Your description says auto-merge is paused until you say otherwise, so I'll leave the timing to you. From my side, with the docs updated it's fine for alpha-1.

@daniellansun

daniellansun commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks Paul. The user guide no longer lists window functions as a strict-mode restriction. Portable GINQ window functions now translate in strict mode under native-sql and jooq-sql to ANSI OVER: rowNumber, rank, denseRank, percentRank, cumeDist, ntile, lead, lag, firstValue, lastValue, nthValue, and the window aggregates count, sum, avg, min and max. The window forms of median, stdev, stdevp, var, varp and agg remain deferred: strict mode rejects them, and mixedMode: true still evaluates that clause in memory.

This branch is rebased onto the current #3155 tip. I'll rebase again once #3155 lands. The earlier Windows failure was the post-build cache scrub, as you noted, not a test failure.

GEP-26's coverage and deferred tables on the website still list window functions as deferred. I'll update those next.

@daniellansun
daniellansun force-pushed the gep26-sql-window branch 3 times, most recently from a4ce5f3 to b5435d7 Compare November 18, 2026 22:25
@testlens-app

testlens-app Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 848656d
▶️ Tests: 134648 executed
⚪️ Checks: 18/18 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun

Copy link
Copy Markdown
Contributor Author

This work landed on master via #3259 (c7337c1).

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