use of org.apache.drill.exec.vector.complex.DictVector in project drill by apache.
the class ColumnBuilder method buildSingleDict.
private ColumnState buildSingleDict(ContainerState parent, ColumnMetadata columnSchema) {
final ProjectionFilter projFilter = parent.projection();
final ProjResult projResult = projFilter.projection(columnSchema);
// Create the dict's offset vector.
final DictVector dictVector;
final UInt4Vector offsetVector;
if (projResult.isProjected) {
// Creating the dict vector will create its contained vectors if we
// give it a materialized field with children. So, instead pass a clone
// without children so we can add them.
final ColumnMetadata dictColMetadata = columnSchema.cloneEmpty();
// vectors can be cached.
assert columnSchema.tupleSchema().isEmpty();
dictVector = new DictVector(dictColMetadata.schema(), parent.loader().allocator(), null);
offsetVector = dictVector.getOffsetVector();
} else {
dictVector = null;
offsetVector = null;
}
// Create the writer using the offset vector
final AbstractObjectWriter writer = ObjectDictWriter.buildDict(columnSchema, dictVector, new ArrayList<>());
// Wrap the offset vector in a vector state
final VectorState offsetVectorState;
if (!projResult.isProjected) {
offsetVectorState = new NullVectorState();
} else {
offsetVectorState = new OffsetVectorState((((AbstractArrayWriter) writer.dict()).offsetWriter()), offsetVector, writer.dict().entry().events());
}
final VectorState mapVectorState = new TupleState.SingleDictVectorState(dictVector, offsetVectorState);
// Assemble it all into the column state.
final SingleDictState dictArrayState = new SingleDictState(parent.loader(), parent.vectorCache().childCache(columnSchema.name()), projResult.mapFilter);
return new TupleState.DictColumnState(dictArrayState, writer, mapVectorState, parent.isVersioned());
}
use of org.apache.drill.exec.vector.complex.DictVector in project drill by apache.
the class SimpleReaderBuilder method buildDict.
private AbstractObjectReader buildDict(ValueVector vector, VectorAccessor va, VectorDescrip descrip) {
boolean isArray = descrip.metadata.isArray();
DictVector dictVector;
VectorAccessor dictAccessor;
if (isArray) {
dictVector = (DictVector) ((RepeatedValueVector) vector).getDataVector();
dictAccessor = new SingleVectorAccessor(dictVector);
} else {
dictVector = (DictVector) vector;
dictAccessor = va;
}
List<AbstractObjectReader> readers = buildMapMembers(dictVector, descrip.childProvider());
AbstractObjectReader reader = DictReaderImpl.build(descrip.metadata, dictAccessor, readers);
if (!isArray) {
return reader;
}
return ArrayReaderImpl.buildTuple(descrip.metadata, va, reader);
}
use of org.apache.drill.exec.vector.complex.DictVector in project drill by apache.
the class TestDictVector method testVectorCreation.
@Test
public void testVectorCreation() {
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();
}
BaseReader.DictReader mapReader = mapVector.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);
}
}
}
}
use of org.apache.drill.exec.vector.complex.DictVector in project drill by apache.
the class TestDictVector method testVectorCreationWithNullValues.
@Test
public void testVectorCreationWithNullValues() {
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, null, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, null, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, null, 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());
Long value = (Long) entry.getValue();
if (value != null) {
mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt(value);
}
// else skip writing a value. Notice that index was incremented
mapWriter.endKeyValuePair();
}
mapWriter.end();
}
BaseReader.DictReader mapReader = mapVector.getReader();
index = 0;
for (Map<Object, Object> map : maps) {
mapReader.setPosition(index++);
assertEquals(map.size(), mapReader.size());
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);
}
}
}
}
use of org.apache.drill.exec.vector.complex.DictVector in project drill by apache.
the class TestDictVector method testVectorCreationWithNullKeys.
@Test
public void testVectorCreationWithNullKeys() {
thrown.expect(DrillRuntimeException.class);
thrown.expectMessage(containsString("Key in DICT cannot be null. Index: 1"));
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, 1.23f, 3L, -0.2f, 4L, 102.07f, 5L), // this map contains null key which is not supported
TestBuilder.mapOfObject(45f, 6L, null, 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();
if (entry.getKey() != null) {
mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
}
// else skip writing a key. Notice that index was incremented.
// Whether the key is written will be checked in endKeyValuePair()
// and an Exception will be thrown in case it was not set (i.e. it is null).
mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((Long) entry.getValue());
mapWriter.endKeyValuePair();
}
mapWriter.end();
}
}
}
Aggregations