use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestResultSetLoaderMaps method testEmptyMapAddition.
/**
* Test adding an empty map to a loader after writing the first row.
* Then add columns in another batch. Yes, this is a bizarre condition,
* but we must check it anyway for robustness.
*/
@Test
public void testEmptyMapAddition() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).build();
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
assertEquals(1, rsLoader.schemaVersion());
RowSetLoader rootWriter = rsLoader.writer();
// Start without the map. Add a map after the first row.
rsLoader.startBatch();
rootWriter.addRow(10);
int mapIndex = rootWriter.addColumn(SchemaBuilder.columnSchema("m", MinorType.MAP, DataMode.REQUIRED));
TupleWriter mapWriter = rootWriter.tuple(mapIndex);
rootWriter.addRow(20, objArray()).addRow(30, objArray());
RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(2, rsLoader.schemaVersion());
assertEquals(3, actual.rowCount());
// Validate first batch
TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").resumeSchema().buildSchema();
SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(10, objArray()).addRow(20, objArray()).addRow(30, objArray()).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
// Now add another column to the map
rsLoader.startBatch();
mapWriter.addColumn(SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED));
rootWriter.addRow(40, objArray("fred")).addRow(50, objArray("barney"));
actual = fixture.wrap(rsLoader.harvest());
assertEquals(3, rsLoader.schemaVersion());
assertEquals(2, actual.rowCount());
// Validate first batch
expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addMap("m").add("a", MinorType.VARCHAR).resumeSchema().buildSchema();
expected = fixture.rowSetBuilder(expectedSchema).addRow(40, objArray("fred")).addRow(50, objArray("barney")).build();
new RowSetComparison(expected).verifyAndClearAll(actual);
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestResultSetLoaderProtocol method testCaseInsensitiveSchema.
/**
* Schemas are case insensitive by default. Verify that
* the schema mechanism works, with emphasis on the
* case insensitive case.
* <p>
* The tests here and elsewhere build columns from a
* <tt>MaterializedField</tt>. Doing so is rather old-school;
* better to use the newer <tt>ColumnMetadata</tt> which provides
* additional information. The code here simply uses the <tt>MaterializedField</tt>
* to create a <tt>ColumnMetadata</tt> implicitly.
*/
@Test
public void testCaseInsensitiveSchema() {
ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator());
RowSetLoader rootWriter = rsLoader.writer();
TupleMetadata schema = rootWriter.schema();
assertEquals(0, rsLoader.schemaVersion());
// No columns defined in schema
assertNull(schema.metadata("a"));
try {
schema.column(0);
fail();
} catch (IndexOutOfBoundsException e) {
// Expected
}
try {
rootWriter.column("a");
fail();
} catch (UndefinedColumnException e) {
// Expected
}
try {
rootWriter.column(0);
fail();
} catch (IndexOutOfBoundsException e) {
// Expected
}
// Define a column
assertEquals(0, rsLoader.schemaVersion());
MaterializedField colSchema = SchemaBuilder.columnSchema("a", MinorType.VARCHAR, DataMode.REQUIRED);
rootWriter.addColumn(colSchema);
assertEquals(1, rsLoader.schemaVersion());
// Can now be found, case insensitive
assertTrue(colSchema.isEquivalent(schema.column(0)));
ColumnMetadata colMetadata = schema.metadata(0);
assertSame(colMetadata, schema.metadata("a"));
assertSame(colMetadata, schema.metadata("A"));
assertNotNull(rootWriter.column(0));
assertNotNull(rootWriter.column("a"));
assertNotNull(rootWriter.column("A"));
assertEquals(1, schema.size());
assertEquals(0, schema.index("a"));
assertEquals(0, schema.index("A"));
try {
rootWriter.addColumn(colSchema);
fail();
} catch (IllegalArgumentException e) {
// Expected
}
try {
MaterializedField testCol = SchemaBuilder.columnSchema("A", MinorType.VARCHAR, DataMode.REQUIRED);
rootWriter.addColumn(testCol);
fail();
} catch (IllegalArgumentException e) {
// Expected
assertTrue(e.getMessage().contains("Duplicate"));
}
// Can still add required fields while writing the first row.
rsLoader.startBatch();
rootWriter.start();
rootWriter.scalar(0).setString("foo");
MaterializedField col2 = SchemaBuilder.columnSchema("b", MinorType.VARCHAR, DataMode.REQUIRED);
rootWriter.addColumn(col2);
assertTrue(col2.isEquivalent(schema.column(1)));
ColumnMetadata col2Metadata = schema.metadata(1);
assertSame(col2Metadata, schema.metadata("b"));
assertSame(col2Metadata, schema.metadata("B"));
assertEquals(2, schema.size());
assertEquals(1, schema.index("b"));
assertEquals(1, schema.index("B"));
rootWriter.scalar(1).setString("second");
// After first row, can add an optional or repeated.
// Also allows a required field: values will be back-filled.
rootWriter.save();
rootWriter.start();
rootWriter.scalar(0).setString("bar");
rootWriter.scalar(1).setString("");
MaterializedField col3 = SchemaBuilder.columnSchema("c", MinorType.VARCHAR, DataMode.REQUIRED);
rootWriter.addColumn(col3);
assertTrue(col3.isEquivalent(schema.column(2)));
ColumnMetadata col3Metadata = schema.metadata(2);
assertSame(col3Metadata, schema.metadata("c"));
assertSame(col3Metadata, schema.metadata("C"));
assertEquals(3, schema.size());
assertEquals(2, schema.index("c"));
assertEquals(2, schema.index("C"));
rootWriter.scalar("c").setString("c.2");
MaterializedField col4 = SchemaBuilder.columnSchema("d", MinorType.VARCHAR, DataMode.OPTIONAL);
rootWriter.addColumn(col4);
assertTrue(col4.isEquivalent(schema.column(3)));
ColumnMetadata col4Metadata = schema.metadata(3);
assertSame(col4Metadata, schema.metadata("d"));
assertSame(col4Metadata, schema.metadata("D"));
assertEquals(4, schema.size());
assertEquals(3, schema.index("d"));
assertEquals(3, schema.index("D"));
rootWriter.scalar("d").setString("d.2");
MaterializedField col5 = SchemaBuilder.columnSchema("e", MinorType.VARCHAR, DataMode.REPEATED);
rootWriter.addColumn(col5);
assertTrue(col5.isEquivalent(schema.column(4)));
ColumnMetadata col5Metadata = schema.metadata(4);
assertSame(col5Metadata, schema.metadata("e"));
assertSame(col5Metadata, schema.metadata("E"));
assertEquals(5, schema.size());
assertEquals(4, schema.index("e"));
assertEquals(4, schema.index("E"));
rootWriter.array(4).set("e1", "e2", "e3");
rootWriter.save();
// Verify. No reason to expect problems, but might as well check.
RowSet result = fixture.wrap(rsLoader.harvest());
assertEquals(5, rsLoader.schemaVersion());
SingleRowSet expected = fixture.rowSetBuilder(result.batchSchema()).addRow("foo", "second", "", null, strArray()).addRow("bar", "", "c.2", "d.2", strArray("e1", "e2", "e3")).build();
new RowSetComparison(expected).verifyAndClearAll(result);
// Handy way to test that close works to abort an in-flight batch
// and clean up.
rsLoader.close();
}
use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
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();
BatchSchema batchSchema = new SchemaBuilder().add("col", majorType).build();
BigDecimal v1 = BigDecimal.ZERO;
BigDecimal v2 = BigDecimal.valueOf(123_456_789, 3);
BigDecimal v3 = BigDecimal.TEN;
SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addSingleCol(new BigDecimal[] {}).addSingleCol(new BigDecimal[] { v1, v2, v3 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ScalarElementReader colReader = reader.elements(0);
assertEquals(ValueType.DECIMAL, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, colReader.size());
assertTrue(reader.next());
assertEquals(3, colReader.size());
assertEquals(0, v1.compareTo(colReader.getDecimal(0)));
assertEquals(0, v2.compareTo(colReader.getDecimal(1)));
assertEquals(0, v3.compareTo(colReader.getDecimal(2)));
assertEquals(0, v2.compareTo((BigDecimal) colReader.getObject(1)));
assertEquals(v2.toString(), colReader.getAsString(1));
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestScalarAccessors method longArrayTester.
private void longArrayTester(MinorType type) {
BatchSchema batchSchema = new SchemaBuilder().addArray("col", type).build();
SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addSingleCol(new long[] {}).addSingleCol(new long[] { 0, 20, 30 }).build();
assertEquals(2, rs.rowCount());
RowSetReader reader = rs.reader();
ScalarElementReader colReader = reader.elements(0);
assertEquals(ValueType.LONG, colReader.valueType());
assertTrue(reader.next());
assertEquals(0, colReader.size());
assertTrue(reader.next());
assertEquals(3, colReader.size());
assertEquals(0, colReader.getLong(0));
assertEquals(20, colReader.getLong(1));
assertEquals(30, colReader.getLong(2));
assertEquals(0L, colReader.getObject(0));
assertEquals(20L, colReader.getObject(1));
assertEquals(30L, colReader.getObject(2));
assertEquals("0", colReader.getAsString(0));
assertEquals("20", colReader.getAsString(1));
assertEquals("30", colReader.getAsString(2));
assertFalse(reader.next());
rs.clear();
}
use of org.apache.drill.test.rowSet.RowSet.SingleRowSet in project drill by axbaretto.
the class TestScalarAccessors method nullableDecimalTester.
private void nullableDecimalTester(MinorType type, int precision) {
MajorType majorType = MajorType.newBuilder().setMinorType(type).setScale(3).setPrecision(precision).setMode(DataMode.OPTIONAL).build();
BatchSchema batchSchema = new SchemaBuilder().add("col", majorType).build();
BigDecimal v1 = BigDecimal.ZERO;
BigDecimal v2 = BigDecimal.valueOf(123_456_789, 3);
SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addRow(v1).addSingleCol(null).addRow(v2).build();
assertEquals(3, rs.rowCount());
RowSetReader reader = rs.reader();
ScalarReader colReader = reader.scalar(0);
assertEquals(ValueType.DECIMAL, colReader.valueType());
assertTrue(reader.next());
assertFalse(colReader.isNull());
assertEquals(0, v1.compareTo(colReader.getDecimal()));
assertTrue(reader.next());
assertTrue(colReader.isNull());
assertNull(colReader.getObject());
assertEquals("null", colReader.getAsString());
assertTrue(reader.next());
assertEquals(0, v2.compareTo(colReader.getDecimal()));
assertFalse(reader.next());
rs.clear();
}
Aggregations