bvolpato opened a new pull request, #27661:
URL: https://github.com/apache/datafusion/pull/27661

   > [!IMPORTANT]
   >
   > **Fork CI setup:** For pull requests from a fork to `main`, enable 
workflows in your fork's **Actions** tab before you push.
   > See the [fork CI setup 
guide](https://datafusion.apache.org/contributor-guide/#pull-requests-from-forks).
   
   ## Which issue does this PR close?
   
   No linked issue.
   
   ## Rationale for this change
   
   In PK-FK and star-schema joins where most or all probe rows match 1:1 (or 
where a clustered probe batch matches a contiguous range), 
`build_batch_from_indices` still calls `arrow::compute::take` on every 
projected probe column. Because `arrow_select::take` has no identity or 
contiguous-slice fast path, DataFusion allocates and copies every probe payload 
column across each join stage even when the output column matches the input 
probe batch.
   
   ## What changes are included in this PR?
   
   - Add a `contiguous_range_u32` helper in `joins::utils` that rejects 
non-contiguous index arrays in O(1) (checking nulls, endpoints, and midpoint) 
and verifies contiguous runs in 8-element XOR-OR chunks once per batch across 
all probe columns.
   - When `probe_indices` is an exact identity (`0..probe_len`), reuse the 
probe column via `Arc::clone`.
   - When `probe_indices` is a contiguous sub-slice retaining at least half of 
the streaming probe batch (`len * 2 >= array.len()`), return 
`array.slice(start, len)` instead of copying through `take`.
   - Keep build-side columns on `take` so output batches never retain 
build-side or `PreparedHashJoinBuild` buffers.
   
   ### Benchmarks
   
   Measured on AMD Ryzen 9 9950X3D (Linux x86_64, `release-nonlto` against 
`main` `77611de53`):
   
   | Benchmark | Before (`main`) | After | Delta / Speedup |
   | :--- | ---: | ---: | ---: |
   | `join_materialize/pk_fk_100pct_wide_probe` | 2.006 ms | 455.5 us | -77.2% 
(4.41x) |
   | `join_materialize/contiguous_slice_75pct` | 1.473 ms | 381.9 us | -75.1% 
(3.86x) |
   | `join_materialize/random_selective_50pct` | 1.846 ms | 1.872 ms | +1.3% 
(no change, `p = 0.51`) |
   | `join_materialize/fanout_2x_untouched_control` | 3.668 ms | 3.685 ms | 
+1.3% (no change, `p = 0.23`) |
   | TPC-DS SF1 Q72 `HashJoin(i_item_sk)` `join_time` | 869.49 ms | 304.56 ms | 
-65.0% (2.85x) |
   | TPC-DS SF1 Q72 `HashJoin(w_warehouse_sk)` `join_time` | 769.86 ms | 260.17 
ms | -66.2% (2.96x) |
   | TPC-H SF10 Q10 (avg of 4) | 907.48 ms | 764.79 ms | -15.7% (1.19x) |
   | TPC-H SF10 Q7 (avg of 4) | 898.65 ms | 807.28 ms | -10.2% (1.11x) |
   | TPC-H SF10 Q8 (avg of 4) | 391.16 ms | 351.58 ms | -10.1% (1.11x) |
   | `dfbench hj` SF10 Q9 (32T, warm median) | 435.4 ms | 377.9 ms | -13.2% 
(1.15x) |
   
   Note: AI assistance was used during profiling, implementation, and benchmark 
verification; I reviewed the full diff and verified the invariants and 
benchmark results directly.
   
   ## What is the testing strategy for this PR?
   
   Added unit tests in `joins::utils` covering `contiguous_range_u32` across 
chunk boundaries, interior perturbations, null indices, and `u32::MAX` 
overflow, plus `build_batch_from_indices` verification for exact identity 
(`Arc::ptr_eq`), `>= 50%` zero-copy slices (including nullable and `Utf8View` 
columns), `< 50%` fallback to `take`, build-side non-sharing, and 
non-contiguous/nullable fallback parity. Verified the existing `joins` unit 
test suite (including prepared-build ownership and symmetric hash join 
reservation tests), confirmed identical row counts across TPC-H SF1, and added 
the `join_materialize` Criterion benchmark.
   
   ## Are there any user-facing changes?
   
   No public API or SQL behavior 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]

Reply via email to