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(data-imports): normalize non-microsecond timestamps before writing to Delta Lake (#75442)

Tom Owers34b3a50ca828
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 03dd583b01f9..0ed2195b5901 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
@@ -219,9 +219,12 @@ def evolve_pyarrow_schema(incoming_table: pa.Table, delta_schema: deltalake.Sche
)
incoming_column = incoming_table.column(column_name)
- # Normalize timestamps to microseconds and no timezone.
+ # Normalize timestamps to microseconds and no timezone. Delta only supports
+ # microsecond precision, so any other unit (not just "ns") must be caught here —
+ # e.g. the Snowflake connector's zero-row `_create_empty_table` path ignores
+ # `force_microsecond_precision` and yields second-precision columns.
if pa.types.is_timestamp(incoming_field.type) and (
- incoming_field.type.unit == "ns" or incoming_field.type.tz is not None
+ incoming_field.type.unit != "us" or incoming_field.type.tz is not None
):
microsecond_timestamps = pc.cast(incoming_column, pa.timestamp("us"), safe=False).combine_chunks()
incoming_table = incoming_table.set_column(
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 4f258ec8f047..01d0c234ee92 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
@@ -3,7 +3,7 @@ import decimal
import hashlib
import datetime
from ipaddress import IPv4Address, IPv6Address
-from typing import Any, cast
+from typing import Any, Literal, cast
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
@@ -929,9 +929,13 @@ class TestEvolveSchemaFirstPass:
assert values[1] == 3661.0
assert values[2] is None
- def test_nanosecond_timestamp_normalized_to_microseconds(self):
- ns_ts = pa.array([1_000_000_000, 2_000_000_000], type=pa.timestamp("ns"))
- arrow_table = pa.table({"ts": ns_ts})
+ @pytest.mark.parametrize("unit", ["ns", "ms", "s"])
+ def test_non_microsecond_timestamp_normalized_to_microseconds(self, unit: Literal["ns", "ms", "s"]):
+ # Delta only accepts microsecond-precision timestamps. "s"/"ms" reach here e.g. when
+ # a Snowflake batch returns zero rows: the connector's empty-table path ignores
+ # `force_microsecond_precision` and falls back to a second-precision schema.
+ ts = pa.array([1_000_000_000, 2_000_000_000], type=pa.timestamp(unit))
+ arrow_table = pa.table({"ts": ts})
result = evolve_pyarrow_schema(arrow_table, None)
assert result.schema.field("ts").type == pa.timestamp("us")