-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49627: opt: add rule to fold two grouping operators into one r=DrewKimball a=DrewKimball Previously, the optimizer could not fold two grouping operators into a single equivalent grouping operator. This patch adds a rule that effects this transformation under the following conditions: 1. All of the outer aggregates are aggregating on the output columns of the inner aggregates. 2. Every inner-outer aggregation pair can be replaced with an equivalent single aggregate. 3. The inner grouping columns functionally determine the outer grouping columns. 4. Both grouping operators are unordered. As an example, the following query pairs are equivalent: ``` SELECT sum(t) FROM (SELECT sum(b) FROM ab GROUP BY a) AS g(t); SELECT sum(b) FROM ab; SELECT max(t) FROM (SELECT max(b) FROM ab GROUP BY a) AS g(t); SELECT max(b) FROM ab; SELECT sum_int(t) FROM (SELECT count(b) FROM ab GROUP BY a) AS g(t); SELECT count(b) FROM ab; ``` This situation is rare in direct SQL queries, but can arise when composing views and queries. Release note (sql change): The optimizer can now fold two grouping operators together when they are aggregating over functions like sum. Co-authored-by: Drew Kimball <drewk@cockroachlabs.com>
- Loading branch information
Showing
9 changed files
with
459 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.