Skip to content

Commit

Permalink
MDEV-35869 Wrong result using degenerated subquery with window function
Browse files Browse the repository at this point in the history
This bug affected queries containing degenerated single-value subqueries
with window functions. The bug led mostly to wrong results for such
queries.

A subquery is called degenerated if it is of the form (SELECT <expr>...).

For degenerated subqueries of the form (SELECT <expr>) the transformation
   (SELECT <expr>) => <expr>
usually is applied. However if <expr> contains set functions or window
functions such rewriting is not valid for an obvious reason. The code
before this patch erroneously applied the transformation when <expr>
contained window functions and did not contain set functions.

Approved by Rex Johnston <rex.johnston@mariadb.com>
  • Loading branch information
igorbabaev committed Jan 23, 2025
1 parent 136e866 commit 77ea99a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
23 changes: 23 additions & 0 deletions mysql-test/main/win.result
Original file line number Diff line number Diff line change
Expand Up @@ -4601,4 +4601,27 @@ w2_total w1_total u
200 200 2
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-35869: degenerated subquery with window function
#
CREATE TABLE t1 (a int DEFAULT 10);
INSERT INTO t1 VALUES (7), (2), (3);
SELECT * FROM t1 WHERE (SELECT AVG(3)) > 2;
a
7
2
3
SELECT * FROM t1 WHERE (SELECT AVG(3) OVER ()) > 2;
a
7
2
3
INSERT INTO t1 VALUES((SELECT avg(4) OVER ()));
SELECT * FROM t1;
a
7
2
3
4
DROP TABLE t1;
# End of 10.5 tests
12 changes: 12 additions & 0 deletions mysql-test/main/win.test
Original file line number Diff line number Diff line change
Expand Up @@ -3001,4 +3001,16 @@ eval $q3;
DROP VIEW v1;
DROP TABLE t1;

--echo #
--echo # MDEV-35869: degenerated subquery with window function
--echo #

CREATE TABLE t1 (a int DEFAULT 10);
INSERT INTO t1 VALUES (7), (2), (3);
SELECT * FROM t1 WHERE (SELECT AVG(3)) > 2;
SELECT * FROM t1 WHERE (SELECT AVG(3) OVER ()) > 2;
INSERT INTO t1 VALUES((SELECT avg(4) OVER ()));
SELECT * FROM t1;
DROP TABLE t1;

--echo # End of 10.5 tests
23 changes: 23 additions & 0 deletions mysql-test/suite/encryption/r/tempfiles_encrypted.result
Original file line number Diff line number Diff line change
Expand Up @@ -4607,6 +4607,29 @@ w2_total w1_total u
200 200 2
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-35869: degenerated subquery with window function
#
CREATE TABLE t1 (a int DEFAULT 10);
INSERT INTO t1 VALUES (7), (2), (3);
SELECT * FROM t1 WHERE (SELECT AVG(3)) > 2;
a
7
2
3
SELECT * FROM t1 WHERE (SELECT AVG(3) OVER ()) > 2;
a
7
2
3
INSERT INTO t1 VALUES((SELECT avg(4) OVER ()));
SELECT * FROM t1;
a
7
2
3
4
DROP TABLE t1;
# End of 10.5 tests
#
# MDEV-23867: select crash in compute_window_func
Expand Down
1 change: 1 addition & 0 deletions sql/item_subselect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,7 @@ Item_singlerow_subselect::select_transformer(JOIN *join)
!select_lex->table_list.elements &&
select_lex->item_list.elements == 1 &&
!select_lex->item_list.head()->with_sum_func() &&
!select_lex->item_list.head()->with_window_func &&
/*
We can't change name of Item_field or Item_ref, because it will
prevent its correct resolving, but we should save name of
Expand Down

0 comments on commit 77ea99a

Please sign in to comment.