: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(data-warehouse): dedupe DuckLake JSON path placeholders (#66054)
Jovan Sakovic9cccc5d11cde
diff --git a/posthog/hogql/printer/duckdb.py b/posthog/hogql/printer/duckdb.pyindex a74ffdefaa9e..4cb9d2f2f2a0 100644--- a/posthog/hogql/printer/duckdb.py+++ b/posthog/hogql/printer/duckdb.py@@ -41,6 +41,7 @@ class DuckDBPrinter(PostgresPrinter):for k, v in parent_handlers.items()if k not in DUCKDB_FUNCTION_RENAMES_LOWER}+ self._jsonpath_placeholders: dict[str, str] = {}def _print_identifier(self, name: str) -> str:# DuckDB has no practical identifier length limit, so skip the Postgres@@ -70,7 +71,13 @@ class DuckDBPrinter(PostgresPrinter):return f'."{escaped}"'path = "$" + "".join(member(k) for k in chain)- return [self.context.add_value(path)]+ # DuckDB rejects `GROUP BY <expr>` when the SELECT and GROUP BY bind the same path to+ # different placeholders, so reuse one placeholder per distinct path within a query.+ placeholder = self._jsonpath_placeholders.get(path)+ if placeholder is None:+ placeholder = self.context.add_value(path)+ self._jsonpath_placeholders[path] = placeholder+ return [placeholder]def _unsafe_json_extract_trim_quotes(self, unsafe_field, unsafe_args):if not unsafe_args:diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex 42f5a9abec24..04e48e758d01 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -6883,6 +6883,20 @@ class TestDuckDBPrinter(BaseTest):self._expr("properties['a\"b']", context=context)self.assertEqual(list(context.values.values()), ['$."a\\"b"'])+ def test_repeated_property_access_reuses_one_placeholder(self):+ # DuckDB rejects `GROUP BY <expr>` when the same JSON path is bound to a different placeholder+ # in the SELECT than in the GROUP BY — it can't prove the two parameterized expressions are+ # equal. Repeated identical reads must collapse to a single bound value so the printed+ # expressions match textually.+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)+ printed = self._select(+ "SELECT properties.$ai_session_id AS s, count() AS n FROM events GROUP BY properties.$ai_session_id",+ context=context,+ )+ self.assertEqual(list(context.values.values()).count('$."$ai_session_id"'), 1)+ # the SELECT and GROUP BY reference the very same placeholder token+ self.assertEqual(printed.count("(events.properties) ->> %(hogql_val_0)s"), 2)+class TestMySQLPrinter(BaseTest):maxDiff = None