use of org.apache.drill.exec.record.metadata.TupleMetadata in project drill by axbaretto.
the class TestSchemaBuilder method testDecimal.
/**
* Test the ability to specify decimal precision and scale. Decimal is
* broken in Drill, so we don't bother about decimals in unions,
* lists or repeated lists, though those methods could be added.
*/
@Test
public void testDecimal() {
TupleMetadata schema = new SchemaBuilder().addDecimal("a", MinorType.DECIMAL18, DataMode.OPTIONAL, 5, 2).addDecimal("b", MinorType.DECIMAL18, DataMode.REQUIRED, 6, 3).addDecimal("c", MinorType.DECIMAL18, DataMode.REPEATED, 7, 4).addMap("m").addDecimal("d", MinorType.DECIMAL18, DataMode.OPTIONAL, 8, 1).resumeSchema().buildSchema();
// Use name methods, just for variety
ColumnMetadata a = schema.metadata("a");
assertEquals(DataMode.OPTIONAL, a.mode());
assertEquals(5, a.precision());
assertEquals(2, a.scale());
ColumnMetadata b = schema.metadata("b");
assertEquals(DataMode.REQUIRED, b.mode());
assertEquals(6, b.precision());
assertEquals(3, b.scale());
ColumnMetadata c = schema.metadata("c");
assertEquals(DataMode.REPEATED, c.mode());
assertEquals(7, c.precision());
assertEquals(4, c.scale());
ColumnMetadata d = schema.metadata("m").mapSchema().metadata("d");
assertEquals(DataMode.OPTIONAL, d.mode());
assertEquals(8, d.precision());
assertEquals(1, d.scale());
}
use of org.apache.drill.exec.record.metadata.TupleMetadata in project drill by axbaretto.
the class DummyWriterTest method testDummyMap.
/**
* Test a dummy map or map array. A (non-enforced) rule is that such maps
* contain only dummy writers. The writers act like "real" writers.
*/
@Test
public void testDummyMap() {
TupleMetadata schema = new SchemaBuilder().addMap("m1").add("a", MinorType.INT).addArray("b", MinorType.VARCHAR).resumeSchema().addMapArray("m2").add("c", MinorType.INT).resumeSchema().buildSchema();
List<AbstractObjectWriter> writers = new ArrayList<>();
{
schema.metadata("m1").setProjected(false);
TupleMetadata mapSchema = schema.metadata("m1").mapSchema();
List<AbstractObjectWriter> members = new ArrayList<>();
members.add(ColumnWriterFactory.buildColumnWriter(mapSchema.metadata("a"), null));
members.add(ColumnWriterFactory.buildColumnWriter(mapSchema.metadata("b"), null));
writers.add(ColumnWriterFactory.buildMapWriter(schema.metadata("m1"), null, members));
}
{
schema.metadata("m2").setProjected(false);
TupleMetadata mapSchema = schema.metadata("m2").mapSchema();
List<AbstractObjectWriter> members = new ArrayList<>();
members.add(ColumnWriterFactory.buildColumnWriter(mapSchema.metadata("c"), null));
writers.add(ColumnWriterFactory.buildMapWriter(schema.metadata("m2"), null, members));
}
AbstractTupleWriter rootWriter = new RootWriterFixture(schema, writers);
// Events are ignored.
rootWriter.startWrite();
rootWriter.startRow();
// Dummy columns seem real.
rootWriter.tuple("m1").scalar("a").setInt(20);
rootWriter.tuple(0).array("b").scalar().setString("foo");
// Dummy array map seems real.
rootWriter.array("m2").tuple().scalar("c").setInt(30);
rootWriter.array("m2").save();
rootWriter.array(1).tuple().scalar(0).setInt(40);
rootWriter.array(1).save();
// More ignored events.
rootWriter.restartRow();
rootWriter.saveRow();
rootWriter.endWrite();
}
use of org.apache.drill.exec.record.metadata.TupleMetadata in project drill by axbaretto.
the class RowSetTest method testMapStructure.
/**
* Test a simple map structure at the top level of a row.
*
* @throws VectorOverflowException should never occur
*/
@Test
public void testMapStructure() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").addArray("b", MinorType.INT).resumeSchema().buildSchema();
ExtendableRowSet rowSet = fixture.rowSet(schema);
RowSetWriter writer = rowSet.writer();
// Map and Int
// Test Invariants
assertEquals(ObjectType.SCALAR, writer.column("a").type());
assertEquals(ObjectType.SCALAR, writer.column(0).type());
assertEquals(ObjectType.TUPLE, writer.column("m").type());
assertEquals(ObjectType.TUPLE, writer.column(1).type());
assertSame(writer.column(1).tuple(), writer.tuple(1));
TupleWriter mapWriter = writer.column(1).tuple();
assertEquals(ObjectType.SCALAR, mapWriter.column("b").array().entry().type());
assertEquals(ObjectType.SCALAR, mapWriter.column("b").array().entryType());
ScalarWriter aWriter = writer.column("a").scalar();
ScalarWriter bWriter = writer.column("m").tuple().column("b").array().entry().scalar();
assertSame(bWriter, writer.tuple(1).array(0).scalar());
assertEquals(ValueType.INTEGER, bWriter.valueType());
try {
writer.column(1).scalar();
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
try {
writer.column(1).array();
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
// Write data
aWriter.setInt(10);
bWriter.setInt(11);
bWriter.setInt(12);
writer.save();
aWriter.setInt(20);
bWriter.setInt(21);
bWriter.setInt(22);
writer.save();
aWriter.setInt(30);
bWriter.setInt(31);
bWriter.setInt(32);
writer.save();
// Finish the row set and get a reader.
SingleRowSet actual = writer.done();
RowSetReader reader = actual.reader();
assertEquals(ObjectType.SCALAR, reader.column("a").type());
assertEquals(ObjectType.SCALAR, reader.column(0).type());
assertEquals(ObjectType.TUPLE, reader.column("m").type());
assertEquals(ObjectType.TUPLE, reader.column(1).type());
assertSame(reader.column(1).tuple(), reader.tuple(1));
ScalarReader aReader = reader.column(0).scalar();
TupleReader mReader = reader.column(1).tuple();
assertEquals(ObjectType.SCALAR, mReader.column("b").array().entryType());
ScalarElementReader bReader = mReader.column(0).elements();
assertEquals(ValueType.INTEGER, bReader.valueType());
assertTrue(reader.next());
assertEquals(10, aReader.getInt());
assertEquals(11, bReader.getInt(0));
assertEquals(12, bReader.getInt(1));
assertTrue(reader.next());
assertEquals(20, aReader.getInt());
assertEquals(21, bReader.getInt(0));
assertEquals(22, bReader.getInt(1));
assertTrue(reader.next());
assertEquals(30, aReader.getInt());
assertEquals(31, bReader.getInt(0));
assertEquals(32, bReader.getInt(1));
assertFalse(reader.next());
// Verify that the map accessor's value count was set.
@SuppressWarnings("resource") MapVector mapVector = (MapVector) actual.container().getValueVector(1).getValueVector();
assertEquals(actual.rowCount(), mapVector.getAccessor().getValueCount());
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(intArray(11, 12))).addRow(20, objArray(intArray(21, 22))).addRow(30, objArray(intArray(31, 32))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
}
use of org.apache.drill.exec.record.metadata.TupleMetadata in project drill by axbaretto.
the class RowSetTest method testRepeatedMapStructure.
@Test
public void testRepeatedMapStructure() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("b", MinorType.INT).add("c", MinorType.INT).resumeSchema().buildSchema();
ExtendableRowSet rowSet = fixture.rowSet(schema);
RowSetWriter writer = rowSet.writer();
// Map and Int
// Pick out components and lightly test. (Assumes structure
// tested earlier is still valid, so no need to exhaustively
// test again.)
assertEquals(ObjectType.SCALAR, writer.column("a").type());
assertEquals(ObjectType.ARRAY, writer.column("m").type());
ArrayWriter maWriter = writer.column(1).array();
assertEquals(ObjectType.TUPLE, maWriter.entryType());
TupleWriter mapWriter = maWriter.tuple();
assertEquals(ObjectType.SCALAR, mapWriter.column("b").type());
assertEquals(ObjectType.SCALAR, mapWriter.column("c").type());
ScalarWriter aWriter = writer.column("a").scalar();
ScalarWriter bWriter = mapWriter.scalar("b");
ScalarWriter cWriter = mapWriter.scalar("c");
assertEquals(ValueType.INTEGER, aWriter.valueType());
assertEquals(ValueType.INTEGER, bWriter.valueType());
assertEquals(ValueType.INTEGER, cWriter.valueType());
// Write data
aWriter.setInt(10);
bWriter.setInt(101);
cWriter.setInt(102);
// Advance to next array position
maWriter.save();
bWriter.setInt(111);
cWriter.setInt(112);
maWriter.save();
writer.save();
aWriter.setInt(20);
bWriter.setInt(201);
cWriter.setInt(202);
maWriter.save();
bWriter.setInt(211);
cWriter.setInt(212);
maWriter.save();
writer.save();
aWriter.setInt(30);
bWriter.setInt(301);
cWriter.setInt(302);
maWriter.save();
bWriter.setInt(311);
cWriter.setInt(312);
maWriter.save();
writer.save();
// Finish the row set and get a reader.
SingleRowSet actual = writer.done();
RowSetReader reader = actual.reader();
// Verify reader structure
assertEquals(ObjectType.SCALAR, reader.column("a").type());
assertEquals(ObjectType.ARRAY, reader.column("m").type());
ArrayReader maReader = reader.column(1).array();
assertEquals(ObjectType.TUPLE, maReader.entryType());
TupleReader mapReader = maReader.tuple();
assertEquals(ObjectType.SCALAR, mapReader.column("b").type());
assertEquals(ObjectType.SCALAR, mapReader.column("c").type());
ScalarReader aReader = reader.column("a").scalar();
ScalarReader bReader = mapReader.scalar("b");
ScalarReader cReader = mapReader.scalar("c");
assertEquals(ValueType.INTEGER, aReader.valueType());
assertEquals(ValueType.INTEGER, bReader.valueType());
assertEquals(ValueType.INTEGER, cReader.valueType());
// Row 1: use index accessors
assertTrue(reader.next());
assertEquals(10, aReader.getInt());
TupleReader ixReader = maReader.tuple(0);
assertEquals(101, ixReader.scalar(0).getInt());
assertEquals(102, ixReader.scalar(1).getInt());
ixReader = maReader.tuple(1);
assertEquals(111, ixReader.scalar(0).getInt());
assertEquals(112, ixReader.scalar(1).getInt());
// Row 2: use common accessor with explicit positioning,
// but access scalars through the map reader.
assertTrue(reader.next());
assertEquals(20, aReader.getInt());
maReader.setPosn(0);
assertEquals(201, mapReader.scalar(0).getInt());
assertEquals(202, mapReader.scalar(1).getInt());
maReader.setPosn(1);
assertEquals(211, mapReader.scalar(0).getInt());
assertEquals(212, mapReader.scalar(1).getInt());
// Row 3: use common accessor for scalars
assertTrue(reader.next());
assertEquals(30, aReader.getInt());
maReader.setPosn(0);
assertEquals(301, bReader.getInt());
assertEquals(302, cReader.getInt());
maReader.setPosn(1);
assertEquals(311, bReader.getInt());
assertEquals(312, cReader.getInt());
assertFalse(reader.next());
// Verify that the map accessor's value count was set.
@SuppressWarnings("resource") RepeatedMapVector mapVector = (RepeatedMapVector) actual.container().getValueVector(1).getValueVector();
assertEquals(3, mapVector.getAccessor().getValueCount());
// Verify the readers and writers again using the testing tools.
SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(objArray(101, 102), objArray(111, 112))).addRow(20, objArray(objArray(201, 202), objArray(211, 212))).addRow(30, objArray(objArray(301, 302), objArray(311, 312))).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
}
use of org.apache.drill.exec.record.metadata.TupleMetadata in project drill by axbaretto.
the class TestFillEmpties method doFillEmptiesScalar.
private void doFillEmptiesScalar(MajorType majorType) {
TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
ExtendableRowSet rs = fixture.rowSet(schema);
RowSetWriter writer = rs.writer();
ScalarWriter colWriter = writer.scalar(0);
ValueType valueType = colWriter.valueType();
boolean nullable = majorType.getMode() == DataMode.OPTIONAL;
for (int i = 0; i < ROW_COUNT; i++) {
if (i % 5 == 0) {
colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
}
writer.save();
}
SingleRowSet result = writer.done();
RowSetReader reader = result.reader();
ScalarReader colReader = reader.scalar(0);
MinorType type = majorType.getMinorType();
boolean isVariable = (type == MinorType.VARCHAR || type == MinorType.VAR16CHAR || type == MinorType.VARBINARY);
for (int i = 0; i < ROW_COUNT; i++) {
assertTrue(reader.next());
if (i % 5 != 0) {
if (nullable) {
// Nullable types fill with nulls.
assertTrue(colReader.isNull());
continue;
}
if (isVariable) {
// Variable width types fill with a zero-length value.
assertEquals(0, colReader.getBytes().length);
continue;
}
}
// All other types fill with zero-bytes, interpreted as some form
// of zero for each type.
Object actual = colReader.getObject();
Object expected = RowSetUtilities.testDataFromInt(valueType, majorType, i % 5 == 0 ? i : 0);
RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "]", valueType, expected, actual);
}
result.clear();
}
Aggregations