[MDEV-12561] Wrong result with SELECT subquery, GROUP_CONCAT and GROUP BY Created: 2017-04-22  Updated: 2017-05-19  Resolved: 2017-05-19

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.1, 10.2
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Oleksandr Byelkin
Resolution: Duplicate Votes: 0
Labels: regression

Sprint: 10.1.24

 Description   

TEST CASE

CREATE TABLE t1 (pk INT PRIMARY KEY, i INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
 
SELECT
   i,
  ( SELECT COUNT(*) FROM t1 WHERE outer_t1.i IN ( SELECT pk FROM t1 ) ) AS cnt
FROM t1 AS outer_t1;
 
SELECT
  ( SELECT COUNT(*) FROM t1 WHERE outer_t1.i IN ( SELECT pk FROM t1 ) ) AS cnt,
  GROUP_CONCAT(i)
FROM t1 AS outer_t1
GROUP BY cnt;
 
DROP TABLE t1;

On 10.1 before revision 8d4871a95340dc, the first query returned the correct result, but the second query did not:

10.1 cd494f4cefb36f (old)

MariaDB [test]> SELECT
    ->    i,
    ->   ( SELECT COUNT(*) FROM t1 WHERE outer_t1.i IN ( SELECT pk FROM t1 ) ) AS cnt
    -> FROM t1 AS outer_t1;
+------+------+
| i    | cnt  |
+------+------+
|    1 |    3 |
|    2 |    3 |
|    3 |    3 |
+------+------+
3 rows in set (0.00 sec)

MariaDB [test]> SELECT
    ->   ( SELECT COUNT(*) FROM t1 WHERE outer_t1.i IN ( SELECT pk FROM t1 ) ) AS cnt,
    ->   GROUP_CONCAT(i)
    -> FROM t1 AS outer_t1
    -> GROUP BY cnt;
+------+-----------------+
| cnt  | GROUP_CONCAT(i) |
+------+-----------------+
|    0 | 1,2,3           |
+------+-----------------+
1 row in set (0.00 sec)

(note 0 in the cnt field, it should be 3).

Now the result for the 2nd query is different, but also wrong:

10.1 3bb32e8682f849 (new)

MariaDB [test]> SELECT
    ->   ( SELECT COUNT(*) FROM t1 WHERE outer_t1.i IN ( SELECT pk FROM t1 ) ) AS cnt,
    ->   GROUP_CONCAT(i)
    -> FROM t1 AS outer_t1
    -> GROUP BY cnt;
+------+-----------------+
| cnt  | GROUP_CONCAT(i) |
+------+-----------------+
|    3 | 1               |
|    3 | 2,3             |
+------+-----------------+
2 rows in set (0.00 sec)

Naturally, there shouldn't be two rows with cnt=3.

However, on 10.2 until recently the result was correct:

10.2.5

MariaDB [test]> SELECT
    ->   ( SELECT COUNT(*) FROM t1 WHERE outer_t1.i IN ( SELECT pk FROM t1 ) ) AS cnt,
    ->   GROUP_CONCAT(i)
    -> FROM t1 AS outer_t1
    -> GROUP BY cnt;
+------+-----------------+
| cnt  | GROUP_CONCAT(i) |
+------+-----------------+
|    3 | 1,2,3           |
+------+-----------------+
1 row in set (0.00 sec)

But now it is not:

10.2 54a995cd2206 (new)

MariaDB [test]> SELECT
    ->   ( SELECT COUNT(*) FROM t1 WHERE outer_t1.i IN ( SELECT pk FROM t1 ) ) AS cnt,
    ->   GROUP_CONCAT(i)
    -> FROM t1 AS outer_t1
    -> GROUP BY cnt;
+------+-----------------+
| cnt  | GROUP_CONCAT(i) |
+------+-----------------+
|    3 | 1               |
|    3 | 2,3             |
+------+-----------------+
2 rows in set (0.00 sec)

So, for 10.2 it's certainly a regression.

The difference appeared after merges, likely from this change in 10.0:

commit 57a699b0a0f3300404948775356d31fb478e80c6
Author: Oleksandr Byelkin <sanja@mariadb.com>
Date:   Fri Jun 17 16:51:11 2016 +0200
 
    MDEV-8642: WHERE Clause not applied on View - Empty result set returned
    
    An attempt to mark reference as dependent lead to transfering this property to
    original view field and through it to other references of this field which
    can't be dependent.

Although in 10.0, at least for the test case above, it did good.



 Comments   
Comment by Oleksandr Byelkin [ 2017-05-19 ]

following patch must fix it

b0395d8701ec49f49ad23f9917a3b2369bb49e7a is the first bad commit
commit b0395d8701ec49f49ad23f9917a3b2369bb49e7a
Author: Igor Babaev <igor@askmonty.org>
Date: Tue Apr 4 10:04:52 2017 -0700

Fixed the bug mdev-12429 and its duplicates mdev-12145 and mdev-9886.

Also fixed a wrong result for a test case for mdev-7691
(the alternative one).
The test cases for all these bug have materialized semi-joins used
inside dependent sub-queries.

The patch actually reverts the change inroduced by Monty in 2003.
It looks like this change is not valid anymore after the implementation
of semi-joins.
Adjusted output from EXPLAIN for many other test cases.

Generated at Thu Feb 08 07:58:40 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.