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(warehouse): avoid materializing full column for hogql json detection (#67441)

estefaniae4a13455bdca
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/hogql_schema.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/hogql_schema.py
index a37c0f8cd822..8f953bc6ca21 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/hogql_schema.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/hogql_schema.py
@@ -1,4 +1,5 @@
import pyarrow as pa
+import pyarrow.compute as pc
from posthog.hogql.database.models import (
BooleanDatabaseField,
@@ -50,15 +51,22 @@ class HogQLSchema:
hogql_type = self._map_arrow_type(field.type)
if hogql_type is StringDatabaseField:
- # Checking for JSON string columns with the first non-null value in the column
- for value_str in column.to_pylist():
- if isinstance(value_str, str):
- if value_str.startswith("{") or value_str.startswith("["):
- hogql_type = StringJSONDatabaseField
- break
+ value = self._first_non_null(column)
+ if isinstance(value, str) and (value.startswith("{") or value.startswith("[")):
+ hogql_type = StringJSONDatabaseField
self.schema[field.name] = hogql_type.__name__
+ @staticmethod
+ def _first_non_null(column: pa.ChunkedArray):
+ for chunk in column.chunks:
+ if chunk.null_count == len(chunk):
+ continue
+ i = pc.index(chunk.is_valid(), True).as_py()
+ if i != -1:
+ return chunk[i].as_py()
+ return None
+
def _map_arrow_type(self, arrow_type: pa.DataType) -> type[DatabaseField]:
if pa.types.is_time(arrow_type):
return DateTimeDatabaseField
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_hogql_schema.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_hogql_schema.py
index 215e2a05f3f7..d88cd81becd4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_hogql_schema.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_hogql_schema.py
@@ -13,6 +13,7 @@ class TestHogQLSchemaJsonDetection:
("plain_string_first", ["plain text", '{"key": "value"}'], "StringDatabaseField"),
("null_then_json", [None, '{"key": "value"}'], "StringJSONDatabaseField"),
("all_nulls", [None, None], "StringDatabaseField"),
+ ("empty_column", [], "StringDatabaseField"),
]
)
def test_json_detection_from_first_non_null_value(self, _name, values, expected_type):
@@ -21,6 +22,22 @@ class TestHogQLSchemaJsonDetection:
schema.add_pyarrow_table(table)
assert schema.schema["col"] == expected_type
+ def test_json_detection_across_chunk_boundary(self):
+ # First chunk all-null, JSON only in a later chunk: scan must cross chunks, not stop at [0].
+ column = pa.chunked_array([[None, None], ['{"a": 1}']], type=pa.string())
+ schema = HogQLSchema()
+ schema.add_pyarrow_table(pa.table({"col": column}))
+ assert schema.schema["col"] == "StringJSONDatabaseField"
+
+ def test_string_upgrades_to_json_on_later_table(self):
+ # All-null first table classifies as plain string; a later table with JSON must still upgrade.
+ schema = HogQLSchema()
+ schema.add_pyarrow_table(pa.table({"col": pa.array([None, None], type=pa.string())}))
+ assert schema.schema["col"] == "StringDatabaseField"
+
+ schema.add_pyarrow_table(pa.table({"col": pa.array(['{"a": 1}'], type=pa.string())}))
+ assert schema.schema["col"] == "StringJSONDatabaseField"
+
class TestAddPyarrowSchema:
@parameterized.expand(