Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static <V extends ValueVector> int getFirstMatch(
V targetVector, VectorValueComparator<V> comparator, V keyVector, int keyIndex) {
comparator.attachVectors(keyVector, targetVector);

int ret = SEARCH_FAIL_RESULT;

int low = 0;
int high = targetVector.getValueCount() - 1;

Expand All @@ -57,30 +59,13 @@ public static <V extends ValueVector> int getFirstMatch(
// the key is larger
low = mid + 1;
} else {
// the key equals the mid value, find the lower bound by going left-ward.

// compare with the left neighbour
int left = mid - 1;
if (left == -1) {
// this is the first value in the vector
return mid;
} else {
int leftResult = comparator.compare(keyIndex, left);
if (leftResult > 0) {
// the key is greater than the left neighbour, and equal to the current one
// we find it
return mid;
} else if (leftResult == 0) {
// the left neighbour is also equal, continue to go left
high = mid - 1;
} else {
// the key is larger than the left neighbour, this is not possible
throw new IllegalStateException("The target vector is not sorted ");
}
}
// an equal element is found
// continue to go left-ward
ret = mid;
high = mid - 1;
}
}
return SEARCH_FAIL_RESULT;
return ret;
}

/**
Expand All @@ -97,6 +82,8 @@ public static <V extends ValueVector> int getLastMatch(
V targetVector, VectorValueComparator<V> comparator, V keyVector, int keyIndex) {
comparator.attachVectors(keyVector, targetVector);

int ret = SEARCH_FAIL_RESULT;

int low = 0;
int high = targetVector.getValueCount() - 1;

Expand All @@ -110,29 +97,12 @@ public static <V extends ValueVector> int getLastMatch(
// the key is larger
low = mid + 1;
} else {
// the key equals the mid value, find the upper bound by going right-ward.

// compare with the right neighbour
int right = mid + 1;
if (right == targetVector.getValueCount()) {
// this is the last value in the vector
return mid;
} else {
int rightResult = comparator.compare(keyIndex, right);
if (rightResult < 0) {
// the key is smaller than the right neighbour, and equal to the current one
// we find it
return mid;
} else if (rightResult == 0) {
// the right neighbour is also equal, continue to go right
low = mid + 1;
} else {
// the key is smaller than the right neighbour, this is not possible
throw new IllegalStateException("The target vector is not sorted ");
}
}
// an equal element is found,
// continue to go right-ward
ret = mid;
low = mid + 1;
}
}
return SEARCH_FAIL_RESULT;
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.apache.arrow.algorithm.sort.DefaultVectorComparators;
import org.apache.arrow.algorithm.sort.VectorValueComparator;
import org.apache.arrow.memory.BufferAllocator;
Expand All @@ -28,14 +31,23 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* Test cases for {@link VectorRangeSearcher}.
*/
@RunWith(Parameterized.class)
public class TestVectorRangeSearcher {

private BufferAllocator allocator;

private int repeat;

public TestVectorRangeSearcher(int repeat) {
this.repeat = repeat;
}

@Before
public void prepare() {
allocator = new RootAllocator(1024 * 1024);
Expand All @@ -49,7 +61,6 @@ public void shutdown() {
@Test
public void testGetLowerBounds() {
final int maxValue = 100;
final int repeat = 5;
try (IntVector intVector = new IntVector("int vec", allocator)) {
// allocate vector
intVector.allocateNew(maxValue * repeat);
Expand Down Expand Up @@ -79,7 +90,6 @@ public void testGetLowerBounds() {
@Test
public void testGetLowerBoundsNegative() {
final int maxValue = 100;
final int repeat = 5;
try (IntVector intVector = new IntVector("int vec", allocator);
IntVector negVector = new IntVector("neg vec", allocator)) {
// allocate vector
Expand Down Expand Up @@ -114,7 +124,6 @@ public void testGetLowerBoundsNegative() {
@Test
public void testGetUpperBounds() {
final int maxValue = 100;
final int repeat = 5;
try (IntVector intVector = new IntVector("int vec", allocator)) {
// allocate vector
intVector.allocateNew(maxValue * repeat);
Expand Down Expand Up @@ -144,7 +153,6 @@ public void testGetUpperBounds() {
@Test
public void testGetUpperBoundsNegative() {
final int maxValue = 100;
final int repeat = 5;
try (IntVector intVector = new IntVector("int vec", allocator);
IntVector negVector = new IntVector("neg vec", allocator)) {
// allocate vector
Expand Down Expand Up @@ -175,4 +183,14 @@ public void testGetUpperBoundsNegative() {
}
}
}

@Parameterized.Parameters(name = "repeat = {0}")
public static Collection<Object[]> getRepeat() {
return Arrays.asList(
new Object[]{1},
new Object[]{2},
new Object[]{5},
new Object[]{10}
);
}
}