use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestScalarAccessors method testVarcharArray.
@Test
public void testVarcharArray() {
TupleMetadata schema = new SchemaBuilder().addArray("col", MinorType.VARCHAR).buildSchema();
SingleRowSet rs = fixture.rowSetBuilder(schema).addSingleCol(strArray()).addSingleCol(strArray("fred", "", "wilma")).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ArrayReader arrayReader = reader.array(0);
ScalarReader colReader = arrayReader.scalar();
assertEquals(ValueType.STRING, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, arrayReader.size());
assertTrue(reader.next());
assertEquals(3, arrayReader.size());
assertTrue(arrayReader.next());
assertEquals("fred", colReader.getString());
assertEquals("fred", colReader.getObject());
assertEquals("\"fred\"", colReader.getAsString());
assertTrue(arrayReader.next());
assertEquals("", colReader.getString());
assertEquals("", colReader.getObject());
assertEquals("\"\"", colReader.getAsString());
assertTrue(arrayReader.next());
assertEquals("wilma", colReader.getString());
assertEquals("wilma", colReader.getObject());
assertEquals("\"wilma\"", colReader.getAsString());
assertFalse(arrayReader.next());
assertEquals("[\"fred\", \"\", \"wilma\"]", arrayReader.getAsString());
assertEquals(Arrays.asList("fred", "", "wilma"), arrayReader.getObject());
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestScalarAccessors method intArrayTester.
private void intArrayTester(MinorType type) {
TupleMetadata schema = new SchemaBuilder().addArray("col", type).buildSchema();
SingleRowSet rs = fixture.rowSetBuilder(schema).addSingleCol(new int[] {}).addSingleCol(new int[] { 0, 20, 30 }).build();
assertEquals(2, rs.rowCount());
// Verify vector state
VectorContainer container = rs.container();
assertEquals(1, container.getNumberOfColumns());
ValueVector v = container.getValueVector(0).getValueVector();
assertTrue(v instanceof RepeatedValueVector);
RepeatedValueVector rv = (RepeatedValueVector) v;
assertEquals(2, rv.getAccessor().getValueCount());
// Data vector: 3 values written above.
ValueVector vv = rv.getDataVector();
assertEquals(3, vv.getAccessor().getValueCount());
assertEquals(3 * BasicTypeHelper.getSize(Types.required(type)), ((BaseDataValueVector) vv).getBuffer().writerIndex());
// Offsets vector: one more than row count
UInt4Vector ov = rv.getOffsetVector();
assertEquals(3, ov.getAccessor().getValueCount());
assertEquals(3 * 4, ov.getBuffer().writerIndex());
RowSetReader reader = rs.reader();
ArrayReader arrayReader = reader.array(0);
ScalarReader colReader = arrayReader.scalar();
assertEquals(ValueType.INTEGER, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, arrayReader.size());
assertTrue(reader.next());
assertEquals(3, arrayReader.size());
assertTrue(arrayReader.next());
assertFalse(colReader.isNull());
assertEquals(0, colReader.getInt());
assertEquals(0, colReader.getObject());
assertEquals("0", colReader.getAsString());
assertTrue(arrayReader.next());
assertFalse(colReader.isNull());
assertEquals(20, colReader.getInt());
assertEquals(20, colReader.getObject());
assertEquals("20", colReader.getAsString());
assertTrue(arrayReader.next());
assertFalse(colReader.isNull());
assertEquals(30, colReader.getInt());
assertEquals(30, colReader.getObject());
assertEquals("30", colReader.getAsString());
assertFalse(arrayReader.next());
assertEquals("[0, 20, 30]", arrayReader.getAsString());
assertEquals(Arrays.asList(0, 20, 30), arrayReader.getObject());
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestScalarAccessors method testIntervalArray.
@Test
public void testIntervalArray() {
TupleMetadata schema = new SchemaBuilder().addArray("col", MinorType.INTERVAL).buildSchema();
Period p1 = Period.days(0);
Period p2 = Period.years(7).plusMonths(8).plusDays(3).plusHours(4).plusMinutes(5).plusSeconds(23);
Period p3 = Period.years(9999).plusMonths(11).plusDays(365).plusHours(23).plusMinutes(59).plusSeconds(59);
SingleRowSet rs = fixture.rowSetBuilder(schema).addSingleCol(new Period[] {}).addSingleCol(new Period[] { p1, p2, p3 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ArrayReader arrayReader = reader.array(0);
ScalarReader colReader = arrayReader.scalar();
assertEquals(ValueType.PERIOD, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, arrayReader.size());
assertTrue(reader.next());
assertEquals(3, arrayReader.size());
assertTrue(arrayReader.next());
assertEquals(p1, colReader.getPeriod().normalizedStandard());
assertTrue(arrayReader.next());
assertEquals(p2, colReader.getPeriod().normalizedStandard());
assertEquals(p2, ((Period) colReader.getObject()).normalizedStandard());
assertEquals(p2.toString(), colReader.getAsString());
assertTrue(arrayReader.next());
assertEquals(p3.normalizedStandard(), colReader.getPeriod().normalizedStandard());
assertFalse(arrayReader.next());
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestScalarAccessors method decimalArrayTester.
private void decimalArrayTester(MinorType type, int precision) {
MajorType majorType = MajorType.newBuilder().setMinorType(type).setScale(3).setPrecision(precision).setMode(DataMode.REPEATED).build();
TupleMetadata schema = new SchemaBuilder().add("col", majorType).buildSchema();
BigDecimal v1 = BigDecimal.ZERO;
BigDecimal v2 = BigDecimal.valueOf(123_456_789, 3);
BigDecimal v3 = BigDecimal.TEN;
SingleRowSet rs = fixture.rowSetBuilder(schema).addSingleCol(new BigDecimal[] {}).addSingleCol(new BigDecimal[] { v1, v2, v3 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ArrayReader arrayReader = reader.array(0);
ScalarReader colReader = arrayReader.scalar();
assertEquals(ValueType.DECIMAL, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, arrayReader.size());
assertTrue(reader.next());
assertEquals(3, arrayReader.size());
assertTrue(arrayReader.next());
assertEquals(0, v1.compareTo(colReader.getDecimal()));
assertTrue(arrayReader.next());
assertEquals(0, v2.compareTo(colReader.getDecimal()));
assertEquals(0, v2.compareTo((BigDecimal) colReader.getObject()));
assertEquals(v2.toString(), colReader.getAsString());
assertTrue(arrayReader.next());
assertEquals(0, v3.compareTo(colReader.getDecimal()));
assertFalse(arrayReader.next());
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.exec.vector.accessor.ArrayReader in project drill by apache.
the class TestRowSet method testRepeatedDictStructure.
@Test
public void testRepeatedDictStructure() {
final String dictName = "d";
final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addDictArray(dictName, MinorType.FLOAT8).value(MinorType.VARCHAR).resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final RowSetWriter writer = rowSet.writer();
// Repeated dict
assertEquals(ObjectType.ARRAY, writer.column(dictName).type());
final ScalarWriter idWriter = writer.scalar(0);
final ArrayWriter dictArrayWriter = writer.column(1).array();
assertEquals(ObjectType.ARRAY, dictArrayWriter.entryType());
DictWriter dictWriter = dictArrayWriter.dict();
assertEquals(ValueType.DOUBLE, dictWriter.keyType());
assertEquals(ObjectType.SCALAR, dictWriter.valueType());
final ScalarWriter keyWriter = dictWriter.keyWriter();
final ScalarWriter valueWriter = dictWriter.valueWriter().scalar();
assertEquals(ValueType.DOUBLE, keyWriter.valueType());
assertEquals(ValueType.STRING, valueWriter.valueType());
// Write data
idWriter.setInt(1);
keyWriter.setDouble(1);
valueWriter.setString("a");
// advance to next entry position
dictWriter.save();
keyWriter.setDouble(2);
valueWriter.setString("b");
dictWriter.save();
// advance to next array position
dictArrayWriter.save();
keyWriter.setDouble(3);
valueWriter.setString("c");
dictWriter.save();
dictArrayWriter.save();
// advance to next row
writer.save();
idWriter.setInt(2);
keyWriter.setDouble(11);
valueWriter.setString("d");
dictWriter.save();
keyWriter.setDouble(12);
valueWriter.setString("e");
dictWriter.save();
dictArrayWriter.save();
writer.save();
idWriter.setInt(3);
keyWriter.setDouble(21);
valueWriter.setString("f");
dictWriter.save();
keyWriter.setDouble(22);
valueWriter.setString("g");
dictWriter.save();
keyWriter.setDouble(23);
valueWriter.setString("h");
dictWriter.save();
dictArrayWriter.save();
keyWriter.setDouble(24);
valueWriter.setString("i");
dictWriter.save();
keyWriter.setDouble(25);
valueWriter.setString("j");
dictWriter.save();
keyWriter.setDouble(26.5);
valueWriter.setString("k");
dictWriter.save();
keyWriter.setDouble(27);
valueWriter.setString("l");
dictWriter.save();
keyWriter.setDouble(28);
valueWriter.setString("m");
dictWriter.save();
dictArrayWriter.save();
writer.save();
// Finish the row set and get a reader.
final SingleRowSet actual = writer.done();
final RowSetReader reader = actual.reader();
// Verify reader structure
assertEquals(ObjectType.ARRAY, reader.column(dictName).type());
final ArrayReader dictArrayReader = reader.array(1);
assertEquals(ObjectType.ARRAY, dictArrayReader.entryType());
final DictReader dictReader = dictArrayReader.entry().dict();
assertEquals(ValueType.DOUBLE, dictReader.keyColumnType());
assertEquals(ObjectType.SCALAR, dictReader.valueColumnType());
final KeyAccessor keyAccessor = dictReader.keyAccessor();
final ScalarReader valueReader = dictReader.valueReader().scalar();
// Row 1
assertTrue(reader.next());
// array is not null
assertFalse(dictArrayReader.isNull());
assertTrue(dictArrayReader.next());
// first dict is not null
assertFalse(dictArrayReader.isNull());
assertTrue(keyAccessor.find(2.0f));
assertEquals("b", valueReader.getObject());
assertTrue(keyAccessor.find(1.0f));
assertEquals("a", valueReader.getObject());
// no entry for given key
assertFalse(keyAccessor.find(1.1f));
assertTrue(dictArrayReader.next());
assertTrue(keyAccessor.find(3.0f));
assertEquals("c", valueReader.getObject());
assertFalse(keyAccessor.find(1.0f));
assertEquals(Arrays.asList(map(1.0, "a", 2.0, "b"), map(3.0, "c")), dictArrayReader.getObject());
// Row 2
assertTrue(reader.next());
assertTrue(dictArrayReader.next());
assertTrue(keyAccessor.find(11.0f));
assertEquals("d", valueReader.getString());
assertFalse(keyAccessor.find(1.0f));
assertTrue(keyAccessor.find(12.0f));
assertEquals("e", valueReader.getString());
// Row 3: use explicit positioning
assertTrue(reader.next());
dictArrayReader.setPosn(1);
assertTrue(keyAccessor.find(24.0f));
assertEquals("i", valueReader.getString());
assertTrue(keyAccessor.find(26.5f));
assertEquals("k", valueReader.getString());
assertTrue(keyAccessor.find(28.0f));
assertEquals("m", valueReader.getString());
assertFalse(keyAccessor.find(35.0f));
assertTrue(keyAccessor.find(27.0f));
assertEquals("l", valueReader.getString());
Map<Object, Object> element1 = map(24.0, "i", 25.0, "j", 26.5, "k", 27.0, "l", 28.0, "m");
assertEquals(element1, dictReader.getObject());
dictArrayReader.setPosn(0);
assertTrue(keyAccessor.find(23.0f));
assertEquals("h", valueReader.getObject());
assertTrue(keyAccessor.find(21.0f));
assertEquals("f", valueReader.getObject());
assertFalse(keyAccessor.find(23.05f));
Map<Object, Object> element0 = map(21.0, "f", 22.0, "g", 23.0, "h");
assertEquals(element0, dictReader.getObject());
assertEquals(Arrays.asList(element0, element1), dictArrayReader.getObject());
assertFalse(reader.next());
// Verify that the dict accessor's value count was set.
final RepeatedDictVector vector = (RepeatedDictVector) actual.container().getValueVector(1).getValueVector();
assertEquals(3, vector.getAccessor().getValueCount());
final SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(1, objArray(map(1.0f, "a", 2.0f, "b"), map(3.0f, "c"))).addRow(2, objArray(singleObjArray(map(11.0f, "d", 12.0f, "e")))).addRow(3, objArray(map(21.0f, "f", 22.0f, "g", 23.0f, "h"), map(24.0f, "i", 25.0f, "j", 26.5f, "k", 27.0f, "l", 28.0f, "m"))).build();
RowSetUtilities.verify(expected, actual);
}
Aggregations