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-warehouse): count incremental rows on readonly clickhouse users (#114351)

Daniel RC5593f2863743
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
index 940c5adf552d..bbb12f56dcbe 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
@@ -1169,12 +1169,16 @@ def _get_incremental_row_count(
quoted_field = _quote_identifier(incremental_field)
last_value_expr = _last_value_expr(incremental_field_type)
query = f"SELECT count() FROM {_qualified_table(database, table_name)} WHERE {quoted_field} > {last_value_expr}"
+ parameters = {"last_value": _last_value_param(last_value, incremental_field_type)}
try:
- result = client.query(
- query,
- parameters={"last_value": _last_value_param(last_value, incremental_field_type)},
- settings={"max_execution_time": 30},
- )
+ try:
+ result = client.query(query, parameters=parameters, settings={"max_execution_time": 30})
+ except ProgrammingError:
+ # A readonly user profile refuses every setting (see `_apply_session_settings`), and the
+ # driver raises before it sends the query. The total-table count the caller falls back to
+ # is also the size the billing limit check gives this sync, so one large table can then
+ # stop every sync of the team. The server's own limits bound the count instead.
+ result = client.query(query, parameters=parameters)
except ClickHouseError as e:
logger.debug(f"_get_incremental_row_count: fell back, count query failed: {e}")
return None
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index 08cc7c7c57d9..bb62651db9ad 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -751,14 +751,42 @@ class TestDuplicateProbeTargetsMergeKey:
assert mock_clickhouse_source.call_args.kwargs["stored_primary_keys"] == ["id"]
+def _failing_server(*_args: object, **_kwargs: object) -> MagicMock:
+ raise ClickHouseError("timeout")
+
+
+def _readonly_server(*_args: object, settings: dict[str, object] | None = None, **_kwargs: object) -> MagicMock:
+ if settings:
+ raise ProgrammingError("Setting max_execution_time is unknown or readonly")
+ return MagicMock(result_rows=[(42,)])
+
+
+def _readonly_server_that_times_out(
+ *_args: object, settings: dict[str, object] | None = None, **_kwargs: object
+) -> MagicMock:
+ if settings:
+ raise ProgrammingError("Setting max_execution_time is unknown or readonly")
+ raise OperationalError("Read timed out")
+
+
class TestGetIncrementalRowCount:
def _logger(self):
return MagicMock()
- def test_returns_none_on_error(self):
+ @pytest.mark.parametrize(
+ "server, expected",
+ [
+ pytest.param(_failing_server, None, id="query_fails"),
+ pytest.param(_readonly_server, 42, id="server_refuses_settings"),
+ pytest.param(_readonly_server_that_times_out, None, id="server_refuses_settings_and_the_count_fails"),
+ ],
+ )
+ def test_counts_the_rows_after_the_cursor_or_gives_none(
+ self, server: Callable[..., MagicMock], expected: int | None
+ ) -> None:
client = MagicMock()
- client.query.side_effect = ClickHouseError("timeout")
- assert _get_incremental_row_count(client, "db", "t", "id", 0, self._logger()) is None
+ client.query.side_effect = server
+ assert _get_incremental_row_count(client, "db", "t", "id", 0, self._logger()) == expected
class TestGetPartitionSettings: