haohuaijin opened a new pull request, #27943:
URL: https://github.com/apache/datafusion/pull/27943
## Which issue does this PR close?
- No issue (standalone planning performance improvement).
## Rationale for this change
Projecting [`EquivalenceProperties`] over wide projections performs
avoidable quadratic and redundant hashing work when the input has few or no
equivalence classes:
1. `EquivalenceGroup::augment_projection_mapping` copies all `N` entries of
`ProjectionMapping` into a new `IndexMap`, and
`EquivalenceGroup::project_expr_indirect` linearly scans all `N` values looking
for an `EquivalenceClass` on every miss—even when the input `EquivalenceGroup`
is empty (`O(N²)` scans across `N` projected expressions).
2. `EquivalenceGroup::project` always hashes all `N` source expressions into
an intermediate `IndexMap<Arc<dyn PhysicalExpr>, EquivalenceClass>` and
allocates `N` single-element classes before filtering out trivial classes, even
when `self.map` is empty and `ProjectionMapping` keys are already distinct.
3. `EquivalenceProperties::normalize_mapping` always clones and re-hashes
the entire `ProjectionMapping`, even when `self.eq_group` is empty and no
source expression is volatile.
## What changes are included in this PR?
- Change `AugmentedMapping` in
`datafusion/physical-expr/src/equivalence/class.rs` to borrow
`&ProjectionMapping` directly and store only entries that belong to an
`EquivalenceClass` in `class_entries: Vec<(&ProjectionTargets,
&EquivalenceClass)>` (zero allocations and zero fallback scans when `self.map`
is empty).
- Skip allocating the intermediate `IndexMap` in `EquivalenceGroup::project`
when `self.map` is empty, creating an `EquivalenceClass` only when a source has
multiple targets or an indirect projection.
- Return `Cow<'a, ProjectionMapping>` from
`EquivalenceProperties::normalize_mapping` in
`datafusion/physical-expr/src/equivalence/properties/mod.rs` to borrow
`mapping` without cloning/re-hashing when `self.eq_group` is empty and no
source is volatile.
## What is the testing strategy for this PR?
- `cargo fmt --all`
- `cargo clippy -p datafusion-physical-expr --all-targets --all-features --
-D warnings`
- `cargo test -p datafusion-physical-expr --lib equivalence` (111 passed)
### Benchmark Results (Apple M2 Max, `rustc 1.99.1`, release profile)
**Micro-benchmark** (`cargo bench -p datafusion-physical-expr --bench
equivalence_properties -- project/wide_struct_fields`):
```text
group
base pr
-----
---- --
equivalence_properties/project/wide_struct_fields/230_of_300
2.72 307.0±17.80µs 1.00 112.7±1.03µs
equivalence_properties/project/wide_struct_fields/230_of_300/ordered_by_s
2.00 469.8±6.29µs 1.00 234.4±3.75µs
```
**End-to-end SQL planning** (`cargo bench -p datafusion --bench
integer_statistics_ordering -- planning`):
```text
group main
pr
----- ----
--
integer_stats_ordering/planning/bounded_wide_projection 1.10
1454.9±37.46µs 1.00 1319.1±29.22µs
integer_stats_ordering/planning/unchanged_no_stats_wide_projection 1.12
1423.7±88.76µs 1.00 1273.2±31.99µs
integer_stats_ordering/planning/overflow_arithmetic 1.01
180.7±5.86µs 1.00 179.6±4.15µs
integer_stats_ordering/planning/safe_arithmetic 1.00
178.8±3.40µs 1.02 181.9±14.50µs
```
## Are there any user-facing changes?
No. Internal planning performance improvement only; no public API changes.
--
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]