: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): render modulo as MOD() for postgres and duckdb dialects (#67553)
posthog[bot]fe527793f06f
diff --git a/posthog/hogql/printer/postgres.py b/posthog/hogql/printer/postgres.pyindex 18263576533e..43daf904e2d4 100644--- a/posthog/hogql/printer/postgres.py+++ b/posthog/hogql/printer/postgres.py@@ -517,7 +517,11 @@ class PostgresPrinter(BasePrinter):elif node.op == ast.ArithmeticOperationOp.Div:return f"({self.visit(node.left)} / {self.visit(node.right)})"elif node.op == ast.ArithmeticOperationOp.Mod:- return f"({self.visit(node.left)} % {self.visit(node.right)})"+ # A bare `%` can't appear in printed SQL — during client-side binding psycopg+ # reads it as the start of a parameter placeholder (valid ones look like+ # `%(hogql_val_0)s`) and errors on the incomplete placeholder. So modulo renders+ # as MOD(a, b). Both Postgres and DuckDB (which subclasses this printer) support MOD().+ return f"MOD({self.visit(node.left)}, {self.visit(node.right)})"else:raise ImpossibleASTError(f"Unknown ArithmeticOperationOp {node.op}")diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex 502a32d41142..896e4cae747a 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -6326,7 +6326,7 @@ class TestPostgresPrinter(BaseTest):self.assertEqual(self._expr("a - b"), "(a - b)")self.assertEqual(self._expr("a * b"), "(a * b)")self.assertEqual(self._expr("a / b"), "(a / b)")- self.assertEqual(self._expr("a % b"), "(a % b)")+ self.assertEqual(self._expr("a % b"), "MOD(a, b)")def test_logical_operators(self):self.assertEqual(self._expr("a AND b"), "((a) AND (b))")diff --git a/posthog/hogql/test/test_direct_postgres_query.py b/posthog/hogql/test/test_direct_postgres_query.pyindex c8ae59a581d2..e1adc2e72de8 100644--- a/posthog/hogql/test/test_direct_postgres_query.py+++ b/posthog/hogql/test/test_direct_postgres_query.py@@ -276,6 +276,49 @@ class TestDirectPostgresQuery(APIBaseTest):self.assertIn("activitylog", sql)self.assertEqual(executor.direct_source_id, str(source.id))+ def test_modulo_renders_as_mod_function(self):+ # A bare `%` in the printed SQL is read by psycopg as a client-side parameter+ # placeholder and blows up before the query runs, so modulo must render as MOD(a, b).+ source = ExternalDataSource.objects.create(+ team=self.team,+ source_id="source_id",+ connection_id="connection_id",+ status=ExternalDataSource.Status.COMPLETED,+ source_type="Postgres",+ access_method=ExternalDataSource.AccessMethod.DIRECT,+ prefix="ph3",+ job_inputs={+ "host": "localhost",+ "port": 5432,+ "database": "postgres",+ "user": "postgres",+ "password": "postgres",+ "schema": "ph3",+ },+ )++ DataWarehouseTable.objects.create(+ name="orders",+ format="Parquet",+ team=self.team,+ external_data_source=source,+ url_pattern="",+ columns={"id": {"hogql": "IntegerDatabaseField", "clickhouse": "Int64", "valid": True}},+ )++ executor = HogQLQueryExecutor(+ query="SELECT id % 2 FROM orders",+ team=self.team,+ connection_id=str(source.id),+ )++ sql, _context = executor.generate_clickhouse_sql()++ self.assertIn("MOD(", sql)+ # Assert the modulo *operator* isn't rendered as a bare `%`; a plain `assertNotIn("%", sql)`+ # would be too broad since bound values legitimately use `%(hogql_val_*)s` placeholders.+ self.assertNotIn(" % ", sql)+def test_generate_sql_for_direct_postgres_table_inside_cte(self):source = ExternalDataSource.objects.create(team=self.team,