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.

feat(web-analytics): apply path cleaning at insert time for paths precompute (#65660)

Lucas Ricoyfb31d70fe93a
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_stats_paths_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/test/test_web_stats_paths_lazy_precompute.py
index e8ab697c7dcb..ccf482845e53 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_stats_paths_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_stats_paths_lazy_precompute.py
@@ -30,6 +30,10 @@ from posthog.models.utils import uuid7
from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import LazyComputationResult
from products.analytics_platform.backend.models.preaggregation_job import PreaggregationJob
from products.web_analytics.backend.hogql_queries.stats_table import WebStatsTableQueryRunner
+from products.web_analytics.backend.hogql_queries.web_stats_paths_lazy_precompute import (
+ _breakdown_value_expr,
+ _entry_breakdown_value_expr,
+)
@override_settings(IN_UNIT_TESTING=True)
@@ -334,17 +338,52 @@ class TestWebStatsPathsLazyPrecompute(ClickhouseTestMixin, APIBaseTest):
self._run(self._build_query(include_scroll_depth=True))
assert PreaggregationJob.objects.filter(team_id=self.team.pk).count() == 0
+ def _set_path_cleaning_rules(self) -> None:
+ self.team.path_cleaning_filters = [
+ {"alias": "/project/<id>", "regex": "\\/project\\/\\d+", "order": 0},
+ {"alias": "/insights/<id>", "regex": "\\/insights\\/[0-9a-zA-Z]+", "order": 1},
+ ]
+ self.team.save()
+
@freeze_time("2024-01-15T12:00:00Z")
def test_path_cleaning_uses_lazy_path(self):
- # Path cleaning is applied at READ time, so the precompute is
- # rule-independent — cleaning rules can change without invalidating
- # stored rows, and the lazy_computation query_hash doesn't carry the
- # regex. A path-cleaning query should create a precompute job.
+ # Path cleaning is baked into the precompute at INSERT time, so a
+ # path-cleaning query is still lazy-eligible and creates a precompute job.
self._seed_two_sessions()
with self._enable_lazy():
self._run(self._build_query(do_path_cleaning=True))
assert PreaggregationJob.objects.filter(team_id=self.team.pk).count() > 0
+ @parameterized.expand([("breakdown", _breakdown_value_expr), ("entry", _entry_breakdown_value_expr)])
+ def test_path_cleaning_baked_into_insert_expr(self, _name: str, fn):
+ # With team rules + doPathCleaning, the insert breakdown/entry exprs carry
+ # the cleaning regex chain; without it (or without rules) they store raw.
+ self._set_path_cleaning_rules()
+ cleaned = fn(WebStatsTableQueryRunner(team=self.team, query=self._build_query(do_path_cleaning=True)))
+ raw = fn(WebStatsTableQueryRunner(team=self.team, query=self._build_query(do_path_cleaning=False)))
+ assert "replaceRegexpAll" in repr(cleaned), f"{fn.__name__} must bake cleaning into the insert"
+ assert "replaceRegexpAll" not in repr(raw), f"{fn.__name__} must store raw paths when cleaning is off"
+
+ @freeze_time("2024-01-15T12:00:00Z")
+ def test_path_cleaning_gets_distinct_cache_entry(self):
+ # Cleaning is part of the insert AST now, so doPathCleaning on/off map to
+ # distinct query_hashes / jobs (a rules change spawns a fresh job).
+ self._seed_two_sessions()
+ self._set_path_cleaning_rules()
+ with self._enable_lazy():
+ self._run(self._build_query(do_path_cleaning=False))
+ raw_hashes = {str(j.query_hash) for j in PreaggregationJob.objects.filter(team_id=self.team.pk)}
+ PreaggregationJob.objects.filter(team_id=self.team.pk).delete()
+
+ self._run(self._build_query(do_path_cleaning=True))
+ cleaned_hashes = {str(j.query_hash) for j in PreaggregationJob.objects.filter(team_id=self.team.pk)}
+
+ assert raw_hashes, "expected raw run to create at least one job"
+ assert cleaned_hashes, "expected cleaned run to create at least one job"
+ assert raw_hashes.isdisjoint(cleaned_hashes), (
+ f"doPathCleaning on/off must produce distinct cache keys, got overlap: {raw_hashes & cleaned_hashes}"
+ )
+
@freeze_time("2024-01-15T12:00:00Z")
def test_uuid_session_mode_falls_through(self):
query = self._build_query()
diff --git a/products/web_analytics/backend/hogql_queries/web_stats_paths_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/web_stats_paths_lazy_precompute.py
index 703650fe14d4..b0f23a9ca637 100644
--- a/products/web_analytics/backend/hogql_queries/web_stats_paths_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/web_stats_paths_lazy_precompute.py
@@ -209,30 +209,39 @@ def _breakdown_value_expr(runner: "WebStatsTableQueryRunner") -> ast.Expr:
entered on this path". Bounce contributes for every row because
`breakdown_value == entry_breakdown_value` is always true.
- Path cleaning is applied at READ time (see `_READ_SQL_TEMPLATE`), not here.
- Storing raw paths keeps the precompute rule-independent: a team can edit
- their cleaning rules and the existing precomputed rows remain valid — the
- next read just groups them by the new cleaned values.
+ Path cleaning is applied HERE, at INSERT time, via `runner._apply_path_cleaning`
+ (a no-op when `doPathCleaning` is off or the team has no rules). Storing
+ already-cleaned paths collapses the breakdown to the cleaned cardinality and
+ lets the read group the column directly — it avoids running the team's
+ cleaning regex chain over every row on every read, which dominates read cost
+ on high-path-cardinality teams. The cache key carries the cleaning: the
+ regexes are part of this INSERT AST, so `doPathCleaning` on/off (and a rules
+ change) produce distinct `query_hash`es / jobs that coexist — a rules edit
+ just spawns a fresh job rather than invalidating in place.
The cache key differentiates PAGE vs INITIAL_PAGE automatically: the AST
for each branch differs, so the lazy_computation `query_hash` differs and
the two precomputes coexist as distinct jobs."""
if runner.query.breakdownBy == WebStatsBreakdown.INITIAL_PAGE:
return _entry_breakdown_value_expr(runner)
- path = ast.Field(chain=["events", "properties", "$pathname"])
+ path: ast.Expr = ast.Field(chain=["events", "properties", "$pathname"])
if runner.query.includeHost:
- return _prepend_host_nullif_empty(ast.Field(chain=["events", "properties", "$host"]), path)
- return path
+ path = _prepend_host_nullif_empty(ast.Field(chain=["events", "properties", "$host"]), path)
+ # Clean after the optional host prefix so the stored value matches what the
+ # read previously produced via `_apply_path_cleaning(breakdown_value)`.
+ return runner._apply_path_cleaning(path)
def _entry_breakdown_value_expr(runner: "WebStatsTableQueryRunner") -> ast.Expr:
"""Entry pathname (optionally `entry_hostname`-prefixed) — must match the
- same shape as `_breakdown_value_expr` so the equality check inside the
- INSERT properly identifies sessions that entered on each path."""
- path = ast.Field(chain=["session", "$entry_pathname"])
+ same shape AND cleaning as `_breakdown_value_expr` so the equality check
+ inside the INSERT properly identifies sessions that entered on each path.
+ Cleaning is applied identically (after the optional host prefix) so a cleaned
+ pathname still matches its cleaned entry path."""
+ path: ast.Expr = ast.Field(chain=["session", "$entry_pathname"])
if runner.query.includeHost:
- return _prepend_host_nullif_empty(ast.Field(chain=["session", "$entry_hostname"]), path)
- return path
+ path = _prepend_host_nullif_empty(ast.Field(chain=["session", "$entry_hostname"]), path)
+ return runner._apply_path_cleaning(path)
# HogQL template for the precompute INSERT. The lazy_computation framework
@@ -410,12 +419,12 @@ ENSURE_BUDGET_MS = 120 * 1000
# directly against the UTC bounds we pass in via `{cur_start}` etc. without
# HogQL coercing them to the team's local timezone.
#
-# `breakdown_expr` is the (raw or cleaned) breakdown column. Path cleaning is
-# applied here in the read rather than baked into the precompute, so rule
-# edits don't invalidate stored rows and the lazy_computation query_hash
-# doesn't carry the regex string. Cleaning is a chain of nested
-# `replaceRegexpAll` calls (see `apply_path_cleaning`), and ClickHouse
-# aggregate `*Merge` functions remain associative across the GROUP BY change.
+# `breakdown_expr` is the stored `breakdown_value` column directly — no read-time
+# cleaning. Path cleaning is baked into the precompute at INSERT time (see
+# `_breakdown_value_expr`), so the stored column is already cleaned (or raw, for a
+# `doPathCleaning=False` job — the two are distinct `query_hash`es). This avoids
+# running the team's nested `replaceRegexpAll` chain over every row on every read,
+# which dominated read cost on high-path-cardinality teams.
# The SELECT alias is deliberately `breakdown` (not `breakdown_value`) to avoid
# shadowing the underlying column name. With the same name, HogQL resolves
# `breakdown_value` in GROUP BY to the SELECT alias (printing without the
@@ -531,7 +540,7 @@ def execute_read_query(
"cur_end": ast.Constant(value=current_end_utc),
"prev_start": ast.Constant(value=prev_start),
"prev_end": ast.Constant(value=prev_end),
- "breakdown_expr": runner._apply_path_cleaning(ast.Field(chain=["breakdown_value"])),
+ "breakdown_expr": ast.Field(chain=["breakdown_value"]),
"fill_fraction_expr": _fill_fraction_expr(sort_column),
}