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

Doc'd that Count("*") is equivalent to COUNT(*) SQL. #16803

Merged
merged 2 commits into from
Apr 27, 2023
Merged

Doc'd that Count("*") is equivalent to COUNT(*) SQL. #16803

merged 2 commits into from
Apr 27, 2023

Conversation

orf
Copy link
Sponsor Contributor

@orf orf commented Apr 26, 2023

I've always known that Count("*") is a valid shorthand for expression=Star(), but I'm surprised that it's not documented. This was added by #2496 / f59fd15 back in 2014.

If we do want to document this, because it is quite useful, then this seems like a simple way to do it. However I'm open to a larger snippet if needed?

@orf
Copy link
Sponsor Contributor Author

orf commented Apr 26, 2023

One other thing to possibly note here is what COUNT(*) actually does? Not sure if this is the right place for a detailed description, but I could add a note about how it differs in counting null values?

Mabe:

Count("*") is shorthand for Count(expression=Star()), which counts NULL values as opposed to Count() which does not

It's a tricky one to word though. Happy to keep it as-is and skip that.

@shangxiao
Copy link
Contributor

shangxiao commented Apr 26, 2023

Hi Tom,

Not sure if this is the right place for a detailed description, but I could add a note about how it differs in counting null values?

It's a nice idea but I reckon it's probably fine to leave it out… afterall this is an SQL fundamental (eg it's documented in the pg docs see below).

If anything I feel like explicitly stating that Count('*') yields the SQL expression "COUNT(*)" would close the gap for folks who are learning SQL and may not know what a "count(*)" even is :)… from here they can google "What is a count(*)?"

pg docs:

Most aggregate functions ignore null inputs, so that rows in which one or more of the expression(s) yield null are discarded. This can be assumed to be true, unless otherwise specified, for all built-in aggregates.

For example, count(*) yields the total number of input rows; count(f1) yields the number of input rows in which f1 is non-null, since count ignores nulls; and count(distinct f1) yields the number of distinct non-null values of f1.

https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-AGGREGATES

@orf
Copy link
Sponsor Contributor Author

orf commented Apr 26, 2023

Makes sense @shangxiao. I've reworded it slightly: Count('*') is shorthand for the SQL expression COUNT(*), how does that sound?

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
@shangxiao
Copy link
Contributor

@orf Good idea :) I was originally thinking of them both… but now that I think about it I'm not even sure if folks would ever even use the Star() object… so yep I reckon this is better 👍

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
@felixxm felixxm changed the title Document that Count("*") is an alias for Count(expression=Star()) Doc't that Count("*") is equivalent to COUNT(*) SQL. Apr 27, 2023
@felixxm felixxm changed the title Doc't that Count("*") is equivalent to COUNT(*) SQL. Doc'd that Count("*") is equivalent to COUNT(*) SQL. Apr 27, 2023
@felixxm felixxm merged commit 23d24f8 into django:main Apr 27, 2023
21 checks passed
@felixxm
Copy link
Member

felixxm commented Apr 27, 2023

@orf Thanks 👍

@orf orf deleted the document-count-star branch April 27, 2023 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants