use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestObjects method testMapAsJson.
@Test
public void testMapAsJson() {
String json = "{a: 6}\n" + "{a: 5, m: null}\n" + "{a: 4, m: {}}\n" + "{a: 2, m: {b: 110}}\n" + "{a: 3, m: {c: 220}}\n" + "{a: 1, m: {b: 10, c: 20}}\n" + "{a: 7, m: {b: 710, c: 720}}";
TupleMetadata schema = new SchemaBuilder().addNullable("a", MinorType.BIGINT).addNullable("m", MinorType.VARCHAR).build();
ColumnMetadata m = schema.metadata("m");
m.setProperty(JsonLoader.JSON_MODE, JsonLoader.JSON_LITERAL_MODE);
JsonLoaderFixture loader = new JsonLoaderFixture();
loader.builder.providedSchema(schema);
loader.open(json);
RowSet results = loader.next();
assertNotNull(results);
RowSet expected = fixture.rowSetBuilder(schema).addRow(6L, null).addRow(5L, "null").addRow(4L, "{}").addRow(2L, "{\"b\": 110}").addRow(3L, "{\"c\": 220}").addRow(1L, "{\"b\": 10, \"c\": 20}").addRow(7L, "{\"b\": 710, \"c\": 720}").build();
RowSetUtilities.verify(expected, results);
assertNull(loader.next());
loader.close();
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestDummyWriter method testDummyDict.
@Test
public void testDummyDict() {
final String dictName = "d";
final String dictArrayName = "da";
TupleMetadata schema = new SchemaBuilder().addDict(dictName, MinorType.INT).repeatedValue(MinorType.VARCHAR).resumeSchema().addDictArray(dictArrayName, MinorType.VARCHAR).value(MinorType.INT).resumeSchema().buildSchema();
List<AbstractObjectWriter> writers = new ArrayList<>();
final String keyFieldName = DictVector.FIELD_KEY_NAME;
final String valueFieldName = DictVector.FIELD_VALUE_NAME;
// Create key and value writers for dict
ColumnMetadata dictMetadata = schema.metadata(dictName);
TupleMetadata dictSchema = dictMetadata.tupleSchema();
List<AbstractObjectWriter> dictFields = new ArrayList<>();
dictFields.add(ColumnWriterFactory.buildColumnWriter(dictSchema.metadata(keyFieldName), null));
dictFields.add(ColumnWriterFactory.buildColumnWriter(dictSchema.metadata(valueFieldName), null));
writers.add(ObjectDictWriter.buildDict(dictMetadata, null, dictFields));
// Create key and value writers for dict array
ColumnMetadata dictArrayMetadata = schema.metadata(dictArrayName);
TupleMetadata dictArraySchema = dictArrayMetadata.tupleSchema();
List<AbstractObjectWriter> dictArrayFields = new ArrayList<>();
dictArrayFields.add(ColumnWriterFactory.buildColumnWriter(dictArraySchema.metadata(keyFieldName), null));
dictArrayFields.add(ColumnWriterFactory.buildColumnWriter(dictArraySchema.metadata(valueFieldName), null));
writers.add(ObjectDictWriter.buildDictArray(dictArrayMetadata, null, dictArrayFields));
AbstractTupleWriter rootWriter = new RootWriterFixture(schema, writers);
// Events are ignored.
rootWriter.startWrite();
rootWriter.startRow();
// Nothing is projected
DictWriter dictWriter = rootWriter.dict(dictName);
assertFalse(dictWriter.isProjected());
assertFalse(dictWriter.keyWriter().isProjected());
assertFalse(dictWriter.valueWriter().array().scalar().isProjected());
DictWriter dictWriter1 = rootWriter.array(dictArrayName).dict();
assertFalse(dictWriter1.isProjected());
assertFalse(dictWriter1.keyWriter().isProjected());
assertFalse(dictWriter1.valueWriter().scalar().isProjected());
// Dummy columns seem real.
rootWriter.dict(dictName).keyWriter().setInt(20);
rootWriter.dict(0).valueWriter().array().scalar().setString("foo");
// Dummy array dict seems real.
rootWriter.array(dictArrayName).dict().keyWriter().setString("foo");
rootWriter.array(dictArrayName).dict().valueWriter().scalar().setInt(30);
rootWriter.array(dictArrayName).save();
rootWriter.array(1).dict().keyWriter().setString("bar");
rootWriter.array(1).dict().valueWriter().scalar().setInt(40);
rootWriter.array(1).save();
// More ignored events.
rootWriter.restartRow();
rootWriter.saveRow();
rootWriter.endWrite();
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestSchemaBuilder method testUnionInMap.
/**
* Verify that the union-in-map plumbing works.
*/
@Test
public void testUnionInMap() {
TupleMetadata schema = new SchemaBuilder().addMap("m1").addUnion("u").addType(MinorType.INT).resumeMap().add("b", MinorType.VARCHAR).resumeSchema().buildSchema();
TupleMetadata m1Schema = schema.metadata("m1").tupleSchema();
VariantMetadata uSchema = m1Schema.metadata("u").variantSchema();
assertTrue(uSchema.hasType(MinorType.INT));
assertFalse(uSchema.hasType(MinorType.VARCHAR));
ColumnMetadata b = m1Schema.metadata(1);
assertEquals("b", b.name());
assertEquals(MinorType.VARCHAR, b.type());
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestSchemaBuilder method testRepeatedListInMap.
/**
* Verify that the repeated list-in-map plumbing works.
*/
@Test
public void testRepeatedListInMap() {
TupleMetadata schema = new SchemaBuilder().addMap("m1").addRepeatedList("r").addArray(MinorType.INT).resumeMap().add("b", MinorType.VARCHAR).resumeSchema().buildSchema();
TupleMetadata m1Schema = schema.metadata("m1").tupleSchema();
ColumnMetadata r = m1Schema.metadata(0);
assertEquals("r", r.name());
assertEquals(MinorType.LIST, r.type());
assertEquals(DataMode.REPEATED, r.mode());
ColumnMetadata child = r.childSchema();
assertEquals(r.name(), child.name());
assertEquals(MinorType.INT, child.type());
ColumnMetadata b = m1Schema.metadata(1);
assertEquals("b", b.name());
assertEquals(MinorType.VARCHAR, b.type());
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestSchemaBuilder method testRowBasics.
@Test
public void testRowBasics() {
TupleMetadata schema = new SchemaBuilder().add("a", MinorType.VARCHAR, // Generic
DataMode.OPTIONAL).add("b", // Required
MinorType.INT).addNullable("c", // Convenience
MinorType.FLOAT8).addArray("d", // Convenience
MinorType.BIGINT).buildSchema();
assertEquals(4, schema.size());
ColumnMetadata a = schema.metadata(0);
assertEquals("a", a.name());
assertEquals(MinorType.VARCHAR, a.type());
assertEquals(DataMode.OPTIONAL, a.mode());
ColumnMetadata b = schema.metadata(1);
assertEquals("b", b.name());
assertEquals(MinorType.INT, b.type());
assertEquals(DataMode.REQUIRED, b.mode());
ColumnMetadata c = schema.metadata(2);
assertEquals("c", c.name());
assertEquals(MinorType.FLOAT8, c.type());
assertEquals(DataMode.OPTIONAL, c.mode());
ColumnMetadata d = schema.metadata(3);
assertEquals("d", d.name());
assertEquals(MinorType.BIGINT, d.type());
assertEquals(DataMode.REPEATED, d.mode());
}
Aggregations