Conversation
serprex
reviewed
Nov 8, 2025
serprex
approved these changes
Nov 8, 2025
quantile(), mimic parametric aggregates
Collaborator
Author
|
Requesting another review because I came up with a better, more general approach to supporting parameterized aggregates. |
ClickHouse provides a number of "parametric aggregates" which take one
or more parameters in a first set of parentheses and then normal
arguments in a second. An example is `quantile`, which can be called
like so:
SELECT quantile(0.25)(val) FROM t;
To mimic this feature, implement a `params` data type and function that
takes a list of values of any type. Detect when it's the first argument
to any aggregates and pass it as ClickHouse parameters before any other
arguments. The equivalent of the above query in PostgreSQL is:
SELECT quantile(params(0.25), val) FROM t;
The pattern is general and can be used for any aggregate functions, but
used here only for the new `quantile` and `quantileExact` aggregate
functions. Others can be added in the future; all they have to do is use
the `clickhouse_func_push_fail` function for their `SFUNC`, with one
variant that takes `params` as the first argument and the remaining
arguments specified as usual (likely as `"any"` to allow data type
enforcement to be pushed down to ClickHouse).
Done by using the new `ch_get_params_function() function to detect
`params()` as the first argument to an aggregate function in
`deparseAggref()` and emitting it before appending the arguments opening
parenthesis.
Document the new functions and explain how to use them.
While at it, simplify the handling of `F_REGEXP_LIKE_TEXT_TEXT` in
Postgres 14 and earlier by simply setting it on those versions, rather
than using `#define` to omit any code that uses it. This aligns with how
the type mapper handles other such data type constants.
Also:
* Fix an `Assert` for a variadic array to apply to the array, rather
than the parent node.
* Also, remove the unused `func_arg` field from the `deparse_expr_cxt`
struct and add a comment explaining the `array_as_tuple` field.
serprex
approved these changes
Nov 8, 2025
Member
serprex
left a comment
There was a problem hiding this comment.
it's hard since it's always going to be awkward how to map (args1)(args2) to postgres. this way at least makes it clearer there's something magical happening
Collaborator
Author
|
Yes, exactly. |
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.
ClickHouse provides a number of "parametric aggregates" which take one or more parameters in a first set of parentheses and then normal arguments in a second. An example is
quantile, which can be called like so:To mimic this feature, implement a
paramsdata type and function that takes a list of values of any type. Detect when it's the first argument to any aggregates and pass it as ClickHouse parameters before any other arguments. The equivalent of the above query in PostgreSQL is:The pattern is general and can be used for any aggregate functions, but used here only for the new
quantileandquantileExactaggregate functions. Others can be added in the future; all they have to do is use theclickhouse_func_push_failfunction for theirSFUNC, with one variant that takesparamsas the first argument and the remaining arguments specified as usual (likely as"any"to allow data type enforcement to be pushed down to ClickHouse).Done by using the new
ch_get_params_function() function to detectparams()as the first argument to an aggregate function indeparseAggref()` and emitting it before appending the arguments opening parenthesis.Document the new functions and explain how to use them.
While at it, simplify the handling of
F_REGEXP_LIKE_TEXT_TEXTin Postgres 14 and earlier by simply setting it on those versions, rather than using#defineto omit any code that uses it. This aligns with how the type mapper handles other such data type constants.Also:
Assertfor a variadic array to apply to the array, rather than the parent node. * Also, remove the unusedfunc_argfield from thedeparse_expr_cxtstruct and add a comment explaining thearray_as_tuplefield.