shikhabhadoria commented on code in PR #41392:
URL: https://github.com/apache/beam/pull/41392#discussion_r4587991297
##########
sdks/python/apache_beam/yaml/yaml_mapping.py:
##########
@@ -871,8 +871,29 @@ def _AssignTimestamps(
"""
timestamp_fn = _as_callable_for_pcoll(pcoll, timestamp, 'timestamp',
language)
T = TypeVar('T')
- return pcoll | beam.Map(lambda x: TimestampedValue(x, timestamp_fn(x))
- ).with_input_types(T).with_output_types(T)
+ return pcoll | beam.Map(
+ lambda x: TimestampedValue(x, _to_timestamp(timestamp_fn(x)))
+ ).with_input_types(T).with_output_types(T)
+
+
+def _to_timestamp(ts):
+ if isinstance(ts, str):
+ if ts and ts[-1] not in 'Zz' and '+' not in ts[10:] and '-' not in ts[10:]:
Review Comment:
Updated `Timestamp.from_rfc3339` in `apache_beam/utils/timestamp.py` to
handle naive datetimes (`dt.replace(tzinfo=pytz.UTC) if dt.tzinfo is None else
dt.astimezone(pytz.UTC)`) and simplified `_to_timestamp` to call
`Timestamp.from_rfc3339(ts)` for all `str` inputs.
##########
sdks/python/apache_beam/yaml/yaml_mapping_test.py:
##########
@@ -541,6 +541,69 @@ def test_extract_windowing_info_duplicate_field(self):
fields: [timestamp]
''')
+ def test_assign_timestamps_iso8601_and_datetime(self):
+ with beam.Pipeline(options=beam.options.pipeline_options.PipelineOptions(
+ pickle_library='cloudpickle')) as p:
+ elements = p | beam.Create([
+ beam.Row(user='alice', event_time='2026-01-01T00:00:10Z'),
+ beam.Row(user='bob', event_time='2026-01-01T02:00:25+02:00'),
+ beam.Row(user='carol', event_time='2026-01-01T00:00:30'),
+ ])
+ result = (
+ elements
+ | 'Assign' >> YamlTransform(
+ '''
+ type: AssignTimestamps
+ config:
+ timestamp: event_time
+ ''')
+ | 'Extract' >> YamlTransform(
+ '''
+ type: ExtractWindowingInfo
+ config:
+ fields: [timestamp]
+ '''))
+ assert_that(
+ result | beam.Map(lambda x: (x.user, x.timestamp)),
+ equal_to([
+ ('alice', Timestamp.from_rfc3339('2026-01-01T00:00:10Z')),
+ ('bob', Timestamp.from_rfc3339('2026-01-01T00:00:25Z')),
+ ('carol', Timestamp.from_rfc3339('2026-01-01T00:00:30Z')),
+ ]))
+
+ def test_assign_timestamps_datetime_callable(self):
Review Comment:
Added a timezone-aware non-UTC `datetime.datetime` test case in
`test_assign_timestamps_datetime_callable`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]