Skip to content

[Python] Add take(n) convenience for PCollection#37470

Merged
jrmccluskey merged 1 commit intoapache:masterfrom
shaheeramjad:take-n-convenience
Feb 3, 2026
Merged

[Python] Add take(n) convenience for PCollection#37470
jrmccluskey merged 1 commit intoapache:masterfrom
shaheeramjad:take-n-convenience

Conversation

@shaheeramjad
Copy link
Copy Markdown
Contributor

Adds take(n) convenience for PCollection: get the first N elements deterministically without using Top.Of + FlatMap manually.

Changes

  • transforms/util.py: Add Take transform and take(n) function (uses Top.Of + FlatMap internally).
  • pvalue.py: Add PCollection.take(n) method.
  • util_test.py: Add TakeTest (function/method syntax, edge cases, invalid n).
  • CHANGES.md: New Features entry for take(n).

Usage

Function syntax

first_10 = pcoll | beam.take(10)

Method syntax

first_10 = pcoll.take(10)

Fixes #37429


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.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

@github-actions github-actions Bot added the python label Feb 2, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @shaheeramjad, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Apache Beam Python SDK by introducing a new take(n) operation for PCollections. This feature provides a convenient and deterministic way to extract a specified number of initial elements from a PCollection, simplifying common data sampling patterns without requiring manual composition of existing transforms like Top.Of and FlatMap.

Highlights

  • New PCollection Convenience Method: Introduces a take(n) method and function for PCollections in the Python SDK, allowing users to easily retrieve the first N elements.
  • Deterministic Element Selection: The new take(n) functionality deterministically selects the first N elements, ensuring consistent results.
  • Simplified API: Provides a user-friendly API (pcoll.take(n) or pcoll | beam.take(n)) that abstracts the underlying implementation using Top.Of and FlatMap transforms.
  • Comprehensive Testing: Includes new unit tests covering various scenarios such as function and method syntax, edge cases (e.g., taking more elements than available, single element, all elements), and validation for positive input values of n.
  • Changelog Updated: An entry has been added to CHANGES.md to document this new feature for the Python SDK.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

* Add Take transform and take() in transforms/util.py
* Add PCollection.take(n) in pvalue.py
* Add TakeTest in util_test.py
* Update CHANGES.md

Fixes apache#37429
@shaheeramjad
Copy link
Copy Markdown
Contributor Author

HI @damccorm,
Just a gentle ping for review when you have time. Thanks!

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 2, 2026

Assigning reviewers:

R: @jrmccluskey 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).

@damccorm
Copy link
Copy Markdown
Contributor

damccorm commented Feb 2, 2026

I'll defer this one to Jack as the assigned reviewer - thanks

Copy link
Copy Markdown
Contributor

@jrmccluskey jrmccluskey left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@jrmccluskey jrmccluskey merged commit fd2babb into apache:master Feb 3, 2026
102 checks passed
@tvalentyn
Copy link
Copy Markdown
Contributor

tvalentyn commented Feb 4, 2026

Thanks @shaheeramjad for the initiative.

Takes the first N elements from this PCollection

This is misleading since PCollections are inherently unordered and distributed. There is no concept of "first X" elements in a pcollection.

  1. The way the docstring is worded may further create a misleading understanding that Take avoids iterating over all elements in the collection. I can't think of an implementation of this helper that avoids iteration over entire collection unless we limit ingestion of elements at the source.

  2.   `first_10 = pcoll.take(10)`
    

this syntax is not typical for Beam python; i wouldn't introduce it without a discussion on dev mailing list (dev@beam.apache.org)

  1. I am not (yet) convinced of the value of this helper if the only reason is to flatten the output from Top/Sample.

  2. Naming of the helper could also use more thought since we cannot easily rename Beam APIs, and would be appropriate to at least discuss on dev@.

I'd like to request we revert this PR until further discussion happens.

Thanks!

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.

[Feature Request]: Add take(n) convenience method to PCollection

4 participants