Repository navigation
Expand file tree
/
Copy pathTestSearchDictionaryEncoder.java
More file actions
357 lines (303 loc) · 13.3 KB
/
Copy pathTestSearchDictionaryEncoder.java
File metadata and controls
357 lines (303 loc) · 13.3 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* 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.dictionary;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Random;
import org.apache.arrow.algorithm.sort.DefaultVectorComparators;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.VarBinaryVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.dictionary.Dictionary;
import org.apache.arrow.vector.dictionary.DictionaryEncoder;
import org.apache.arrow.vector.types.pojo.DictionaryEncoding;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Test cases for {@link SearchDictionaryEncoder}.
*/
public class TestSearchDictionaryEncoder {
private final int VECTOR_LENGTH = 50;
private final int DICTIONARY_LENGTH = 10;
private BufferAllocator allocator;
byte[] zero = "000".getBytes(StandardCharsets.UTF_8);
byte[] one = "111".getBytes(StandardCharsets.UTF_8);
byte[] two = "222".getBytes(StandardCharsets.UTF_8);
byte[][] data = new byte[][]{zero, one, two};
@Before
public void prepare() {
allocator = new RootAllocator(1024 * 1024);
}
@After
public void shutdown() {
allocator.close();
}
@Test
public void testEncodeAndDecode() {
Random random = new Random();
try (VarCharVector rawVector = new VarCharVector("original vector", allocator);
IntVector encodedVector = new IntVector("encoded vector", allocator);
VarCharVector dictionary = new VarCharVector("dictionary", allocator)) {
// set up dictionary
dictionary.allocateNew();
for (int i = 0; i < DICTIONARY_LENGTH; i++) {
// encode "i" as i
dictionary.setSafe(i, String.valueOf(i).getBytes());
}
dictionary.setValueCount(DICTIONARY_LENGTH);
// set up raw vector
rawVector.allocateNew(10 * VECTOR_LENGTH, VECTOR_LENGTH);
for (int i = 0; i < VECTOR_LENGTH; i++) {
int val = (random.nextInt() & Integer.MAX_VALUE) % DICTIONARY_LENGTH;
rawVector.set(i, String.valueOf(val).getBytes());
}
rawVector.setValueCount(VECTOR_LENGTH);
SearchDictionaryEncoder<IntVector, VarCharVector> encoder =
new SearchDictionaryEncoder<>(
dictionary, DefaultVectorComparators.createDefaultComparator(rawVector), false);
// perform encoding
encodedVector.allocateNew();
encoder.encode(rawVector, encodedVector);
// verify encoding results
assertEquals(rawVector.getValueCount(), encodedVector.getValueCount());
for (int i = 0; i < VECTOR_LENGTH; i++) {
assertArrayEquals(rawVector.get(i), String.valueOf(encodedVector.get(i)).getBytes());
}
// perform decoding
Dictionary dict = new Dictionary(dictionary, new DictionaryEncoding(1L, false, null));
try (VarCharVector decodedVector = (VarCharVector) DictionaryEncoder.decode(encodedVector, dict)) {
// verify decoding results
assertEquals(encodedVector.getValueCount(), decodedVector.getValueCount());
for (int i = 0; i < VECTOR_LENGTH; i++) {
assertArrayEquals(String.valueOf(encodedVector.get(i)).getBytes(), decodedVector.get(i));
}
}
}
}
@Test
public void testEncodeAndDecodeWithNull() {
Random random = new Random();
try (VarCharVector rawVector = new VarCharVector("original vector", allocator);
IntVector encodedVector = new IntVector("encoded vector", allocator);
VarCharVector dictionary = new VarCharVector("dictionary", allocator)) {
// set up dictionary
dictionary.allocateNew();
dictionary.setNull(0);
for (int i = 1; i < DICTIONARY_LENGTH; i++) {
// encode "i" as i
dictionary.setSafe(i, String.valueOf(i).getBytes());
}
dictionary.setValueCount(DICTIONARY_LENGTH);
// set up raw vector
rawVector.allocateNew(10 * VECTOR_LENGTH, VECTOR_LENGTH);
for (int i = 0; i < VECTOR_LENGTH; i++) {
if (i % 10 == 0) {
rawVector.setNull(i);
} else {
int val = (random.nextInt() & Integer.MAX_VALUE) % (DICTIONARY_LENGTH - 1) + 1;
rawVector.set(i, String.valueOf(val).getBytes());
}
}
rawVector.setValueCount(VECTOR_LENGTH);
SearchDictionaryEncoder<IntVector, VarCharVector> encoder =
new SearchDictionaryEncoder<>(
dictionary, DefaultVectorComparators.createDefaultComparator(rawVector), true);
// perform encoding
encodedVector.allocateNew();
encoder.encode(rawVector, encodedVector);
// verify encoding results
assertEquals(rawVector.getValueCount(), encodedVector.getValueCount());
for (int i = 0; i < VECTOR_LENGTH; i++) {
if (i % 10 == 0) {
assertEquals(0, encodedVector.get(i));
} else {
assertArrayEquals(rawVector.get(i), String.valueOf(encodedVector.get(i)).getBytes());
}
}
// perform decoding
Dictionary dict = new Dictionary(dictionary, new DictionaryEncoding(1L, false, null));
try (VarCharVector decodedVector = (VarCharVector) DictionaryEncoder.decode(encodedVector, dict)) {
// verify decoding results
assertEquals(encodedVector.getValueCount(), decodedVector.getValueCount());
for (int i = 0; i < VECTOR_LENGTH; i++) {
if (i % 10 == 0) {
assertTrue(decodedVector.isNull(i));
} else {
assertArrayEquals(String.valueOf(encodedVector.get(i)).getBytes(), decodedVector.get(i));
}
}
}
}
}
@Test
public void testEncodeNullWithoutNullInDictionary() {
try (VarCharVector rawVector = new VarCharVector("original vector", allocator);
IntVector encodedVector = new IntVector("encoded vector", allocator);
VarCharVector dictionary = new VarCharVector("dictionary", allocator)) {
// set up dictionary, with no null in it.
dictionary.allocateNew();
for (int i = 0; i < DICTIONARY_LENGTH; i++) {
// encode "i" as i
dictionary.setSafe(i, String.valueOf(i).getBytes());
}
dictionary.setValueCount(DICTIONARY_LENGTH);
// the vector to encode has a null inside.
rawVector.allocateNew(1);
rawVector.setNull(0);
rawVector.setValueCount(1);
encodedVector.allocateNew();
SearchDictionaryEncoder<IntVector, VarCharVector> encoder =
new SearchDictionaryEncoder<>(
dictionary, DefaultVectorComparators.createDefaultComparator(rawVector), true);
// the encoder should encode null, but no null in the dictionary,
// so an exception should be thrown.
assertThrows(IllegalArgumentException.class, () -> {
encoder.encode(rawVector, encodedVector);
});
}
}
@Test
public void testEncodeStrings() {
// Create a new value vector
try (final VarCharVector vector = new VarCharVector("foo", allocator);
final IntVector encoded = new IntVector("encoded", allocator);
final VarCharVector dictionaryVector = new VarCharVector("dict", allocator)) {
vector.allocateNew(512, 5);
encoded.allocateNew();
// set some values
vector.setSafe(0, zero, 0, zero.length);
vector.setSafe(1, one, 0, one.length);
vector.setSafe(2, one, 0, one.length);
vector.setSafe(3, two, 0, two.length);
vector.setSafe(4, zero, 0, zero.length);
vector.setValueCount(5);
// set some dictionary values
dictionaryVector.allocateNew(512, 3);
dictionaryVector.setSafe(0, zero, 0, one.length);
dictionaryVector.setSafe(1, one, 0, two.length);
dictionaryVector.setSafe(2, two, 0, zero.length);
dictionaryVector.setValueCount(3);
SearchDictionaryEncoder<IntVector, VarCharVector> encoder =
new SearchDictionaryEncoder<>(
dictionaryVector, DefaultVectorComparators.createDefaultComparator(vector));
encoder.encode(vector, encoded);
// verify indices
assertEquals(5, encoded.getValueCount());
assertEquals(0, encoded.get(0));
assertEquals(1, encoded.get(1));
assertEquals(1, encoded.get(2));
assertEquals(2, encoded.get(3));
assertEquals(0, encoded.get(4));
// now run through the decoder and verify we get the original back
Dictionary dict = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
try (VarCharVector decoded = (VarCharVector) DictionaryEncoder.decode(encoded, dict)) {
assertEquals(vector.getValueCount(), decoded.getValueCount());
for (int i = 0; i < 5; i++) {
assertEquals(vector.getObject(i), decoded.getObject(i));
}
}
}
}
@Test
public void testEncodeLargeVector() {
// Create a new value vector
try (final VarCharVector vector = new VarCharVector("foo", allocator);
final IntVector encoded = new IntVector("encoded", allocator);
final VarCharVector dictionaryVector = new VarCharVector("dict", allocator)) {
vector.allocateNew();
encoded.allocateNew();
int count = 10000;
for (int i = 0; i < 10000; ++i) {
vector.setSafe(i, data[i % 3], 0, data[i % 3].length);
}
vector.setValueCount(count);
dictionaryVector.allocateNew(512, 3);
dictionaryVector.setSafe(0, zero, 0, one.length);
dictionaryVector.setSafe(1, one, 0, two.length);
dictionaryVector.setSafe(2, two, 0, zero.length);
dictionaryVector.setValueCount(3);
SearchDictionaryEncoder<IntVector, VarCharVector> encoder =
new SearchDictionaryEncoder<>(
dictionaryVector, DefaultVectorComparators.createDefaultComparator(vector));
encoder.encode(vector, encoded);
assertEquals(count, encoded.getValueCount());
for (int i = 0; i < count; ++i) {
assertEquals(i % 3, encoded.get(i));
}
// now run through the decoder and verify we get the original back
Dictionary dict = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
try (VarCharVector decoded = (VarCharVector) DictionaryEncoder.decode(encoded, dict)) {
assertEquals(vector.getClass(), decoded.getClass());
assertEquals(vector.getValueCount(), decoded.getValueCount());
for (int i = 0; i < count; ++i) {
assertEquals(vector.getObject(i), decoded.getObject(i));
}
}
}
}
@Test
public void testEncodeBinaryVector() {
// Create a new value vector
try (final VarBinaryVector vector = new VarBinaryVector("foo", allocator);
final VarBinaryVector dictionaryVector = new VarBinaryVector("dict", allocator);
final IntVector encoded = new IntVector("encoded", allocator)) {
vector.allocateNew(512, 5);
vector.allocateNew();
encoded.allocateNew();
// set some values
vector.setSafe(0, zero, 0, zero.length);
vector.setSafe(1, one, 0, one.length);
vector.setSafe(2, one, 0, one.length);
vector.setSafe(3, two, 0, two.length);
vector.setSafe(4, zero, 0, zero.length);
vector.setValueCount(5);
// set some dictionary values
dictionaryVector.allocateNew(512, 3);
dictionaryVector.setSafe(0, zero, 0, one.length);
dictionaryVector.setSafe(1, one, 0, two.length);
dictionaryVector.setSafe(2, two, 0, zero.length);
dictionaryVector.setValueCount(3);
SearchDictionaryEncoder<IntVector, VarBinaryVector> encoder =
new SearchDictionaryEncoder<>(
dictionaryVector, DefaultVectorComparators.createDefaultComparator(vector));
encoder.encode(vector, encoded);
assertEquals(5, encoded.getValueCount());
assertEquals(0, encoded.get(0));
assertEquals(1, encoded.get(1));
assertEquals(1, encoded.get(2));
assertEquals(2, encoded.get(3));
assertEquals(0, encoded.get(4));
// now run through the decoder and verify we get the original back
Dictionary dict = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
try (VarBinaryVector decoded = (VarBinaryVector) DictionaryEncoder.decode(encoded, dict)) {
assertEquals(vector.getClass(), decoded.getClass());
assertEquals(vector.getValueCount(), decoded.getValueCount());
for (int i = 0; i < 5; i++) {
Assert.assertTrue(Arrays.equals(vector.getObject(i), decoded.getObject(i)));
}
}
}
}
}