Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mysql): map datetime values to pyarrow when absent from batch schema (#76485)

Tom Owerse0a05fe560f7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/arrow_utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/arrow_utils.py
index de5cbf983c6f..88fa587ed46b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/arrow_utils.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/arrow_utils.py
@@ -759,6 +759,16 @@ def _python_type_to_pyarrow_type(type_: type, value: Any):
return pa.decimal256(DEFAULT_NUMERIC_PRECISION, DEFAULT_NUMERIC_SCALE)
+ # `datetime` before `date`: `datetime.datetime` subclasses `datetime.date`.
+ if issubclass(type_, datetime.datetime) and isinstance(value, datetime.datetime):
+ return pa.timestamp("us", tz="UTC") if value.tzinfo is not None else pa.timestamp("us")
+
+ if issubclass(type_, datetime.date) and isinstance(value, datetime.date):
+ return pa.date32()
+
+ if issubclass(type_, datetime.time) and isinstance(value, datetime.time):
+ return pa.time64("us")
+
raise ValueError(f"Python type {type_} has no pyarrow mapping")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py
index 485d4c7a252c..c7f01feeb1c4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py
@@ -319,6 +319,25 @@ def test_table_from_py_list_with_null_filled_binary_column():
)
+ "value, expected_type",
+ [
+ (datetime.datetime(2024, 1, 2, 3, 4, 5), pa.timestamp("us")),
+ (datetime.datetime(2024, 1, 2, 3, 4, 5, tzinfo=datetime.UTC), pa.timestamp("us", tz="UTC")),
+ (datetime.date(2024, 1, 2), pa.date32()),
+ (datetime.time(3, 4, 5), pa.time64("us")),
+ ],
+)
+def test_table_from_py_list_schema_missing_temporal_column(value, expected_type):
+ # A column present in the batch but absent from the provided schema has its Arrow field
+ # inferred by `_python_type_to_pyarrow_type`, which must handle temporal values.
+ schema = pa.schema(cast(Any, [pa.field("id", pa.int64())]))
+ table = table_from_py_list([{"id": 1, "ts": value}], schema)
+
+ assert table.schema.field("ts").type == expected_type
+ assert table.column("ts").to_pylist() == [value]
+
+
@pytest.mark.parametrize(
"error_msg, large_type, values",
[