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

opt: add rule to fold two grouping operators into one #49627

Merged
merged 1 commit into from
Jun 2, 2020

Commits on Jun 1, 2020

  1. opt: add rule to fold two grouping operators into one

    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.
    DrewKimball committed Jun 1, 2020
    Configuration menu
    Copy the full SHA
    2b14fcc View commit details
    Browse the repository at this point in the history