Skip to content

Commit ff6be71

Browse files
authoredMar 26, 2025··
Fix(optimizer): avoid merging window function nested under a projection (#4919)
1 parent 2d64512 commit ff6be71

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed
 

‎sqlglot/optimizer/merge_subqueries.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ def _mergeable(
129129
inner_select = inner_scope.expression.unnest()
130130

131131
def _is_a_window_expression_in_unmergable_operation():
132-
window_expressions = inner_select.find_all(exp.Window)
133-
window_alias_names = {window.parent.alias_or_name for window in window_expressions}
132+
window_aliases = {s.alias_or_name for s in inner_select.selects if s.find(exp.Window)}
134133
inner_select_name = from_or_join.alias_or_name
135134
unmergable_window_columns = [
136135
column
@@ -142,7 +141,7 @@ def _is_a_window_expression_in_unmergable_operation():
142141
window_expressions_in_unmergable = [
143142
column
144143
for column in unmergable_window_columns
145-
if column.table == inner_select_name and column.name in window_alias_names
144+
if column.table == inner_select_name and column.name in window_aliases
146145
]
147146
return any(window_expressions_in_unmergable)
148147

‎tests/fixtures/optimizer/merge_subqueries.sql

+19-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ FROM
259259
t1;
260260
WITH t1 AS (SELECT x.a AS a, x.b AS b, ROW_NUMBER() OVER (PARTITION BY x.a ORDER BY x.a) AS row_num FROM x AS x) SELECT SUM(t1.row_num) AS total_rows FROM t1 AS t1;
261261

262-
# title: Test prevent merging of window if in group by func
262+
# title: Test prevent merging of window if in group by
263263
with t1 as (
264264
SELECT
265265
x.a,
@@ -277,7 +277,7 @@ GROUP BY t1.row_num
277277
ORDER BY t1.row_num;
278278
WITH t1 AS (SELECT x.a AS a, x.b AS b, ROW_NUMBER() OVER (PARTITION BY x.a ORDER BY x.a) AS row_num FROM x AS x) SELECT t1.row_num AS row_num, SUM(t1.a) AS total FROM t1 AS t1 GROUP BY t1.row_num ORDER BY row_num;
279279

280-
# title: Test prevent merging of window if in order by func
280+
# title: Test prevent merging of window if in order by
281281
with t1 as (
282282
SELECT
283283
x.a,
@@ -294,6 +294,23 @@ FROM
294294
ORDER BY t1.row_num, t1.a;
295295
WITH t1 AS (SELECT x.a AS a, x.b AS b, ROW_NUMBER() OVER (PARTITION BY x.a ORDER BY x.a) AS row_num FROM x AS x) SELECT t1.row_num AS row_num, t1.a AS a FROM t1 AS t1 ORDER BY t1.row_num, t1.a;
296296

297+
# title: Test preventing merging of window nested under complex projection if in order by
298+
WITH t1 AS (
299+
SELECT
300+
x.a,
301+
x.b,
302+
ROW_NUMBER() OVER (PARTITION BY x.a ORDER BY x.a) - 1 AS row_num
303+
FROM
304+
x
305+
)
306+
SELECT
307+
t1.row_num AS row_num,
308+
t1.a AS a
309+
FROM
310+
t1
311+
ORDER BY t1.row_num, t1.a;
312+
WITH t1 AS (SELECT x.a AS a, x.b AS b, ROW_NUMBER() OVER (PARTITION BY x.a ORDER BY x.a) - 1 AS row_num FROM x AS x) SELECT t1.row_num AS row_num, t1.a AS a FROM t1 AS t1 ORDER BY t1.row_num, t1.a;
313+
297314
# title: Test allow merging of window function
298315
with t1 as (
299316
SELECT

0 commit comments

Comments
 (0)
Please sign in to comment.