Skip to content

Portable tuple logical type - #40081

Open
Abacn wants to merge 1 commit into
apache:masterfrom
Abacn:preserve_tuple
Open

Portable tuple logical type#40081
Abacn wants to merge 1 commit into
apache:masterfrom
Abacn:preserve_tuple

Conversation

@Abacn

@Abacn Abacn commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Fix #40079

  • Fixed-length tuple logical type (beam:logical_type:fixed_tuple:v1): Backed by a Row with anonymous fields (f0, f1, ...) and converts to/from Python tuple, properly supporting heterogeneous elements (e.g. Tuple[str, int]) while keep serialization exactly the same as non-portable TupleCoder.

  • Variable-length tuple logical type (beam:logical_type:var_tuple:v1): Backed by an Array and converts to/from Python tuple for Tuple[T, ...]. This also keeps serialization the same as IterableCoder backing ArrayType.

  • Static row encoding in RowCoderImpl: Added support for schema option beam:option:row:static_encoding, omitting the field count and null-mask bitset when fields are non-nullable to match TupleCoder wire efficiency.

  • Symmetrical _from_runner_api on LogicalType: Enabled logical types to reconstruct parameterized typing from their proto representations, keeping typing_from_runner_api generic and preserving exact type annotations across serialization boundaries.

Please add a meaningful description for your change here


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)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@Abacn
Abacn force-pushed the preserve_tuple branch 2 times, most recently from 6824aba to 8b014a5 Compare September 9, 2026 19:24
@Abacn
Abacn marked this pull request as ready for review September 9, 2026 20:21
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping for label python.

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

@Abacn

Abacn commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

Newly added test failing on Python3.10-3.12 but passing on Python3.13,3.14. Root cause: a CPython behavior change between 3.12 and 3.13 in issubclass() when dealing with types.GenericAlias (e.g. tuple[int, str]):

  1. issubclass difference on GenericAlias:

    • In Python <= 3.12: issubclass(tuple[int, str], tuple) returned False directly without raising an exception.
    • In Python >= 3.13: CPython changed issubclass so that passing a types.GenericAlias raises TypeError: issubclass() arg 1 must be a class.
  2. Impact on _safe_issubclass:

    • _safe_issubclass previously only fell back to inspecting derived.__origin__ inside its except (TypeError, AttributeError): block.
    • On Python 3.13, issubclass raised TypeError, triggering the __origin__ check which returned True.
    • On Python <= 3.12, issubclass simply returned False, so _safe_issubclass(tuple[...], tuple) evaluated to False.
  3. Consequence during Schema Translation:

    • In safe_issubclass(type, Sequence) and not safe_issubclass(type, (str, tuple)):
      • _safe_issubclass(tuple[...], Sequence) was True (because typing.Sequence.__subclasscheck__ raises TypeError across all Python versions, triggering the fallback).
      • _safe_issubclass(tuple[...], (str, tuple)) returned False on Python <= 3.12.
      • As a result, Python <= 3.12 treated tuple[...] as an arbitrary Sequence and translated it into ArrayType (which decodes to a list rather than a tuple).

* Fixed-length tuple logical type (`beam:logical_type:fixed_tuple:v1`): Backed by a Row with anonymous fields (`f0`, `f1`, ...) and converts to/from Python `tuple`, properly supporting heterogeneous elements (e.g. `Tuple[str, int]`) while keep serialization exactly the same as non-portable TupleCoder.

* Variable-length tuple logical type (`beam:logical_type:var_tuple:v1`): Backed by an Array and converts to/from Python `tuple` for `Tuple[T, ...]`. This also keeps serialization the same as IterableCoder backing ArrayType.

* Static row encoding in `RowCoderImpl`: Added support for schema option `beam:option:row:static_encoding`, omitting the field count and null-mask bitset when fields are non-nullable to match `TupleCoder` wire efficiency.

* Symmetrical `_from_runner_api` on `LogicalType`: Enabled logical types to reconstruct parameterized typing from their proto representations, keeping `typing_from_runner_api` generic and preserving exact type annotations across serialization boundaries.

* Add a test case for Beam SQL using python tuples
@codecov

codecov Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.95775% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.53%. Comparing base (84713ce) to head (d0c241d).
⚠️ Report is 19 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/typehints/schemas.py 92.04% 7 Missing ⚠️
sdks/python/apache_beam/coders/coder_impl.py 95.12% 2 Missing ⚠️
sdks/python/apache_beam/coders/row_coder.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #40081      +/-   ##
============================================
+ Coverage     57.24%   58.53%   +1.29%     
- Complexity     3696     5980    +2284     
============================================
  Files          1200     1459     +259     
  Lines        194031   208567   +14536     
  Branches       3889     5364    +1475     
============================================
+ Hits         111070   122093   +11023     
- Misses        79366    82177    +2811     
- Partials       3595     4297     +702     
Flag Coverage Δ
python 79.85% <92.95%> (+0.01%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 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.

[Bug]: Inconsistent types for tuple fields in Beam Schema and Row through serialization boundary

1 participant