sunchao commented on code in PR #27887:
URL: https://github.com/apache/datafusion/pull/27887#discussion_r4750028248


##########
datafusion/physical-plan/src/joins/sort_merge_join/bitwise_stream.rs:
##########
@@ -174,9 +180,22 @@ fn find_key_group_end(cmp: &JoinKeyComparator, from: 
usize, len: usize) -> usize
         return len;
     }
 
-    // Binary search the interior: key at `next` matches, key at `last` 
doesn't.
+    // The key at `next` matches and the key at `last` doesn't: rows before
+    // `lo` match and the row at `hi` doesn't. Gallop from the start of the
+    // group in doubling steps, then binary search the step that overshot.
     let mut lo = next + 1;
     let mut hi = last;
+    let mut step = 1;
+    while lo + step - 1 < hi {
+        let probe = lo + step - 1;
+        if cmp.compare(from, probe) == Ordering::Equal {

Review Comment:
   This can jump over the only matching row on a composite key. With left 
`[(-0.0, 1)]`, `SortExec` produces right `[(-0.0, 0) × 3, (-0.0, 1), (+0.0, 0), 
(+0.0, 2)]`. The join comparator folds signed zeros, so probes 2 and 4 both 
compare equal to row 0. Advancing `lo` skips row 3. In a `SortExec` → 
`SortMergeJoinExec` test, the base returned `(semi rows=1, anti rows=0, 
mark=true)` and this PR returned `(0, 1, false)`.
   
   The [sort/equality 
mismatch](https://github.com/apache/datafusion/blob/3304ec2ac22871cd66eab7aa2aa6f2c713cee86e/datafusion/common/src/float_semantics.rs#L7-L24)
 predates this PR. Could we add this regression and keep the previous search 
for composite keys with a non-final float until sorting and joining agree on 
normalization?



##########
datafusion/physical-plan/src/joins/sort_merge_join/bitwise_stream.rs:
##########
@@ -174,9 +180,22 @@ fn find_key_group_end(cmp: &JoinKeyComparator, from: 
usize, len: usize) -> usize
         return len;
     }
 
-    // Binary search the interior: key at `next` matches, key at `last` 
doesn't.
+    // The key at `next` matches and the key at `last` doesn't: rows before
+    // `lo` match and the row at `hi` doesn't. Gallop from the start of the
+    // group in doubling steps, then binary search the step that overshot.
     let mut lo = next + 1;
     let mut hi = last;
+    let mut step = 1;
+    while lo + step - 1 < hi {

Review Comment:
   Could we use `while step <= hi - lo` so the bound check itself can’t 
overflow? On a 32-bit target, two physical runs with ends `[2147483649, 
2147483650]` and values `[0, 1]` are enough. After probing row `2^31`, `lo + 
step` overflows. Checked arithmetic panics, and wrapping arithmetic eventually 
sets `step` to zero and keeps probing row 0. I reproduced both with `u32` index 
arithmetic and the Arrow comparator on a 64-bit host. The subtraction-based 
guard returned the correct end, `2147483649`.



-- 
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