Skip to content

Encode paneinfo with PaneInfoCoder. #34824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion sdks/python/apache_beam/coders/coders.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
from apache_beam.portability.api import beam_runner_api_pb2
from apache_beam.typehints import typehints
from apache_beam.utils import proto_utils
from apache_beam.utils import windowed_value

if TYPE_CHECKING:
from apache_beam.coders.typecoders import CoderRegistry
Expand Down Expand Up @@ -113,7 +114,8 @@
'WindowedValueCoder',
'ParamWindowedValueCoder',
'BigIntegerCoder',
'DecimalCoder'
'DecimalCoder',
'PaneInfoCoder'
]

T = TypeVar('T')
Expand Down Expand Up @@ -1753,6 +1755,18 @@ def __hash__(self):
return hash(type(self))


class PaneInfoCoder(FastCoder):
def _create_impl(self):
return coder_impl.PaneInfoCoderImpl()

def is_deterministic(self):
# type: () -> bool
return True

def to_type_hint(self):
return windowed_value.PaneInfo


class DecimalCoder(FastCoder):
def _create_impl(self):
return coder_impl.DecimalCoderImpl()
Expand Down
13 changes: 13 additions & 0 deletions sdks/python/apache_beam/coders/coders_test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,18 @@ def test_interval_window_coder(self):
coders.TupleCoder((coders.IntervalWindowCoder(), )),
(window.IntervalWindow(0, 10), ))

def test_paneinfo_window_coder(self):
self.check_coder(
coders.PaneInfoCoder(),
*[
windowed_value.PaneInfo(
is_first=y == 0,
is_last=y == 9,
timing=windowed_value.PaneInfoTiming.EARLY,
index=y,
nonspeculative_index=-1) for y in range(0, 10)
])

def test_timestamp_coder(self):
self.check_coder(
coders.TimestampCoder(),
Expand Down Expand Up @@ -539,6 +551,7 @@ def test_windowed_value_coder(self):
def test_param_windowed_value_coder(self):
from apache_beam.transforms.window import IntervalWindow
from apache_beam.utils.windowed_value import PaneInfo
# pylint: disable=too-many-function-args
wv = windowed_value.create(
b'',
# Milliseconds to microseconds
Expand Down
2 changes: 2 additions & 0 deletions sdks/python/apache_beam/coders/typecoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def MakeXyzs(v):

from apache_beam.coders import coders
from apache_beam.typehints import typehints
from apache_beam.utils import windowed_value

__all__ = ['registry']

Expand All @@ -92,6 +93,7 @@ def register_standard_coders(self, fallback_coder):
self._register_coder_internal(bytes, coders.BytesCoder)
self._register_coder_internal(bool, coders.BooleanCoder)
self._register_coder_internal(str, coders.StrUtf8Coder)
self._register_coder_internal(windowed_value.PaneInfo, coders.PaneInfoCoder)
Copy link
Contributor

Choose a reason for hiding this comment

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

This fix has a large blast radius. It changed registered standard coders which is default behavior globally. Shall we do this or just put a coder implementation in new ReShuffle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Are you suggesting to define PaneInfoCoder here

and then use CoderRegistry.register_coder?

I don't have strong opinion, is there a disadvantage to doing it here? It seems like an 'internal' coder use case?

Copy link
Contributor

Choose a reason for hiding this comment

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

@kennknowles What do you think about this? We should use PaneInfoCoder but it could break the job update.

Copy link
Contributor

Choose a reason for hiding this comment

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

is there a disadvantage to doing it here

This introduces a breaking change such that windowed_value.PaneInfo has changed default coder. This is not limited to ReShuffle but also user pipeline.

Since test results shows PaneInfoCoder is much more efficient than FastPrimitiveCoder for PaneInfo, I am fine with the change, just need to know this is another breaking change introduced in 2.65.0

If we keep this change I recommend to also mention this in https://github.com/apache/beam/blob/master/CHANGES.md#breaking-changes-1

Copy link
Contributor

Choose a reason for hiding this comment

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

self._register_coder_internal(typehints.TupleConstraint, coders.TupleCoder)
self._register_coder_internal(typehints.DictConstraint, coders.MapCoder)
self._register_coder_internal(
Expand Down
19 changes: 19 additions & 0 deletions sdks/python/apache_beam/coders/typecoders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from apache_beam.coders import typecoders
from apache_beam.internal import pickler
from apache_beam.typehints import typehints
from apache_beam.utils import windowed_value


class CustomClass(object):
Expand Down Expand Up @@ -141,6 +142,24 @@ def test_nullable_coder(self):
self.assertEqual(expected_coder.encode(None), real_coder.encode(None))
self.assertEqual(expected_coder.encode(b'abc'), real_coder.encode(b'abc'))

def test_paneinfo_coder(self):
expected_coder = coders.PaneInfoCoder()
real_coder = typecoders.registry.get_coder(windowed_value.PaneInfo)
self.assertEqual(expected_coder, real_coder)
for i in range(10):
pane_info = windowed_value.PaneInfo(
is_first=i==0,
is_last=i==9,
timing=windowed_value.PaneInfoTiming.EARLY, # 0
index=i,
nonspeculative_index=-1
)

encoded = real_coder.encode(pane_info)

self.assertEqual(expected_coder.encode(pane_info), encoded)
self.assertEqual(pane_info, real_coder.decode(encoded))


if __name__ == '__main__':
unittest.main()
Loading