Pushdown percentile_cont() as quantile()#36
Merged
Merged
Conversation
Detect use of `percentile_cont()`, an ordered set aggregate, and push it down to ClickHouse as `quantile()`. Like `quantile()`'s parameters, the argument to `percentile_cont()` is evaluates only once, but the arguments to `WITHIN GROUP (ORDER BY)` are evaluated for each row. In examples seen in the wild, including [HouseClick] and [this blog post], this appears to be a valid mapping, as long as there is no `DESC`, `USING >`, or `NULLS FIRST`. In the future we should be able to map `percentile_cont(float[])` to `quantiles()`, though it will require converting the array argument to a parameter list. It might also make sense to map `percentile_disc` to `quantileExactHigh()`. To implement this syntax conversion, `deparseAggref()` borrows from `postgres_fdw` to output the arguments to the aggregate as a parameter list and then the `WITHIN GROUP (ORDER)` as the normal parameter arguments. Thus this PostgreSQL query: ```sql SELECT percentile_cont(0.25) WITHIN GROUP (ORDER BY a) FROM t1; ``` Maps to this ClickHouse query: ```sql SELECT quantile(0.25)(a) FROM t1; ``` The non-default `ORDER BY` suffixes `DESC` and `NULLS FIRST` are not supported and will raise an error. For normal aggregates it keeps the previous behavior. [HouseClick]: https://github.com/ClickHouse/HouseClick/blob/fa449b2/app/lib/analytics_queries.ts [this blog post]: https://clickhouse.com/blog/redshift-vs-clickhouse-comparison#ethereum-gas-used-by-week "Optimizing Analytical Workloads: Comparing Redshift vs ClickHouse"
serprex
approved these changes
Nov 11, 2025
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.
Detect use of
percentile_cont(), an ordered set aggregate, and push it down to ClickHouse asquantile(). Likequantile()'s parameters, the argument topercentile_cont()is evaluates only once, but the arguments toWITHIN GROUP (ORDER BY)are evaluated for each row.In examples seen in the wild, including HouseClick and this blog post, this appears to be a valid mapping, as long as there is no
DESC,USING >, orNULLS FIRST.In the future we should be able to map
percentile_cont(float[])toquantiles(), though it will require converting the array argument to a parameter list. It might also make sense to mappercentile_disctoquantileExactHigh().To implement this syntax conversion,
deparseAggref()borrows frompostgres_fdwto output the arguments to the aggregate as a parameter list and then theWITHIN GROUP (ORDER)as the normal parameter arguments. Thus this PostgreSQL query:Maps to this ClickHouse query:
The non-default
ORDER BYsuffixesDESCandNULLS FIRSTare not supported and will raise an error.For normal aggregates it keeps the previous behavior.