diff options
author | Monty <monty@mariadb.org> | 2022-02-02 14:25:25 +0200 |
---|---|---|
committer | Monty <monty@mariadb.org> | 2022-02-08 14:32:29 +0200 |
commit | 38058c04a4fc021f381f8000e40ed23bd4fb8d75 (patch) | |
tree | 3f94cf20f203e68c3627ad2fdcf6d17f3ba5cde8 /mysql-test/main/group_min_max.test | |
parent | d314bd266491baf0954d13fa51dc22b730a6f4d1 (diff) | |
download | mariadb-git-38058c04a4fc021f381f8000e40ed23bd4fb8d75.tar.gz |
MDEV-26585 Wrong query results when `using index for group-by`
The problem was that "group_min_max optimization" does not work if
some aggregate functions, like COUNT(*), is used.
The function get_best_group_min_max() is using the join->sum_funcs
array to check which aggregate functions are used.
The bug was that aggregates in HAVING where not yet added to
join->sum_funcs at the time get_best_group_min_max() was called.
Fixed by populate join->sum_funcs already in prepare, which means that
all sum functions will be in join->sum_funcs in get_best_group_min_max().
A benefit of this approach is that we can remove several calls to
make_sum_func_list() from the code and simplify the function.
I removed some wrong setting of 'sort_and_group'.
This variable is set when alloc_group_fields() is called, as part
of allocating the cache needed by end_send_group() and does not need
to be set by other functions.
One problematic thing was that Spider is using *join->sum_funcs to detect
at which stage the optimizer is and do internal calculations of aggregate
functions. Updating join->sum_funcs early caused Spider to fail when trying
to find min/max values in opt_sum_query().
Fixed by temporarily resetting sum_funcs during opt_sum_query().
Reviewer: Sergei Petrunia
Diffstat (limited to 'mysql-test/main/group_min_max.test')
-rw-r--r-- | mysql-test/main/group_min_max.test | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/mysql-test/main/group_min_max.test b/mysql-test/main/group_min_max.test index 4622fe473a0..b1d912684c6 100644 --- a/mysql-test/main/group_min_max.test +++ b/mysql-test/main/group_min_max.test @@ -4,7 +4,7 @@ # --source include/default_optimizer_switch.inc - +--source include/have_innodb.inc # # TODO: # Add queries with: @@ -1700,3 +1700,23 @@ INSERT INTO t1 VALUES (0,100),(2,100),(2,101),(3,102); explain SELECT DISTINCT b FROM t1 WHERE EXISTS ( SELECT 1 FROM DUAL WHERE a > 1 ); SELECT DISTINCT b FROM t1 WHERE EXISTS ( SELECT 1 FROM DUAL WHERE a > 1 ); DROP TABLE t1; + +--echo # +--echo # MDEV-26585 Wrong query results when `using index for group-by` +--echo # + +CREATE TABLE `t1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `owner_id` int(11) DEFAULT NULL, + `foo` tinyint(1) DEFAULT 0, + `whatever` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `index_t1_on_owner_id_and_foo` (`owner_id`,`foo`) +) engine=InnoDB DEFAULT CHARSET=utf8; + +INSERT INTO t1 (owner_id, foo, whatever) +VALUES (1, TRUE, "yello"), (1, FALSE, "yello"), (2, TRUE, "yello"), + (2, TRUE, "yello"), (2, FALSE, "yello"); +EXPLAIN SELECT DISTINCT owner_id FROM t1 WHERE foo = true GROUP BY owner_id HAVING (COUNT(*) = 1); +SELECT DISTINCT owner_id FROM t1 WHERE foo = true GROUP BY owner_id HAVING (COUNT(*) = 1); +DROP TABLE t1; |