use of org.apache.drill.exec.vector.complex.impl.SingleDictWriter in project drill by apache.
the class TestDictVector method testSplitAndTransfer.
@Test
public void testSplitAndTransfer() {
MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
try (DictVector mapVector = new DictVector(field, allocator, null)) {
mapVector.allocateNew();
List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, 3L, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, 8L, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, 12L, 0.03f, 13L));
BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
int index = 0;
for (Map<Object, Object> map : maps) {
mapWriter.setPosition(index++);
mapWriter.start();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
mapWriter.startKeyValuePair();
mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((long) entry.getValue());
mapWriter.endKeyValuePair();
}
mapWriter.end();
}
int start = 1;
int length = 2;
DictVector newMapVector = new DictVector(field, allocator, null);
TransferPair transferPair = mapVector.makeTransferPair(newMapVector);
transferPair.splitAndTransfer(start, length);
BaseReader.DictReader mapReader = newMapVector.getReader();
index = 0;
for (Map<Object, Object> map : maps.subList(start, start + length)) {
mapReader.setPosition(index++);
for (Map.Entry<Object, Object> entry : map.entrySet()) {
mapReader.next();
Float actualKey = mapReader.reader(DictVector.FIELD_KEY_NAME).readFloat();
Long actualValue = mapReader.reader(DictVector.FIELD_VALUE_NAME).readLong();
assertEquals(entry.getKey(), actualKey);
assertEquals(entry.getValue(), actualValue);
}
}
newMapVector.clear();
}
}
use of org.apache.drill.exec.vector.complex.impl.SingleDictWriter in project drill by apache.
the class TestDictVector method testGetByKey.
@Test
public void testGetByKey() {
MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
try (DictVector mapVector = new DictVector(field, allocator, null)) {
mapVector.allocateNew();
List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, 3L, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, 8L, 4f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(-2.5f, 11L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 12L, 9.73f, 13L, 4f, 14L));
BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
int index = 0;
for (Map<Object, Object> map : maps) {
mapWriter.setPosition(index++);
mapWriter.start();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
mapWriter.startKeyValuePair();
mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((long) entry.getValue());
mapWriter.endKeyValuePair();
}
mapWriter.end();
}
BaseReader.DictReader mapReader = mapVector.getReader();
float key = 4.0f;
// Due to limitations of Calcite, we can pass NameSegment and ArraySegment
// only as String and int respectively hence we need to transform float key
// to String to be able to use it with DictReader which then
// will derive appropriate type internally.
String stringKey = String.valueOf(key);
NullableBigIntHolder valueHolder = new NullableBigIntHolder();
index = 0;
for (Map<Object, Object> map : maps) {
mapReader.setPosition(index++);
mapReader.next();
mapReader.read(stringKey, valueHolder);
assertEquals(map.get(key), valueHolder.isSet == 1 ? valueHolder.value : null);
// reset value holder to reuse it for the next row
valueHolder.isSet = 0;
}
}
}
use of org.apache.drill.exec.vector.complex.impl.SingleDictWriter in project drill by apache.
the class TestDictVector method testLoadValueVector.
@Test
public void testLoadValueVector() {
MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
try (DictVector mapVector = new DictVector(field, allocator, null)) {
mapVector.allocateNew();
List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, 3L, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, 8L, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, 12L, 0.03f, 13L));
BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
int index = 0;
for (Map<Object, Object> map : maps) {
mapWriter.setPosition(index++);
mapWriter.start();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
mapWriter.startKeyValuePair();
mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((long) entry.getValue());
mapWriter.endKeyValuePair();
}
mapWriter.end();
}
UserBitShared.SerializedField oldField = mapVector.getMetadata();
WritableBatch writableBatch = WritableBatch.getBatchNoHV(oldField.getValueCount(), Collections.singletonList(mapVector), false);
DrillBuf byteBuf = TestLoad.serializeBatch(allocator, writableBatch);
DictVector newMapVector = new DictVector(field.clone(), allocator, null);
newMapVector.load(oldField, byteBuf);
BaseReader.DictReader mapReader = newMapVector.getReader();
index = 0;
for (Map<Object, Object> map : maps) {
mapReader.setPosition(index++);
for (Map.Entry<Object, Object> entry : map.entrySet()) {
mapReader.next();
Float actualKey = mapReader.reader(DictVector.FIELD_KEY_NAME).readFloat();
Long actualValue = mapReader.reader(DictVector.FIELD_VALUE_NAME).readLong();
assertEquals(entry.getKey(), actualKey);
assertEquals(entry.getValue(), actualValue);
}
}
newMapVector.clear();
byteBuf.release();
writableBatch.clear();
}
}
Aggregations