mirror of
https://github.com/MariaDB/server.git
synced 2025-08-01 03:47:19 +03:00
MDEV-29307 Wrong result when joining two derived tables over the same view
This bug could affect queries containing a join of derived tables over grouping views such that one of the derived tables contains a window function while another uses view V with dependent subquery DSQ containing a set function aggregated outside of the subquery in the view V. The subquery also refers to the fields from the group clause of the view.Due to this bug execution of such queries could produce wrong result sets. When the fix_fields() method performs context analysis of a set function AF first, at the very beginning the function Item_sum::init_sum_func_check() is called. The function copies the pointer to the embedding set function, if any, stored in THD::LEX::in_sum_func into the corresponding field of the set function AF simultaneously changing the value of THD::LEX::in_sum_func to point to AF. When at the very end of the fix_fields() method the function Item_sum::check_sum_func() is called it is supposed to restore the value of THD::LEX::in_sum_func to point to the embedding set function. And in fact Item_sum::check_sum_func() did it, but only for regular set functions, not for those used in window functions. As a result after the context analysis of AF had finished THD::LEX::in_sum_func still pointed to AF. It confused the further context analysis. In particular it led to wrong resolution of Item_outer_ref objects in the fix_inner_refs() function. This wrong resolution forced reading the values of grouping fields referred in DSQ not from the temporary table used for aggregation from which they were supposed to be read, but from the table used as the source table for aggregation. This patch guarantees that the value of THD::LEX::in_sum_func is properly restored after the call of fix_fields() for any set function.
This commit is contained in:
@ -2873,3 +2873,96 @@ create table t1 (a int);
|
||||
insert into t1 values (1),(2),(3);
|
||||
SELECT row_number() OVER (order by a) FROM t1 order by NAME_CONST('myname',NULL);
|
||||
drop table t1;
|
||||
|
||||
--echo #
|
||||
--echo # MDEV-29307: join of 2 derived tables over the same grouping view such
|
||||
--echo # that the first of the joined tables contains a window
|
||||
--echo # function and the view's specification contains a subquery
|
||||
--echo # with a set function aggregated on the top level
|
||||
--echo #
|
||||
|
||||
CREATE TABLE t1 (
|
||||
tst int NOT NULL,
|
||||
flat tinyint unsigned NOT NULL,
|
||||
type tinyint unsigned NOT NULL,
|
||||
val int NOT NULL,
|
||||
PRIMARY KEY (tst,flat,type)
|
||||
) ENGINE=ARIA;
|
||||
|
||||
INSERT INTO t1 VALUES
|
||||
(5, 20, 2, 100),
|
||||
(7, 20, 2, 150),
|
||||
(9, 20, 1, 200);
|
||||
|
||||
CREATE VIEW v1 AS (
|
||||
SELECT
|
||||
flat,
|
||||
type,
|
||||
( SELECT val FROM t1 sw
|
||||
WHERE sw.tst = MAX(w.tst) AND sw.flat = w.flat AND sw.type = w.type)
|
||||
AS total
|
||||
FROM t1 w
|
||||
GROUP BY flat, type
|
||||
);
|
||||
|
||||
let $q1=
|
||||
SELECT w2.total AS w2_total, w1.total AS w1_total
|
||||
FROM
|
||||
(
|
||||
SELECT flat, type, total
|
||||
FROM v1
|
||||
WHERE type = 1
|
||||
) AS w1
|
||||
JOIN
|
||||
(
|
||||
SELECT flat, type, total
|
||||
FROM v1
|
||||
WHERE type = 2
|
||||
) AS w2
|
||||
ON w1.flat = w2.flat;
|
||||
|
||||
eval EXPLAIN EXTENDED $q1;
|
||||
eval $q1;
|
||||
|
||||
let $q2=
|
||||
SELECT w2.total AS w2_total, w1.total AS w1_total
|
||||
FROM
|
||||
(
|
||||
SELECT flat, type, total,
|
||||
COUNT(total) OVER (PARTITION BY type ORDER BY type) AS u
|
||||
FROM v1
|
||||
WHERE type = 1
|
||||
) AS w1
|
||||
JOIN
|
||||
(
|
||||
SELECT flat, type, total
|
||||
FROM v1
|
||||
WHERE type = 2
|
||||
) AS w2
|
||||
ON w1.flat = w2.flat;
|
||||
|
||||
eval EXPLAIN EXTENDED $q2;
|
||||
eval $q2;
|
||||
|
||||
let $q3=
|
||||
SELECT w2.total AS w2_total, w1.total AS w1_total, u
|
||||
FROM
|
||||
(
|
||||
SELECT flat, type, total,
|
||||
COUNT(total) OVER (PARTITION BY flat ORDER BY flat) AS u
|
||||
FROM v1
|
||||
) AS w1
|
||||
JOIN
|
||||
(
|
||||
SELECT flat, type, total
|
||||
FROM v1
|
||||
) AS w2;
|
||||
|
||||
eval EXPLAIN EXTENDED $q3;
|
||||
--sorted_result
|
||||
eval $q3;
|
||||
|
||||
DROP VIEW v1;
|
||||
DROP TABLE t1;
|
||||
|
||||
--echo # End of 10.5 tests
|
||||
|
Reference in New Issue
Block a user