Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIN and MAX return data from WHERE clause on empty input #8911

Closed
tv42 opened this issue Jan 19, 2024 · 2 comments · Fixed by #8914
Closed

MIN and MAX return data from WHERE clause on empty input #8911

tv42 opened this issue Jan 19, 2024 · 2 comments · Fixed by #8914
Labels
bug Something isn't working

Comments

@tv42
Copy link
Contributor

tv42 commented Jan 19, 2024

Describe the bug

Calling MIN or MAX on an empty input values (or one with all-NULL values) should return NULL.

Quoting https://www.postgresql.org/docs/16/functions-aggregate.html

It should be noted that except for count, these functions return a null value when no rows are selected.

To Reproduce

CREATE TABLE mytable(col0 INTEGER);
0 rows in set. Query took 0.024 seconds.SELECT * FROM mytable;
0 rows in set. Query took 0.005 seconds.SELECT MIN(col0) FROM mytable WHERE col0=1;
+-------------------+
| MIN(mytable.col0) |
+-------------------+
| 1                 |
+-------------------+
1 row in set. Query took 0.013 seconds.SELECT MIN(col0) FROM mytable WHERE col0=2;
+-------------------+
| MIN(mytable.col0) |
+-------------------+
| 2                 |
+-------------------+
1 row in set. Query took 0.012 seconds.SELECT MIN(col0) FROM mytable WHERE col0=3;
+-------------------+
| MIN(mytable.col0) |
+-------------------+
| 3                 |
+-------------------+
1 row in set. Query took 0.013 seconds.SELECT MAX(col0) FROM mytable WHERE col0=1;
+-------------------+
| MAX(mytable.col0) |
+-------------------+
| 1                 |
+-------------------+
1 row in set. Query took 0.013 seconds.SELECT MAX(col0) FROM mytable WHERE col0=2;
+-------------------+
| MAX(mytable.col0) |
+-------------------+
| 2                 |
+-------------------+
1 row in set. Query took 0.005 seconds.SELECT MAX(col0) FROM mytable WHERE col0=3;
+-------------------+
| MAX(mytable.col0) |
+-------------------+
| 3                 |
+-------------------+
1 row in set. Query took 0.013 seconds.

Expected behavior

NULL returned.

Compare to SQLite:

sqlite> CREATE TABLE mytable(col0 INTEGER);
sqlite> SELECT MIN(col0) FROM mytable WHERE col0=1;
NULL
sqlite> SELECT MIN(col0) FROM mytable WHERE col0=2;
NULL
sqlite> SELECT MIN(col0) FROM mytable WHERE col0=3;
NULL
sqlite> SELECT MAX(col0) FROM mytable WHERE col0=1;
NULL
sqlite> SELECT MAX(col0) FROM mytable WHERE col0=2;
NULL
sqlite> SELECT MAX(col0) FROM mytable WHERE col0=3;
NULL

Compare to Postgres:

postgres=# \pset null 'NULL'
Null display is "NULL".
postgres=# CREATE TABLE mytable(col0 INTEGER);
CREATE TABLE
postgres=# SELECT MIN(col0) FROM mytable WHERE col0=1;
 min
------
 NULL
(1 row)

postgres=# SELECT MIN(col0) FROM mytable WHERE col0=2;
 min
------
 NULL
(1 row)

postgres=# SELECT MIN(col0) FROM mytable WHERE col0=3;
 min
------
 NULL
(1 row)

postgres=# SELECT MAX(col0) FROM mytable WHERE col0=1;
 max
------
 NULL
(1 row)

postgres=# SELECT MAX(col0) FROM mytable WHERE col0=2;
 max
------
 NULL
(1 row)

postgres=# SELECT MAX(col0) FROM mytable WHERE col0=3;
 max
------
 NULL
(1 row)

Additional context

No response

@tv42 tv42 added the bug Something isn't working label Jan 19, 2024
@alamb
Copy link
Contributor

alamb commented Jan 19, 2024

I agree this is a bug. Thanks for filing @tv42

@alamb
Copy link
Contributor

alamb commented Jan 21, 2024

Bam! Open/closed in ~ 24 hours 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants