: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
fix(hogql): strip SAMPLE modifier when lazy table expands to subquery (#67309)
posthog[bot]de091fe12fe2
diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex d1979a5cf465..ceb0c4ee0e7a 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -2107,6 +2107,8 @@ class TestPrinter(BaseTest):enable_select_queries=True,modifiers=HogQLQueryModifiers(personsArgMaxVersion=PersonsArgMaxVersion.V2),)+ # A SAMPLE on a lazy table is dropped when the table expands into a subquery: ClickHouse+ # rejects SAMPLE on a subquery, and sampling a pre-aggregated table is meaningless.self.assertEqual(self._select("SELECT events.event FROM events SAMPLE 2/78 OFFSET 999 JOIN persons SAMPLE 0.1 ON persons.id=events.person_id",@@ -2124,7 +2126,7 @@ class TestPrinter(BaseTest):"HAVING and(equals(argMax(person.is_deleted, person.version), 0), ""less(argMax(toTimeZone(person.created_at, %(hogql_val_0)s), person.version), ""plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))))))) "- "SETTINGS optimize_aggregation_in_order=1) AS persons SAMPLE 0.1 ON equals(persons.id, if(not(empty(events__override.distinct_id)), "+ "SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, if(not(empty(events__override.distinct_id)), "f"events__override.person_id, events.person_id)) WHERE equals(events.team_id, {self.team.pk}) LIMIT {MAX_SELECT_RETURNED_ROWS}",)@@ -2153,6 +2155,7 @@ class TestPrinter(BaseTest):enable_select_queries=True,modifiers=HogQLQueryModifiers(personsArgMaxVersion=PersonsArgMaxVersion.V2),)+ # SAMPLE on the lazy persons table is dropped once it expands into a subquery.expected = self._select("SELECT events.event FROM events SAMPLE 2/78 OFFSET 999 JOIN persons SAMPLE 0.1 ON persons.id=events.person_id",context,@@ -2164,7 +2167,7 @@ class TestPrinter(BaseTest):f"max(person.version) AS version FROM person WHERE equals(person.team_id, {self.team.pk}) GROUP BY person.id "f"HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, "f"%(hogql_val_0)s), person.version), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) "- f"AS persons SAMPLE 0.1 ON equals(persons.id, events.person_id) WHERE equals(events.team_id, {self.team.pk}) LIMIT {MAX_SELECT_RETURNED_ROWS}",+ f"AS persons ON equals(persons.id, events.person_id) WHERE equals(events.team_id, {self.team.pk}) LIMIT {MAX_SELECT_RETURNED_ROWS}",)def test_count_distinct(self):diff --git a/posthog/hogql/transforms/lazy_tables.py b/posthog/hogql/transforms/lazy_tables.pyindex c5e24579ff4c..e0d7edab4293 100644--- a/posthog/hogql/transforms/lazy_tables.py+++ b/posthog/hogql/transforms/lazy_tables.py@@ -595,6 +595,16 @@ class LazyTableResolver(TraversingVisitor):join_ptr.table = subqueryjoin_ptr.type = select_type.tables[table_name]join_ptr.alias = table_name+ # A lazy table expands into an aggregating subquery. ClickHouse rejects a SAMPLE+ # modifier on a subquery, and sampling a pre-aggregated table is meaningless, so+ # drop the modifier rather than emitting invalid SQL.+ if join_ptr.sample is not None:+ self.context.add_warning(+ f'SAMPLE clause ignored: the "{table_name}" table is computed on the fly and cannot be sampled',+ start=join_ptr.sample.start,+ end=join_ptr.sample.end,+ )+ join_ptr.sample = Nonebreakjoin_ptr = join_ptr.next_joindiff --git a/posthog/hogql/transforms/test/test_lazy_tables.py b/posthog/hogql/transforms/test/test_lazy_tables.pyindex 4bc502acc889..8851df122019 100644--- a/posthog/hogql/transforms/test/test_lazy_tables.py+++ b/posthog/hogql/transforms/test/test_lazy_tables.py@@ -83,6 +83,18 @@ class TestLazyJoins(BaseTest):printed = self._print_select("select count() from persons")assert printed == self.snapshot+ def test_sample_on_lazy_table_is_stripped(self):+ # A lazy table expands into an aggregating subquery; ClickHouse rejects a SAMPLE modifier on+ # a subquery, so it must be dropped rather than left to produce invalid SQL — but not+ # silently: the user asked for sampling and needs to know it did not apply.+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True, modifiers=HogQLQueryModifiers())+ expr = parse_select("select session_id, $channel_type from sessions SAMPLE 10")+ printed, _ = prepare_and_print_ast(expr, context, "clickhouse")+ assert "SAMPLE" not in printed+ assert [warning.message for warning in context.warnings] == [+ 'SAMPLE clause ignored: the "sessions" table is computed on the fly and cannot be sampled'+ ]+def _print_select(self, select: str, modifiers: HogQLQueryModifiers | None = None):expr = parse_select(select)query, _ = prepare_and_print_ast(