adriangb opened a new pull request, #27867: URL: https://github.com/apache/datafusion/pull/27867
## Which issue does this PR close? - Closes https://github.com/apache/datafusion/issues/27776. This is an alternative to https://github.com/apache/datafusion/pull/27777. The diagnosis in the issue is by [ivan-kiselev](https://github.com/ivan-kiselev). ## Rationale for this change A scan emits columns that only its pushed-down predicate reads. They cross a round robin repartition before a computed projection drops them. A struct is not necessary: ```sql SET datafusion.execution.target_partitions = 4; SET datafusion.execution.parquet.pushdown_filters = true; COPY (SELECT value AS timestamp, value * 3 AS period, value * 7 AS val FROM range(0, 20000)) TO 'repro.parquet' STORED AS PARQUET; CREATE EXTERNAL TABLE t STORED AS PARQUET LOCATION 'repro.parquet'; EXPLAIN FORMAT INDENT SELECT val * period AS result FROM t WHERE timestamp >= 0; ``` ```diff -ProjectionExec: expr=[val@2 * period@1 as result] +ProjectionExec: expr=[val@1 * period@0 as result] RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 - DataSourceExec: projection=[timestamp, period, val], predicate=timestamp@0 >= 0 + DataSourceExec: projection=[period, val], predicate=timestamp@0 >= 0 ``` ## What changes are included in this PR? https://github.com/apache/datafusion/pull/27152 splits a projection across a round robin repartition, but only if the projection has a leaf expression. This PR also splits it if that prunes a column. ```diff try_split_projection - no leaf expression -> keep the plan + no leaf expression and no column to prune -> keep the plan the child declines the lower projection -> keep the plan + push the lower projection as far as it goes + no leaf, and the plan gains a ProjectionExec -> keep the plan + an ordering of the output is lost -> keep the plan otherwise -> commit the split ``` - **Projection count.** A child that accepts the lower projection does not always absorb it. `CoalescePartitionsExec` puts it below itself, and so does `SortPreservingMergeExec` if its key survives. This PR therefore rejects a split that only prunes if the plan gains a `ProjectionExec`. Leaf splits keep their current rule. - **Ordering.** With input sorted by `c` and the predicate `c = a + b`, `SELECT a + b AS s ... ORDER BY s` needs `c` although no expression reads it. The check compares the output orderings. A check of `preserve_order()` misses a repartition of one input partition, which keeps the order without that flag. ### Trade-offs The first absorption check looks one operator below the repartition. If that operator declines, its columns stay. In these two queries https://github.com/apache/datafusion/pull/27777 prunes `url` and this PR does not: ```sql SELECT val * period FROM (SELECT * FROM e WHERE id >= 0 ORDER BY url LIMIT n); SELECT rn * 2 FROM (SELECT url, row_number() OVER (ORDER BY url) AS rn FROM e); ``` That PR adds a `ProjectionExec` below the repartition there, and far fewer bytes cross it. The benchmark below shows no clear time difference. It does not cover a repartition that spills, where the bytes could matter. ### Benchmarks Median of 15 interleaved runs on an 18 core laptop that was not idle. Profile `release-nonlto`, `target_partitions = 4`, `pushdown_filters = true`, `repartition_file_scans = false`. `t` has 10M rows. `e` has 3M rows with a 97 byte `url`. `main` is [bb1dad1](https://github.com/apache/datafusion/commit/bb1dad19baf34774abd8bbc8f397d0253c0d9945). "PR 27777" is [858edef](https://github.com/apache/datafusion/commit/858edefa812454dc98b6e1d5b06de62c368a91d9) on its own base. | Query | main | PR 27777 | this PR | round robin `output_bytes` (main / PR 27777 / this PR) | | --- | --: | --: | --: | --- | | issue query on `t` | 207 ms | 183 ms | 184 ms | 352.9 / 152.6 / 152.6 MB | | no struct, predicate always true | 143 ms | 99 ms | 99 ms | 228.9 / 152.6 / 152.6 MB | | no struct, half the rows pass | 163 ms | 157 ms | 156 ms | 114.7 / 76.5 / 76.5 MB | | wide predicate column `url` | 121 ms | 31 ms | 31 ms | 670.9 / 45.9 / 45.9 MB | | `url` predicate below `LIMIT 2000000` | 56 ms | 14 ms | 14 ms | 542.5 / 30.6 / 30.6 MB | | `ORDER BY url LIMIT 300000` (trade-off) | 1026 ms | 1020 ms | 1032 ms | 10.4 GB / 4.6 MB / 10.4 GB | | window over `url` (trade-off) | 318 ms | 318 ms | 317 ms | 103.3 GB / 22.9 MB / 103.3 GB | | `GROUP BY`, plan not affected | 104 ms | 104 ms | 103 ms | 152.6 MB for all | `output_bytes` counts a string buffer again for each batch that shares it, so the GB values are not memory use. A checksum of the answers agrees for every query except the bare `LIMIT`, which returns an arbitrary set of rows. ### AI assistance Claude Code wrote the code, the tests and this description at the request of the PR author. One assumption deserves review: an output ordering is the only property that pruning can remove and that an ancestor relies on. ## What is the testing strategy for this PR? Review commit by commit. The first commit adds cases to `projection_pushdown.slt` with the plans of `main`. The second commit has the fix and shows what it changes in them. No expectation that exists on `main` changes. - Plans that change: the issue query, the same without a struct, duplicate columns, zero referenced columns, `CoalescePartitionsExec` over a scan, and the case from https://github.com/apache/datafusion/pull/27777. - Plans that must not change: the ordering queries, a window with `rn * 2`, and a window where `get_field` must stay above the repartition. - The two trade-off queries, pinned as they are. - Unit tests for two plans that the `slt` cases do not reach: a `CoalescePartitionsExec` whose input absorbs nothing, and a hash repartition. With any one of the checks removed, at least one of these tests fails. ## Are there any user-facing changes? No public API changes. `EXPLAIN` can show fewer columns in a scan. 🤖 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]
