MDEV-35869 Wrong result using degenerated subquery with window function

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>
This commit is contained in:
Igor Babaev 2025-01-23 13:48:00 -08:00
parent 136e866119
commit 77ea99a4b5
4 changed files with 59 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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