Skip to content

Error when encountering an unsupported aggregate function#1020

Open
sgrif wants to merge 1 commit into
mainfrom
sg-error-on-unsupported-aggregates
Open

Error when encountering an unsupported aggregate function#1020
sgrif wants to merge 1 commit into
mainfrom
sg-error-on-unsupported-aggregates

Conversation

@sgrif
Copy link
Copy Markdown
Contributor

@sgrif sgrif commented May 28, 2026

When handling aggregate functions in cross-shard queries, we can only return a correct result for functions that we have explicit support for. No matter what the aggregate function is doing, we need to know how to combine the results from the separate shards.

Prior to this change, we would just silently do the wrong thing, treating this as any other non-aggregate expression and just returning a union of the rows from each query. We now explicitly check if an aggregate function with that name exists and error if we don't recognize it.

This is future proofed against new functions in later postgres versions, as well as user defined aggregates (which we can likely never support). This will produce a false positive if a function exists and is defined as aggregate for some argument types but not others. But frankly anyone doing that is asking for trouble.

This logic is objectively in the wrong place. What we are doing here is validation, not parsing. However as I'm familiarizing myself with these code paths and preparing a larger rearchitecture, I'm slowly hammering things into a shape that's easier to move around. I do not intend on leaving this logic here long term.

@blacksmith-sh

This comment has been minimized.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

❌ Patch coverage is 88.46154% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pgdog/src/backend/pool/connection/aggregate.rs 12.50% 7 Missing ⚠️
pgdog/src/frontend/router/parser/aggregate.rs 92.30% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment thread pgdog/src/frontend/router/parser/aggregate.rs
@sgrif sgrif changed the base branch from main to sg-cache-key-pair June 1, 2026 17:26
@sgrif sgrif force-pushed the sg-error-on-unsupported-aggregates branch from fb4cfe1 to ed257bc Compare June 1, 2026 17:28
@sgrif sgrif changed the base branch from sg-cache-key-pair to main June 2, 2026 12:47
@sgrif sgrif force-pushed the sg-error-on-unsupported-aggregates branch from ed257bc to a4687cb Compare June 4, 2026 13:30
@sgrif sgrif marked this pull request as ready for review June 4, 2026 13:30
@sgrif sgrif requested a review from levkk June 4, 2026 13:30
@sgrif
Copy link
Copy Markdown
Contributor Author

sgrif commented Jun 4, 2026

Decided to move forward with this without handling direct-to-shard

When handling aggregate functions in cross-shard queries, we can only
return a correct result for functions that we have explicit support
for. No matter what the aggregate function is doing, we need to know how
to combine the results from the separate shards.

Prior to this change, we would just silently do the wrong thing,
treating this as any other non-aggregate expression and just returning a
union of the rows from each query. We now explicitly check if an
aggregate function with that name exists and error if we don't recognize
it.

This is future proofed against new functions in later postgres versions,
as well as user defined aggregates (which we can likely never support).
This will produce a false positive if a function exists and is defined
as aggregate for some argument types but not others. But frankly anyone
doing that is asking for trouble.

In theory we could potentially not error on direct-to-shard queries, but
ultimately handling that proved to be a massive rabbit hole. It's
something I'd like to do in the future after more refactoring, but for
the time being it seems reasonable to assume that if `SELECT mysum(...)`
fails, `SELECT mysum(...) WHERE shardkey = 1` would also fail
@sgrif sgrif force-pushed the sg-error-on-unsupported-aggregates branch from c9484f1 to 40c5c84 Compare June 4, 2026 15:46
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