adriangb opened a new pull request, #27925:
URL: https://github.com/apache/datafusion/pull/27925

   ## Which issue does this PR close?
   
   No issue. This is also preparation for 
https://github.com/apache/datafusion/pull/27867, which is stacked on it.
   
   ## Rationale for this change
   
   https://github.com/apache/datafusion/pull/27152 splits a projection across a 
round robin repartition to extract leaf expressions such as `s['v']`. The split 
can drop a column that gives the output its ordering, or makes it constant. If 
an ancestor relies on that, the query fails to plan:
   
   ```sql
   SET datafusion.execution.target_partitions = 4;
   SET datafusion.execution.batch_size = 2;
   SET datafusion.execution.parquet.pushdown_filters = true;
   SET datafusion.optimizer.enable_leaf_expression_pushdown = false;
   COPY (SELECT value * 2 AS c, value AS a, value AS b, named_struct('v', 
value) AS s
         FROM range(0, 20)) TO 'o.parquet' STORED AS PARQUET;
   CREATE EXTERNAL TABLE o STORED AS PARQUET LOCATION 'o.parquet' WITH ORDER (c 
ASC);
   SELECT s['v'] + 1 AS k, a + b AS m FROM o WHERE c = a + b ORDER BY m LIMIT 2;
   -- SanityCheckPlan: order and distribution requirements stopped holding after
   -- physical optimizer rule 'ProjectionPushdown'
   ```
   
   All the failures we know need the non-default 
`enable_leaf_expression_pushdown = false`. The pruning in 
https://github.com/apache/datafusion/pull/27867 reaches the same problem with 
the default settings.
   
   ## What changes are included in this PR?
   
   ```diff
    try_split_leaf_projection
   +  columns = those that the expressions read
   +  an equality or constant would be lost   -> columns += those of the input 
equivalences
   +  an ordering that matters would be lost  -> columns += the sort columns of 
the input
      push the lower projection
   +  one of these properties is lost         -> keep the plan
      otherwise                               -> commit the split
   ```
   
   - **Constants and equalities** are always protected, because sorts and 
repartitions keep them. Equalities that contain a leaf expression are not 
compared: every split that extracts the leaf loses them, also on `main`.
   - **Orderings** are protected only if an ancestor can rely on them. A sort 
replaces the ordering of its input, and `OutputRequirementExec` declares the 
one that the query needs.
   - **Unknown operators** count as relying on their input ordering, because 
they may not declare that. `PartialSortExec` does not, and nothing would detect 
the wrong rows. The list of known operators in 
`relies_only_on_declared_ordering` deserves review.
   
   Some queries that split today do more planning work, because the check 
builds a trial split. [The benchmarks of the stacked 
PR](https://github.com/apache/datafusion/pull/27867) include this cost.
   
   Review commit by commit:
   
   1. Extract two helpers. No behavior change.
   2. Add tests with the plans and the errors of `main`.
   3. Protect every property. Two plans read a column more than on `main`.
   4. Protect only the orderings that an ancestor can rely on. Those two plans 
are again as on `main`.
   
   ### AI assistance
   
   Claude Code wrote the code, the tests and this description at the request of 
the PR author.
   
   ## What is the testing strategy for this PR?
   
   - SLT cases for a merge, an aggregate and a constant sort key that fail on 
`main`, and for two plans that must not change.
   - A unit test for a `PartialSortExec`. It fails if unknown operators lose 
the protection.
   - A differential test, not part of this PR: 20,736 generated queries with 
constants, equalities, grouping, joins, unions and windows. The results agree 
wherever `main` plans the query. None newly fails to plan, and 12 that fail on 
`main` now succeed.
   
   ## Are there any user-facing changes?
   
   No public API changes. Some queries that fail to plan now succeed.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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