Skip to content

Add Sample.Any to the Python SDK to match Java's Sample.any#39442

Open
SreeramaYeshwanthGowd wants to merge 1 commit into
apache:masterfrom
SreeramaYeshwanthGowd:python-sample-any-18552
Open

Add Sample.Any to the Python SDK to match Java's Sample.any#39442
SreeramaYeshwanthGowd wants to merge 1 commit into
apache:masterfrom
SreeramaYeshwanthGowd:python-sample-any-18552

Conversation

@SreeramaYeshwanthGowd

@SreeramaYeshwanthGowd SreeramaYeshwanthGowd commented Jul 22, 2026

Copy link
Copy Markdown

Problem

Java has Sample.any(n), which returns up to n arbitrary elements from a PCollection without the cost of a uniform random sample. The Python SDK has no equivalent. Its only global option is Sample.FixedSizeGlobally(n), which runs a uniform reservoir sample and returns a single list[T]. This gap was reported in #18552.

Solution

Add Sample.Any, the Python equivalent of Java's Sample.any. It keeps up to n arbitrary elements using a small CombineFn and returns them as a PCollection of individual elements. It makes no uniformity guarantee and is cheaper than FixedSizeGlobally because it skips the per element random sampling. If the input has fewer than n elements, all of them are returned. This mirrors Java's semantics and its element returning result type.

Testing

Added unit tests in combiners_test.py that run on the DirectRunner and cover the normal case, an input smaller than n, a windowed input, an empty input, and n equal to zero. Also extended the existing display data test to cover the new transform.

Fixes #18552


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

Python only had Sample.FixedSizeGlobally, which runs a uniform reservoir
sample and returns a single list. Add Sample.Any, the equivalent of Java's
Sample.any, which returns up to n arbitrary elements as a PCollection without
the random sampling cost. If the input has fewer than n elements, all are
returned. Includes unit tests on the DirectRunner and a CHANGES.md entry.

Fixes apache#18552
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.

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.

No python equivalent of org.apache.beam.sdk.transforms.Sample.any(100)?

1 participant