Skip to content

[Python] Respect without_defaults in Top.Of with the global window - #41570

Open
Thanatos9404 wants to merge 2 commits into
apache:masterfrom
Thanatos9404:top-without-defaults
Open

Thanatos9404 wants to merge 2 commits into
apache:masterfrom
Thanatos9404:top-without-defaults

Conversation

@Thanatos9404

Copy link
Copy Markdown
Contributor

Fixes #41568

With default windowing, Top.Of takes a per-bundle path. That path always flattened in an empty accumulator so the output would never be empty, and _TopPerBundle.finish_bundle also emitted one for a bundle without elements. So Top.Of(n).without_defaults(), Top.Largest(n, has_defaults=False) and Top.Smallest(n, has_defaults=False) still produced [] for an empty input. With other windowing the flag already worked, because that path uses CombineGlobally(...).without_defaults().

Now:

  • the empty accumulator is added only when has_defaults is set, so the graph for the default case is unchanged;
  • _TopPerBundle emits nothing for a bundle that had no elements.

Tests in combiners_test.py:

  • test_empty_global_top_without_defaults checks Top.Of, Top.Largest and Top.Smallest on an empty input, and fails without the change.
  • test_global_top_without_defaults checks that a non-empty input still gives its result.

test_empty_global_top (with defaults) still gives [[]], and the rest of combiners_test.py passes. yapf is clean. I also added a CHANGES.md entry under Bugfixes for 2.79.0.

With default windowing, Top.Of took a per-bundle path that always added
an empty accumulator, and _TopPerBundle emitted one for bundles without
elements, so has_defaults=False still produced [] for an empty input.
Add the empty accumulator only when has_defaults is set, and emit
nothing for a bundle without elements.

Fixes apache#41568
@github-actions github-actions Bot added python core size/s S: 10-49 lines changed labels Nov 22, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

This PR properly ensures Top.Of(n).without_defaults() as well as Top.Largest and Top.Smallest with has_defaults=False respect the has_defaults flag when operating on a PCollection with default global windowing. The implementation cleanly avoids injecting the default empty accumulator and suppresses emitting empty heaps from _TopPerBundle.finish_bundle when a bundle processed no elements.

🔍 General Feedback

  • The changes to Top.Of.expand and _TopPerBundle are minimal, accurate, and preserve backward-compatible graph structure when has_defaults=True.
  • Unit tests in combiners_test.py thoroughly cover empty and non-empty inputs across Top.Of, Top.Largest, and Top.Smallest.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.
Backup reviewer: damccorm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Top.Of(n).without_defaults() still outputs [] for an empty input in the global window

1 participant