0z5a opened a new issue, #20340:
URL: https://github.com/apache/pinot/issues/20340

   `intersectIndices(values1, values2)` is meant to return the common elements 
of two increasing index arrays, but it only advances the second array on a 
match. Once the second array has an element that is not in the first array, the 
loop never gets past it and every later match is lost.
   
   ### What happens
   
   Calling `ArrayFunctions.intersectIndices` directly:
   
   ```
   intersectIndices([0, 1, 2, 3], [1, 2, 3, 4])  -> [1, 2, 3]   correct
   intersectIndices([1, 2, 3, 4], [0, 1, 2, 3])  -> []          expected [1, 2, 
3]
   intersectIndices([0, 3], [1, 2, 3])           -> []          expected [3]
   intersectIndices([1, 3], [0, 3])              -> []          expected [3]
   intersectIndices([2, 5, 9], [0, 1, 5, 9])     -> []          expected [5, 9]
   ```
   
   So the result depends on which array happens to start with the smaller 
index, and swapping the arguments changes the answer.
   
   `[0, 3] ∩ [1, 2, 3] -> [3]` is the example from #11442, the request this 
function was added for. There, `tag_names = [x, foo, foo, x]` and `tag_values = 
[bar, y, y, y]`: the indexes where `tag_names = 'x'` are `[0, 3]`, the indexes 
where `tag_values = 'y'` are `[1, 2, 3]`, and the expected intersection is 
`[3]`. On `master` it returns `[]`. The same thing happens with the columns in 
the method's own doc comment: for `col1 = [a, b, a, b]` and `col2 = [c, d, d, 
c]`, looking up `col1 = 'b'` and `col2 = 'c'` gives `intersectIndices([1, 3], 
[0, 3])`, which returns `[]` instead of `[3]`.
   
   ### Cause
   
   ```java
   while (i < values1.length && j < values2.length) {
     if (values1[i] == values2[j]) {
       indices.add(values1[i]);
       j++;
     }
     i++;
   }
   ```
   
   When `values1[i] > values2[j]`, `i` is advanced and `j` is not, so 
`values2[j]` stays smaller than every remaining `values1` element until the 
loop ends.
   
   The existing test 
(`ScalarTransformFunctionWrapperTest.testIntersectIndices`) only uses inputs 
where the first array starts lower (`[0..n-1]` and `[1..n]`), which is the one 
order that works.
   
   ### Expected
   
   A normal sorted merge: advance `i` when `values1[i] < values2[j]`, advance 
`j` when `values1[i] > values2[j]`, and advance both on a match.
   
   ### Version
   
   Current `master`; the loop has been unchanged since the function was added 
in #11555. I reproduced it by calling `ArrayFunctions` directly on macOS with 
JDK 17; nothing in it depends on the OS.
   


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