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): write empty parquet without a zero-row row group (#75413)

Jovan Sakovic330a743023cf
diff --git a/posthog/temporal/data_modeling/activities/materialize_view.py b/posthog/temporal/data_modeling/activities/materialize_view.py
index 7c0f3ed67dcf..5b8a463d8193 100644
--- a/posthog/temporal/data_modeling/activities/materialize_view.py
+++ b/posthog/temporal/data_modeling/activities/materialize_view.py
@@ -259,7 +259,10 @@ async def _write_empty_parquet_for_zero_rows(table_uri: str, schema: pa.Schema,
is still queryable.
"""
buf = pa.BufferOutputStream()
- pq.write_table(schema.empty_table(), buf)
+ # write_table() on an empty table emits a 0-row row group, which ClickHouse rejects,
+ # so write only the schema with no row groups.
+ with pq.ParquetWriter(buf, schema):
+ pass
parquet_bytes = buf.getvalue().to_pybytes()
file_uri = f"{table_uri}/empty_{uuid.uuid4().hex}.parquet"
s3 = get_s3_client()
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 57bbf2659de6..56a43d84fded 100644
--- a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
@@ -1,6 +1,7 @@
import asyncio
import contextlib
from collections.abc import AsyncIterator, Collection, Iterable
+from io import BytesIO
from typing import Any, cast
import pytest
@@ -12,6 +13,7 @@ from django.test import override_settings
import pyarrow as pa
import deltalake
import pytest_asyncio
+import pyarrow.parquet as pq
from posthog.hogql.resolver import ResolverFactory
@@ -32,6 +34,7 @@ from posthog.temporal.data_modeling.activities.materialize_view import (
LOGGER,
InvalidNodeTypeException,
_get_aws_storage_options,
+ get_s3_client,
hogql_table,
)
@@ -1057,6 +1060,12 @@ class TestMaterializeViewActivity:
pyarrow_table = delta_table.to_pyarrow_table()
assert pyarrow_table.num_rows == 0
assert set(pyarrow_table.column_names) == {"id", "name"}
+ # ClickHouse rejects a parquet containing a 0-row row group, so the file must be metadata-only
+ s3 = get_s3_client()
+ with s3.open(result.file_uris[0], "rb") as f:
+ empty_parquet = pq.ParquetFile(BytesIO(f.read()))
+ assert empty_parquet.metadata.num_row_groups == 0
+ assert empty_parquet.schema_arrow.names == ["id", "name"]
async def test_write_failure_surfaces(self, activity_environment, ateam, anode, ajob, bucket_name, adag):
# regression: a failure in a per-batch write_deltalake call must surface from the