sql: implement the any_value() aggregate function#172656
Open
virajchogle wants to merge 1 commit into
Open
Conversation
virajchogle
requested review from
bghal and
michae2
and removed request for
a team
July 19, 2026 06:29
|
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. |
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
force-pushed
the
any-value-aggregate-172590
branch
from
July 21, 2026 19:51
a59b193 to
5f8f0c8
Compare
|
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. |
|
Detected infrastructure failure (matched: self-hosted runner lost communication with the server). Automatically rerunning failed jobs. (run link) |
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.
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.