: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): print GlobalIn/GlobalNotIn in the Postgres dialect (#68034)
posthog[bot]dac923612052
diff --git a/posthog/hogql/printer/postgres.py b/posthog/hogql/printer/postgres.pyindex 8f8062aa6465..18263576533e 100644--- a/posthog/hogql/printer/postgres.py+++ b/posthog/hogql/printer/postgres.py@@ -382,6 +382,11 @@ class PostgresPrinter(BasePrinter):return f"({left} IN {right})"elif op == ast.CompareOperationOp.NotIn:return f"({left} NOT IN {right})"+ elif op == ast.CompareOperationOp.GlobalIn:+ # Postgres has no distributed GLOBAL concept, so it maps to a plain IN+ return f"({left} IN {right})"+ elif op == ast.CompareOperationOp.GlobalNotIn:+ return f"({left} NOT IN {right})"elif op == ast.CompareOperationOp.Regex:return f"({left} ~ {right})"elif op == ast.CompareOperationOp.NotRegex:diff --git a/posthog/hogql/test/test_direct_postgres_query.py b/posthog/hogql/test/test_direct_postgres_query.pyindex 31509c4f5a46..c8ae59a581d2 100644--- a/posthog/hogql/test/test_direct_postgres_query.py+++ b/posthog/hogql/test/test_direct_postgres_query.py@@ -10,7 +10,9 @@ from django.test import override_settingsimport psycopgfrom parameterized import parameterized+from posthog.hogql import astfrom posthog.hogql.constants import HogQLGlobalSettings+from posthog.hogql.context import HogQLContextfrom posthog.hogql.database.schema.duckdb_table_functions import is_dangerous_table_functionfrom posthog.hogql.direct_sql.postgres_adapter import (LenientDirectPostgresDateLoader,@@ -23,6 +25,7 @@ from posthog.hogql.direct_sql.postgres_adapter import (from posthog.hogql.direct_sql.raw_sql import ensure_single_direct_statementfrom posthog.hogql.errors import ExposedHogQLError, QueryErrorfrom posthog.hogql.escape_sql import escape_postgres_identifier+from posthog.hogql.printer.postgres import PostgresPrinterfrom posthog.hogql.query import HogQLQueryExecutorfrom posthog.models import Team@@ -57,6 +60,19 @@ class TestDirectPostgresQuery(APIBaseTest):def test_parse_lenient_direct_postgres_date(self, _name: str, value: str, expected: date):self.assertEqual(parse_lenient_direct_postgres_date(value), expected)+ @parameterized.expand(+ [+ ("global_in", ast.CompareOperationOp.GlobalIn, "(x IN (SELECT session_id FROM sessions))"),+ ("global_not_in", ast.CompareOperationOp.GlobalNotIn, "(x NOT IN (SELECT session_id FROM sessions))"),+ ]+ )+ def test_postgres_printer_maps_global_in_operators(self, _name: str, op: ast.CompareOperationOp, expected_sql: str):+ # The resolver rewrites sessions/events IN subqueries to GlobalIn/GlobalNotIn; Postgres has no+ # distributed GLOBAL concept, so the printer must emit a plain IN/NOT IN rather than crashing.+ printer = PostgresPrinter(context=HogQLContext(team_id=self.team.pk))++ self.assertEqual(printer._get_compare_op(op, "x", "(SELECT session_id FROM sessions)"), expected_sql)+def test_direct_postgres_session_setup_sql_uses_search_path_for_postgres(self):self.assertEqual(direct_postgres_session_setup_sql("ph3"),