xavlee opened a new pull request, #27759:
URL: https://github.com/apache/datafusion/pull/27759
## Which issue does this PR close?
Part of #19795.
## Rationale for this change
SQL planning can spend time resolving the types of nested binary expressions
more than once. The analyzer already resolves both operands to choose the types
the operator needs, but `Expr::cast_to` resolves the type again before
returning a same-type operand unchanged. For a chain of arithmetic expressions
or predicates, each of those lookups can walk the subtree again.
## What changes are included in this PR?
The analyzer returns the operand immediately when its resolved type already
matches the requested type. It uses the existing Arrow castability check,
normal casts, and date/time arithmetic handling for the cases that need them.
## What is the testing strategy for this PR?
The optimizer's existing tests cover mixed numeric types, date and interval
arithmetic, and timestamp coercion. All 983 optimizer unit tests passed.
Criterion `sql_planner`, running the same workloads on builds of Apache
DataFusion main `73043ac` with and without the change. Rust 1.99.1,
`release-nonlto`, Linux aarch64 (Neoverse-N1), pinned to one CPU.
| Workload | Main | With change | Planning time |
| --- | ---: | ---: | ---: |
| SQL analysis, 50-term integer addition | 5.049 ms | 3.630 ms | −28.1% |
| SQL analysis, 200 filter predicates | 11.653 ms | 6.909 ms | −40.7% |
| SQL analysis, 64 mixed-type additions | 2.337 ms | 2.335 ms | No
difference detected |
| SQL analysis, single-column projection | 229.32 µs | 228.50 µs | Within
noise threshold |
| TPC-H physical planning, all queries | 73.396 ms | 73.441 ms | No
difference detected |
The mixed-type and TPC-H rows are from the reverse-order repeat (change
before main). Criterion reported p = 0.14 and p = 0.81 respectively. The first
TPC-H run estimated a 2.4% improvement, and the reverse-order run put the
difference within run-to-run variation.
<details>
<summary>Benchmark cases and commands</summary>
The SQL analysis cases time `analyzed_plan` in
`datafusion/core/benches/sql_planner.rs`. The existing `t1` fixture exposes
`a0` through `a199` as `Int32` columns. These definitions go after the context
and runtime setup in `criterion_benchmark` on both revisions.
```rust
let additions = (0..50).map(|i| format!("a{i}")).collect::<Vec<_>>();
let nested = format!("SELECT {} AS sum_value FROM t1", additions.join(" +
"));
let mut mix = 1009_u32;
let mixed = (0..64)
.map(|i| {
mix = mix.wrapping_mul(1664525).wrapping_add(1013904223);
let j = i + 64;
match mix >> 30 {
0 => format!("CAST(a{i} AS DOUBLE) + a{j} AS r{i}"),
1 => format!("a{i} + CAST(a{j} AS DOUBLE) AS r{i}"),
_ => format!("a{i} + a{j} AS r{i}"),
}
})
.collect::<Vec<_>>();
let mixed = format!("SELECT {} FROM t1", mixed.join(", "));
let predicates = (0..200).map(|i| format!("a{i} > 0")).collect::<Vec<_>>();
let filters = format!("SELECT a0 FROM t1 WHERE {}", predicates.join(" AND
"));
for (name, sql) in [
("analyzed_binary_coercion_nested_50", nested.as_str()),
("analyzed_binary_coercion_mixed_64", mixed.as_str()),
("analyzed_binary_coercion_filter_200", filters.as_str()),
("analyzed_binary_coercion_control", "SELECT a0 FROM t1"),
] {
c.bench_function(name, |b| {
b.iter(|| black_box(analyzed_plan(&ctx, &rt, sql)))
});
}
```
Run with `--save-baseline main` on main and `--baseline main` with the
change. Share `CRITERION_HOME` between revisions if using separate checkouts.
```bash
export
CLICKBENCH_DATA_PATH="$PWD/datafusion/core/tests/data/clickbench_hits_10.parquet"
filter='^(analyzed_binary_coercion_(nested_50|mixed_64|filter_200|control)|physical_plan_tpch_all)$'
taskset -c 8 cargo bench --locked -p datafusion --bench sql_planner
--profile release-nonlto -- \
"$filter" --warm-up-time 1 --measurement-time 4 --sample-size 30 --noplot
--save-baseline main
```
</details>
## Are there any user-facing changes?
Planning can be faster for deeply nested binary expressions.
Prepared with AI assistance (OpenAI Codex).
--
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]