jackwener commented on issue #4854:
URL: 
https://github.com/apache/arrow-datafusion/issues/4854#issuecomment-1399222461

   So sorry for late do it. Recent I am busy working🥲.
   I've been investigating this for a while. But I find It isn't easy thing.
   
   
![image](https://user-images.githubusercontent.com/30525741/213861825-a82b711b-4e6a-437c-8638-97715c2f64d8.png)
   
   In standard sql, `order by` is after `project`. BUT, in fact, reality is not 
so.
   `order by` is from `project`.
   
   but sometime `order by` is from child of `project`. For example:
   
   ```sql
   spark-sql> explain extended select uuid from hudi_test order by price+1;
   == Parsed Logical Plan ==
   'Sort [('price + 1) ASC NULLS FIRST], true
   +- 'Project ['uuid]
      +- 'UnresolvedRelation [hudi_test], [], false
   
   == Analyzed Logical Plan ==
   uuid: int
   Project [uuid#46]
   +- Sort [(price#48 + cast(1 as double)) ASC NULLS FIRST], true
      +- Project [uuid#46, price#48]
         +- SubqueryAlias spark_catalog.default.hudi_test
            +- Relation 
default.hudi_test[_hoodie_commit_time#41,_hoodie_commit_seqno#42,_hoodie_record_key#43,_hoodie_partition_path#44,_hoodie_file_name#45,uuid#46,name#47,price#48]
 parquet
   ```
   
   we can find `order by` is before project.
   
   So, when exist `aggregation`. like above
   
   ```sql
   select
     sum(value) AS "value",
     date_trunc('month',time) AS "time"
   FROM t
   GROUP BY time
   ORDER BY time;
   ```
   
   two condition: orderby-project-agg / project-orderby-agg.
   
   In this sql, `order-by: time`, `time` exist in `agg`, so datafusion use 
`project-orderby-agg`, but in fact, `time` is from `project`.
   
   We should prefer to use `orderby-project-agg` and if fail then use 
`project-orderby-agg`
   


-- 
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