From 3c7a53150220561bb83bca6b7a15c98633c40278 Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Wed, 12 Aug 2026 17:16:56 -0400 Subject: [PATCH 1/8] Add failing tests to olap.test --- mysql-test/main/olap.result | 22 ++++++++++++++++++++++ mysql-test/main/olap.test | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/mysql-test/main/olap.result b/mysql-test/main/olap.result index 22186ee085c5b..964df991aef17 100644 --- a/mysql-test/main/olap.result +++ b/mysql-test/main/olap.result @@ -905,3 +905,25 @@ NULL NULL DROP TABLE t1; # End of 10.3 Tests +# +# MDEV-40698: ROLLUP query returns incorrect result with empty result set +# +CREATE TABLE t0(x INT); +SELECT * FROM t0 GROUP BY x WITH ROLLUP; +x +NULL +SELECT * FROM t0 WHERE 1=0 GROUP BY x WITH ROLLUP; +x +NULL +SELECT *, ROW_NUMBER() OVER () FROM t0 GROUP BY x WITH ROLLUP; +x ROW_NUMBER() OVER () +NULL 1 +INSERT INTO t0 VALUES (0); +SELECT EXISTS (SELECT t0.x FROM t0 a WHERE 1=0 GROUP BY a.x WITH ROLLUP) AS e +FROM t0; +e +1 +DROP TABLE t0; +# +# End of 10.11 tests +# diff --git a/mysql-test/main/olap.test b/mysql-test/main/olap.test index 3b4852ebbdceb..1d8d8953c19f9 100644 --- a/mysql-test/main/olap.test +++ b/mysql-test/main/olap.test @@ -533,3 +533,21 @@ SELECT NULLIF( CAST( 'foo' AS DATE ), NULL & 'bar' ) AS f FROM t1 GROUP BY f WIT DROP TABLE t1; --echo # End of 10.3 Tests + +--echo # +--echo # MDEV-40698: ROLLUP query returns incorrect result with empty result set +--echo # + +CREATE TABLE t0(x INT); +SELECT * FROM t0 GROUP BY x WITH ROLLUP; +SELECT * FROM t0 WHERE 1=0 GROUP BY x WITH ROLLUP; +SELECT *, ROW_NUMBER() OVER () FROM t0 GROUP BY x WITH ROLLUP; + +INSERT INTO t0 VALUES (0); +SELECT EXISTS (SELECT t0.x FROM t0 a WHERE 1=0 GROUP BY a.x WITH ROLLUP) AS e + FROM t0; +DROP TABLE t0; + +--echo # +--echo # End of 10.11 tests +--echo # From 540ea098acf388a774a592833d262738fe09c1e0 Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Wed, 12 Aug 2026 17:28:08 -0400 Subject: [PATCH 2/8] Make the approprate changes to send_row_on_empty_set and empty_result --- sql/sql_select.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/sql/sql_select.h b/sql/sql_select.h index 1c607883be91e..d03edab058186 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -1746,12 +1746,27 @@ class JOIN :public Sql_alloc void cleanup(bool full); void clear(table_map *cleared_tables); void inline clear_sum_funcs(); + bool need_empty_set_row(bool end_of_records, + bool *empty_set_send_rollup_total) const + { + *empty_set_send_rollup_total= + end_of_records && !first_record && rollup.state != ROLLUP::STATE_NONE; + return (end_of_records && !first_record && !group && + !group_optimized_away) || + *empty_set_send_rollup_total; + } bool send_row_on_empty_set() { - return (do_send_rows && implicit_grouping && !group_optimized_away && + return (do_send_rows && + (select_lex->olap == ROLLUP_TYPE || + (implicit_grouping && !group_optimized_away)) && having_value != Item::COND_FALSE); } - bool empty_result() { return (zero_result_cause && !implicit_grouping); } + bool empty_result() + { + return (zero_result_cause && !implicit_grouping && + select_lex->olap != ROLLUP_TYPE); + } bool change_result(select_result *new_result, select_result *old_result); bool is_top_level_join() const { From 494b5f8be5ea654d27a4cb9a3525274a9c9b32b6 Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Wed, 12 Aug 2026 19:22:45 -0400 Subject: [PATCH 3/8] Correctly handle both empty-set send cases in end_send_group and end_write_group --- sql/sql_select.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 05d3bae186c8b..36ac4367bbb47 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -24496,6 +24496,7 @@ end_send_group(JOIN *join, JOIN_TAB *join_tab, bool end_of_records) join->fields. */ List *fields= join_tab ? (join_tab-1)->fields : join->fields; + bool empty_set_send_rollup_total= 0; DBUG_ENTER("end_send_group"); if (!join->items3.is_null() && !join->set_group_rpa) @@ -24511,7 +24512,8 @@ end_send_group(JOIN *join, JOIN_TAB *join_tab, bool end_of_records) if (!join->group_sent && (join->first_record || - (end_of_records && !join->group && !join->group_optimized_away))) + join->need_empty_set_row(end_of_records, + &empty_set_send_rollup_total))) { table_map cleared_tables= (table_map) 0; if (join->procedure) @@ -24567,7 +24569,8 @@ end_send_group(JOIN *join, JOIN_TAB *join_tab, bool end_of_records) join->send_records++; join->group_sent= true; } - if (unlikely(join->rollup.state != ROLLUP::STATE_NONE && error <= 0)) + if (unlikely(join->rollup.state != ROLLUP::STATE_NONE && + !empty_set_send_rollup_total && error <= 0)) { if (join->rollup_send_data((uint) (idx+1))) error= 1; @@ -24888,13 +24891,15 @@ end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), { TABLE *table= join_tab->table; int idx= -1; + bool empty_set_send_rollup_total= 0; DBUG_ENTER("end_write_group"); join->accepted_rows++; if (!join->first_record || end_of_records || (idx=test_if_group_changed(join->group_fields)) >= 0) { - if (join->first_record || (end_of_records && !join->group)) + if (join->first_record || + join->need_empty_set_row(end_of_records, &empty_set_send_rollup_total)) { table_map cleared_tables= (table_map) 0; if (join->procedure) @@ -24919,7 +24924,8 @@ end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), error, 0, NULL)) DBUG_RETURN(NESTED_LOOP_ERROR); } - if (unlikely(join->rollup.state != ROLLUP::STATE_NONE)) + if (unlikely(join->rollup.state != ROLLUP::STATE_NONE && + !empty_set_send_rollup_total)) { if (unlikely(join->rollup_write_data((uint) (idx+1), join_tab->tmp_table_param, From a2ebe5957d487eef3d00af087901a0af7b1cd241 Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Wed, 12 Aug 2026 19:23:58 -0400 Subject: [PATCH 4/8] Commit new corrected test cases --- mysql-test/main/olap.result | 41 +++++++++++++++++++++++++------------ mysql-test/main/olap.test | 26 +++++++++++++++-------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/mysql-test/main/olap.result b/mysql-test/main/olap.result index 964df991aef17..48918efa42b02 100644 --- a/mysql-test/main/olap.result +++ b/mysql-test/main/olap.result @@ -908,22 +908,37 @@ DROP TABLE t1; # # MDEV-40698: ROLLUP query returns incorrect result with empty result set # -CREATE TABLE t0(x INT); -SELECT * FROM t0 GROUP BY x WITH ROLLUP; -x -NULL -SELECT * FROM t0 WHERE 1=0 GROUP BY x WITH ROLLUP; -x -NULL -SELECT *, ROW_NUMBER() OVER () FROM t0 GROUP BY x WITH ROLLUP; -x ROW_NUMBER() OVER () -NULL 1 -INSERT INTO t0 VALUES (0); -SELECT EXISTS (SELECT t0.x FROM t0 a WHERE 1=0 GROUP BY a.x WITH ROLLUP) AS e +CREATE TABLE t0(a INT, b INT, c INT); +SELECT *, COUNT(0) FROM t0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0) FROM t0 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) ROW_NUMBER() OVER () +NULL NULL NULL 0 1 +INSERT INTO t0 VALUES (0, 0, 0); +SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t0; e 1 -DROP TABLE t0; +CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; +SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0) FROM t1 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) ROW_NUMBER() OVER () +NULL NULL NULL 0 1 +INSERT INTO t1 VALUES (0, 0, 0); +SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e +FROM t1; +e +1 +DROP TABLE t0, t1; # # End of 10.11 tests # diff --git a/mysql-test/main/olap.test b/mysql-test/main/olap.test index 1d8d8953c19f9..023cba084efdd 100644 --- a/mysql-test/main/olap.test +++ b/mysql-test/main/olap.test @@ -1,3 +1,5 @@ +--source include/have_innodb.inc + set @sav_dpi= @@div_precision_increment; set div_precision_increment= 5; show variables like 'div_precision_increment'; @@ -538,15 +540,23 @@ DROP TABLE t1; --echo # MDEV-40698: ROLLUP query returns incorrect result with empty result set --echo # -CREATE TABLE t0(x INT); -SELECT * FROM t0 GROUP BY x WITH ROLLUP; -SELECT * FROM t0 WHERE 1=0 GROUP BY x WITH ROLLUP; -SELECT *, ROW_NUMBER() OVER () FROM t0 GROUP BY x WITH ROLLUP; - -INSERT INTO t0 VALUES (0); -SELECT EXISTS (SELECT t0.x FROM t0 a WHERE 1=0 GROUP BY a.x WITH ROLLUP) AS e +CREATE TABLE t0(a INT, b INT, c INT); +SELECT *, COUNT(0) FROM t0 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0) FROM t0 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; +INSERT INTO t0 VALUES (0, 0, 0); +SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t0; -DROP TABLE t0; + +CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; +SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0) FROM t1 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; +INSERT INTO t1 VALUES (0, 0, 0); +SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e + FROM t1; + +DROP TABLE t0, t1; --echo # --echo # End of 10.11 tests From 2addac94752fb823da93ad942c6c8e20075f4cf8 Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Wed, 12 Aug 2026 20:51:18 -0400 Subject: [PATCH 5/8] Add runtime-evaluated WHERE cases to olap tests --- mysql-test/main/olap.result | 10 ++++++++++ mysql-test/main/olap.test | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/mysql-test/main/olap.result b/mysql-test/main/olap.result index 48918efa42b02..db3e83996d475 100644 --- a/mysql-test/main/olap.result +++ b/mysql-test/main/olap.result @@ -919,10 +919,15 @@ SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; a b c COUNT(0) ROW_NUMBER() OVER () NULL NULL NULL 0 1 INSERT INTO t0 VALUES (0, 0, 0); +INSERT INTO t0 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t0 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t0; e 1 +1 CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; a b c COUNT(0) @@ -934,10 +939,15 @@ SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; a b c COUNT(0) ROW_NUMBER() OVER () NULL NULL NULL 0 1 INSERT INTO t1 VALUES (0, 0, 0); +INSERT INTO t1 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t1 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t1; e 1 +1 DROP TABLE t0, t1; # # End of 10.11 tests diff --git a/mysql-test/main/olap.test b/mysql-test/main/olap.test index 023cba084efdd..26f4230170cbb 100644 --- a/mysql-test/main/olap.test +++ b/mysql-test/main/olap.test @@ -545,6 +545,8 @@ SELECT *, COUNT(0) FROM t0 GROUP BY a, b, c WITH ROLLUP; SELECT *, COUNT(0) FROM t0 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; INSERT INTO t0 VALUES (0, 0, 0); +INSERT INTO t0 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t0 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t0; @@ -553,6 +555,8 @@ SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; SELECT *, COUNT(0) FROM t1 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; INSERT INTO t1 VALUES (0, 0, 0); +INSERT INTO t1 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t1 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t1; From 59656d775b183dd75e278021160fa93cfcd1a49c Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Wed, 12 Aug 2026 20:53:35 -0400 Subject: [PATCH 6/8] Move tests into separate file --- mysql-test/main/mdev_40698.result | 47 +++++++++++++++++++++++++++++++ mysql-test/main/mdev_40698.test | 33 ++++++++++++++++++++++ mysql-test/main/olap.result | 47 ------------------------------- mysql-test/main/olap.test | 32 --------------------- 4 files changed, 80 insertions(+), 79 deletions(-) create mode 100644 mysql-test/main/mdev_40698.result create mode 100644 mysql-test/main/mdev_40698.test diff --git a/mysql-test/main/mdev_40698.result b/mysql-test/main/mdev_40698.result new file mode 100644 index 0000000000000..e24bc8e12ca04 --- /dev/null +++ b/mysql-test/main/mdev_40698.result @@ -0,0 +1,47 @@ +# +# MDEV-40698: ROLLUP query returns incorrect result with empty result set +# +CREATE TABLE t0(a INT, b INT, c INT); +SELECT *, COUNT(0) FROM t0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0) FROM t0 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) ROW_NUMBER() OVER () +NULL NULL NULL 0 1 +INSERT INTO t0 VALUES (0, 0, 0); +INSERT INTO t0 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t0 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e +FROM t0; +e +1 +1 +CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; +SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0) FROM t1 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) ROW_NUMBER() OVER () +NULL NULL NULL 0 1 +INSERT INTO t1 VALUES (0, 0, 0); +INSERT INTO t1 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t1 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; +a b c COUNT(0) +NULL NULL NULL 0 +SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e +FROM t1; +e +1 +1 +DROP TABLE t0, t1; +# +# End of 10.11 tests +# diff --git a/mysql-test/main/mdev_40698.test b/mysql-test/main/mdev_40698.test new file mode 100644 index 0000000000000..c87b755112421 --- /dev/null +++ b/mysql-test/main/mdev_40698.test @@ -0,0 +1,33 @@ +--source include/have_innodb.inc + +--echo # +--echo # MDEV-40698: ROLLUP query returns incorrect result with empty result set +--echo # + +CREATE TABLE t0(a INT, b INT, c INT); +SELECT *, COUNT(0) FROM t0 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0) FROM t0 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; +INSERT INTO t0 VALUES (0, 0, 0); +INSERT INTO t0 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t0 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; +SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e + FROM t0; +SELECT DISTINCT a FROM t1 GROUP BY a, b WITH ROLLUP + +CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; +SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0) FROM t1 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; +SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; +INSERT INTO t1 VALUES (0, 0, 0); +INSERT INTO t1 VALUES (0, 0, 0); +SELECT *, COUNT(0) FROM t1 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; +SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e + FROM t1; +SELECT DISTINCT a FROM t1 GROUP BY a, b WITH ROLLUP + +DROP TABLE t0, t1; + +--echo # +--echo # End of 10.11 tests +--echo # diff --git a/mysql-test/main/olap.result b/mysql-test/main/olap.result index db3e83996d475..22186ee085c5b 100644 --- a/mysql-test/main/olap.result +++ b/mysql-test/main/olap.result @@ -905,50 +905,3 @@ NULL NULL DROP TABLE t1; # End of 10.3 Tests -# -# MDEV-40698: ROLLUP query returns incorrect result with empty result set -# -CREATE TABLE t0(a INT, b INT, c INT); -SELECT *, COUNT(0) FROM t0 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) -NULL NULL NULL 0 -SELECT *, COUNT(0) FROM t0 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) -NULL NULL NULL 0 -SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) ROW_NUMBER() OVER () -NULL NULL NULL 0 1 -INSERT INTO t0 VALUES (0, 0, 0); -INSERT INTO t0 VALUES (0, 0, 0); -SELECT *, COUNT(0) FROM t0 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) -NULL NULL NULL 0 -SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e -FROM t0; -e -1 -1 -CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; -SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) -NULL NULL NULL 0 -SELECT *, COUNT(0) FROM t1 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) -NULL NULL NULL 0 -SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) ROW_NUMBER() OVER () -NULL NULL NULL 0 1 -INSERT INTO t1 VALUES (0, 0, 0); -INSERT INTO t1 VALUES (0, 0, 0); -SELECT *, COUNT(0) FROM t1 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; -a b c COUNT(0) -NULL NULL NULL 0 -SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e -FROM t1; -e -1 -1 -DROP TABLE t0, t1; -# -# End of 10.11 tests -# diff --git a/mysql-test/main/olap.test b/mysql-test/main/olap.test index 26f4230170cbb..3b4852ebbdceb 100644 --- a/mysql-test/main/olap.test +++ b/mysql-test/main/olap.test @@ -1,5 +1,3 @@ ---source include/have_innodb.inc - set @sav_dpi= @@div_precision_increment; set div_precision_increment= 5; show variables like 'div_precision_increment'; @@ -535,33 +533,3 @@ SELECT NULLIF( CAST( 'foo' AS DATE ), NULL & 'bar' ) AS f FROM t1 GROUP BY f WIT DROP TABLE t1; --echo # End of 10.3 Tests - ---echo # ---echo # MDEV-40698: ROLLUP query returns incorrect result with empty result set ---echo # - -CREATE TABLE t0(a INT, b INT, c INT); -SELECT *, COUNT(0) FROM t0 GROUP BY a, b, c WITH ROLLUP; -SELECT *, COUNT(0) FROM t0 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; -SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t0 GROUP BY a, b, c WITH ROLLUP; -INSERT INTO t0 VALUES (0, 0, 0); -INSERT INTO t0 VALUES (0, 0, 0); -SELECT *, COUNT(0) FROM t0 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; -SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e - FROM t0; - -CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; -SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; -SELECT *, COUNT(0) FROM t1 WHERE 1=0 GROUP BY a, b, c WITH ROLLUP; -SELECT *, COUNT(0), ROW_NUMBER() OVER () FROM t1 GROUP BY a, b, c WITH ROLLUP; -INSERT INTO t1 VALUES (0, 0, 0); -INSERT INTO t1 VALUES (0, 0, 0); -SELECT *, COUNT(0) FROM t1 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; -SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e - FROM t1; - -DROP TABLE t0, t1; - ---echo # ---echo # End of 10.11 tests ---echo # From b1d9ad7aa27d279e21d4b40195019f486107e3ad Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Wed, 12 Aug 2026 20:55:25 -0400 Subject: [PATCH 7/8] Add distinct case --- mysql-test/main/mdev_40698.result | 8 ++++++++ mysql-test/main/mdev_40698.test | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mysql-test/main/mdev_40698.result b/mysql-test/main/mdev_40698.result index e24bc8e12ca04..cf91f431df294 100644 --- a/mysql-test/main/mdev_40698.result +++ b/mysql-test/main/mdev_40698.result @@ -21,6 +21,10 @@ FROM t0; e 1 1 +SELECT DISTINCT a FROM t0 GROUP BY a, b WITH ROLLUP; +a +0 +NULL CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; a b c COUNT(0) @@ -41,6 +45,10 @@ FROM t1; e 1 1 +SELECT DISTINCT a FROM t1 GROUP BY a, b WITH ROLLUP; +a +0 +NULL DROP TABLE t0, t1; # # End of 10.11 tests diff --git a/mysql-test/main/mdev_40698.test b/mysql-test/main/mdev_40698.test index c87b755112421..482d095a48341 100644 --- a/mysql-test/main/mdev_40698.test +++ b/mysql-test/main/mdev_40698.test @@ -13,7 +13,7 @@ INSERT INTO t0 VALUES (0, 0, 0); SELECT *, COUNT(0) FROM t0 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; SELECT EXISTS (SELECT t0.a FROM t0 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t0; -SELECT DISTINCT a FROM t1 GROUP BY a, b WITH ROLLUP +SELECT DISTINCT a FROM t0 GROUP BY a, b WITH ROLLUP; CREATE TABLE t1 (a INT, b INT, c INT) ENGINE=InnoDB; SELECT *, COUNT(0) FROM t1 GROUP BY a, b, c WITH ROLLUP; @@ -24,7 +24,7 @@ INSERT INTO t1 VALUES (0, 0, 0); SELECT *, COUNT(0) FROM t1 WHERE a > 0 GROUP BY a, b, c WITH ROLLUP; SELECT EXISTS (SELECT t1.a FROM t1 x WHERE 1=0 GROUP BY x.a WITH ROLLUP) AS e FROM t1; -SELECT DISTINCT a FROM t1 GROUP BY a, b WITH ROLLUP +SELECT DISTINCT a FROM t1 GROUP BY a, b WITH ROLLUP; DROP TABLE t0, t1; From f83223d4dab8d3dea48f1a94bf36eba7e69b85c9 Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Thu, 13 Aug 2026 03:35:10 -0400 Subject: [PATCH 8/8] Fix incorrect prior tests --- mysql-test/main/ps_error.result | 1 + mysql-test/main/win.result | 1 + mysql-test/suite/encryption/r/tempfiles_encrypted.result | 1 + 3 files changed, 3 insertions(+) diff --git a/mysql-test/main/ps_error.result b/mysql-test/main/ps_error.result index ad178f869155a..f3fa78a1bbe36 100644 --- a/mysql-test/main/ps_error.result +++ b/mysql-test/main/ps_error.result @@ -10,6 +10,7 @@ EXECUTE stmt; ERROR 22007: Truncated incorrect DOUBLE value: 'foo' SELECT a FROM t1 GROUP BY NULL WITH ROLLUP; a +NULL DROP TABLE t1; SET sql_mode=DEFAULT; SET SQL_MODE= 'STRICT_ALL_TABLES'; diff --git a/mysql-test/main/win.result b/mysql-test/main/win.result index 8f38c424ddf9c..7891c737dfd43 100644 --- a/mysql-test/main/win.result +++ b/mysql-test/main/win.result @@ -3503,6 +3503,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables select distinct 1, row_number() over (order by 1) from t1 where a=0 group by a with rollup; 1 row_number() over (order by 1) +1 1 drop table t1; explain SELECT DISTINCT BIT_OR(100) OVER () FROM dual diff --git a/mysql-test/suite/encryption/r/tempfiles_encrypted.result b/mysql-test/suite/encryption/r/tempfiles_encrypted.result index aff2b9306aa73..f856ff1a465f1 100644 --- a/mysql-test/suite/encryption/r/tempfiles_encrypted.result +++ b/mysql-test/suite/encryption/r/tempfiles_encrypted.result @@ -3509,6 +3509,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables select distinct 1, row_number() over (order by 1) from t1 where a=0 group by a with rollup; 1 row_number() over (order by 1) +1 1 drop table t1; explain SELECT DISTINCT BIT_OR(100) OVER () FROM dual