marikomedlock opened a new issue, #27526: URL: https://github.com/apache/datafusion/issues/27526
### Is your feature request related to a problem or challenge? Both rules build a merged schema of all of a node's inputs before rewriting that node's expressions: - `SimplifyExpressions::optimize_internal` (`datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs`): `merge_schema(&plan.inputs())` for any node with inputs. - `RewriteSetComparison::rewrite_plan` (`datafusion/optimizer/src/rewrite_set_comparison.rs`): `merge_schema(&plan.inputs())` for every node. `merge_schema` folds the inputs with `DFSchema::merge`, and each `merge` rebuilds hash sets of every field merged so far. For a `Union` with N inputs that's quadratic in the number of fields, and both rules run on every optimizer pass. A `Union` has no expressions of its own, so the merged schema is never used. This shows up once unions are built N-ary (#27523): On a plan with 7 unions of 186, 186, 186, 91, 91, 91 and 3 inputs, these merges were a large share of what was left of `SessionState::optimize`. ### Describe the solution you'd like Return `Transformed::no(plan)` before building the schema when there is nothing to rewrite: - `SimplifyExpressions`: When the node has no expressions of its own. - `RewriteSetComparison`: When none of the node's own expressions contains an `Expr::SetComparison`. For such nodes both rules already return the plan unchanged, so plans are identical. ### Describe alternatives you've considered Making `merge_schema` itself cheaper for many inputs (build the field sets once). That would help every caller, but these two rules don't need the schema at all for these nodes, so skipping is simpler and cheaper. The two could be done independently. ### Additional context Measured together with #27523, time in `SessionState::optimize` for the plan above (mean of 9–12 warm runs): | | 4-vCPU Graviton2 (shared) | Apple M-series laptop | |---|---|---| | N-ary unions (#27523) | 2.34s | 0.332s | | N-ary unions + this change | 1.34s (−43%) | 0.199s (−40%) | Related: #27523, #17261. ### Willingness to contribute I plan to contribute to this feature independently -- 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]
