: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(hogql): bound events uuid point lookups via uuidv7 timestamps (#72761)
Andy Zhaob0f5b87c8529
diff --git a/posthog/hogql/printer/utils.py b/posthog/hogql/printer/utils.pyindex 4e3d3c62b7eb..fa61a57c4de7 100644--- a/posthog/hogql/printer/utils.py+++ b/posthog/hogql/printer/utils.py@@ -43,6 +43,7 @@ from posthog.hogql.transforms.type_aware_simplification import (simplify_argmax_over_non_nullable,simplify_redundant_type_operations,)+from posthog.hogql.transforms.uuid_timestamp_bounds import apply_uuid_v7_timestamp_boundsfrom posthog.hogql.visitor import clone_exprfrom posthog.hogql.workload import WorkloadCollector@@ -201,6 +202,12 @@ def prepare_ast_for_printing(with context.timings.measure("type_aware_cast_simplification"):node = simplify_redundant_type_operations(node, context, dialect)+ # ClickHouse only: must run before predicate pushdown so the bound lands in its+ # pre-filtering subquery, and the HogQL dialect must echo the user's query unchanged.+ if dialect == "clickhouse":+ with context.timings.measure("uuid_v7_timestamp_bounds"):+ node = apply_uuid_v7_timestamp_bounds(node)+# Detect workload from resolved table types and store on contextwith context.timings.measure("workload_detection"):collector = WorkloadCollector(default_workload=Workload.DEFAULT)diff --git a/posthog/hogql/transforms/test/test_uuid_timestamp_bounds.py b/posthog/hogql/transforms/test/test_uuid_timestamp_bounds.pynew file mode 100644index 000000000000..5521da9f2826--- /dev/null+++ b/posthog/hogql/transforms/test/test_uuid_timestamp_bounds.py@@ -0,0 +1,123 @@+from posthog.test.base import BaseTest++from parameterized import parameterized++from posthog.schema import HogQLQueryModifiers++from posthog.hogql.constants import HogQLDialect+from posthog.hogql.context import HogQLContext+from posthog.hogql.parser import parse_select+from posthog.hogql.printer.utils import prepare_and_print_ast++# Embeds 2026-07-20 12:00:00.000 UTC+UUID_LATE = "019f7f65-b600-7123-8abc-0123456789ab"+LATE_LOWER = "toDateTime64('2026-07-17 12:00:00.000000', 6, 'UTC')"+LATE_UPPER = "toDateTime64('2026-07-23 12:00:00.000000', 6, 'UTC')"++# Embeds 2026-06-25 18:17:07.840 UTC+UUID_EARLY = "019f0000-0000-7123-8abc-0123456789ab"+EARLY_LOWER = "toDateTime64('2026-06-22 18:17:07.840000', 6, 'UTC')"+EARLY_UPPER = "toDateTime64('2026-06-28 18:17:07.840000', 6, 'UTC')"++UUID_V4 = "a1b2c3d4-e5f6-4711-8899-aabbccddeeff"+++class TestUuidTimestampBounds(BaseTest):+ def _print(self, select: str, dialect: HogQLDialect = "clickhouse") -> str:+ query, _ = prepare_and_print_ast(+ parse_select(select),+ HogQLContext(team_id=self.team.pk, enable_select_queries=True, modifiers=HogQLQueryModifiers()),+ dialect,+ )+ return query++ @parameterized.expand(+ [+ ("eq", f"SELECT event FROM events WHERE uuid = '{UUID_LATE}'", LATE_LOWER, LATE_UPPER, "events"),+ ("eq_flipped", f"SELECT event FROM events WHERE '{UUID_LATE}' = uuid", LATE_LOWER, LATE_UPPER, "events"),+ (+ "eq_touuid",+ f"SELECT event FROM events WHERE uuid = toUUID('{UUID_LATE}')",+ LATE_LOWER,+ LATE_UPPER,+ "events",+ ),+ (+ "in_list_spans_min_to_max",+ f"SELECT event FROM events WHERE uuid IN ('{UUID_LATE}', '{UUID_EARLY}')",+ EARLY_LOWER,+ LATE_UPPER,+ "events",+ ),+ (+ "and_with_other_filter",+ f"SELECT event FROM events WHERE uuid = '{UUID_LATE}' AND event = 'x'",+ LATE_LOWER,+ LATE_UPPER,+ "events",+ ),+ (+ "or_with_both_branches_bounded",+ f"SELECT event FROM events WHERE uuid = '{UUID_LATE}' OR uuid = '{UUID_EARLY}'",+ EARLY_LOWER,+ LATE_UPPER,+ "events",+ ),+ (+ "aliased_scan",+ f"SELECT event FROM events e WHERE e.uuid = '{UUID_LATE}'",+ LATE_LOWER,+ LATE_UPPER,+ "e",+ ),+ ]+ )+ def test_bounds_injected(self, _name: str, select: str, lower: str, upper: str, table: str) -> None:+ printed = self._print(select)+ assert f"greaterOrEquals({table}.timestamp, {lower})" in printed+ assert f"lessOrEquals({table}.timestamp, {upper})" in printed++ @parameterized.expand(+ [+ ("uuid_v4", f"SELECT event FROM events WHERE uuid = '{UUID_V4}'"),+ ("nil_uuid", "SELECT event FROM events WHERE uuid = '00000000-0000-0000-0000-000000000000'"),+ (+ "v7_beyond_datetime_range",+ "SELECT event FROM events WHERE uuid = 'ffffffff-ffff-7fff-8fff-ffffffffffff'",+ ),+ (+ "v7_near_datetime_max",+ "SELECT event FROM events WHERE uuid = 'e677c7d3-2400-7abc-8abc-0123456789ab'",+ ),+ ("non_constant", "SELECT event FROM events WHERE uuid = toUUID(event)"),+ (+ "or_with_unbounded_branch",+ f"SELECT event FROM events WHERE uuid = '{UUID_LATE}' OR event = 'x'",+ ),+ ("negated", f"SELECT event FROM events WHERE NOT (uuid = '{UUID_LATE}')"),+ ("not_in", f"SELECT event FROM events WHERE uuid NOT IN ('{UUID_LATE}')"),+ (+ "in_subquery",+ f"SELECT event FROM events WHERE uuid IN (SELECT uuid FROM events WHERE event = 'x')",+ ),+ (+ "in_list_with_non_v7_element",+ f"SELECT event FROM events WHERE uuid IN ('{UUID_LATE}', '{UUID_V4}')",+ ),+ ]+ )+ def test_no_bounds_injected(self, _name: str, select: str) -> None:+ printed = self._print(select)+ assert "greaterOrEquals" not in printed+ assert "lessOrEquals" not in printed++ def test_bound_applies_to_the_select_owning_the_lookup(self) -> None:+ printed = self._print(+ f"SELECT event FROM (SELECT event, timestamp FROM events WHERE uuid = '{UUID_LATE}') WHERE timestamp > '2020-01-01'"+ )+ assert printed.count(f"greaterOrEquals(events.timestamp, {LATE_LOWER})") == 1++ def test_hogql_dialect_is_not_rewritten(self) -> None:+ printed = self._print(f"SELECT event FROM events WHERE uuid = '{UUID_LATE}'", dialect="hogql")+ assert "greaterOrEquals" not in printed+ assert "timestamp" not in printeddiff --git a/posthog/hogql/transforms/uuid_timestamp_bounds.py b/posthog/hogql/transforms/uuid_timestamp_bounds.pynew file mode 100644index 000000000000..ed91b75f48d5--- /dev/null+++ b/posthog/hogql/transforms/uuid_timestamp_bounds.py@@ -0,0 +1,264 @@+"""Add timestamp bounds to events queries that look up rows by UUIDv7 constants.++Event uuids are UUIDv7, so the uuid constant in `uuid = '<constant>'` carries the row's+approximate timestamp in its high 48 bits. When every row a WHERE clause can match is pinned+to known UUIDv7 constants, we AND a buffered constant timestamp range onto that WHERE, so+ClickHouse prunes partitions instead of scanning the team's full event history.++The buffer covers clock skew between the uuid's embedded time and the stored timestamp.+Rows further out than the buffer (for example a backfill sent with a fresh uuid but a+historical timestamp) will not be found by the bounded lookup. Constants that are not valid+UUIDv7s contribute nothing, so those lookups keep their unbounded scan.+"""++from dataclasses import dataclass+from datetime import UTC, datetime, timedelta+from typing import Optional+from uuid import UUID++import structlog++from posthog.hogql import ast+from posthog.hogql.base import _T_AST+from posthog.hogql.database.schema.events import EventsTable+from posthog.hogql.visitor import TraversingVisitor++logger = structlog.get_logger(__name__)++UUID_V7_TIMESTAMP_BUFFER = timedelta(days=3)++# Reject embedded times this far out: they can only come from garbage uuids, and values near+# datetime.max would overflow the buffer arithmetic.+_MAX_PLAUSIBLE_EMBEDDED = datetime(9000, 1, 1, tzinfo=UTC)+++def apply_uuid_v7_timestamp_bounds(node: _T_AST) -> _T_AST:+ UuidV7TimestampBoundsTransform().visit(node)+ return node+++@dataclass+class _Bound:+ lower: datetime+ upper: datetime+ # Chain prefix of the uuid field that produced the bound (["e"] for e.uuid), reused to+ # reference timestamp on the same scan.+ chain_prefix: list[str | int]+++def _uuid_v7_embedded_datetime(value: object) -> Optional[datetime]:+ if isinstance(value, UUID):+ parsed = value+ elif isinstance(value, str):+ try:+ parsed = UUID(value)+ except ValueError:+ return None+ else:+ return None+ if parsed.version != 7:+ return None+ try:+ # The high 48 bits of a UUIDv7 hold the Unix millisecond timestamp.+ embedded = datetime.fromtimestamp((parsed.int >> 80) / 1000, tz=UTC)+ except (ValueError, OverflowError, OSError):+ return None+ if embedded > _MAX_PLAUSIBLE_EMBEDDED:+ return None+ return embedded+++def _unwrap_aliases(expr: ast.Expr) -> ast.Expr:+ while isinstance(expr, ast.Alias):+ expr = expr.expr+ return expr+++def _constant_embedded_datetime(expr: ast.Expr) -> Optional[datetime]:+ expr = _unwrap_aliases(expr)+ if isinstance(expr, ast.Call) and expr.name in ("toUUID", "toUUIDOrNull", "toUUIDOrZero") and len(expr.args) == 1:+ expr = _unwrap_aliases(expr.args[0])+ if isinstance(expr, ast.Constant):+ return _uuid_v7_embedded_datetime(expr.value)+ return None+++class UuidV7TimestampBoundsTransform(TraversingVisitor):+ def visit_select_query(self, node: ast.SelectQuery):+ super().visit_select_query(node)+ try:+ self._apply_bounds(node)+ except Exception as err:+ # Pure optimization: any unexpected failure must leave the query untouched, not break it.+ logger.warning("uuid_v7_timestamp_bounds_unexpected_error", error=str(err), exc_info=True)++ def _apply_bounds(self, node: ast.SelectQuery) -> None:+ scan_ids = self._events_scan_ids(node)+ if not scan_ids:+ return++ bounds: dict[int, _Bound] = {}+ for clause in (node.where, node.prewhere):+ if clause is not None:+ bounds = _intersect_bounds(bounds, self._bounds_from_expr(clause, scan_ids))++ for scan_id, bound in bounds.items():+ # This runs after the type resolver, so the injected fields are typed by hand+ # against the same table type the uuid comparison resolved to.+ scan_type = scan_ids[scan_id]+ lower_field = ast.Field(+ chain=[*bound.chain_prefix, "timestamp"],+ type=ast.FieldType(name="timestamp", table_type=scan_type),+ )+ upper_field = ast.Field(+ chain=[*bound.chain_prefix, "timestamp"],+ type=ast.FieldType(name="timestamp", table_type=scan_type),+ )+ condition = ast.And(+ exprs=[+ ast.CompareOperation(+ op=ast.CompareOperationOp.GtEq,+ left=lower_field,+ right=ast.Constant(value=bound.lower - UUID_V7_TIMESTAMP_BUFFER),+ ),+ ast.CompareOperation(+ op=ast.CompareOperationOp.LtEq,+ left=upper_field,+ right=ast.Constant(value=bound.upper + UUID_V7_TIMESTAMP_BUFFER),+ ),+ ]+ )+ node.where = condition if node.where is None else ast.And(exprs=[node.where, condition])++ def _events_scan_ids(self, node: ast.SelectQuery) -> dict[int, ast.TableType | ast.TableAliasType]:+ """Table types of events table scans in this SELECT's FROM/JOIN chain, keyed by identity."""+ result: dict[int, ast.TableType | ast.TableAliasType] = {}+ join = node.select_from+ while join is not None:+ join_type = join.type+ if isinstance(join_type, (ast.TableType, ast.TableAliasType)):+ base: ast.Type = join_type+ while isinstance(base, ast.TableAliasType):+ base = base.table_type+ if isinstance(base, ast.TableType) and isinstance(base.table, EventsTable):+ result[id(join_type)] = join_type+ join = join.next_join+ return result++ def _bounds_from_expr(+ self, expr: ast.Expr, scan_ids: dict[int, ast.TableType | ast.TableAliasType]+ ) -> dict[int, _Bound]:+ """Derive per-scan timestamp bounds implied by the expression, failing safe to no bound.++ AND conjuncts each contribute bounds on their own. An OR contributes only when every+ branch is bounded, taking the union of the ranges. NOT and anything unrecognized+ contribute nothing.+ """+ if isinstance(expr, ast.And):+ merged: dict[int, _Bound] = {}+ for child in expr.exprs:+ merged = _intersect_bounds(merged, self._bounds_from_expr(child, scan_ids))+ return merged++ if isinstance(expr, ast.Or):+ branch_bounds = [self._bounds_from_expr(child, scan_ids) for child in expr.exprs]+ if not branch_bounds:+ return {}+ shared_keys = set(branch_bounds[0])+ for branch in branch_bounds[1:]:+ shared_keys &= set(branch)+ return {+ key: _Bound(+ lower=min(branch[key].lower for branch in branch_bounds),+ upper=max(branch[key].upper for branch in branch_bounds),+ chain_prefix=branch_bounds[0][key].chain_prefix,+ )+ for key in shared_keys+ }++ if isinstance(expr, ast.Call):+ if expr.name == "and":+ return self._bounds_from_expr(ast.And(exprs=expr.args), scan_ids)+ if expr.name == "or":+ return self._bounds_from_expr(ast.Or(exprs=expr.args), scan_ids)+ if expr.name == "equals" and len(expr.args) == 2:+ return self._bounds_from_compare(+ ast.CompareOperation(op=ast.CompareOperationOp.Eq, left=expr.args[0], right=expr.args[1]),+ scan_ids,+ )+ if expr.name == "in" and len(expr.args) == 2:+ return self._bounds_from_compare(+ ast.CompareOperation(op=ast.CompareOperationOp.In, left=expr.args[0], right=expr.args[1]),+ scan_ids,+ )+ return {}++ if isinstance(expr, ast.CompareOperation):+ return self._bounds_from_compare(expr, scan_ids)++ return {}++ def _bounds_from_compare(+ self, node: ast.CompareOperation, scan_ids: dict[int, ast.TableType | ast.TableAliasType]+ ) -> dict[int, _Bound]:+ if node.op == ast.CompareOperationOp.Eq:+ for field_side, constant_side in ((node.left, node.right), (node.right, node.left)):+ scan_match = self._match_uuid_field(field_side, scan_ids)+ if scan_match is None:+ continue+ embedded = _constant_embedded_datetime(constant_side)+ if embedded is None:+ return {}+ scan_id, chain_prefix = scan_match+ return {scan_id: _Bound(lower=embedded, upper=embedded, chain_prefix=chain_prefix)}+ return {}++ if node.op == ast.CompareOperationOp.In:+ scan_match = self._match_uuid_field(node.left, scan_ids)+ if scan_match is None:+ return {}+ if isinstance(node.right, (ast.Tuple, ast.Array)):+ elements = node.right.exprs+ else:+ elements = [node.right]+ if not elements:+ return {}+ embedded_datetimes = [_constant_embedded_datetime(element) for element in elements]+ if any(embedded is None for embedded in embedded_datetimes):+ return {}+ resolved = [embedded for embedded in embedded_datetimes if embedded is not None]+ scan_id, chain_prefix = scan_match+ return {scan_id: _Bound(lower=min(resolved), upper=max(resolved), chain_prefix=chain_prefix)}++ return {}++ def _match_uuid_field(+ self, expr: ast.Expr, scan_ids: dict[int, ast.TableType | ast.TableAliasType]+ ) -> Optional[tuple[int, list[str | int]]]:+ expr = _unwrap_aliases(expr)+ if not isinstance(expr, ast.Field):+ return None+ field_type = expr.type+ if isinstance(field_type, ast.FieldAliasType):+ field_type = field_type.type+ if not isinstance(field_type, ast.FieldType) or field_type.name != "uuid":+ return None+ if id(field_type.table_type) not in scan_ids:+ return None+ return id(field_type.table_type), list(expr.chain[:-1])+++def _intersect_bounds(left: dict[int, _Bound], right: dict[int, _Bound]) -> dict[int, _Bound]:+ """Merge bounds from two conjuncts, keeping the tighter range where both bound the same scan."""+ merged = dict(left)+ for key, bound in right.items():+ existing = merged.get(key)+ if existing is None:+ merged[key] = bound+ else:+ merged[key] = _Bound(+ lower=max(existing.lower, bound.lower),+ upper=min(existing.upper, bound.upper),+ chain_prefix=existing.chain_prefix,+ )+ return merged