Skip to content

Comments

[python][io] Add native GenerateSequence bounded PTransform (#18088)#37675

Open
mtauha wants to merge 4 commits intoapache:masterfrom
mtauha:feature/python-generate-sequence-18088
Open

[python][io] Add native GenerateSequence bounded PTransform (#18088)#37675
mtauha wants to merge 4 commits intoapache:masterfrom
mtauha:feature/python-generate-sequence-18088

Conversation

@mtauha
Copy link

@mtauha mtauha commented Feb 21, 2026

Description

This PR adds a native Python GenerateSequence bounded PTransform to the
Python SDK, equivalent to the Java SDK's GenerateSequence (formerly known
as CountingInput).

Addresses #18088

Motivation

The Python SDK previously had no native equivalent of Java's GenerateSequence
/ CountingInput transform. The only existing Python implementation
(apache_beam/io/external/generate_sequence.py) requires a Java expansion
service and only works with the Flink runner, making it inaccessible to most
Python users.

This PR introduces a pure Python implementation that works on all runners
(DirectRunner, Dataflow, etc.) without any Java dependency.

Changes

  • Added sdks/python/apache_beam/io/generate_sequence.py:
    • GenerateSequence — a PTransform that produces a bounded sequence
      of integers from start (inclusive) to stop (exclusive)
    • _BoundedCountingSource — a BoundedSource backed by
      OffsetRangeTracker, supporting efficient splitting and dynamic
      work rebalancing across workers
  • Added sdks/python/apache_beam/io/generate_sequence_test.py with unit
    tests covering basic usage, edge cases, splitting behaviour, and
    size estimation

Notes

  • This is Phase 1 (bounded only). Unbounded streaming support with
    rate limiting will follow in a separate PR.
  • The existing external Flink-only version at
    apache_beam/io/external/generate_sequence.py is untouched.
  • Implementation is modelled after the Java CountingSource.java and
    follows the same BoundedSource + OffsetRangeTracker pattern used
    by other Python SDK IO sources.

Testing

cd sdks/python
python -m pytest apache_beam/io/generate_sequence_test.py -v

mtauha and others added 3 commits February 21, 2026 23:38
This adds a native Python implementation of GenerateSequence, equivalent
to the Java SDK's GenerateSequence/CountingSource. The transform generates
a bounded sequence of integers from start (inclusive) to stop (exclusive).

Key features:
- BoundedSource implementation with efficient splitting support
- OffsetRangeTracker for dynamic work rebalancing
- VarIntCoder for efficient integer encoding
- DisplayData support for pipeline visualization

The unbounded mode is not yet implemented and will raise
NotImplementedError if stop is not specified.

Closes apache#18088
Adds comprehensive tests for the native Python GenerateSequence transform:
- Basic bounded sequence generation
- Edge cases (empty range, single element, start > 0)
- Invalid input validation (negative start, stop < start)
- Large sequence handling
- Unbounded mode error handling
- BoundedSource API tests (split, estimate_size, range_tracker, read)
- DisplayData tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Document the new native Python GenerateSequence transform in the
CHANGES.md file under New Features / Improvements for version 2.73.0.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @mtauha, 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 significantly enhances the Python SDK by introducing a native GenerateSequence PTransform. This new capability allows Python users to generate bounded sequences of integers directly within their pipelines, ensuring compatibility across all Beam runners without external Java dependencies. The implementation leverages Beam's BoundedSource and OffsetRangeTracker for robust and scalable data generation, filling a critical gap in the Python SDK's feature set.

Highlights

  • Native Python GenerateSequence PTransform: Introduced a pure Python implementation of the GenerateSequence PTransform, providing a native equivalent to the Java SDK's GenerateSequence (formerly CountingInput).
  • Runner Compatibility: The new GenerateSequence works across all Apache Beam runners (DirectRunner, Dataflow, etc.) without requiring any Java dependencies, unlike the previous Flink-only external version.
  • Bounded Source Implementation: Implemented _BoundedCountingSource, a BoundedSource backed by OffsetRangeTracker, which supports efficient splitting and dynamic work rebalancing for generating integer sequences.
  • Comprehensive Testing: Added extensive unit tests covering basic usage, edge cases, splitting behavior, and size estimation for both GenerateSequence and _BoundedCountingSource.
  • Future Unbounded Support: This initial release focuses on bounded sequence generation, with plans for unbounded streaming support and rate limiting to be addressed in a subsequent pull request.

🧠 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.

Changelog
  • CHANGES.md
    • Added an entry for the new native Python GenerateSequence transform.
  • sdks/python/apache_beam/io/generate_sequence.py
    • Added GenerateSequence PTransform for generating bounded integer sequences.
    • Implemented _BoundedCountingSource as a BoundedSource for sequence generation, including estimate_size, split, get_range_tracker, and read methods.
    • Included example usage and documentation for the new transform.
  • sdks/python/apache_beam/io/generate_sequence_test.py
    • Added unit tests for GenerateSequence covering basic functionality, start/stop values, empty ranges, single elements, and invalid inputs.
    • Added unit tests for _BoundedCountingSource to verify size estimation, splitting logic, range tracking, reading behavior, and default output coder.
Activity
  • No human activity has been recorded on this pull request yet.
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.

@github-actions
Copy link
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@mtauha
Copy link
Author

mtauha commented Feb 21, 2026

assign set of reviewers

@github-actions
Copy link
Contributor

Assigning reviewers:

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

@mtauha
Copy link
Author

mtauha commented Feb 21, 2026

remind me after tests pass

@github-actions
Copy link
Contributor

Ok - I'll remind @mtauha after tests pass

@mtauha
Copy link
Author

mtauha commented Feb 21, 2026

Hey @damccorm @robertwb @claudevdm @kennknowles @shunping!

I'm a GSoC 2026 applicant and this is one of my first contributions to Apache Beam as part of my prep. I've implemented the bounded GenerateSequence PTransform in Python (Phase 1 of #18088), modelled after Java's CountingSource.

Looks like some checks might be failing and I'm honestly not sure why. Would really appreciate it if you could take a look and guide me in the right direction! Any review feedback is super welcome too.

Thanks a lot for your time!

R: @damccorm @robertwb @claudevdm @kennknowles @shunping

@github-actions
Copy link
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

@mtauha mtauha changed the title Feature/python generate sequence 18088 [python][io] Add native GenerateSequence bounded PTransform (#18088) Feb 21, 2026
@codecov
Copy link

codecov bot commented Feb 21, 2026

Codecov Report

❌ Patch coverage is 97.95918% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 57.03%. Comparing base (87c50c6) to head (b07aa51).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/io/generate_sequence.py 97.95% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             master   #37675       +/-   ##
=============================================
+ Coverage     40.08%   57.03%   +16.94%     
  Complexity     3416     3416               
=============================================
  Files          1178     1179        +1     
  Lines        187411   187580      +169     
  Branches       3588     3588               
=============================================
+ Hits          75120   106981    +31861     
+ Misses       108901    77209    -31692     
  Partials       3390     3390               
Flag Coverage Δ
python 80.43% <97.95%> (+40.71%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant