Skip to content

Commit

Permalink
Fix ARRAYS_OVERLAP function bug
Browse files Browse the repository at this point in the history
In some arrays containing arrays that contain null values, arrays_overlap
was not properly comparing values. Add proper null comparison checks to
array_overlap

Resolves: #23730
  • Loading branch information
infvg committed Oct 24, 2024
1 parent 27eb666 commit 29f4c0c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
@Description("Returns true if arrays have common elements")
public final class ArraysOverlapFunction
{
private static final int NESTED_LOOP_THRESHOLD = 200;

private ArraysOverlapFunction() {}

@TypeParameter("T")
Expand All @@ -38,41 +36,6 @@ public static Boolean arraysOverlap(
@TypeParameter("T") Type elementType,
@SqlType("array(T)") Block leftArray,
@SqlType("array(T)") Block rightArray)
{
if (leftArray.getPositionCount() == 0 || rightArray.getPositionCount() == 0) {
return false;
}

if (Math.max(rightArray.getPositionCount(), leftArray.getPositionCount()) >= NESTED_LOOP_THRESHOLD) {
return arraysOverlapSetBased(elementType, leftArray, rightArray);
}

boolean hasNull = false;
for (int i = 0; i < leftArray.getPositionCount(); i++) {
if (leftArray.isNull(i)) {
hasNull = true;
continue;
}

for (int j = 0; j < rightArray.getPositionCount(); j++) {
if (rightArray.isNull(j)) {
hasNull = true;
continue;
}
if (elementType.equalTo(leftArray, i, rightArray, j)) {
return true;
}
}
}
if (hasNull) {
return null;
}
return false;
}

private static Boolean arraysOverlapSetBased(Type type,
Block leftArray,
Block rightArray)
{
int leftPositionCount = leftArray.getPositionCount();
int rightPositionCount = rightArray.getPositionCount();
Expand All @@ -89,7 +52,7 @@ private static Boolean arraysOverlapSetBased(Type type,
}

boolean itrArrHasNull = false;
TypedSet typedSet = new TypedSet(type, lookArray.getPositionCount(), "arraysOverlap");
TypedSet typedSet = new TypedSet(elementType, lookArray.getPositionCount(), "arraysOverlap");
for (int i = 0; i < lookArray.getPositionCount(); i++) {
typedSet.add(lookArray, i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,8 @@ public void testArraysOverlap()
assertFunction("ARRAYS_OVERLAP(ARRAY [NULL, 3], ARRAY [2, 1])", BooleanType.BOOLEAN, null);
assertFunction("ARRAYS_OVERLAP(ARRAY [3, NULL], ARRAY [2, 1])", BooleanType.BOOLEAN, null);
assertFunction("ARRAYS_OVERLAP(ARRAY [3, NULL], ARRAY [2, 1, NULL])", BooleanType.BOOLEAN, null);
assertFunction("ARRAYS_OVERLAP(ARRAY[ARRAY[1, 2], ARRAY[1, NULL]], ARRAY[ARRAY[1, 2]])", BooleanType.BOOLEAN, true);
assertFunction("ARRAYS_OVERLAP(ARRAY[ARRAY[1, NULL], ARRAY[1, 2]], ARRAY[ARRAY[1, 2]])", BooleanType.BOOLEAN, true);

assertFunction("ARRAYS_OVERLAP(ARRAY [CAST(1 AS BIGINT), 2], ARRAY [NULL, CAST(2 AS BIGINT)])", BooleanType.BOOLEAN, true);
assertFunction("ARRAYS_OVERLAP(ARRAY [CAST(1 AS BIGINT), 2], ARRAY [CAST(2 AS BIGINT), NULL])", BooleanType.BOOLEAN, true);
Expand Down

0 comments on commit 29f4c0c

Please sign in to comment.