adriangb commented on code in PR #27777:
URL: https://github.com/apache/datafusion/pull/27777#discussion_r4751100384
##########
datafusion/physical-plan/src/projection.rs:
##########
@@ -169,6 +169,34 @@ impl ProjectionExec {
Self::try_from_projector(projector, input, overrides_metadata)
}
+ pub(crate) fn with_pruned_input(
+ &self,
+ exprs: Vec<ProjectionExpr>,
+ input: Arc<dyn ExecutionPlan>,
+ ) -> Result<Self> {
+ // Input pruning must preserve output values, metadata, ordering, and
partitioning.
+ let projector = self
+ .projector
+
.with_new_projection_preserving_schema(ProjectionExprs::new(exprs));
+ debug_assert_eq!(
+ projector.output_schema(),
+ projector
+ .projection()
+ .make_projector_with_schema_metadata(
+ &input.schema(),
+ self.schema().as_ref()
+ )?
+ .output_schema()
+ );
+ Ok(Self {
+ projector,
+ input,
+ metrics: ExecutionPlanMetricsSet::new(),
+ cache: Arc::clone(&self.cache),
Review Comment:
The rewrite can remove an ordering that an ancestor needs. The query then
fails to plan.
```sql
SET datafusion.execution.target_partitions = 4;
SET datafusion.execution.parquet.pushdown_filters = true;
COPY (SELECT value * 2 AS c, value AS a, value AS b FROM range(0, 1000)
ORDER BY c)
TO 'ordered.parquet' STORED AS PARQUET;
CREATE EXTERNAL TABLE ordered STORED AS PARQUET LOCATION 'ordered.parquet'
WITH ORDER (c ASC);
SELECT a + b AS k FROM ordered WHERE c = a + b ORDER BY k LIMIT 2;
```
| | Result |
| --- | --- |
| `main` | `0`, `2` |
| this PR | `SanityCheckPlan: order and distribution requirements stopped
holding after physical optimizer rule 'ProjectionPushdown'` |
The file is sorted by `c`, and the predicate makes `c` equal to `a + b`. The
plan therefore does not sort `k`. No expression reads `c`, so the rewrite
prunes it, and the input can no longer show that `k` is sorted.
The projection keeps its cached properties here, so the rewrite does not see
the loss. A possible fix is to recompute the properties and to compare the
output orderings before and after the rewrite. If an ordering is lost, the
rewrite can keep the columns that the input is sorted by.
Could you add this query as an SLT case?
##########
datafusion/physical-plan/src/repartition/mod.rs:
##########
@@ -1951,6 +1954,37 @@ impl ExecutionPlan for RepartitionExec {
&self,
projection: &ProjectionExec,
) -> Result<Option<Arc<dyn ExecutionPlan>>> {
+ if !all_columns(projection.expr())
Review Comment:
The inserted `ProjectionExec` can move `get_field` below the round robin,
where it runs on one partition.
```sql
SET datafusion.execution.target_partitions = 4;
CREATE TABLE t AS
SELECT value AS c, named_struct('v', value, 'w', 1) AS s FROM range(0,
50000);
EXPLAIN FORMAT INDENT
SELECT s['v'] + 1 AS v, rn FROM (SELECT s, row_number() OVER (ORDER BY c) AS
rn FROM t);
```
The window column is shortened to `rn`:
```diff
-ProjectionExec: expr=[get_field(s@1, v) + 1 as v, rn@2 as rn]
+ProjectionExec: expr=[__datafusion_leaf_0@1 + 1 as v, rn@0 as rn]
RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
- BoundedWindowAggExec
+ ProjectionExec: expr=[rn@2 as rn, get_field(s@1, v) as
__datafusion_leaf_0]
+ BoundedWindowAggExec
```
I think that the leaf split from
https://github.com/apache/datafusion/pull/27152 sees the inserted projection as
an operator that absorbs the leaf. I did not measure the cost, and it is
probably small for `get_field`. A possible fix is to not let a leaf split merge
into this projection.
--
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]