Support an in-call LIMIT clause for ARRAY_AGG() - #4600
Merged
Conversation
robert-brunel
added this pull request to stack #4601
September 9, 2026 14:22
robert-brunel
force-pushed
the
apple/robert-brunel/array_agg-limit-2
branch
from
September 9, 2026 14:42
3d1e277 to
3314ce2
Compare
pengpeng-lu
reviewed
Sep 9, 2026
robert-brunel
force-pushed
the
apple/robert-brunel/array_agg-limit-2
branch
from
September 10, 2026 13:55
3314ce2 to
f99e800
Compare
Base automatically changed from
apple/robert-brunel/array_agg-limit-1
to
main
September 10, 2026 16:41
robert-brunel
force-pushed
the
apple/robert-brunel/array_agg-limit-2
branch
from
September 10, 2026 16:45
f99e800 to
e4d5660
Compare
`ARRAY_AGG(«expr» LIMIT «n»)` restricts the number of elements collected into the array per group. Rows past the limit are neither converted nor collected by the accumulator, although they still have to be consumed in order to determine the group boundary. The `LIMIT` clause must come last, after the `{RESPECT|IGNORE} NULLS` and in-call `ORDER BY` clauses.
`LIMIT` can be used as a guard to impose a bound on both the arrays held in memory and the partial state serialized into a continuation. In conjunction with an in-call `ORDER BY` clause (which is anticipated by the grammar but not yet supported), `LIMIT` is also a convenient way to produce “top N” lists.
Regarding the grammar, note that the new `aggregateLimitClause` (which is admitted for `ARRAY_AGG()` only) is deliberately narrower than the existing (but unsupported) `limitClause` of a `SELECT` query: It takes no `OFFSET`, and `«n»` must be a literal, since the limit gets baked into the plan rather than bound per execution.
Resolves #4599.
robert-brunel
force-pushed
the
apple/robert-brunel/array_agg-limit-2
branch
from
September 11, 2026 12:04
e4d5660 to
a8eeefd
Compare
📊 Metrics Diff Analysis ReportSummary
ℹ️ About this analysisThis automated analysis compares query planner metrics between the base branch and this PR. It categorizes changes into:
The last category in particular may indicate planner regressions that should be investigated. New QueriesCount of new queries by file:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ARRAY_AGG(«expr» LIMIT «n»)restricts the number of elements collected into the array per group. Rows past the limit are neither converted nor collected by the accumulator, although they still have to be consumed in order to determine the group boundary. TheLIMITclause must come last, after the{RESPECT|IGNORE} NULLSand in-callORDER BYclauses.LIMITcan be used as a guard to impose a bound on both the arrays held in memory and the partial state serialized into a continuation. In conjunction with an in-callORDER BYclause (which is anticipated by the grammar but not yet supported),LIMITis also a convenient way to produce “top N” lists.Regarding the grammar, note that the new
aggregateLimitClause(which is admitted forARRAY_AGG()only) is deliberately narrower than the existing (but unsupported)limitClauseof aSELECTquery: It takes noOFFSET, and«n»must be a literal, since the limit gets baked into the plan rather than bound per execution.Resolves #4599.