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): keep multirange values through the arrow column build (#105646)

Tom Owers9f385cacd7ba
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 314e7cb79ec9..d92043c2333e 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
@@ -1375,6 +1375,17 @@ def _to_list_array(column_data: pa.Array | pa.ChunkedArray | np.ndarray[Any, np.
return column_data.tolist()
+def _object_array(values: Sequence[object | None]) -> np.ndarray[Any, np.dtype[Any]]:
+ # Filled element by element rather than with `np.array(values, dtype=object)`: numpy reads a
+ # sequence-like value (a psycopg `Multirange`) as a nested dimension, so a column of equal
+ # length ones flattens into a 2-D array of the items and the values themselves are lost.
+ array = np.empty(len(values), dtype=object)
+ for index, value in enumerate(values):
+ array[index] = value
+
+ return array
+
+
def _serialize_dict_columns(table_data: list[dict]) -> tuple[list[dict], set[str]]:
"""JSON-serialize columns whose non-None values are all dicts, before any Arrow work.
@@ -1471,7 +1482,7 @@ def _process_batch(
columnar_table_data[col] = pa.array(values)
except:
# Some values can't be interpreted by pyarrows directly
- columnar_table_data[col] = np.array(values, dtype=object)
+ columnar_table_data[col] = _object_array(values)
for field_name in columnar_table_data.keys():
py_type: type = type(None)
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 87973543e7e5..4db7b4a78ad5 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
@@ -675,6 +675,12 @@ def test_table_from_py_list_with_postgres_range(value, expected):
assert table.schema.field("column").type == pa.string()
assert table.column("column").to_pylist() == [expected, None]
+ # A batch holding no null: numpy reads a multirange as a nested sequence, so a column of
+ # equal length ones flattens into a 2-D array unless the column is built element by element.
+ table = table_from_py_list([{"column": value}], schema)
+
+ assert table.column("column").to_pylist() == [expected]
+
def test_table_from_py_list_list_of_ranges_is_json_of_range_text():
# A Postgres array of ranges reaches the JSON fallback, which must render each element as