Re:The Curious Case of the Aggregation Query
by derefr on Tuesday December 05, 2023 @12:05PM
(#38533624)
What's an aggregation per se? A SQL query is best thought of an arbitrary generator function. You can, using functions like UNNEST, end up emitting multiple row-tuples for each processed input row-tuple. An aggregation is just a generator function that happens to reduce over all the input rows and then emit one row-tuple when there's no more input. Query-planning engines do not special-case this. It's just a generator node like any other generator node.
Consider: using window functions, you can do partial aggregations over subsets of the input — without even necessarily partitioning the input (i.e. you can compute "running totals" and other wacky state-machine-like outputs.)
Re:The Curious Case of the Aggregation Query
by marcosdumay on Tuesday December 05, 2023 @11:57AM
(#38533474)
Not writing `group by` is the same as writing `group by ()`.
And yeah, the difference between that and a value is one of those really surprising things on SQL that actually make sense and should be this way. Unfortunately, there are many of those.
Re:The Curious Case of the Aggregation Query
by foldU on Tuesday December 05, 2023 @11:59AM
(#38533507)
Ah my apologies, I wasn't familiar with that syntax.
Re:The Curious Case of the Aggregation Query
by marcosdumay on Tuesday December 05, 2023 @01:13PM
(#38534748)
No need to apologize. My post didn't make that clear at all.
Not that I have a better solution offhand, in SQL grouping by a constant value is not actually the same as not writing `GROUP BY` at all since the behaviour on empty tables is different.