Skip to content

Conversation

@dylanhz
Copy link
Contributor

@dylanhz dylanhz commented Dec 5, 2025

What is the purpose of the change

Fix catastrophic cancellation in naive algorithm of variance related functions.

Brief change log

  • Introduce Welford's online algorithm for variance related functions.
  • Add a new internal built-in agg function WELFORD_M2.
  • Add new rewrite rule to reuse the intermediate result welford_m2 for all variance related functions.

Verifying this change

New test class: MathAggFunctionITCase.
Update some existing tests using variance related functions.

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): (no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): (yes)
  • The serializers: (no)
  • The runtime per-record code paths (performance sensitive): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
  • The S3 file system connector: (no)

Documentation

  • Does this pull request introduce a new feature? (no)
  • If yes, how is the feature documented? (not documented)

@dylanhz
Copy link
Contributor Author

dylanhz commented Dec 5, 2025

The first commit will be removed once it is merged.
#27319

@flinkbot
Copy link
Collaborator

flinkbot commented Dec 5, 2025

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

@lincoln-lil
Copy link
Contributor

@flinkbot run azure

1 similar comment
@dylanhz
Copy link
Contributor Author

dylanhz commented Dec 23, 2025

@flinkbot run azure

…e related functions to avoid catastrophic cancellation in naive algorithm
@dylanhz
Copy link
Contributor Author

dylanhz commented Dec 24, 2025

The @flinkbot run azure command is not working, we need some help to fix it.

Copy link
Contributor

@lincoln-lil lincoln-lil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylanhz Thank you for fixing this! This pr is of high code quality with solid test coverage, and it effectively resolves the long-standing numerical stability issues in these functions.

I checked:
The core implementation in WelfordM2AggFunction (accumulate, merge, retract, and numerical safeguards in getValue).
The related changes in AggregateReduceFunctionsRule, with corresponding plan tests.

Overall, LGTM! Only minor comments.

@Override
public Double getValue(WelfordM2Accumulator acc) {
// Theoretically, acc.m2 should always be non-negative.
// But in practice it may be negative if records are out of order, which is different from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: IIUC, perhaps we should clarify here that out-of-order itself doesn't affect the result mathematically, the issue mainly stems from accumulated precision loss caused by floating-point arithmetic in combination with retractions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input. Precision is irrelevant here actually. The negative value is simply a mathematical result of the algorithm when processing unmatched retractions.

Regarding the comment, I've updated it to be more explicit and focused on the current logic. What do you think of this version?

        // Theoretically, acc.m2 should always be non-negative.
        // But in practice it may become negative due to unmatched retractions.
        // (e.g., [+I, 1], [+I, 2] followed by [-D, 3], which results in acc.m2 = -4)
        // Therefore, return null in such cases to indicate an invalid result.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants