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(data-modeling): convert array elements for Arrow (#117743)

Jovan Sakovic2740bf4d97bd
diff --git a/posthog/temporal/data_modeling/activities/materialize_view.py b/posthog/temporal/data_modeling/activities/materialize_view.py
index 69b14dad29b0..8dd4f91809c6 100644
--- a/posthog/temporal/data_modeling/activities/materialize_view.py
+++ b/posthog/temporal/data_modeling/activities/materialize_view.py
@@ -663,17 +663,29 @@ async def hogql_table(
"IPv6": ("toString", ()),
}
+ def _conversion_target(ch_type: str) -> str:
+ # Arrays are converted per element, so only the element's own type name can select a conversion.
+ # Matching the whole string would also hit names nested deeper, e.g. the `uuid` field of
+ # `Array(Tuple(uuid String))`, and wrongly stringify structures Arrow already supports.
+ if not ch_type.lower().startswith("array("):
+ return ch_type
+ element = ch_type[len("array(") : -1].strip()
+ while element.lower().startswith(("nullable(", "lowcardinality(")):
+ element = element[element.index("(") + 1 : -1].strip()
+ return element.split("(", 1)[0]
+
def _needs_conversion(ch_type: str) -> bool:
- # Skip array types from conversion — they are already properly typed by ClickHouse
- # and attempting to convert them causes errors like:
- # "Illegal type Array(DateTime) of argument of function toTimezone"
- is_array_type = ch_type.lower().startswith("array(")
- if is_array_type:
- return False
- return any(uat.lower() in ch_type.lower() for uat in arrow_type_conversion)
+ target = _conversion_target(ch_type).lower()
+ return any(uat.lower() in target for uat in arrow_type_conversion)
get_call_tuple = lambda ch_type: next(
- iter([call_tuple for uat, call_tuple in arrow_type_conversion.items() if uat.lower() in ch_type.lower()])
+ iter(
+ [
+ call_tuple
+ for uat, call_tuple in arrow_type_conversion.items()
+ if uat.lower() in _conversion_target(ch_type).lower()
+ ]
+ )
)
try:
@@ -706,15 +718,30 @@ async def hogql_table(
select_fields: list[ast.Expr] = []
for column_name, ch_type, call_tuple in query_typings:
if call_tuple is not None:
- await logger.adebug(
- f"Converting {column_name} of type {ch_type} to be wrapped with {call_tuple[0]}(..)"
- )
- select_fields.append(
- ast.Alias(
- expr=ast.Call(name=call_tuple[0], args=[ast.Field(chain=[column_name]), *call_tuple[1]]),
- alias=column_name,
+ if ch_type.lower().startswith("array("):
+ await logger.adebug(
+ f"Converting {column_name} of type {ch_type} using arrayMap with {call_tuple[0]}(..)"
+ )
+ element_conversion = ast.Lambda(
+ args=["x"],
+ expr=ast.Call(name=call_tuple[0], args=[ast.Field(chain=["x"]), *call_tuple[1]]),
+ )
+ select_fields.append(
+ ast.Alias(
+ expr=ast.Call(name="arrayMap", args=[element_conversion, ast.Field(chain=[column_name])]),
+ alias=column_name,
+ )
+ )
+ else:
+ await logger.adebug(
+ f"Converting {column_name} of type {ch_type} to be wrapped with {call_tuple[0]}(..)"
+ )
+ select_fields.append(
+ ast.Alias(
+ expr=ast.Call(name=call_tuple[0], args=[ast.Field(chain=[column_name]), *call_tuple[1]]),
+ alias=column_name,
+ )
)
- )
else:
select_fields.append(ast.Field(chain=[column_name]))
query_node = ast.SelectQuery(select=select_fields, select_from=ast.JoinExpr(table=query_node))
diff --git a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
index f38b994d4042..214e722f9422 100644
--- a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
@@ -1908,6 +1908,40 @@ class _EmptyArrowClient:
class TestHogqlTableModifiers:
+ @pytest.mark.parametrize(
+ "clickhouse_type,expected_conversion",
+ [
+ ("Array(UUID)", "arrayMap(x -> toString(x), person_ids) AS person_ids"),
+ ("Array(IPv4)", "arrayMap(x -> toString(x), person_ids) AS person_ids"),
+ ("Array(IPv6)", "arrayMap(x -> toString(x), person_ids) AS person_ids"),
+ ("Array(DateTime)", "arrayMap(x -> toTimeZone(x, %(hogql_val_0)s), person_ids) AS person_ids"),
+ ("Array(Nullable(UUID))", "arrayMap(x -> toString(x), person_ids) AS person_ids"),
+ ("Array(String)", None),
+ ("Array(Tuple(uuid String))", None),
+ ("Array(Array(UUID))", None),
+ ],
+ )
+ async def test_applies_arrow_conversions_to_array_elements(
+ self, ateam: Team, clickhouse_type: str, expected_conversion: str | None
+ ) -> None:
+ client = _EmptyArrowClient(pa.schema([pa.field("person_ids", pa.list_(pa.string()))]))
+ client.describe_body = f"person_ids\t{clickhouse_type}\n".encode()
+
+ @contextlib.asynccontextmanager
+ async def fake_get_client(**kwargs: Any) -> AsyncIterator[_EmptyArrowClient]:
+ yield client
+
+ with unittest.mock.patch(
+ "posthog.temporal.data_modeling.activities.materialize_view.get_clickhouse_client", fake_get_client
+ ):
+ _ = [batch async for batch in hogql_table("SELECT [] AS person_ids", ateam, LOGGER.bind())]
+
+ assert client.arrow_query is not None
+ if expected_conversion is None:
+ assert "arrayMap(" not in client.arrow_query
+ else:
+ assert expected_conversion in client.arrow_query
+
@pytest.mark.parametrize(
"query,team_modifiers,expected_sql,expected_sql_new_events_schema",
[