/
TestMapBlock.java
288 lines (255 loc) · 13 KB
/
TestMapBlock.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
284
285
286
287
288
/*
* Licensed 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 com.facebook.presto.block;
import com.facebook.presto.metadata.FunctionRegistry;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.block.ByteArrayBlock;
import com.facebook.presto.spi.block.MapBlockBuilder;
import com.facebook.presto.spi.block.SingleMapBlock;
import com.facebook.presto.spi.type.MapType;
import com.facebook.presto.spi.type.TypeManager;
import com.facebook.presto.sql.analyzer.FeaturesConfig;
import com.facebook.presto.type.TypeRegistry;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static com.facebook.presto.block.BlockAssertions.createLongsBlock;
import static com.facebook.presto.block.BlockAssertions.createStringsBlock;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.TinyintType.TINYINT;
import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
import static com.facebook.presto.util.StructuralTestUtil.mapType;
import static io.airlift.slice.Slices.utf8Slice;
import static java.util.Objects.requireNonNull;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;
public class TestMapBlock
extends AbstractTestBlock
{
private static final TypeManager TYPE_MANAGER = new TypeRegistry();
static {
// associate TYPE_MANAGER with a function registry
new FunctionRegistry(TYPE_MANAGER, new BlockEncodingManager(TYPE_MANAGER), new FeaturesConfig());
}
@Test
public void test()
{
testWith(createTestMap(9, 3, 4, 0, 8, 0, 6, 5));
}
@Test
public void testCompactBlock()
{
Block emptyBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]);
Block compactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes());
Block compactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes());
Block inCompactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes());
Block inCompactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes());
int[] offsets = {0, 1, 1, 2, 4, 8, 16};
boolean[] mapIsNull = {false, true, false, false, false, false};
testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.empty(), new int[1], emptyBlock, emptyBlock));
testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, compactKeyBlock, compactValueBlock));
// TODO: Add test case for a sliced MapBlock
// underlying key/value block is not compact
testIncompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, inCompactKeyBlock, inCompactValueBlock));
}
private Map<String, Long>[] createTestMap(int... entryCounts)
{
Map<String, Long>[] result = new Map[entryCounts.length];
for (int rowNumber = 0; rowNumber < entryCounts.length; rowNumber++) {
int entryCount = entryCounts[rowNumber];
Map<String, Long> map = new HashMap<>();
for (int entryNumber = 0; entryNumber < entryCount; entryNumber++) {
map.put("key" + entryNumber, entryNumber == 5 ? null : rowNumber * 100L + entryNumber);
}
result[rowNumber] = map;
}
return result;
}
private void testWith(Map<String, Long>[] expectedValues)
{
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);
Block block = createBlockWithValuesFromKeyValueBlock(expectedValues);
assertBlock(block, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlockFilteredPositions(expectedValues, block, () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, block, () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);
Map<String, Long>[] expectedValuesWithNull = (Map<String, Long>[]) alternatingNullValues(expectedValues);
BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull);
assertBlock(blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlock(blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
Block blockWithNull = createBlockWithValuesFromKeyValueBlock(expectedValuesWithNull);
assertBlock(blockWithNull, () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockWithNull, () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockWithNull, () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
}
private BlockBuilder createBlockBuilderWithValues(Map<String, Long>[] maps)
{
MapType mapType = mapType(VARCHAR, BIGINT);
BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
for (Map<String, Long> map : maps) {
createBlockBuilderWithValues(map, mapBlockBuilder);
}
return mapBlockBuilder;
}
private Block createBlockWithValuesFromKeyValueBlock(Map<String, Long>[] maps)
{
List<String> keys = new ArrayList<>();
List<Long> values = new ArrayList<>();
int[] offsets = new int[maps.length + 1];
boolean[] mapIsNull = new boolean[maps.length];
for (int i = 0; i < maps.length; i++) {
Map<String, Long> map = maps[i];
mapIsNull[i] = map == null;
if (map == null) {
offsets[i + 1] = offsets[i];
}
else {
for (Map.Entry<String, Long> entry : map.entrySet()) {
keys.add(entry.getKey());
values.add(entry.getValue());
}
offsets[i + 1] = offsets[i] + map.size();
}
}
return mapType(VARCHAR, BIGINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, createStringsBlock(keys), createLongsBlock(values));
}
private void createBlockBuilderWithValues(Map<String, Long> map, BlockBuilder mapBlockBuilder)
{
if (map == null) {
mapBlockBuilder.appendNull();
}
else {
BlockBuilder elementBlockBuilder = mapBlockBuilder.beginBlockEntry();
for (Map.Entry<String, Long> entry : map.entrySet()) {
VARCHAR.writeSlice(elementBlockBuilder, utf8Slice(entry.getKey()));
if (entry.getValue() == null) {
elementBlockBuilder.appendNull();
}
else {
BIGINT.writeLong(elementBlockBuilder, entry.getValue());
}
}
mapBlockBuilder.closeEntry();
}
}
@Override
protected <T> void assertPositionValue(Block block, int position, T expectedValue)
{
if (expectedValue instanceof Map) {
assertValue(block, position, (Map<String, Long>) expectedValue);
return;
}
super.assertPositionValue(block, position, expectedValue);
}
private void assertValue(Block mapBlock, int position, Map<String, Long> map)
{
MapType mapType = mapType(VARCHAR, BIGINT);
// null maps are handled by assertPositionValue
requireNonNull(map, "map is null");
assertFalse(mapBlock.isNull(position));
SingleMapBlock elementBlock = (SingleMapBlock) mapType.getObject(mapBlock, position);
assertEquals(elementBlock.getPositionCount(), map.size() * 2);
// Test new/hash-index access: assert inserted keys
for (Map.Entry<String, Long> entry : map.entrySet()) {
int pos = elementBlock.seekKey(utf8Slice(entry.getKey()));
assertNotEquals(pos, -1);
if (entry.getValue() == null) {
assertTrue(elementBlock.isNull(pos));
}
else {
assertFalse(elementBlock.isNull(pos));
assertEquals(BIGINT.getLong(elementBlock, pos), (long) entry.getValue());
}
}
// Test new/hash-index access: assert non-existent keys
for (int i = 0; i < 10; i++) {
assertEquals(elementBlock.seekKey(utf8Slice("not-inserted-" + i)), -1);
}
// Test legacy/iterative access
for (int i = 0; i < elementBlock.getPositionCount(); i += 2) {
String actualKey = VARCHAR.getSlice(elementBlock, i).toStringUtf8();
Long actualValue;
if (elementBlock.isNull(i + 1)) {
actualValue = null;
}
else {
actualValue = BIGINT.getLong(elementBlock, i + 1);
}
assertTrue(map.containsKey(actualKey));
assertEquals(actualValue, map.get(actualKey));
}
}
@Test
public void testCloseEntryStrict()
throws Exception
{
MapType mapType = mapType(BIGINT, BIGINT);
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, 1);
// Add 100 maps with only one entry but the same key
for (int i = 0; i < 100; i++) {
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(entryBuilder, 1);
BIGINT.writeLong(entryBuilder, -1);
mapBlockBuilder.closeEntry();
}
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
// Add 50 keys so we get some chance to get hash conflict
// The purpose of this test is to make sure offset is calculated correctly in MapBlockBuilder.closeEntryStrict()
for (int i = 0; i < 50; i++) {
BIGINT.writeLong(entryBuilder, i);
BIGINT.writeLong(entryBuilder, -1);
}
mapBlockBuilder.closeEntryStrict();
}
@Test
public void testEstimatedDataSizeForStats()
{
Map<String, Long>[] expectedValues = (Map<String, Long>[]) alternatingNullValues(createTestMap(9, 3, 4, 0, 8, 0, 6, 5));
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
Block block = blockBuilder.build();
assertEquals(block.getPositionCount(), expectedValues.length);
for (int i = 0; i < block.getPositionCount(); i++) {
int expectedSize = getExpectedEstimatedDataSize(expectedValues[i]);
assertEquals(blockBuilder.getEstimatedDataSizeForStats(i), expectedSize);
assertEquals(block.getEstimatedDataSizeForStats(i), expectedSize);
}
}
private static int getExpectedEstimatedDataSize(Map<String, Long> map)
{
if (map == null) {
return 0;
}
int size = 0;
for (Map.Entry<String, Long> entry : map.entrySet()) {
size += entry.getKey().length();
size += entry.getValue() == null ? 0 : Long.BYTES;
}
return size;
}
}