Skip to content

Add support for datetime, date, and frozenset to FastPrimitivesCoder#38590

Open
mattalbr wants to merge 3 commits into
apache:masterfrom
mattalbr:fast_primitives
Open

Add support for datetime, date, and frozenset to FastPrimitivesCoder#38590
mattalbr wants to merge 3 commits into
apache:masterfrom
mattalbr:fast_primitives

Conversation

@mattalbr
Copy link
Copy Markdown

@mattalbr mattalbr commented May 21, 2026

Significantly reduces bytes written to the wire for datetime, date and frozenset which previously would have fallen through to pickling.

Fixes #38549

Sorry for the significant formatting changes, they appear to be a result of the yapf pre-commit. Let me know if you'd like me to keep them or not.


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.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 FastPrimitivesCoder by adding native support for datetime, date, and frozenset types. By moving away from the generic pickling fallback for these common types, the changes significantly improve serialization efficiency and reduce the amount of data transmitted over the wire.

Highlights

  • Added support for datetime, date, and frozenset: Implemented native encoding and decoding for datetime, date, and frozenset types in FastPrimitivesCoder, replacing the previous fallback to pickling to reduce serialized size.
  • Codebase reformatting: Applied yapf formatting to the modified files, resulting in significant whitespace and stylistic changes.
New Features

🧠 You can now enable Memory (public preview) 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 the 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 counterproductive. 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.

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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for datetime.datetime, datetime.date, and frozenset types in the FastPrimitivesCoder, utilizing RFC 9557 for lossless datetime timezone encoding. It also includes extensive formatting updates, such as quote normalization. Review feedback points out critical compatibility issues with Python 3.8 due to the direct use of the zoneinfo module, which requires conditional imports and availability checks. Additionally, the encoding logic for datetime and date needs to be corrected to properly handle nested contexts by using a single write operation and correctly passing the nested parameter to the stream.

Comment thread sdks/python/apache_beam/coders/coder_impl.py Outdated
Comment on lines +452 to +464
elif t is datetime.datetime:
# We use RFC 9557 for lossless encoding of timezone info.
stream.write_byte(DATETIME_TYPE)
stream.write(value.isoformat().encode("utf-8"))
if value.tzinfo is not None and type(
value.tzinfo) is not datetime.timezone:
stream.write(f"[{value.tzinfo}]".encode("utf-8"))
if type(
value.tzinfo) is datetime.timezone and (tzname :=
value.tzname()) is not None:
stream.write(f"[tzn={tzname}]".encode("utf-8"))
if value.fold != 0:
stream.write(f"[f={value.fold}]".encode("utf-8"))
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.

high

The current implementation of datetime encoding has two issues:

  1. It performs multiple stream.write calls. If nested=True, each call would potentially create a separate length-prefixed block (depending on the OutputStream implementation), but the decoder uses a single stream.read_all(nested) call, which would only read the first part.
  2. It omits the nested parameter in the stream.write calls, which will cause a mismatch with the decoder's read_all(nested) when the coder is used in a nested context (e.g., as an element in a list).

You should construct the full string first and write it once using the nested parameter.

Suggested change
elif t is datetime.datetime:
# We use RFC 9557 for lossless encoding of timezone info.
stream.write_byte(DATETIME_TYPE)
stream.write(value.isoformat().encode("utf-8"))
if value.tzinfo is not None and type(
value.tzinfo) is not datetime.timezone:
stream.write(f"[{value.tzinfo}]".encode("utf-8"))
if type(
value.tzinfo) is datetime.timezone and (tzname :=
value.tzname()) is not None:
stream.write(f"[tzn={tzname}]".encode("utf-8"))
if value.fold != 0:
stream.write(f"[f={value.fold}]".encode("utf-8"))
elif t is datetime.datetime:
# We use RFC 9557 for lossless encoding of timezone info.
stream.write_byte(DATETIME_TYPE)
dt_str = value.isoformat()
if value.tzinfo is not None and type(value.tzinfo) is not datetime.timezone:
dt_str += f"[{value.tzinfo}]"
if type(value.tzinfo) is datetime.timezone and (tzname := value.tzname()) is not None:
dt_str += f"[tzn={tzname}]"
if value.fold != 0:
dt_str += f"[f={value.fold}]"
stream.write(dt_str.encode("utf-8"), nested)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

nested defaults to False and this matches the style of the other implementations which also omit the nested parameter when not needed.

It's also likely more efficient to write to the stream multiple times which has a byte buffer vs constructing new immutable strings

Comment on lines +465 to +467
elif t is datetime.date:
stream.write_byte(DATE_TYPE)
stream.write(value.isoformat().encode("utf-8"))
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.

high

The stream.write call for date is missing the nested parameter. This will cause a mismatch with the decoder's read_all(nested) when this coder is used in a nested context.

Suggested change
elif t is datetime.date:
stream.write_byte(DATE_TYPE)
stream.write(value.isoformat().encode("utf-8"))
elif t is datetime.date:
stream.write_byte(DATE_TYPE)
stream.write(value.isoformat().encode("utf-8"), nested)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

nested defaults to False and this matches the style of the other implementations which also omit the nested parameter when not needed

Comment thread sdks/python/apache_beam/coders/coder_impl.py Outdated
import sys
import textwrap
import unittest
import zoneinfo
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.

medium

The zoneinfo module is not available in Python 3.8. To ensure tests pass on all supported Python versions, use a conditional import.

Suggested change
import zoneinfo
try:
import zoneinfo
except ImportError:
zoneinfo = None

Comment thread sdks/python/apache_beam/coders/coders_test_common.py Outdated
@github-actions
Copy link
Copy Markdown
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

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Support for efficient serialization of more built-in types

1 participant