aaron-perpetual opened a new issue, #25921:
URL: https://github.com/apache/datafusion/issues/25921

   ### Describe the bug
   
   A left join followed by sorting on a structure field can fail with 
`SanityCheckPlan: does not satisfy order requirements`.
   
   The local sort orders by `get_field(n, label)`, while the sort-preserving 
merge requires an equivalent extracted column. A later projection loses the 
equivalence between those expressions causing plan validation to fail.
   
   ### To Reproduce
   
   ```
   SET datafusion.execution.target_partitions = 2;
   
   CREATE TABLE events (id BIGINT) AS VALUES (1), (2), (3);
   
   CREATE TABLE networks AS
   SELECT 'b' AS label UNION ALL SELECT 'a' AS label;
   
   SELECT id, n
   FROM events
   LEFT JOIN (
     SELECT named_struct('label', label) AS n
     FROM networks
   ) ON id > 1
   ORDER BY id, n['label'];
   ```
   
   The `UNION ALL` creates two partitions, which is necessary to force the 
local sort that reproduces the failure.
   
   ### Expected behavior
   
   The query should succeed
   
   ### Additional context
   
   Because `EquivalenceGroup::project_expr_indirect` substitutes equivalent 
expressions before trying to reconstruct the original expression from its 
projected children, both sides of an equality like `get_field(n, 'label') = 
extracted_label` can project to `extracted_label` even though `n` survives the 
projection and the field access expression remains valid. Losing this 
equivalence prevents DataFusion from validating plans whose ordering 
requirements use different, equivalent expressions e.g. 
   
   ```
   SortPreservingMerge: [id, extracted_label]
     Sort within each partition: [id, get_field(n, 'label')]
       Left join
   ```


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