: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-modeling): print literal-row unions as one values list on trino (#123147)
Jovan Sakovicc7a930d7af3b
diff --git a/posthog/hogql/printer/test/test_trino_printer.py b/posthog/hogql/printer/test/test_trino_printer.pyindex c8bb6eabd96d..3382df85d423 100644--- a/posthog/hogql/printer/test/test_trino_printer.py+++ b/posthog/hogql/printer/test/test_trino_printer.py@@ -1367,7 +1367,9 @@ def test_lowers_staged_window_filters(query: str) -> None:)def test_lowers_named_set_operations(operator: str) -> None:sql, _ = prepare_and_print_ast(- parse_select(f"SELECT 1 AS first, 2 AS second {operator} BY NAME SELECT 2 AS second, 1 AS first"),+ parse_select(+ f"SELECT 1 AS first, 2 AS second FROM users {operator} BY NAME SELECT 2 AS second, 1 AS first FROM users"+ ),_context_with_trino_table(),"trino",)@@ -2932,3 +2934,42 @@ def test_trino_signatures_do_not_change_other_dialect_validation(expression: strelse:with pytest.raises(QueryError, match="expects"):print_prepared_ast(resolve_types(query, context, dialect), context, dialect)+++ "query, expected",+ [+ (+ "SELECT name, count() FROM (SELECT 'a' AS name, 1 AS id UNION ALL SELECT 'b', 2 UNION ALL SELECT 'a', 3) "+ "GROUP BY name ORDER BY name",+ [("a", 2), ("b", 1)],+ ),+ (+ "SELECT id FROM (SELECT 1 AS id UNION ALL SELECT 1 + 1 UNION ALL SELECT NULL) ORDER BY id NULLS LAST",+ [(1,), (2,), (None,)],+ ),+ ],+)+def test_lowers_constant_row_unions_to_one_values_list(query: str, expected: list[tuple[object, ...]]) -> None:+ # Trino plans every UNION ALL branch below a repartitioning exchange as its own stage.+ context = _context_with_trino_table()+ sql, _ = prepare_and_print_ast(parse_select(query), context, "trino")+ assert "UNION" not in sql+ assert "VALUES" in sql+ sql, values = convert_pyformat_placeholders(sql, context.values)+ with duckdb.connect(":memory:") as connection:+ assert connection.execute(sql, values).fetchall() == expected+++ "query",+ [+ "SELECT 'a' AS user_id UNION ALL SELECT user_id FROM users",+ "SELECT tuple(1, 2) AS t UNION ALL SELECT tuple(3, 4)",+ "SELECT first_value(1) AS x UNION ALL SELECT 2",+ ],+)+def test_keeps_union_when_a_branch_is_not_a_constant_row(query: str) -> None:+ sql, _ = prepare_and_print_ast(parse_select(query), _context_with_trino_table(), "trino")+ assert "UNION ALL" in sql+ assert "VALUES" not in sqldiff --git a/posthog/hogql/printer/trino.py b/posthog/hogql/printer/trino.pyindex 69e3acbf73bd..069e850a695c 100644--- a/posthog/hogql/printer/trino.py+++ b/posthog/hogql/printer/trino.py@@ -345,6 +345,9 @@ class TrinoPrinter(PostgresPrinter):def visit_select_set_query(self, node: ast.SelectSetQuery) -> str:node = self._align_set_query_types(node)+ values_sql = self._constant_rows_as_values(node)+ if values_sql is not None:+ return f"({values_sql})" if len(self.stack) > 1 else values_sqlif not isinstance(node.initial_select_query, ast.SelectQuery) or not node.initial_select_query.ctes:return super().visit_select_set_query(node)ctes = node.initial_select_query.ctes@@ -422,6 +425,89 @@ class TrinoPrinter(PostgresPrinter):branch.select[index] = cast_expressionreturn lowered+ def _constant_rows_as_values(self, node: ast.SelectSetQuery) -> str | None:+ """Print a UNION ALL of literal-only rows as one VALUES list.++ Trino gives every UNION ALL branch its own stage when the union feeds a repartitioning+ exchange (GROUP BY, DISTINCT, JOIN), so a hand-written list of a few hundred rows can+ exceed the query stage limit. VALUES is one plan node, with the same type coercion.+ """+ if node.limit is not None or node.offset is not None:+ return None+ if any(+ subsequent.set_operator != "UNION ALL" or not isinstance(subsequent.select_query, ast.SelectQuery)+ for subsequent in node.subsequent_select_queries+ ):+ return None+ queries = [+ node.initial_select_query,+ *(subsequent.select_query for subsequent in node.subsequent_select_queries),+ ]+ branches = [query for query in queries if isinstance(query, ast.SelectQuery)]+ if len(branches) != len(queries) or not all(self._is_constant_row(branch) for branch in branches):+ return None+ first = branches[0]+ if not all(isinstance(projection, ast.Alias) for projection in first.select):+ return None+ names = [projection.alias for projection in first.select if isinstance(projection, ast.Alias)]+ if len(set(names)) != len(names) or any(len(branch.select) != len(names) for branch in branches):+ return None+ rows = []+ for branch in branches:+ row = [+ self.visit(projection.expr if isinstance(projection, ast.Alias) else projection)+ for projection in branch.select+ ]+ rows.append(f"({', '.join(row)})")+ columns = ", ".join(self._print_identifier(name) for name in names)+ return f"SELECT * FROM (VALUES {', '.join(rows)}) AS {self._print_identifier('constant_rows')}({columns})"++ def _is_constant_row(self, node: ast.SelectQuery) -> bool:+ if (+ node.ctes+ or node.distinct+ or node.select_from is not None+ or node.array_join_list+ or node.window_exprs+ or node.where is not None+ or node.prewhere is not None+ or node.having is not None+ or node.qualify is not None+ or node.group_by+ or node.order_by+ or node.limit is not None+ or node.limit_by is not None+ or node.offset is not None+ ):+ return False+ expressions = [+ projection.expr if isinstance(projection, ast.Alias) else projection for projection in node.select+ ]+ # Trino expands a ROW value inside VALUES into separate columns, so a tuple column would change shape.+ if any(isinstance(expr, ast.Call) and expr.name.lower() == "tuple" for expr in expressions):+ return False+ return all(self._is_constant_expression(expr) for expr in expressions)++ def _is_constant_expression(self, node: ast.Expr) -> bool:+ if isinstance(node, ast.Constant):+ return True+ if isinstance(node, ast.TypeCast):+ return self._is_constant_expression(node.expr)+ if isinstance(node, ast.ArithmeticOperation):+ return self._is_constant_expression(node.left) and self._is_constant_expression(node.right)+ if isinstance(node, ast.Call):+ return (+ not node.params+ and not node.distinct+ and find_hogql_aggregation(node.name) is None+ # Window functions such as first_value print as Trino aggregates, which VALUES can't hold.+ and node.name.lower() not in _TRINO_WINDOW_FUNCTIONS+ and node.name.lower() not in _TRINO_WINDOW_FUNCTION_RENAMES+ and node.name.lower() not in _TRINO_CONDITIONAL_WINDOW_FUNCTIONS+ and all(self._is_constant_expression(arg) for arg in node.args)+ )+ return False+def _set_query_branches(self, node: ast.SelectSetQuery) -> list[ast.SelectQuery]:branches: list[ast.SelectQuery] = []for branch in node.select_queries():