: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): parse a delta commit log as newline-delimited json (#108551)
Tom Owers72a697aa35a8
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.pyindex 667d682c4358..cf2e7939859a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py@@ -246,9 +246,20 @@ def _read_commit_actions(table_uri: str, storage_options: dict[str, str], versiofilesystem = pafs.PyFileSystem(DeltaStorageHandler(table_uri, storage_options))with filesystem.open_input_stream(f"_delta_log/{version:020d}.json") as stream:raw = stream.read()+ return _parse_commit_actions(raw)+++def _parse_commit_actions(raw: bytes) -> list[dict[str, Any]]:+ """The actions of one `_delta_log` commit file, which holds one JSON action per line feed.++ Split on the line feed alone, because `str.splitlines()` also breaks on U+0085, U+2028 and+ U+2029. JSON allows those characters raw inside a string, and an action's `stats` embed the+ table's own min and max strings, so one of them cuts an action in half and leaves the fragment+ unparseable.+ """# Floats as Decimal: a decimal column's stats arrive as JSON numbers, and a float would lose# the digits the stored representation keeps.- return [json.loads(line, parse_float=Decimal) for line in raw.decode().splitlines() if line.strip()]+ return [json.loads(line, parse_float=Decimal) for line in raw.decode().split("\n") if line.strip()]# Actions a commit may carry without touching the live file set or the schema. Anything elsediff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.pyindex 351d9dad61bc..76cf4550aa45 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py@@ -33,6 +33,7 @@ from products.warehouse_sources.backend.temporal.data_imports.workflow_activitieComputeTableStatisticsInputs,ComputeTableStatisticsWorkflow,_aggregate_add_action_stats,+ _parse_commit_actions,_parse_log_value,compute_table_statistics_activity,compute_table_statistics_sync,@@ -137,6 +138,32 @@ class TestAggregateAddActionStats:assert stats["v"].max_value == expected_max+class TestParseCommitActions:+ @parameterized.expand([("next_line", "\u0085"), ("line_separator", "\u2028"), ("paragraph_separator", "\u2029")])+ def test_string_stat_holding_a_unicode_line_boundary_stays_one_action(self, _name, char) -> None:+ # Regression: splitlines() broke the commit on these characters, which a string column's+ # min/max carries through into `stats`, so the fragment raised JSONDecodeError. delta-rs+ # writes them raw rather than escaped, hence ensure_ascii=False here.+ stats = json.dumps(+ {"numRecords": 3, "minValues": {"title": f"a{char}b"}, "maxValues": {"title": "z"}}, ensure_ascii=False+ )+ raw = (json.dumps({"add": {"path": "part-0.parquet", "stats": stats}}, ensure_ascii=False) + "\n").encode()+ assert char.encode() in raw++ actions = _parse_commit_actions(raw)++ assert len(actions) == 1+ assert json.loads(actions[0]["add"]["stats"])["minValues"]["title"] == f"a{char}b"++ def test_reads_every_action_and_keeps_stats_floats_exact(self) -> None:+ raw = b'{"protocol": {"minReaderVersion": 1}}\n{"add": {"path": "part-0.parquet", "size": 0.1}}\n'++ actions = _parse_commit_actions(raw)++ assert [next(iter(action)) for action in actions] == ["protocol", "add"]+ assert actions[1]["add"]["size"] == Decimal("0.1")++class TestParseLogValue:def test_decimal_using_its_full_precision_is_not_mistaken_for_unparseable(self) -> None:# Regression: quantize() used the default decimal context (28 significant digits), which is