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

Support no distinct count/max/min/sum aggregate in single_distinct_to_group_by rule #8123

Closed
haohuaijin opened this issue Nov 10, 2023 · 0 comments · Fixed by #8266
Closed
Labels
enhancement New feature or request

Comments

@haohuaijin
Copy link
Contributor

Is your feature request related to a problem or challenge?

In current single_distinct_to_group_by rule, we only support all aggregate function are distinct aggregate.
But if the no distinct aggregate is count/min/max/sum, we can also do same transform like in single_distinct_to_group_by
before

select a, count(distinct b), count(c)
from t
group by a

after

select a, count(alias1), sum(alias2)
from (
  select a, b as alias1, count(c) as alias2
  from t
  group by a, b
)
group by a

Describe the solution you'd like

By write, we can improve the perfmance of distinct aggregate

❯ SELECT "RegionID", SUM("AdvEngineID"), COUNT(DISTINCT "UserID") FROM '../benchmarks/data/hits.parquet' GROUP BY "RegionID" order by "RegionID" LIMIT 10;
+----------+--------------------------------------------------+--------------------------------------------------------+
| RegionID | SUM(../benchmarks/data/hits.parquet.AdvEngineID) | COUNT(DISTINCT ../benchmarks/data/hits.parquet.UserID) |
+----------+--------------------------------------------------+--------------------------------------------------------+
| 0        | 0                                                | 8                                                      |
| 1        | 147946                                           | 239380                                                 |
| 2        | 441662                                           | 1081016                                                |
| 3        | 39724                                            | 131195                                                 |
| 4        | 34557                                            | 79500                                                  |
| 5        | 13502                                            | 40914                                                  |
| 6        | 24338                                            | 55768                                                  |
| 7        | 28417                                            | 64989                                                  |
| 8        | 34483                                            | 65472                                                  |
| 9        | 38047                                            | 91576                                                  |
+----------+--------------------------------------------------+--------------------------------------------------------+
10 rows in set. Query took 1.357 seconds.

❯ SELECT "RegionID", SUM(t1),  count("UserID") from (select "UserID", "RegionID", sum("AdvEngineID") as t1 from '../benchmarks/data/hits.parquet' group by "UserID", "RegionID") group by "RegionID" order by "RegionID" limit 10;
+----------+---------+-----------------------------------------------+
| RegionID | SUM(t1) | COUNT(../benchmarks/data/hits.parquet.UserID) |
+----------+---------+-----------------------------------------------+
| 0        | 0       | 8                                             |
| 1        | 147946  | 239380                                        |
| 2        | 441662  | 1081016                                       |
| 3        | 39724   | 131195                                        |
| 4        | 34557   | 79500                                         |
| 5        | 13502   | 40914                                         |
| 6        | 24338   | 55768                                         |
| 7        | 28417   | 64989                                         |
| 8        | 34483   | 65472                                         |
| 9        | 38047   | 91576                                         |
+----------+---------+-----------------------------------------------+
10 rows in set. Query took 0.919 seconds.

Describe alternatives you've considered

No response

Additional context

https://www.querifylabs.com/blog/distinct-aggregation-optimization-in-apache-calcite-and-trino
https://github.com/apache/calcite/blob/96b05ee12f936ed057265072ff6a2de8ea0a249e/core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java#L286-L298

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
1 participant