460 karma · joined December 2, 2013
https://buttondown.email/jaffray
Speaking from experience, there's lots of rewrites you'd want to do in a query optimizer that having access to efficient DAG-shaped query plans would make tenable, but as a specific example they are an important part of doing full subquery decorrelation [3].
[1] https://buttondown.email/jaffray/archive/why-are-query-plans...
[2] https://egraphs-good.github.io/
[3] https://www.scattered-thoughts.net/writing/materialize-decor...
SELECT (SELECT sum(1) FROM xx LIMIT 1) FROM aa;
which returns 3
3
3
Personally, I think having the inner aggregation always attach to the nearest `SELECT` would have been an equally valid way of defining how this works, but it just so happens it is not defined that way.[1]: https://buttondown.email/jaffray/archive/sql-scoping-is-surp...
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.
[0]: https://github.com/cockroachdb/cockroach/blob/master/docs/RF...
And thanks for the info! I would love to learn more about compiler design and garbage collection in the future. Still working my way through some other reading materials but certainly something I would like to get to more!