use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.
the class UnnestRecordBatch method getUnnestFieldTransferPair.
/**
* The data layout is the same for the actual data within a repeated field, as
* it is in a scalar vector for the same sql type. For example, a repeated int
* vector has a vector of offsets into a regular int vector to represent the
* lists. As the data layout for the actual values in the same in the repeated
* vector as in the scalar vector of the same type, we can avoid making
* individual copies for the column being unnested, and just use vector copies
* between the inner vector of the repeated field to the resulting scalar
* vector from the unnest operation. This is completed after we determine how
* many records will fit (as we will hit either a batch end, or the end of one
* of the other vectors while we are copying the data of the other vectors
* alongside each new unnested value coming out of the repeated field.)
*/
private TransferPair getUnnestFieldTransferPair(FieldReference reference) {
int[] typeFieldIds = unnestTypedFieldId.getFieldIds();
Class<?> vectorClass = incoming.getSchema().getColumn(typeFieldIds[0]).getValueClass();
ValueVector unnestField = incoming.getValueAccessorById(vectorClass, typeFieldIds).getValueVector();
TransferPair tp = null;
if (unnestField instanceof RepeatedMapVector) {
tp = ((RepeatedMapVector) unnestField).getTransferPairToSingleMap(reference.getAsNamePart().getName(), oContext.getAllocator());
} else if (!(unnestField instanceof RepeatedValueVector)) {
if (incoming.getRecordCount() != 0) {
throw UserException.unsupportedError().message("Unnest does not support inputs of non-list values.").build(logger);
}
logger.error("Cannot cast {} to RepeatedValueVector", unnestField);
// when incoming recordCount is 0, don't throw exception since the type being seen here is not solid
ValueVector vv = new RepeatedMapVector(unnestField.getField(), oContext.getAllocator(), null);
tp = RepeatedValueVector.class.cast(vv).getTransferPair(reference.getAsNamePart().getName(), oContext.getAllocator());
} else {
ValueVector vvIn = RepeatedValueVector.class.cast(unnestField).getDataVector();
// vvIn may be null because of fast schema return for repeated list vectors
if (vvIn != null) {
tp = vvIn.getTransferPair(reference.getAsNamePart().getName(), oContext.getAllocator());
}
}
return tp;
}
use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.
the class TestMapAccessors method testBasicRepeatedMap.
@Test
public void testBasicRepeatedMap() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("c", MinorType.INT).add("d", MinorType.VARCHAR).resumeSchema().buildSchema();
RowSetBuilder builder = fixture.rowSetBuilder(schema);
RowSetWriter rootWriter = builder.writer();
// Verify structure and schema
TupleMetadata actualSchema = rootWriter.tupleSchema();
assertEquals(2, actualSchema.size());
assertTrue(actualSchema.metadata(1).isArray());
assertTrue(actualSchema.metadata(1).isMap());
assertEquals(2, actualSchema.metadata("m").tupleSchema().size());
assertEquals(2, actualSchema.column("m").getChildren().size());
TupleWriter mapWriter = rootWriter.array("m").tuple();
assertSame(actualSchema.metadata("m").tupleSchema(), mapWriter.schema().tupleSchema());
assertSame(mapWriter.tupleSchema(), mapWriter.schema().tupleSchema());
assertSame(mapWriter.tupleSchema().metadata(0), mapWriter.scalar(0).schema());
assertSame(mapWriter.tupleSchema().metadata(1), mapWriter.scalar(1).schema());
// Write a couple of rows with arrays.
rootWriter.addRow(10, mapArray(mapValue(110, "d1.1"), mapValue(120, "d2.2"))).addRow(20, mapArray()).addRow(30, mapArray(mapValue(310, "d3.1"), mapValue(320, "d3.2"), mapValue(330, "d3.3")));
// Verify the first batch
RowSet actual = builder.build();
RepeatedMapVector mapVector = (RepeatedMapVector) actual.container().getValueVector(1).getValueVector();
MaterializedField mapField = mapVector.getField();
assertEquals(2, mapField.getChildren().size());
Iterator<MaterializedField> iter = mapField.getChildren().iterator();
assertTrue(mapWriter.scalar(0).schema().schema().isEquivalent(iter.next()));
assertTrue(mapWriter.scalar(1).schema().schema().isEquivalent(iter.next()));
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, mapArray(mapValue(110, "d1.1"), mapValue(120, "d2.2"))).addRow(20, mapArray()).addRow(30, mapArray(mapValue(310, "d3.1"), mapValue(320, "d3.2"), mapValue(330, "d3.3"))).build();
new RowSetComparison(expected).verify(actual);
// Test that the row set rebuilds its internal structure from
// a vector container.
RowSet wrapped = fixture.wrap(actual.container());
RowSetUtilities.verify(expected, wrapped);
}
use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.
the class VectorTest method buildFromMap.
private RepeatedMapVector buildFromMap() {
RepeatedMapVector v = buildRepeatedMap();
// Can't figure out how to get the indexes to step for
// inner and outer values.
// If we get the int writer from the repeated map writer,
// the column will be converted to nullable int.
IntVector iv = getInner(v);
IntVector.Mutator im = iv.getMutator();
UInt4Vector ov = v.getOffsetVector();
UInt4Vector.Mutator om = ov.getMutator();
om.set(0, 0);
// Record 1: [10, 20]
im.set(0, 10);
im.set(1, 20);
om.set(1, 2);
// Record 2: [30, 40, 50]
im.set(2, 30);
im.set(3, 40);
im.set(4, 50);
om.set(2, 5);
// Record 3: []
om.set(3, 5);
om.setValueCount(4);
v.getMutator().setValueCount(3);
// Sanity check
validateFrom(v);
return v;
}
use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by axbaretto.
the class TestRecordBatchSizer method testSizerRepeatedMap.
@Test
public void testSizerRepeatedMap() {
BatchSchema schema = new SchemaBuilder().addMapArray("map").add("key", MinorType.INT).add("value", MinorType.VARCHAR).resumeSchema().build();
RowSetBuilder builder = fixture.rowSetBuilder(schema);
for (int i = 0; i < 10; i++) {
builder.addRow((Object) new Object[] { new Object[] { 110, "a" }, new Object[] { 120, "b" } });
}
RowSet rows = builder.build();
// Run the record batch sizer on the resulting batch.
RecordBatchSizer sizer = new RecordBatchSizer(rows.container());
assertEquals(1, sizer.columns().size());
/**
* stdDataSize:50+4, stdNetSize:50+4+4+4, dataSizePerEntry:(4+1)*2,
* netSizePerEntry: 4*2+1*2+4*2+4,
* totalDataSize:5*2*10, totalNetSize:netSizePerEntry*2,
* valueCount:10,
* elementCount:20, estElementCountPerArray:2, isVariableWidth:true
*/
verifyColumnValues(sizer.columns().get("map"), 54, 62, 10, 22, 100, 220, 10, 20, 2, false);
SingleRowSet empty = fixture.rowSet(schema);
VectorAccessible accessible = empty.vectorAccessible();
UInt4Vector offsetVector;
for (VectorWrapper<?> vw : accessible) {
ValueVector v = vw.getValueVector();
RecordBatchSizer.ColumnSize colSize = sizer.getColumn(v.getField().getName());
// Allocates to nearest power of two
colSize.allocateVector(v, testRowCount);
RepeatedMapVector mapVector = (RepeatedMapVector) v;
offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
assertEquals((Integer.highestOneBit(testRowCount) << 1), offsetVector.getValueCapacity());
ValueVector keyVector = mapVector.getChild("key");
ValueVector valueVector1 = mapVector.getChild("value");
assertEquals(((Integer.highestOneBit(testRowCount) << 1) * 2), keyVector.getValueCapacity());
offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
assertEquals((Integer.highestOneBit(testRowCount) << 1) * 2, offsetVector.getValueCapacity());
assertEquals(Integer.highestOneBit(testRowCount << 1) * 2 - 1, valueVector1.getValueCapacity());
// Allocates the same as value passed since it is already power of two.
colSize.allocateVector(v, testRowCountPowerTwo - 1);
mapVector = (RepeatedMapVector) v;
offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
assertEquals(testRowCountPowerTwo, offsetVector.getValueCapacity());
keyVector = mapVector.getChild("key");
valueVector1 = mapVector.getChild("value");
assertEquals(testRowCountPowerTwo * 2, keyVector.getValueCapacity());
offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
assertEquals(testRowCountPowerTwo * 2, offsetVector.getValueCapacity());
assertEquals(testRowCountPowerTwo * 2 - 1, valueVector1.getValueCapacity());
// Allocate for max rows.
colSize.allocateVector(v, ValueVector.MAX_ROW_COUNT - 1);
mapVector = (RepeatedMapVector) v;
offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
assertEquals(ValueVector.MAX_ROW_COUNT, offsetVector.getValueCapacity());
keyVector = mapVector.getChild("key");
valueVector1 = mapVector.getChild("value");
assertEquals(ValueVector.MAX_ROW_COUNT * 2, keyVector.getValueCapacity());
offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
assertEquals(ValueVector.MAX_ROW_COUNT * 2, offsetVector.getValueCapacity());
assertEquals(ValueVector.MAX_ROW_COUNT * 2 - 1, valueVector1.getValueCapacity());
// Allocate for 0 rows. should atleast do allocation for 1 row.
colSize.allocateVector(v, 0);
mapVector = (RepeatedMapVector) v;
offsetVector = ((RepeatedValueVector) mapVector).getOffsetVector();
assertEquals(ValueVector.MIN_ROW_COUNT, offsetVector.getValueCapacity());
keyVector = mapVector.getChild("key");
valueVector1 = mapVector.getChild("value");
assertEquals(ValueVector.MIN_ROW_COUNT, keyVector.getValueCapacity());
offsetVector = ((VariableWidthVector) valueVector1).getOffsetVector();
assertEquals(ValueVector.MIN_ROW_COUNT + 1, offsetVector.getValueCapacity());
assertEquals(ValueVector.MIN_ROW_COUNT, valueVector1.getValueCapacity());
v.clear();
}
empty.clear();
rows.clear();
}
use of org.apache.drill.exec.vector.complex.RepeatedMapVector in project drill by apache.
the class VectorTest method testRepeatedMapWriter.
@Test
public void testRepeatedMapWriter() throws Exception {
// The writers will create a nullable int inner vector.
MaterializedField field = MaterializedField.create("repeated_map", Types.repeated(TypeProtos.MinorType.MAP));
try (RepeatedMapVector v = new RepeatedMapVector(field, allocator, null)) {
SpecialMapVector mapVector = new SpecialMapVector(v);
@SuppressWarnings("resource") SingleMapWriter mapRoot = new SingleMapWriter(mapVector, null, false);
ListWriter lw = mapRoot.list("repeated_map");
MapWriter mw = lw.map();
// Record 1: [10, 20]
lw.setPosition(0);
lw.startList();
mw.start();
IntWriter iw = mw.integer("inner");
iw.writeInt(10);
mw.end();
mw.start();
iw.writeInt(20);
mw.end();
lw.endList();
// Record 2: [30, 40, 50]
lw.setPosition(1);
lw.startList();
mw.start();
iw.writeInt(30);
mw.end();
mw.start();
iw.writeInt(40);
mw.end();
mw.start();
iw.writeInt(50);
mw.end();
lw.endList();
// Record 3: []
lw.setPosition(2);
lw.startList();
lw.endList();
v.getMutator().setValueCount(3);
assertEquals(3, v.getAccessor().getValueCount());
UInt4Vector.Accessor oa = v.getOffsetVector().getAccessor();
assertEquals(4, oa.getValueCount());
assertEquals(0, oa.get(0));
assertEquals(2, oa.get(1));
assertEquals(5, oa.get(2));
assertEquals(5, oa.get(3));
// Past end
assertEquals(0, oa.get(4));
NullableIntVector inner = v.addOrGet("inner", Types.optional(TypeProtos.MinorType.INT), NullableIntVector.class);
UInt1Vector.Accessor ba = inner.getBitsVector().getAccessor();
assertEquals(1, ba.get(0));
assertEquals(1, ba.get(1));
assertEquals(1, ba.get(2));
assertEquals(1, ba.get(3));
assertEquals(1, ba.get(4));
// Past end
assertEquals(0, ba.get(5));
NullableIntVector.Accessor ia = inner.getAccessor();
assertEquals(10, ia.get(0));
assertEquals(20, ia.get(1));
assertEquals(30, ia.get(2));
assertEquals(40, ia.get(3));
assertEquals(50, ia.get(4));
}
}
Aggregations