Bug #12882 min/max inconsistent on empty table
Submitted: 30 Aug 2005 15:11 Modified: 20 Jun 2010 22:47
Reporter: Martin Friebe (Gold Quality Contributor) (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:4.1.14, any OS:FreeBSD (freebsd, Windows)
Assigned to: CPU Architecture:Any

[30 Aug 2005 15:11] Martin Friebe
Description:
I have checked the documentation on http://dev.mysql.com/doc/mysql/en/group-by-functions.html

It does not describe, if min/max are expected to return null, or a value, on an empty set.

The examples below, show that min/max on a constant, return sometime the vqalue, sometime null.

Should that be consistent?

How to repeat:
create table t1 (a int); #empty table

select max(1) from DUAL;
+--------+
| max(1) |
+--------+
|   NULL |
+--------+
1 row in set (0.01 sec)

select max(1) from t1;
+--------+
| max(1) |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

Suggested fix:
- decide, if consitency, is wanted.
[30 Aug 2005 16:18] Valeriy Kravchuk
According to the SQL-2003 standard (draft):

"If no row qualifies, then the result of COUNT is 0 (zero), and the result of any other aggregate function is the null value."

mysql> create table t1 (a int);
Query OK, 0 rows affected (0.28 sec)

mysql> select max(1) from t1;
+--------+
| max(1) |
+--------+
|      1 |
+--------+
1 row in set (0.01 sec)

mysql> select count(1) from t1;
+----------+
| count(1) |
+----------+
|        0 |
+----------+
1 row in set (0.04 sec)

mysql> select max(1);
+--------+
| max(1) |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

So, the result of "select max(1) from t1;" in this case is a bug.
[31 Aug 2005 19:11] Paul DuBois
I'll update the group-by functions page to indicate what
each function returns when there are no matching rows,
for those descriptions that don't already say.
[14 Sep 2005 14:07] 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/internals/29842
[15 Sep 2005 13:48] 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/internals/29905
[17 Sep 2005 10:35] 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/internals/30007
[21 Sep 2005 6: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/internals/30132
[21 Sep 2005 15:09] Timour Katchaounov
Fixed in 4.1.15.
[23 Sep 2005 18:03] Paul DuBois
Noted in 4.1.15 changelog.
[24 Jul 2006 21:24] 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/9520
[5 May 2010 15:12] Bugs System
Pushed into 5.1.47 (revid:joro@sun.com-20100505145753-ivlt4hclbrjy8eye) (version source revid:vasil.dimov@oracle.com-20100331130613-8ja7n0vh36a80457) (merge vers: 5.1.46) (pib:16)
[6 May 2010 1:35] Paul DuBois
Push resulted from incorporation of InnoDB tree. No changes pertinent to this bug. Re-closing.
[28 May 2010 5:52] Bugs System
Pushed into mysql-next-mr (revid:alik@sun.com-20100524190136-egaq7e8zgkwb9aqi) (version source revid:vasil.dimov@oracle.com-20100331130613-8ja7n0vh36a80457) (pib:16)
[28 May 2010 6:21] Bugs System
Pushed into 6.0.14-alpha (revid:alik@sun.com-20100524190941-nuudpx60if25wsvx) (version source revid:vasil.dimov@oracle.com-20100331130613-8ja7n0vh36a80457) (merge vers: 5.1.46) (pib:16)
[28 May 2010 6:49] Bugs System
Pushed into 5.5.5-m3 (revid:alik@sun.com-20100524185725-c8k5q7v60i5nix3t) (version source revid:vasil.dimov@oracle.com-20100331130613-8ja7n0vh36a80457) (merge vers: 5.1.46) (pib:16)
[30 May 2010 1:03] Paul DuBois
Push resulted from incorporation of InnoDB tree. No changes pertinent to this bug.
Re-closing.
[17 Jun 2010 11:52] Bugs System
Pushed into 5.1.47-ndb-7.0.16 (revid:martin.skold@mysql.com-20100617114014-bva0dy24yyd67697) (version source revid:vasil.dimov@oracle.com-20100331130613-8ja7n0vh36a80457) (merge vers: 5.1.46) (pib:16)
[17 Jun 2010 12:30] Bugs System
Pushed into 5.1.47-ndb-6.2.19 (revid:martin.skold@mysql.com-20100617115448-idrbic6gbki37h1c) (version source revid:vasil.dimov@oracle.com-20100331130613-8ja7n0vh36a80457) (merge vers: 5.1.46) (pib:16)
[17 Jun 2010 13:18] Bugs System
Pushed into 5.1.47-ndb-6.3.35 (revid:martin.skold@mysql.com-20100617114611-61aqbb52j752y116) (version source revid:vasil.dimov@oracle.com-20100331130613-8ja7n0vh36a80457) (merge vers: 5.1.46) (pib:16)