sunchao commented on code in PR #24668:
URL: https://github.com/apache/datafusion/pull/24668#discussion_r3877820324


##########
datafusion/physical-expr/src/equivalence/properties/mod.rs:
##########
@@ -240,6 +252,12 @@ impl EquivalenceProperties {
             // No point in substituting an expression with itself.
             return None;
         }
+        if double_negation_input(&r_expr).is_some_and(|expr| 
expr.eq(&sort_expr.expr)) {

Review Comment:
   **[P2] Preserve suffix ordering across composed negation projections**
   
   Only one pair is unwrapped here. When two retained double-negation 
projections are fused, `r_expr` becomes `-(-(-(-k)))`, so this compares `-(-k)` 
with the input sort key `k` and fails. The projected result then loses the 
ordering of the suffix `j`.
   
   I reproduced this on head `a9279fc37` versus base `a38bb10c3`:
   
   ```sql
   SELECT y, j
   FROM (
     SELECT -(-x) AS y, j
     FROM (SELECT -(-k) AS x, j FROM s)
   )
   ORDER BY y, j LIMIT 1;
   ```
   
   The source is a single-partition, unbounded `StreamingTable` ordered by `(k 
ASC NULLS LAST, j ASC NULLS LAST)`, with `target_partitions=1` and 
`batch_size=1`. It emits two batches with `k = TimestampNanosecond(1)` and `j = 
[0, 1]`, then remains pending without end-of-stream.
   
   Base returns the first row immediately. Head retains `PartialSortExec: 
TopK(fetch=1), common_prefix_length=[1]` above the fused projection and 
produces no row before the two-second timeout. With a permanently fixed leading 
key, that partial sort cannot finish its first tie group. The same difference 
reproduces for nullable timestamps and `IntervalYearMonth(1)`, and for a 
four-negation aggregate alias followed by `ORDER BY`. A single double-negation 
projection and direct four-negation ordering both return on head.
   
   Could the ordering proof walk successive deterministic negation pairs, while 
retaining the original expression for checked evaluation, and cover composed 
projections with a pending-stream regression?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to