Skip to content

Fix offset-only aggregate queries - #2

Draft
wtsnz wants to merge 1 commit into
mainfrom
fix/lateral-offset-only-aggregates
Draft

Fix offset-only aggregate queries#2
wtsnz wants to merge 1 commit into
mainfrom
fix/lateral-offset-only-aggregates

Conversation

@wtsnz

@wtsnz wtsnz commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Summary

  • Wrap offset-only aggregate inputs in a subquery.
  • Preserve ordering when a limit or offset selects the aggregate input.
  • Continue to remove irrelevant ordering when neither operator is present.

Problem

Aggregate queries were wrapped only when they used distinct or limit. An ordered query with an offset but no limit could aggregate the wrong input set because the offset was not applied inside the aggregate subquery.

This is an existing AshSQL lateral-query defect that we found while we reviewed the grouped aggregate extraction. This fix is separate from the grouped strategy PR.

Impact

Root query aggregates now apply offset-only row selection before aggregation.

Regression coverage

The Postgres regression is in wtsnz/ash_postgres#1.

Validation

  • mix format --check-formatted
  • mix test — 1 test passed
  • The dependent Postgres regression passes on the wtsnz/ash_postgres:test/lateral-offset-only-aggregates branch.

Problem:
Aggregate queries were wrapped only for `distinct` or `limit`. An ordered query with only an offset could therefore aggregate the wrong input set because the offset was not applied inside the aggregate subquery.

Change:
Wrap offset-only inputs in all three aggregate-query paths and preserve ordering whenever a limit or offset depends on it. Continue removing irrelevant ordering when no row-selection operator requires it.

Provenance:
This is an existing AshSQL lateral-query defect discovered while reviewing the grouped aggregate extraction. It is intentionally isolated from the grouped feature branch.
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