Bug #48920 | COUNT DISTINCT returns 1 for NULL values when in a subquery in the select list | ||
---|---|---|---|
Submitted: | 19 Nov 2009 19:43 | Modified: | 7 Mar 2010 1:32 |
Reporter: | Patrick Crews | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
Version: | 6.0 | OS: | Any |
Assigned to: | Jørgen Løland | CPU Architecture: | Any |
Tags: | regression, SELECT subquery, subquery |
[19 Nov 2009 19:43]
Patrick Crews
[20 Nov 2009 16:10]
Patrick Crews
UPDATE: MySQL 6.0 (based on the codebase-bugfixing tree) is not in agreement with Postgres or JavaDB on this query, thus indicating that 6.0 is incorrect (it is off from 5.0, 5.1, javadb, and postgres)
[8 Dec 2009 14:43]
Øystein Grøvlen
6.0 result does not depend on optimizer_switch settings. Query plan is same for both 5.1 and 6.0: id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY <derived3> ALL NULL NULL NULL NULL 100 100.00 3 DERIVED D ALL NULL NULL NULL NULL 100 100.00 2 DEPENDENT SUBQUERY SUBQUERY4_t1 ALL varchar_key NULL NULL NULL 20 75.00 Using where 2 DEPENDENT SUBQUERY SUBQUERY4_t2 ALL NULL NULL NULL NULL 20 100.00 Using where; Using join buffer Warnings: Note 1276 Field or reference 'table1.varchar_nokey' of SELECT #2 was resolved in SELECT #1 Note 1003 select (select count(distinct `test`.`SUBQUERY4_t1`.`int_key`) AS `COUNT( DISTINCT SUBQUERY4_t1 .``int_key`` )` from `test`.`CC` `SUBQUERY4_t1` join `test`.`CC` `SUBQUERY4_t2` where ((`test`.`SUBQUERY4_t2`.`varchar_nokey` = `test`.`SUBQUERY4_t1`.`varchar_key`) and (`test`.`SUBQUERY4_t1`.`varchar_key` = `table1`.`varchar_nokey`))) AS `( SELECT COUNT( DISTINCT SUBQUERY4_t1 .``int_key`` ) FROM CC SUBQUERY4_t1 JOIN CC SUBQUERY4_t2 ON SUBQUERY4_t2 .``varchar_nokey`` = SUBQUERY4_t1 .``varchar_key`` WHERE SUBQUERY4_t2 .``varchar_nokey`` = table1 .``varchar_nokey`` )` from (select `test`.`D`.`int_key` AS `int_key`,`test`.`D`.`varchar_key` AS `varchar_key`,`test`.`D`.`varchar_nokey` AS `varchar_nokey` from `test`.`D`) `table1`
[21 Dec 2009 10:13]
Jørgen Løland
Reduced test case: ------------------ SET SESSION debug = 'd,optimizer_no_icp' ; CREATE TABLE t1 ( i int(11) DEFAULT NULL, v varchar(1) DEFAULT NULL, KEY i_key (i) ); INSERT INTO t1 VALUES (8,'v'); INSERT INTO t1 VALUES (9,'r'); INSERT INTO t1 VALUES (NULL,'y'); CREATE TABLE t2 ( i int(11) DEFAULT NULL, v varchar(1) DEFAULT NULL, KEY i_key (i) ); INSERT INTO t2 VALUES (NULL,'r'); INSERT INTO t2 VALUES (0,'c'); INSERT INTO t2 VALUES (0,'o'); INSERT INTO t2 VALUES (7,'c'); --echo Failing query without count: SELECT i, v, (SELECT DISTINCT i FROM t1 WHERE v = t2.v) as subsel FROM t2; --echo Failing query: SELECT i, v, (SELECT COUNT(DISTINCT i) FROM t1 WHERE v = t2.v) as subsel FROM t2; Output: ------- Failing query without count: SELECT i, v, (SELECT DISTINCT i FROM t1 WHERE v = t2.v) as subsel FROM t2; i v subsel NULL r 9 0 c NULL 0 o NULL 7 c NULL Failing query: SELECT i, v, (SELECT COUNT(DISTINCT i) FROM t1 WHERE v = t2.v) as subsel FROM t2; i v subsel NULL r 1 0 c 1 0 o 1 7 c 1 The problem: ------------ The count() function returns 1 for count(NULL), but correct output should be 0 like in 5.1: SELECT i, v, (SELECT COUNT(DISTINCT i) FROM t1 WHERE v = t2.v) as subsel FROM t2; i v subsel 0 c 0 0 o 0 7 c 0 This problem does not appear for other count(NULL)s: SELECT COUNT(NULL); COUNT(NULL) 0
[21 Dec 2009 10:15]
Jørgen Løland
Oops... cut'n paste error. Output from 5.1 also contains the "NULL record": SELECT i, v, (SELECT COUNT(DISTINCT i) FROM t1 WHERE v = t2.v) as subsel FROM t2; i v subsel NULL r 1 0 c 0 0 o 0 7 c 0
[22 Dec 2009 14:01]
Bugs System
A patch for this bug has been committed. After review, it may be pushed to the relevant source trees for release in the next version. You can access the patch from: http://lists.mysql.com/commits/95377 3792 Jorgen Loland 2009-12-22 Bug#48920: COUNT DISTINCT returns 1 for NULL values when in a subquery in the select list When a dependent subquery with count(distinct <col>) was evaluated multiple times, the Distinct_Aggregator was reused. However, the Aggregator was not reset, so when the subquery was evaluated for the next record in the outer select, old dependent info was used. The fix is to clear() the existing aggregator in Item_sum::set_aggregator(). This ensures that the aggregator is reevaluated with the new dependent information. if the agg @ mysql-test/r/subselect3.result Added test case for BUG#48920 @ mysql-test/r/subselect3_jcl6.result Added test case for BUG#48920 @ mysql-test/t/subselect3.test Added test case for BUG#48920 @ sql/item_sum.cc If an aggregator exists when Item_sum::set_aggregator() is called (i.e., set_aggregator is called in a dependent subquery), the aggregator is reset so that the aggregator is reevaluated with the dependent information from the outer record being evaluated.
[23 Dec 2009 8:36]
Tor Didriksen
You should commit it to next-mr-bugfixing.
[4 Jan 2010 9:40]
Bugs System
A patch for this bug has been committed. After review, it may be pushed to the relevant source trees for release in the next version. You can access the patch from: http://lists.mysql.com/commits/95868 2955 Jorgen Loland 2010-01-04 Bug#48920: COUNT DISTINCT returns 1 for NULL values when in a subquery in the select list When a dependent subquery with count(distinct <col>) was evaluated multiple times, the Distinct_Aggregator was reused. However, the Aggregator was not reset, so when the subquery was evaluated for the next record in the outer select, old dependent info was used. The fix is to clear() the existing aggregator in Item_sum::set_aggregator(). This ensures that the aggregator is reevaluated with the new dependent information. @ mysql-test/r/subselect3.result Added test case for BUG#48920 @ mysql-test/t/subselect3.test Added test case for BUG#48920 @ sql/item_sum.cc If an aggregator exists when Item_sum::set_aggregator() is called (i.e., set_aggregator is called in a dependent subquery), the aggregator is reset so that the aggregator is reevaluated with the dependent information from the outer record being evaluated.
[4 Jan 2010 9:49]
Bugs System
A patch for this bug has been committed. After review, it may be pushed to the relevant source trees for release in the next version. You can access the patch from: http://lists.mysql.com/commits/95871 3805 Jorgen Loland 2010-01-04 [merge] Merge BUG#48920 from next-mr-bugfixing to 6.0-codebase-bugfixing
[4 Jan 2010 9:51]
Jørgen Løland
Pushed to mysql-next-mr-bugfixing and merged to mysql-6.0-codebase-bugfixing
[12 Jan 2010 16:25]
Bugs System
Pushed into 6.0.14-alpha (revid:alik@sun.com-20100112162328-2sblcul1kl08bbib) (version source revid:guilhem@mysql.com-20100108092756-k0zzf4kvx9b7bh38) (merge vers: 6.0.14-alpha) (pib:15)
[12 Jan 2010 16:25]
Bugs System
Pushed into mysql-next-mr (revid:alik@sun.com-20100112162142-nrjoxn6h2nrnh2o0) (version source revid:guilhem@mysql.com-20100108092756-k0zzf4kvx9b7bh38) (pib:15)
[23 Jan 2010 0:28]
Paul DuBois
Noted in 6.0.14 changelog. A dependent subquery containing COUNT(DISTINCT col_name)) could be evaluated incorrectly. Setting report to NDI pending push to Celosia.
[6 Mar 2010 10:57]
Bugs System
Pushed into 5.5.3-m3 (revid:alik@sun.com-20100306103849-hha31z2enhh7jwt3) (version source revid:guilhem@mysql.com-20100108092756-k0zzf4kvx9b7bh38) (merge vers: 6.0.14-alpha) (pib:16)
[7 Mar 2010 1:32]
Paul DuBois
Noted in 5.5.3 changelog.