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

Address aggregate functions and typing behavior when there is/isn't a group #4530

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eygraber
Copy link
Contributor

Came across a case in one of my projects where I was using COUNT without a group, and had other columns in the projection. If the table is empty, the other column will be null even if it is a non nullable column:

CREATE TABLE test (
  id INTEGER NOT NULL
);

-- returns null | 0
SELECT id, COUNT(*) FROM test;

While testing this, I also found that functions like MAX will not return null for MAX if there is a GROUP BY specified even if the table is empty. Current behavior always sets MAX to nullable. The Sqlite docs are a little tricky because they specify that:

Aggregate max() returns NULL if and only if there are no non-NULL values in the group

which means that if there are no values in the group, it won't return null (instead the result set is empty). Postgres (v15) and MySQL (v8) seems to have the same behavior.

-- null
SELECT MAX(id) FROM test;

--empty
SELECT MAX(id) FROM test GROUP BY id;

--empty
SELECT id, MAX(id) FROM test GROUP BY id;

-- Error in Postgres and MySQL
-- null | null on Sqlite
SELECT id, MAX(id) FROM test;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant