mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
Bug #24484:
To correctly decide which predicates can be evaluated with a given table the optimizer must know the exact set of tables that a predicate depends on. If that mask is too wide (refer to non-existing tables) the optimizer can erroneously skip a predicate. One such case of wrong table usage mask were the aggregate functions. The have a all-1 mask (meaning depend on all tables, including non-existent ones). Fixed by making a real used_tables mask for the aggregates. The mask is constructed in the following way : 1. OR the table dependency masks of all the arguments of the aggregate. 2. If all the arguments of the function are from the local name resolution context and it is evaluated in the same name resolution context where it is referenced all the tables from that name resolution context are OR-ed to the dependency mask. This is to denote that an aggregate function depends on the number of rows it processes. 3. Handle correctly the case of an aggregate function optimization (such that the aggregate function can be pre-calculated and made a constant). Made sure that an aggregate function is never a constant (unless subject of a specific optimization and pre-calculation). One other flaw was revealed and fixed in the process : references were not calling the recalculation method for used_tables of their targets.
This commit is contained in:
@ -645,3 +645,56 @@ a b Z
|
||||
2 2 0
|
||||
3 3 1
|
||||
drop table t1,t2;
|
||||
CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
|
||||
INSERT INTO t1 VALUES (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
|
||||
(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'),(3,3,'j'), (3,2,'k'), (3,1,'l'),
|
||||
(1,9,'m');
|
||||
CREATE TABLE t2 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
|
||||
INSERT INTO t2 SELECT * FROM t1;
|
||||
SELECT a, MAX(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b))
|
||||
as test FROM t1 GROUP BY a;
|
||||
a MAX(b) test
|
||||
1 9 m
|
||||
2 3 h
|
||||
3 4 i
|
||||
SELECT * FROM t1 GROUP by t1.a
|
||||
HAVING (MAX(t1.b) > (SELECT MAX(t2.b) FROM t2 WHERE t2.c < t1.c
|
||||
HAVING MAX(t2.b+t1.a) < 10));
|
||||
a b c
|
||||
SELECT a, AVG(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b))
|
||||
AS test FROM t1 GROUP BY a;
|
||||
a AVG(b) test
|
||||
1 4.0000 NULL
|
||||
2 2.0000 k
|
||||
3 2.5000 NULL
|
||||
SELECT a,b,c FROM t1 WHERE b in (9,3,4) ORDER BY b,c;
|
||||
a b c
|
||||
1 3 c
|
||||
2 3 h
|
||||
3 3 j
|
||||
1 4 d
|
||||
3 4 i
|
||||
1 9 m
|
||||
SELECT a, MAX(b),
|
||||
(SELECT COUNT(DISTINCT t.c) FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)
|
||||
LIMIT 1)
|
||||
as cnt,
|
||||
(SELECT t.b FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
|
||||
as t_b,
|
||||
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
|
||||
as t_b,
|
||||
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) ORDER BY t.c LIMIT 1)
|
||||
as t_b
|
||||
FROM t1 GROUP BY a;
|
||||
a MAX(b) cnt t_b t_b t_b
|
||||
1 9 1 9 m m
|
||||
2 3 1 3 h h
|
||||
3 4 1 4 i i
|
||||
SELECT a, MAX(b),
|
||||
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1) as test
|
||||
FROM t1 GROUP BY a;
|
||||
a MAX(b) test
|
||||
1 9 m
|
||||
2 3 h
|
||||
3 4 i
|
||||
DROP TABLE t1, t2;
|
||||
|
@ -489,3 +489,44 @@ select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1;
|
||||
|
||||
drop table t1,t2;
|
||||
|
||||
#
|
||||
# Bug #24484: Aggregate function used in column list subquery gives erroneous
|
||||
# error
|
||||
#
|
||||
CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
|
||||
INSERT INTO t1 VALUES (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
|
||||
(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'),(3,3,'j'), (3,2,'k'), (3,1,'l'),
|
||||
(1,9,'m');
|
||||
CREATE TABLE t2 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
|
||||
INSERT INTO t2 SELECT * FROM t1;
|
||||
|
||||
# Gives error, but should work since it is (a, b) is the PK so only one
|
||||
# given match possible
|
||||
SELECT a, MAX(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b))
|
||||
as test FROM t1 GROUP BY a;
|
||||
SELECT * FROM t1 GROUP by t1.a
|
||||
HAVING (MAX(t1.b) > (SELECT MAX(t2.b) FROM t2 WHERE t2.c < t1.c
|
||||
HAVING MAX(t2.b+t1.a) < 10));
|
||||
SELECT a, AVG(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b))
|
||||
AS test FROM t1 GROUP BY a;
|
||||
|
||||
SELECT a,b,c FROM t1 WHERE b in (9,3,4) ORDER BY b,c;
|
||||
|
||||
SELECT a, MAX(b),
|
||||
(SELECT COUNT(DISTINCT t.c) FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)
|
||||
LIMIT 1)
|
||||
as cnt,
|
||||
(SELECT t.b FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
|
||||
as t_b,
|
||||
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
|
||||
as t_b,
|
||||
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) ORDER BY t.c LIMIT 1)
|
||||
as t_b
|
||||
FROM t1 GROUP BY a;
|
||||
|
||||
SELECT a, MAX(b),
|
||||
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1) as test
|
||||
FROM t1 GROUP BY a;
|
||||
|
||||
|
||||
DROP TABLE t1, t2;
|
||||
|
Reference in New Issue
Block a user