JoeryH opened a new issue, #27869:
URL: https://github.com/apache/datafusion/issues/27869
### Is your feature request related to a problem or challenge?
#22169 (DISTINCT ON with aggregation and windows) moved the SELECT-list
UNNEST handling into `rewrite_unnest_expr_groups` and dropped the fast path
that `try_process_unnest` had since #16389:
```rust
// Fast path: If there is are no unnests in the select_exprs, wrap the plan
in a projection
if !intermediate_select_exprs.iter().any(has_unnest_expr_recursively) {
return LogicalPlanBuilder::from(intermediate_plan)
.project(intermediate_select_exprs)?
.build();
}
```
So since 55.0.0, every SELECT runs `rewrite_recursive_unnest_bottom_up` on
every select item and pushes every bare column into the deduplicated inner
projection, even when there is no UNNEST anywhere in the query. The result is
thrown away, because no unnest column was found.
On main that costs a `schema_name()` string and a hash insert per column,
about 4–14% of SQL planning for wide SELECTs (numbers below).
On `branch-55` it is much worse. `push_projection_dedupl` there still
compares the new name against the formatted `schema_name()` of every expression
already pushed, which is quadratic in the select width. #25236 fixed that on
main, and it is in 56.0.0-rc1 but not in 55.3.0. Every plain SELECT therefore
plans in O(width²). For views that nest wide projections a few levels deep, we
saw SQL planning go up by roughly 3x from 54 to 55, almost all of it inside
`push_projection_dedupl` under `rewrite_recursive_unnest_bottom_up`. Restoring
the fast path, or backporting #25236, each brings 55.2 back to 54's planning
time.
### Describe the solution you'd like
Restore the fast path at the start of `rewrite_unnest_expr_groups` (after
`try_process_aggregate_unnest`, as before), so both `try_process_unnest` and
`try_process_distinct_on_unnest` skip the rewrite when no expression contains
an `Expr::Unnest`. The loop already returns the input plan and expressions
unchanged in that case, so the output is identical.
Benchmarks on main (new `sql_planner_extended` cases, `SELECT a0, ..., aN
FROM t`, optionally nested 4 levels):
| case | main | with fast path | 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_1000_columns` (control) | 10.56 ms | 10.72 ms | noise
|
| `logical_unnest_in_200_exprs` (control) | 4.002 ms | 3.971 ms | noise |
### Describe alternatives you've considered
Backporting #25236 to `branch-55` removes the quadratic part there, and
would be worth doing on its own for 55.x users. Without the fast path, the
linear cost remains.
### Additional context
_No response_
--
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]