Skip to content

sql: implement the any_value() aggregate function#172656

Open
virajchogle wants to merge 1 commit into
cockroachdb:masterfrom
virajchogle:any-value-aggregate-172590
Open

sql: implement the any_value() aggregate function#172656
virajchogle wants to merge 1 commit into
cockroachdb:masterfrom
virajchogle:any-value-aggregate-172590

Conversation

@virajchogle

Copy link
Copy Markdown
Contributor

any_value() was added in PostgreSQL 16 and returns an arbitrary non-NULL value from the input group, or NULL if there are no non-NULL values. See #172590

Implementation

CockroachDB already has an internal aggregate with exactly these semantics: any_not_null, used by the optimizer to pass through columns that are constant within a group. This PR exposes it under the PostgreSQL name rather than duplicating it: the builtin reuses the any_not_null constructor, and the optimizer lowers any_value to the existing AnyNotNullAgg operator, following the precedent of aliases like every/bool_and. Vectorized and distributed execution work without introducing a new AggregatorSpec function.

One consequence is that EXPLAIN shows any-not-null-agg for the aggregation (the output column keeps the any_value name). Happy to switch to a dedicated operator if that is preferred.

Latent window bug

any_value(x) OVER (...) initially crashed with an internal error: the vectorized planner assumes every optimized aggregate can run as a window aggregate, but any_not_null is the only one without a generated window variant. This was unreachable before since users had no way to name any_not_null. The Windower support check now rejects it so these queries fall back to the row-based windower.

Tests

Logic tests cover NULL skipping, all-NULL group, empty input, GROUP BY, FILTER, DISTINCT, and window usage; all logictest configs pass, including mixed-version. Optbuilder testdata locks in the lowering.

Resolves: #172590
Epic: none

Release note (sql change): Added the any_value() aggregate function, which returns an arbitrary non-NULL value from the input group, matching PostgreSQL 16.

@virajchogle
virajchogle requested review from a team as code owners July 19, 2026 06:29
@virajchogle
virajchogle requested review from bghal and michae2 and removed request for a team July 19, 2026 06:29
@blathers-crl

blathers-crl Bot commented Jul 19, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Jul 19, 2026
Reuse the internal any_not_null aggregate, which has the same semantics as this PostgreSQL 16 function. Window queries fall back to the row-based windower since any_not_null has no vectorized window implementation.

Resolves: cockroachdb#172590
Epic: none

Release note (sql change): Added the any_value() aggregate function, which returns an arbitrary non-NULL value from the input group, matching PostgreSQL 16.
@virajchogle
virajchogle force-pushed the any-value-aggregate-172590 branch from a59b193 to 5f8f0c8 Compare July 21, 2026 19:51
@blathers-crl

blathers-crl Bot commented Jul 21, 2026

Copy link
Copy Markdown

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl

blathers-crl Bot commented Jul 21, 2026

Copy link
Copy Markdown

Detected infrastructure failure (matched: self-hosted runner lost communication with the server). Automatically rerunning failed jobs. (run link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: implement any_value() aggregate function

1 participant