helgikrs opened a new issue, #9177:
URL: https://github.com/apache/arrow-datafusion/issues/9177

   ### Describe the bug
   
   (Bounded)WindowAggExec returns an "Expects PARTITION BY expression to be 
ordered" error when the partition by column is a constant.
   
   ### To Reproduce
   
   We can reproduce by running a query with a window function, partitioning by 
a filtered input.
   
   ```
   DataFusion CLI v35.0.0
   ❯ create table a (a bigint);
   0 rows in set. Query took 0.017 seconds.
   
   ❯ select count(*) over (partition by a order by a) from (select * from a 
where a = 1);
   Execution error: Expects PARTITION BY expression to be ordered
   ```
   
   If we omit the order by clause, we get a different error.
   
   ```
   ❯ select count(*) over (partition by a) from (select * from a where a = 1);
   Internal error: All partition by columns should have an ordering.
   This was likely caused by a bug in DataFusion's code and we would welcome 
that you file an bug report in our issue tracker
   ```
   
   I think the cause for both is the same (see below).
   
   ### Expected behavior
   
   The query should execute successfully.
   
   ### Additional context
   
   I think the bug is in the `EquivalenceProperties`. Specifically 
[`ordering_satisfy_requirement`](https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-expr/src/equivalence/properties.rs#L276)
 returns true when the [ordering requirements is a singleton 
constant](https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-expr/src/equivalence/properties.rs#L324).
 This function is used by `EnforceSorting` to determine if a `SortExec` needs 
to be added. However, `WindowAggExec` uses 
[`longest_permutation`](https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-expr/src/equivalence/properties.rs#L690C12-L690C24)
 to compute the partition by sort-keys, which does [not include singletons in 
its 
output](https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-expr/src/equivalence/properties.rs#L713),
 causing the error above to be returned 
[here](https://github.com/apache/arrow-datafusion/blob/main/datafusion/physical-plan/sr
 c/windows/mod.rs#L364).
   
   I'm not positive about its correctness, but the following patch makes the 
above queries succeed
   ```patch
   -                    if let SortProperties::Ordered(options) = data {
   -                        Some((PhysicalSortExpr { expr, options }, idx))
   -                    } else {
   -                        None
   +                    match data {
   +                        SortProperties::Ordered(options) => {
   +                            Some((PhysicalSortExpr { expr, options }, idx))
   +                        }
   +                        SortProperties::Singleton => {
   +                            Some((PhysicalSortExpr { expr, options: 
Default::default() }, idx))
   +                        }
   +                        SortProperties::Unordered => None
   ```


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