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

Ouverte
#41,559 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 2 jours

@anishmehta24 y travaille déjà.

Depuis le 22/11/2026.

  • #41560 par @anishmehta24 — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
25/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
python
Domaine
data

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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
Langage dominant
Java
Étoiles
8.7k
Forks
4.8k
Merge moyen
3 j 16 h
PR mergées (30 j)
445

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de apache/beam

Toutes les issues de apache/beam

Issues similaires

Plus d'issues Java

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.