yjshen commented on issue #5325:
URL: 
https://github.com/apache/arrow-datafusion/issues/5325#issuecomment-1444994728

   We could rewrite an aggregate query with distinct aggregations into an 
expanded double aggregation, as Spark did:
   
   Before
   ```
   Aggregate(
      key = ['key]
      functions = [COUNT(DISTINCT 'cat1),
                   COUNT(DISTINCT 'cat2),
                   sum('value)]
      output = ['key, 'cat1_cnt, 'cat2_cnt, 'total])
     LocalTableScan [...]
   ```
   
   After rewrite:
   ```
   Aggregate(
      key = ['key]
      functions = [count('cat1) FILTER (WHERE 'gid = 1),
                   count('cat2) FILTER (WHERE 'gid = 2),
                   first('total) ignore nulls FILTER (WHERE 'gid = 0)]
      output = ['key, 'cat1_cnt, 'cat2_cnt, 'total])
     Aggregate(
        key = ['key, 'cat1, 'cat2, 'gid]
        functions = [sum('value)]
        output = ['key, 'cat1, 'cat2, 'gid, 'total])
       Expand(
          projections = [('key, null, null, 0, cast('value as bigint)),
                         ('key, 'cat1, null, 1, null),
                         ('key, null, 'cat2, 2, null)]
          output = ['key, 'cat1, 'cat2, 'gid, 'value])
         LocalTableScan [...]
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to