JoeryH opened a new pull request, #27871: URL: https://github.com/apache/datafusion/pull/27871
## Which issue does this PR close? - Closes #27869. ## Rationale for this change Since #22169, every SELECT runs the UNNEST rewrite (`rewrite_recursive_unnest_bottom_up` over each select item, plus a deduplicated inner projection of every bare column), even when the query has no UNNEST. The rewrite then finds no unnest column and returns the input unchanged. Before #22169, `try_process_unnest` skipped all of this with a fast path (#16389). On main the wasted work is linear, but it is a noticeable share of planning for wide SELECTs. On `branch-55`, where `push_projection_dedupl` is still quadratic (#25236 is only on main), it dominates planning of wide nested projections. The issue has the details. ## What changes are included in this PR? `rewrite_unnest_expr_groups` returns the plan and expressions as they are when no expression contains an `Expr::Unnest`. It does this after `try_process_aggregate_unnest`, which is what the old fast path did. Both callers, `try_process_unnest` and `try_process_distinct_on_unnest`, get it. The loop already returned exactly this on its first iteration in that case, so plans are unchanged. Benchmarks with the cases from #27870, against main: | case | main | this PR | change | |---|---|---|---| | `logical_no_unnest_200_columns` | 1.020 ms | 0.872 ms | −13.8% | | `logical_no_unnest_200_columns_4_levels` | 5.205 ms | 4.487 ms | −13.8% | | `logical_no_unnest_1000_columns` | 8.551 ms | 8.212 ms | −4.0% | | `logical_no_unnest_1000_columns_4_levels` | 45.71 ms | 42.17 ms | −7.7% | | `logical_unnest_plus_200_columns` | 1.387 ms | 1.406 ms | no change | | `logical_unnest_plus_1000_columns` | 10.56 ms | 10.72 ms | +1.5% (noise) | | `logical_unnest_in_200_exprs` | 4.002 ms | 3.971 ms | no change | ## What is the testing strategy for this PR? Plans are unchanged, so this relies on the existing tests: `datafusion-sql` unit tests and the full sqllogictest suite pass locally, including `unnest.slt` and `distinct_on.slt`, which cover UNNEST in SELECT, GROUP BY and DISTINCT ON. The benchmark cases in #27870 cover performance. ## Are there any user-facing changes? No. Planning is faster and plans are identical. If it helps, I can open the `branch-55` backports of #25236 and this change. --- Prepared with AI assistance (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]
