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(postgres): handle binary offset overflow when materializing batches (#65037)

Tom Owers590c98d9945e
diff --git a/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py b/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py
index e8df0eaed9dc..8a4311f1c68b 100644
--- a/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py
+++ b/posthog/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py
@@ -17,6 +17,7 @@ from posthog.temporal.data_imports.pipelines.pipeline.consts import PARTITION_KE
from posthog.temporal.data_imports.pipelines.pipeline.utils import (
SchemaColumnTypeChangedException,
_get_max_decimal_type,
+ _to_list_array,
append_partition_key_to_table,
evolve_pyarrow_schema,
normalize_table_column_names,
@@ -230,6 +231,40 @@ def test_table_from_py_list_with_null_filled_binary_column():
)
+ "error_msg, large_type, values",
+ [
+ (
+ "offset overflow while concatenating arrays, consider casting input from `binary` to `large_binary` first.",
+ pa.large_binary(),
+ [b"a", b"b", b"c"],
+ ),
+ (
+ "offset overflow while concatenating arrays, consider casting input from `string` to `large_string` first.",
+ pa.large_string(),
+ ["a", "b"],
+ ),
+ ],
+)
+def test_to_list_array_offset_overflow_falls_back_to_large_type(error_msg, large_type, values):
+ # A column whose chunk exceeds 2GB makes `combine_chunks()` overflow the int32 offset;
+ # the helper must recover by casting to the large-offset variant instead of raising.
+ overflowing = MagicMock(spec=pa.ChunkedArray)
+ overflowing.combine_chunks.side_effect = pa.ArrowInvalid(error_msg)
+ overflowing.cast.return_value = pa.chunked_array([pa.array(values, type=large_type)])
+
+ assert _to_list_array(overflowing) == values
+ overflowing.cast.assert_called_once_with(large_type)
+
+
+def test_to_list_array_reraises_unrelated_arrow_invalid():
+ failing = MagicMock(spec=pa.ChunkedArray)
+ failing.combine_chunks.side_effect = pa.ArrowInvalid("some other arrow problem")
+
+ with pytest.raises(pa.ArrowInvalid, match="some other arrow problem"):
+ _to_list_array(failing)
+
+
def test_table_from_py_list_with_mixed_decimal_float_sizes():
table = table_from_py_list([{"column": decimal.Decimal(1.0)}, {"column": 1000.01}])
diff --git a/posthog/temporal/data_imports/pipelines/pipeline/utils.py b/posthog/temporal/data_imports/pipelines/pipeline/utils.py
index bd6810059dd1..546cbfcc18da 100644
--- a/posthog/temporal/data_imports/pipelines/pipeline/utils.py
+++ b/posthog/temporal/data_imports/pipelines/pipeline/utils.py
@@ -669,8 +669,14 @@ def _to_list_array(column_data: pa.Array | pa.ChunkedArray | np.ndarray[Any, np.
try:
return column_data.combine_chunks().tolist()
except pa.ArrowInvalid as e:
- if "consider casting input from `string` to `large_string`" in "".join(e.args):
+ joined_args = "".join(e.args)
+ if "consider casting input from `string` to `large_string`" in joined_args:
return column_data.cast(pa.large_string()).combine_chunks().tolist()
+ # Same int32 offset overflow as above, for `binary`-typed columns (e.g. a Postgres
+ # `bytea` whose chunk exceeds 2GB). large_binary uses 64-bit offsets so combining
+ # the chunks no longer overflows.
+ if "consider casting input from `binary` to `large_binary`" in joined_args:
+ return column_data.cast(pa.large_binary()).combine_chunks().tolist()
raise