forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicBlockTests.java
283 lines (254 loc) · 12.9 KB
/
BasicBlockTests.java
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.compute.data;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.test.ESTestCase;
import java.util.BitSet;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
public class BasicBlockTests extends ESTestCase {
public void testEmpty() {
Block intBlock = new IntArrayBlock(new int[] {}, 0);
assertThat(0, is(intBlock.getPositionCount()));
Block longBlock = new LongArrayBlock(new long[] {}, 0);
assertThat(0, is(longBlock.getPositionCount()));
Block doubleBlock = new DoubleArrayBlock(new double[] {}, 0);
assertThat(0, is(doubleBlock.getPositionCount()));
}
public void testIntBlock() {
for (int i = 0; i < 1000; i++) {
int positionCount = randomIntBetween(1, 16 * 1024);
int[] values = IntStream.range(0, positionCount).toArray();
Block block = new IntArrayBlock(values, positionCount);
assertThat(positionCount, is(block.getPositionCount()));
assertThat(0, is(block.getInt(0)));
assertThat(positionCount - 1, is(block.getInt(positionCount - 1)));
int pos = block.getInt(randomIntBetween(0, positionCount - 1));
assertThat(pos, is(block.getInt(pos)));
assertThat((long) pos, is(block.getLong(pos)));
assertThat((double) pos, is(block.getDouble(pos)));
assertNullValues(
positionCount,
nulls -> new IntArrayBlock(values, positionCount, nulls),
(randomNonNullPosition, b) -> { assertThat((int) randomNonNullPosition, is(b.getInt(randomNonNullPosition.intValue()))); }
);
}
}
public void testConstantIntBlock() {
for (int i = 0; i < 1000; i++) {
int positionCount = randomIntBetween(0, Integer.MAX_VALUE);
int value = randomInt();
Block block = new ConstantIntBlock(value, positionCount);
assertThat(positionCount, is(block.getPositionCount()));
assertThat(value, is(block.getInt(0)));
assertThat(value, is(block.getInt(positionCount - 1)));
assertThat(value, is(block.getInt(randomIntBetween(1, positionCount - 1))));
}
}
public void testLongBlock() {
for (int i = 0; i < 1000; i++) {
int positionCount = randomIntBetween(1, 16 * 1024);
long[] values = LongStream.range(0, positionCount).toArray();
Block block = new LongArrayBlock(values, positionCount);
assertThat(positionCount, is(block.getPositionCount()));
assertThat(0L, is(block.getLong(0)));
assertThat((long) positionCount - 1, is(block.getLong(positionCount - 1)));
int pos = (int) block.getLong(randomIntBetween(0, positionCount - 1));
assertThat((long) pos, is(block.getLong(pos)));
assertThat((double) pos, is(block.getDouble(pos)));
assertNullValues(
positionCount,
nulls -> new LongArrayBlock(values, positionCount, nulls),
(randomNonNullPosition, b) -> { assertThat((long) randomNonNullPosition, is(b.getLong(randomNonNullPosition.intValue()))); }
);
}
}
public void testConstantLongBlock() {
for (int i = 0; i < 1000; i++) {
int positionCount = randomIntBetween(1, Integer.MAX_VALUE);
long value = randomLong();
Block block = new ConstantLongBlock(value, positionCount);
assertThat(positionCount, is(block.getPositionCount()));
assertThat(value, is(block.getLong(0)));
assertThat(value, is(block.getLong(positionCount - 1)));
assertThat(value, is(block.getLong(randomIntBetween(1, positionCount - 1))));
}
}
public void testDoubleBlock() {
for (int i = 0; i < 1000; i++) {
int positionCount = randomIntBetween(1, 16 * 1024);
double[] values = LongStream.range(0, positionCount).asDoubleStream().toArray();
Block block = new DoubleArrayBlock(values, positionCount);
assertThat(positionCount, is(block.getPositionCount()));
assertThat(0d, is(block.getDouble(0)));
assertThat((double) positionCount - 1, is(block.getDouble(positionCount - 1)));
int pos = (int) block.getDouble(randomIntBetween(0, positionCount - 1));
assertThat((double) pos, is(block.getDouble(pos)));
expectThrows(UOE, () -> block.getInt(pos));
expectThrows(UOE, () -> block.getLong(pos));
assertNullValues(
positionCount,
nulls -> new DoubleArrayBlock(values, positionCount, nulls),
(randomNonNullPosition, b) -> {
assertThat((double) randomNonNullPosition, is(b.getDouble(randomNonNullPosition.intValue())));
}
);
}
}
public void testConstantDoubleBlock() {
for (int i = 0; i < 1000; i++) {
int positionCount = randomIntBetween(1, Integer.MAX_VALUE);
double value = randomDouble();
Block block = new ConstantDoubleBlock(value, positionCount);
assertThat(positionCount, is(block.getPositionCount()));
assertThat(value, is(block.getDouble(0)));
assertThat(value, is(block.getDouble(positionCount - 1)));
assertThat(value, is(block.getDouble(randomIntBetween(1, positionCount - 1))));
assertThat(
block.getObject(randomIntBetween(1, positionCount - 1)),
is(block.getDouble(randomIntBetween(1, positionCount - 1)))
);
}
}
public void testBytesRefBlock() {
int positionCount = randomIntBetween(0, 16 * 1024);
BytesRefArrayBlock.Builder builder = BytesRefArrayBlock.builder(positionCount);
BytesRef[] values = new BytesRef[positionCount];
for (int i = 0; i < positionCount; i++) {
BytesRef bytesRef = new BytesRef(randomByteArrayOfLength(between(1, 20)));
if (bytesRef.length > 0 && randomBoolean()) {
bytesRef.offset = randomIntBetween(0, bytesRef.length - 1);
bytesRef.length = randomIntBetween(0, bytesRef.length - bytesRef.offset);
}
values[i] = bytesRef;
if (randomBoolean()) {
bytesRef = BytesRef.deepCopyOf(bytesRef);
}
builder.append(bytesRef);
}
BytesRefArrayBlock block = builder.build();
assertThat(positionCount, is(block.getPositionCount()));
BytesRef bytes = new BytesRef();
for (int i = 0; i < positionCount; i++) {
int pos = randomIntBetween(0, positionCount - 1);
bytes = block.getBytesRef(pos, bytes);
assertThat(bytes, equalTo(values[pos]));
assertThat(block.getObject(pos), equalTo(values[pos]));
expectThrows(UOE, () -> block.getInt(pos));
expectThrows(UOE, () -> block.getLong(pos));
expectThrows(UOE, () -> block.getDouble(pos));
}
assertNullValues(
positionCount,
nulls -> new BytesRefArrayBlock(positionCount, builder.getBytes(), nulls),
(randomNonNullPosition, b) -> assertThat(
values[randomNonNullPosition],
is(b.getBytesRef(randomNonNullPosition, new BytesRef()))
)
);
}
public void testBytesRefBlockBuilder() {
int positionCount = randomIntBetween(1, 128);
BytesRefArrayBlock.Builder builder = BytesRefArrayBlock.builder(positionCount);
int firstBatch = randomIntBetween(0, positionCount - 1);
for (int i = 0; i < firstBatch; i++) {
builder.append(new BytesRef(randomByteArrayOfLength(between(1, 20))));
IllegalStateException error = expectThrows(IllegalStateException.class, builder::build);
assertThat(error.getMessage(), startsWith("Incomplete block; expected "));
}
int secondBatch = positionCount - firstBatch;
for (int i = 0; i < secondBatch; i++) {
IllegalStateException error = expectThrows(IllegalStateException.class, builder::build);
assertThat(error.getMessage(), startsWith("Incomplete block; expected "));
builder.append(new BytesRef(randomByteArrayOfLength(between(1, 20))));
}
int extra = between(1, 10);
for (int i = 0; i < extra; i++) {
BytesRef bytes = new BytesRef(randomByteArrayOfLength(between(1, 20)));
IllegalStateException error = expectThrows(IllegalStateException.class, () -> builder.append(bytes));
assertThat(error.getMessage(), startsWith("Block is full; "));
}
BytesRefArrayBlock block = builder.build();
assertThat(block.getPositionCount(), equalTo(positionCount));
}
public void testBytesRefBlockBuilderWithNulls() {
int positionCount = randomIntBetween(0, 16 * 1024);
BytesRefArrayBlock.Builder builder = BytesRefArrayBlock.builder(positionCount);
BytesRef[] values = new BytesRef[positionCount];
for (int i = 0; i < positionCount; i++) {
if (randomBoolean()) {
// Add random sparseness
builder.appendNull();
values[i] = null;
} else {
BytesRef bytesRef = new BytesRef(randomByteArrayOfLength(between(1, 20)));
if (bytesRef.length > 0 && randomBoolean()) {
bytesRef.offset = randomIntBetween(0, bytesRef.length - 1);
bytesRef.length = randomIntBetween(0, bytesRef.length - bytesRef.offset);
}
values[i] = bytesRef;
if (randomBoolean()) {
bytesRef = BytesRef.deepCopyOf(bytesRef);
}
builder.append(bytesRef);
}
}
BytesRefArrayBlock block = builder.build();
assertThat(positionCount, is(block.getPositionCount()));
BytesRef bytes = new BytesRef();
for (int i = 0; i < positionCount; i++) {
int pos = randomIntBetween(0, positionCount - 1);
bytes = block.getBytesRef(pos, bytes);
if (values[pos] == null) {
assertThat(block.isNull(pos), equalTo(true));
assertThat(bytes, equalTo(new BytesRef()));
} else {
assertThat(bytes, equalTo(values[pos]));
assertThat(block.getObject(pos), equalTo(values[pos]));
}
expectThrows(UOE, () -> block.getInt(pos));
expectThrows(UOE, () -> block.getLong(pos));
expectThrows(UOE, () -> block.getDouble(pos));
}
}
public void testConstantBytesRefBlock() {
for (int i = 0; i < 1000; i++) {
int positionCount = randomIntBetween(1, Integer.MAX_VALUE);
BytesRef value = new BytesRef(randomByteArrayOfLength(between(1, 20)));
Block block = new ConstantBytesRefBlock(value, positionCount);
assertThat(block.getPositionCount(), is(positionCount));
assertThat(block.getObject(0), is(value));
assertThat(block.getObject(positionCount - 1), is(value));
assertThat(block.getObject(randomIntBetween(1, positionCount - 1)), is(value));
BytesRef bytes = new BytesRef();
bytes = block.getBytesRef(0, bytes);
assertThat(bytes, is(value));
bytes = block.getBytesRef(positionCount - 1, bytes);
assertThat(bytes, is(value));
bytes = block.getBytesRef(randomIntBetween(1, positionCount - 1), bytes);
assertThat(bytes, is(value));
}
}
private void assertNullValues(int positionCount, Function<BitSet, Block> blockConstructor, BiConsumer<Integer, Block> asserter) {
int randomNullPosition = randomIntBetween(0, positionCount - 1);
int randomNonNullPosition = randomValueOtherThan(randomNullPosition, () -> randomIntBetween(0, positionCount - 1));
BitSet nullsMask = new BitSet(positionCount);
nullsMask.set(randomNullPosition);
Block block = blockConstructor.apply(nullsMask);
assertThat(positionCount, is(block.getPositionCount()));
asserter.accept(randomNonNullPosition, block);
assertTrue(block.isNull(randomNullPosition));
assertFalse(block.isNull(randomNonNullPosition));
}
static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
}