[Bug]: Weighted ApproximateQuantiles depends on the scale of the weights

オープン
#41,559 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@anishmehta24 がすでに取り組んでいます。

2026年11月22日 から。

  • #41560 @anishmehta24 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
data

調査の方向性

The bug is in ApproximateQuantilesCombineFn.extract_output in the Python SDK's apache_beam/transforms/stats.py, where the offset = (total_weight - 1) / (num_quantiles - 1) term is used for weighted input. Read the docstring example and stats_test.py first, then check that weighted output stays the same when all weights are scaled. Linked PR #41560 is already open against this, so review or extend that PR instead of starting a parallel fix.

索引モデルが issue の本文から書いたものです。

説明

P2 python
What happened?

With weighted=True, ApproximateQuantiles gives different quantiles when every weight is multiplied by the same number, which shouldn't change a weighted quantile. Small weights give quantiles that are all the minimum:

import apache_beam as beam
from apache_beam.transforms.stats import ApproximateQuantiles

with beam.Pipeline() as p:
  for w in (1, 0.5, 0.01, 0.001):
    _ = (
        p | f'Create {w}' >> beam.Create([(i, w) for i in range(101)])
        | f'Quantiles {w}' >> ApproximateQuantiles.Globally(5, weighted=True)
        | f'Print {w}' >> beam.Map(print))
# w=1     -> [0, 25, 50, 75, 100]
# w=0.5   -> [0, 24, 50, 75, 100]
# w=0.01  -> [0, 0, 25, 50, 100]
# w=0.001 -> [0, 0, 0, 0, 100]

The weighted example in the ApproximateQuantiles docstring ([(i, 1 if i<10 else 1e-5) for i in range(101)], documented output [0, 2, 5, 7, 100]) gives [0, 2, 4, 7, 100] on master. It gives the documented output only when the weights are multiplied by 1000.

ApproximateQuantilesCombineFn.extract_output uses offset = (total_weight - 1) / (num_quantiles - 1) in both cases. The - 1 turns an element count into the index of the last element, which is right without weights, but with weights it subtracts one unit of weight, so the targets move with the scale. With offset = step for the weighted case, the k-th quantile is the first element whose cumulative weight exceeds k * total_weight / (num_quantiles - 1). That gives [0, 25, 50, 75, 100] for every scale above and the documented docstring output, and the existing tests in stats_test.py still pass. I have a PR ready.

Issue Priority

Priority: 2 (default / most bugs should be filed as P2)

Issue Components
  • Component: Python SDK
主要言語
Java
スター
8.7k
フォーク
4.8k
平均マージ
3日 16時間
マージ済み PR(30日)
445

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/beam のほかの issue

apache/beam の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。