Repository navigation
Expand file tree
/
Copy pathTestFixedWidthSorting.java
More file actions
172 lines (141 loc) · 5.94 KB
/
Copy pathTestFixedWidthSorting.java
File metadata and controls
172 lines (141 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.arrow.algorithm.sort;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BaseFixedWidthVector;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
/**
* Test sorting fixed width vectors with random data.
*/
@RunWith(Parameterized.class)
public class TestFixedWidthSorting<V extends BaseFixedWidthVector, U extends Comparable<U>> {
static final int[] VECTOR_LENGTHS = new int[] {2, 5, 10, 50, 100, 1000, 3000};
static final double[] NULL_FRACTIONS = {0, 0.1, 0.3, 0.5, 0.7, 0.9, 1};
private final int length;
private final double nullFraction;
private final boolean inPlace;
private final Function<BufferAllocator, V> vectorGenerator;
private final TestSortingUtil.DataGenerator<V, U> dataGenerator;
private BufferAllocator allocator;
@Before
public void prepare() {
allocator = new RootAllocator(Integer.MAX_VALUE);
}
@After
public void shutdown() {
allocator.close();
}
public TestFixedWidthSorting(
int length, double nullFraction, boolean inPlace, String desc,
Function<BufferAllocator, V> vectorGenerator, TestSortingUtil.DataGenerator<V, U> dataGenerator) {
this.length = length;
this.nullFraction = nullFraction;
this.inPlace = inPlace;
this.vectorGenerator = vectorGenerator;
this.dataGenerator = dataGenerator;
}
@Test
public void testSort() {
if (inPlace) {
sortInPlace();
} else {
sortOutOfPlace();
}
}
void sortInPlace() {
try (V vector = vectorGenerator.apply(allocator)) {
U[] array = dataGenerator.populate(vector, length, nullFraction);
TestSortingUtil.sortArray(array);
FixedWidthInPlaceVectorSorter sorter = new FixedWidthInPlaceVectorSorter();
VectorValueComparator<V> comparator = DefaultVectorComparators.createDefaultComparator(vector);
sorter.sortInPlace(vector, comparator);
TestSortingUtil.verifyResults(vector, array);
}
}
void sortOutOfPlace() {
try (V vector = vectorGenerator.apply(allocator)) {
U[] array = dataGenerator.populate(vector, length, nullFraction);
TestSortingUtil.sortArray(array);
// sort the vector
FixedWidthOutOfPlaceVectorSorter sorter = new FixedWidthOutOfPlaceVectorSorter();
VectorValueComparator<V> comparator = DefaultVectorComparators.createDefaultComparator(vector);
try (V sortedVec = (V) vector.getField().getFieldType().createNewSingleVector("", allocator, null)) {
sortedVec.allocateNew(vector.getValueCount());
sortedVec.setValueCount(vector.getValueCount());
sorter.sortOutOfPlace(vector, sortedVec, comparator);
// verify results
TestSortingUtil.verifyResults(sortedVec, array);
}
}
}
@Parameterized.Parameters(name = "length = {0}, null fraction = {1}, in place = {2}, vector = {3}")
public static Collection<Object[]> getParameters() {
List<Object[]> params = new ArrayList<>();
for (int length : VECTOR_LENGTHS) {
for (double nullFrac : NULL_FRACTIONS) {
for (boolean inPlace : new boolean[] {true, false}) {
params.add(new Object[] {
length, nullFrac, inPlace, "TinyIntVector",
(Function<BufferAllocator, TinyIntVector>) (allocator -> new TinyIntVector("vector", allocator)),
TestSortingUtil.TINY_INT_GENERATOR
});
params.add(new Object[] {
length, nullFrac, inPlace, "SmallIntVector",
(Function<BufferAllocator, SmallIntVector>) (allocator -> new SmallIntVector("vector", allocator)),
TestSortingUtil.SMALL_INT_GENERATOR
});
params.add(new Object[] {
length, nullFrac, inPlace, "IntVector",
(Function<BufferAllocator, IntVector>) (allocator -> new IntVector("vector", allocator)),
TestSortingUtil.INT_GENERATOR
});
params.add(new Object[] {
length, nullFrac, inPlace, "BigIntVector",
(Function<BufferAllocator, BigIntVector>) (allocator -> new BigIntVector("vector", allocator)),
TestSortingUtil.LONG_GENERATOR
});
params.add(new Object[] {
length, nullFrac, inPlace, "Float4Vector",
(Function<BufferAllocator, Float4Vector>) (allocator -> new Float4Vector("vector", allocator)),
TestSortingUtil.FLOAT_GENERATOR
});
params.add(new Object[] {
length, nullFrac, inPlace, "Float8Vector",
(Function<BufferAllocator, Float8Vector>) (allocator -> new Float8Vector("vector", allocator)),
TestSortingUtil.DOUBLE_GENERATOR
});
}
}
}
return params;
}
}