: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): stringify mixed list/scalar columns instead of crashing (#68843)
Tom Owers7ed3bdb01243
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.pyindex 4513a276f04e..7061dfdaa24d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py@@ -131,6 +131,23 @@ def test_table_from_py_list_inconsistent_types_with_str_and_dict():)+ "rows,expected",+ [+ # bool in one row, list in another: wrapping to a list yields [true]/["x"], whose element+ # types differ, so an intermediate pyarrow list array would raise "tried to convert to boolean".+ ([{"column": True}, {"column": ["x"]}], ["[true]", '["x"]']),+ # dict in one row, list in another: would raise "cannot mix struct and non-struct values".+ ([{"column": {"a": 1}}, {"column": ["x"]}], ['[{"a":1}]', '["x"]']),+ ],+)+def test_table_from_py_list_list_mixed_with_incompatible_element_types(rows, expected):+ table = table_from_py_list(rows)++ assert table.equals(pa.table({"column": expected}))+ assert table.schema.equals(pa.schema([("column", pa.string())]))++def test_table_from_py_list_with_lists():table = table_from_py_list([{"column": ["hello"]}, {"column": ["hi"]}])diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.pyindex cec90ee31ae2..94a59bfa3c0f 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py@@ -1121,14 +1121,20 @@ def _process_batch(table_data: list[dict], schema: Optional[pa.Schema] = None) -field_index, arrow_schema.field(field_index).with_type(overflow_arr.type))- # If one type is a list, then make everything into a list+ # If one type is a list, wrap scalars into single-element lists and JSON-stringify the column.+ # Element types can differ across rows (e.g. a WordPress field returned as a bool in one row+ # and an object in another), which no single pyarrow list type can hold, so serialize directly+ # rather than via an intermediate list array that would raise ArrowInvalid on the mismatch.if len(unique_types_in_column) > 1 and list in unique_types_in_column:- list_array = pa.array(- [s if isinstance(s, list) else [s] for s in _to_list_array(columnar_table_data[field_name])]+ json_array = pa.array(+ [+ None if s is None else _json_dumps(s if isinstance(s, list) else [s])+ for s in _to_list_array(columnar_table_data[field_name])+ ])- columnar_table_data[field_name] = list_array- py_type = list- unique_types_in_column = {list}+ columnar_table_data[field_name] = json_array+ py_type = str+ unique_types_in_column = {str}if arrow_schema:arrow_schema = arrow_schema.set(field_index, arrow_schema.field(field_index).with_type(pa.string()))