Skip to content

[Feature Request]: native exact weighted percentiles with window support #29944

Closed

Description

@dengn

Why this is needed

Latency samples with counts, position-size-weighted price distributions and compressed histogram buckets need a weighted distribution. Expanding integer weights into repeated rows is impractical for large counts. Unweighted PERCENTILE_CONT does not compute the same statistic.

Requested behavior and scope

Provide an exact weighted-percentile primitive and document its weighted CDF/interpolation convention, supported weights, paired NULL rules and window support. This verified positive-integer-weight profile uses a lower weighted CDF threshold at p=0.5. The weighted P50 is 2 while supported unweighted PERCENTILE_CONT yields 3. Other ClickHouse quantile estimators/interpolation variants are separate contracts, not claimed equivalent. #25144 requests basic ordered-set percentiles, which already execute on this tested build.

Tested revision / existing issue check

  • MatrixOne commit: 23d8f61252f3017dff218b530817a240206fd762.
  • Existing issues were searched before filing; related requests/closed fixes are distinguished above.
  • All data below is synthetic. The probe is read-only and requires no persisted tables.

Executable unsupported case

SELECT quantileExactWeighted(v,w,0.5) result
FROM (
  SELECT 1 id,1 gid,0 tick,CAST(NULL AS DOUBLE) v,CAST(2.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 2 id,1 gid,0 tick,CAST(2.0 AS DOUBLE) v,CAST(4.0 AS DOUBLE) y,CAST(2.0 AS DOUBLE) w,0 flag
  UNION ALL SELECT 3 id,1 gid,1 tick,CAST(4.0 AS DOUBLE) v,CAST(3.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 4 id,1 gid,3 tick,CAST(4.0 AS DOUBLE) v,CAST(7.0 AS DOUBLE) y,CAST(3.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 5 id,1 gid,3 tick,CAST(NULL AS DOUBLE) v,CAST(5.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,0 flag
  UNION ALL SELECT 6 id,1 gid,8 tick,CAST(-1.0 AS DOUBLE) v,CAST(8.0 AS DOUBLE) y,CAST(2.0 AS DOUBLE) w,1 flag) fixture;

Actual MO result, reproduced 3/3:

(20105, "not supported: function or operator 'quantileexactweighted'")

This probe checks entry-name availability only. A rejection does not establish that the business calculation is impossible or that this is a valid DolphinDB script signature. The requested mathematical operation is defined above and checked by the independent profile below.

Additional actually executed cases

Each listed rejection was executed three times on the same synthetic fixture.

Case MO result Declared expected values / scope
quantileExactWeighted_entry rejected 3/3 Entry resolution only; no invented engine result

Working SQL control / alternative

The following control was executed 3/3 and its complete output values were checked, not merely execution success:

SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY v) result
FROM (
  SELECT 1 id,1 gid,0 tick,CAST(NULL AS DOUBLE) v,CAST(2.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 2 id,1 gid,0 tick,CAST(2.0 AS DOUBLE) v,CAST(4.0 AS DOUBLE) y,CAST(2.0 AS DOUBLE) w,0 flag
  UNION ALL SELECT 3 id,1 gid,1 tick,CAST(4.0 AS DOUBLE) v,CAST(3.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 4 id,1 gid,3 tick,CAST(4.0 AS DOUBLE) v,CAST(7.0 AS DOUBLE) y,CAST(3.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 5 id,1 gid,3 tick,CAST(NULL AS DOUBLE) v,CAST(5.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,0 flag
  UNION ALL SELECT 6 id,1 gid,8 tick,CAST(-1.0 AS DOUBLE) v,CAST(8.0 AS DOUBLE) y,CAST(2.0 AS DOUBLE) w,1 flag) fixture;
Control Verified values
unweighted_percentile_control 3

Live weighted reference

ClickHouse 26.9.16.11 executed the actual parameterized signature:

SELECT quantileExactWeighted(0.5)(v,CAST(w AS UInt64)) result
FROM (
  SELECT 1 id,1 gid,0 tick,CAST(NULL AS Nullable(Float64)) v,CAST(2.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 2 id,1 gid,0 tick,CAST(2.0 AS DOUBLE) v,CAST(4.0 AS DOUBLE) y,CAST(2.0 AS DOUBLE) w,0 flag
  UNION ALL SELECT 3 id,1 gid,1 tick,CAST(4.0 AS DOUBLE) v,CAST(3.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 4 id,1 gid,3 tick,CAST(4.0 AS DOUBLE) v,CAST(7.0 AS DOUBLE) y,CAST(3.0 AS DOUBLE) w,1 flag
  UNION ALL SELECT 5 id,1 gid,3 tick,CAST(NULL AS Nullable(Float64)) v,CAST(5.0 AS DOUBLE) y,CAST(1.0 AS DOUBLE) w,0 flag
  UNION ALL SELECT 6 id,1 gid,8 tick,CAST(-1.0 AS DOUBLE) v,CAST(8.0 AS DOUBLE) y,CAST(2.0 AS DOUBLE) w,1 flag) fixture;

It returned weighted P50 2, matching an independent weighted-CDF calculation. The MO entry probe uses a flat candidate SQL name only to show absence; it is not ClickHouse's curried signature. MO's already supported unweighted P50 is 3, so it is not a substitute.

Acceptance / regression coverage

Retain the self-contained fixture and assert complete values, NULL bits and row alignment. Add partitions, ties, empty/all-NULL frames, frame-entry/exit and type boundaries appropriate to this operator. Validate named/API availability separately from mathematical rewrites. Benchmark any claimed state/performance advantage with identical data and correctness rules; no speedup is asserted by these small tests.

Subsequent SELECT 1 succeeded after the test matrix. DolphinDB references are official documentation; no DolphinDB runtime or all-parameter parity is claimed.

Reference

https://clickhouse.com/docs/reference/aggregate-functions/reference/quantileexactweighted

Activity

  1. XuPeng-SH commented on Oct 12, 2026

    @XuPeng-SH
    Contributor

    Triage decision: defer.

    Reason: this is a specialized window/time-series capability. The report establishes a syntax or API gap, but does not show a confirmed customer blocker or near-term roadmap commitment. The umbrella report also notes that many profiles can be composed from existing SQL and does not provide a native-performance comparison.

    Promote when a concrete customer workload or roadmap commitment is identified and correctness/performance acceptance criteria are agreed.

  2. added this to the Backlog milestone on Oct 12, 2026
  3. self-assigned this
    on Oct 12, 2026
  4. added
    severity/s0
    ai-easy
    and removed on Oct 23, 2026
  5. gouhongshen commented on Oct 23, 2026

    @gouhongshen
    Contributor

    PR #31000 is trying to fix this issue.

  6. gouhongshen commented on Oct 25, 2026

    @gouhongshen
    Contributor

    PR #31000 has merged and is awaiting manual validation. The issue remains open.

  7. XuPeng-SH commented on Oct 25, 2026

    @XuPeng-SH
    Contributor

    Manual validation at fixed source SHA 1732bb3297f598bc9a90a41b8c5e4c9557cf0e9c.

    Command: PYTHONPATH=/tmp/qa-pymysql python3 /workspace/qa-evidence-1732bb/run_weighted_quantile_bvt.py against the isolated service at 127.0.0.1:16601. The runner executes the checked-in test/distributed/cases/function/func_aggr_weighted_quantile.test case.

    Result: 76 SQL statements, 5 expected invalid-input errors, 0 unexpected failures. On the issue fixture, weighted P50 was 2 and the unweighted PERCENTILE_CONT control was 3. Grouped and window results, ties, zero weights, paired NULLs, integer/DECIMAL weights, frame entry/exit, prepared level rebinding, and invalid level/weight inputs matched the case expectations.

    This is functional SQL evidence only. No performance comparison or speedup is claimed; estimator conventions outside the tested positive-integer weighted-CDF profile remain separate. The issue remains open.

  8. Ariznawlll commented on Oct 26, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main;commit:5fe08a3307e246e1ab9951ab690b50557af0f509,隔离编译二进制对应此 SHA,服务 git_version()=5fe08a3307

    • 部署方式:macOS arm64;独立持久化数据目录,单进程 LogService/TN/CN,MySQL 协议端口 6001;此前相同 SHA 的 SQL 矩阵也在隔离双 CN+Proxy 上通过

    • 测试日期:2026-10-26

    • 修复:PR #31000 已 MERGED,merge commit 在此官方 main 的祖先链。

    原问题验证

    原 issue 六行样本的 quantileExactWeighted(v,w,0.5) 实际为 2,未加权 PERCENTILE_CONT(0.5) 为 3,符合独立加权 CDF 下阈值。三组新库 3/3,MOTR 重复后 9/9。

    Happy Path 覆盖

    1. 分组 P50 为 (gid1,2)/(gid2,20)/(gid3,NULL);窗口按组分别广播 2、20、NULL。2. ties 的 P50/P90=5/9;零权重的 P0/P50/P100=0/5/5;空表及成对 NULL 样本返回 NULL。3. UINT64 大于 2^53 与 DECIMAL(20,0) 权重分别得 1、2;滑动 ROWS 帧结果为 1/1/3/2/2;PREPARE 后两次重绑 level 0.25/0.75 得 -1/4。

    Unhappy Path 覆盖

    1. level=1.5、负权重与小数权重均返回错误;错误后同一连接的有效 P50 仍为 2。没有把无效输入写成成功 golden。

    白盒验证

    运行真实 SQL 产品路径;PR #31000 同时提交了 docs/design/20261023-exact-weighted-percentiles.md 与功能 BVT。未在本轮运行全量 UT 或做等量数据性能对照,故不宣称任何性能收益。

    回归测试

    已加入 MOTR #278 的 script/14_issue_regression/issue_20261026_recent_assigned_sql.py 和配对 .out。本地 MOTR 定向 --repeat 3 成功 3/3,脚本每次使用三组独立数据库运行本 issue 场景,故计 9/9。完整 suite 与远端 CI 未运行。

    测试证据

    MOTR 回归脚本;本地定向记录 /private/tmp/mo-qa-new-20261026-ROza73/motr-278/var/log/run-1792983499457134000;脚本使用精确值、类型、行数和错误码断言。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/featureseverity/s0

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions