Skip to content

Commit

Permalink
[ARROW-5920] [Java] Provide a utility to create the default comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
liyafan82 committed Jul 12, 2019
1 parent 46d2c11 commit cbd8c3f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,49 @@

import static org.apache.arrow.vector.BaseVariableWidthVector.OFFSET_WIDTH;

import org.apache.arrow.vector.BaseFixedWidthVector;
import org.apache.arrow.vector.BaseVariableWidthVector;
import org.apache.arrow.vector.BigIntVector;
import org.apache.arrow.vector.Float4Vector;
import org.apache.arrow.vector.Float8Vector;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.SmallIntVector;
import org.apache.arrow.vector.TinyIntVector;
import org.apache.arrow.vector.ValueVector;

/**
* Default comparator implementations for different types of vectors.
*/
public class DefaultVectorComparators {

/**
* Create the default comparator for the vector.
* @param vector the vector.
* @param <T> the vector type.
* @return the default comparator.
*/
public static <T extends ValueVector> VectorValueComparator<T> createDefaultComparator(T vector) {
if (vector instanceof BaseFixedWidthVector) {
if (vector instanceof TinyIntVector) {
return (VectorValueComparator<T>) new ByteComparator();
} else if (vector instanceof SmallIntVector) {
return (VectorValueComparator<T>) new ShortComparator();
} else if (vector instanceof IntVector) {
return (VectorValueComparator<T>) new IntComparator();
} else if (vector instanceof BigIntVector) {
return (VectorValueComparator<T>) new LongComparator();
} else if (vector instanceof Float4Vector) {
return (VectorValueComparator<T>) new Float4Comparator();
} else if (vector instanceof Float8Vector) {
return (VectorValueComparator<T>) new Float8Comparator();
}
} else if (vector instanceof BaseVariableWidthVector) {
return (VectorValueComparator<T>) new VariableWidthComparator();
}

throw new IllegalArgumentException("No default comparator for " + vector.getClass().getCanonicalName());
}

/**
* Default comparator for bytes.
* The comparison is based on values, with null comes first.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void testBinarySearchInt() {
negVector.set(0, -333);

// do search
VectorValueComparator<IntVector> comparator = new DefaultVectorComparators.IntComparator();
VectorValueComparator<IntVector> comparator =
DefaultVectorComparators.createDefaultComparator(rawVector);
for (int i = 0; i < VECTOR_LENGTH; i++) {
int result = VectorSearcher.binarySearch(rawVector, comparator, rawVector, i);
assertEquals(i, result);
Expand Down Expand Up @@ -100,7 +101,8 @@ public void testLinearSearchInt() {
negVector.set(0, -333);

// do search
VectorValueComparator<IntVector> comparator = new DefaultVectorComparators.IntComparator();
VectorValueComparator<IntVector> comparator =
DefaultVectorComparators.createDefaultComparator(rawVector);
for (int i = 0; i < VECTOR_LENGTH; i++) {
int result = VectorSearcher.linearSearch(rawVector, comparator, rawVector, i);
assertEquals(i, result);
Expand Down Expand Up @@ -139,7 +141,7 @@ public void testBinarySearchVarChar() {

// do search
VectorValueComparator<BaseVariableWidthVector> comparator =
new DefaultVectorComparators.VariableWidthComparator();
DefaultVectorComparators.createDefaultComparator(rawVector);
for (int i = 0; i < VECTOR_LENGTH; i++) {
int result = VectorSearcher.binarySearch(rawVector, comparator, rawVector, i);
assertEquals(i, result);
Expand Down Expand Up @@ -178,7 +180,7 @@ public void testLinearSearchVarChar() {

// do search
VectorValueComparator<BaseVariableWidthVector> comparator =
new DefaultVectorComparators.VariableWidthComparator();
DefaultVectorComparators.createDefaultComparator(rawVector);
for (int i = 0; i < VECTOR_LENGTH; i++) {
int result = VectorSearcher.linearSearch(rawVector, comparator, rawVector, i);
assertEquals(i, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testSortInt() {

// sort the vector
FixedWidthInPlaceVectorSorter sorter = new FixedWidthInPlaceVectorSorter();
DefaultVectorComparators.IntComparator comparator = new DefaultVectorComparators.IntComparator();
VectorValueComparator<IntVector> comparator = DefaultVectorComparators.createDefaultComparator(vec);

sorter.sortInPlace(vec, comparator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testSortByte() {

// sort the vector
FixedWidthOutOfPlaceVectorSorter sorter = new FixedWidthOutOfPlaceVectorSorter();
DefaultVectorComparators.ByteComparator comparator = new DefaultVectorComparators.ByteComparator();
VectorValueComparator<TinyIntVector> comparator = DefaultVectorComparators.createDefaultComparator(vec);

TinyIntVector sortedVec =
(TinyIntVector) vec.getField().getFieldType().createNewSingleVector("", allocator, null);
Expand Down Expand Up @@ -117,7 +117,7 @@ public void testSortShort() {

// sort the vector
FixedWidthOutOfPlaceVectorSorter sorter = new FixedWidthOutOfPlaceVectorSorter();
DefaultVectorComparators.ShortComparator comparator = new DefaultVectorComparators.ShortComparator();
VectorValueComparator<SmallIntVector> comparator = DefaultVectorComparators.createDefaultComparator(vec);

SmallIntVector sortedVec =
(SmallIntVector) vec.getField().getFieldType().createNewSingleVector("", allocator, null);
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testSortInt() {

// sort the vector
FixedWidthOutOfPlaceVectorSorter sorter = new FixedWidthOutOfPlaceVectorSorter();
DefaultVectorComparators.IntComparator comparator = new DefaultVectorComparators.IntComparator();
VectorValueComparator<IntVector> comparator = DefaultVectorComparators.createDefaultComparator(vec);

IntVector sortedVec = (IntVector) vec.getField().getFieldType().createNewSingleVector("", allocator, null);
sortedVec.allocateNew(vec.getValueCount());
Expand Down Expand Up @@ -210,7 +210,7 @@ public void testSortLong() {

// sort the vector
FixedWidthOutOfPlaceVectorSorter sorter = new FixedWidthOutOfPlaceVectorSorter();
DefaultVectorComparators.LongComparator comparator = new DefaultVectorComparators.LongComparator();
VectorValueComparator<BigIntVector> comparator = DefaultVectorComparators.createDefaultComparator(vec);

BigIntVector sortedVec = (BigIntVector) vec.getField().getFieldType().createNewSingleVector("", allocator, null);
sortedVec.allocateNew(vec.getValueCount());
Expand Down Expand Up @@ -256,7 +256,7 @@ public void testSortFloat() {

// sort the vector
FixedWidthOutOfPlaceVectorSorter sorter = new FixedWidthOutOfPlaceVectorSorter();
DefaultVectorComparators.Float4Comparator comparator = new DefaultVectorComparators.Float4Comparator();
VectorValueComparator<Float4Vector> comparator = DefaultVectorComparators.createDefaultComparator(vec);

Float4Vector sortedVec = (Float4Vector) vec.getField().getFieldType().createNewSingleVector("", allocator, null);
sortedVec.allocateNew(vec.getValueCount());
Expand Down Expand Up @@ -302,7 +302,7 @@ public void testSortDobule() {

// sort the vector
FixedWidthOutOfPlaceVectorSorter sorter = new FixedWidthOutOfPlaceVectorSorter();
DefaultVectorComparators.Float8Comparator comparator = new DefaultVectorComparators.Float8Comparator();
VectorValueComparator<Float8Vector> comparator = DefaultVectorComparators.createDefaultComparator(vec);

Float8Vector sortedVec = (Float8Vector) vec.getField().getFieldType().createNewSingleVector("", allocator, null);
sortedVec.allocateNew(vec.getValueCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BaseVariableWidthVector;
import org.apache.arrow.vector.VarCharVector;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -65,8 +66,8 @@ public void testSortString() {

// sort the vector
VariableWidthOutOfPlaceVectorSorter sorter = new VariableWidthOutOfPlaceVectorSorter();
DefaultVectorComparators.VariableWidthComparator comparator =
new DefaultVectorComparators.VariableWidthComparator();
VectorValueComparator<BaseVariableWidthVector> comparator =
DefaultVectorComparators.createDefaultComparator(vec);

VarCharVector sortedVec =
(VarCharVector) vec.getField().getFieldType().createNewSingleVector("", allocator, null);
Expand Down

0 comments on commit cbd8c3f

Please sign in to comment.