use of org.apache.drill.exec.record.metadata.TupleSchema in project drill by axbaretto.
the class TestTupleSchema method testListSchema.
@Test
public void testListSchema() {
TupleMetadata schema = new SchemaBuilder().addList("list").addType(MinorType.BIGINT).addType(MinorType.VARCHAR).resumeSchema().buildSchema();
assertEquals(1, schema.size());
ColumnMetadata col = schema.metadata(0);
assertTrue(col instanceof VariantColumnMetadata);
// Implementation shows through here: actual major
// type is (LIST, OPTIONAL) even though the metadata
// lies that this is a variant array.
assertEquals(MinorType.LIST, col.type());
assertEquals(DataMode.OPTIONAL, col.mode());
assertTrue(col.isNullable());
assertTrue(col.isArray());
assertTrue(col.isVariant());
assertEquals(StructureType.VARIANT, col.structureType());
VariantMetadata union = col.variantSchema();
assertNotNull(union);
assertEquals(2, union.size());
assertTrue(union.hasType(MinorType.BIGINT));
assertTrue(union.hasType(MinorType.VARCHAR));
assertFalse(union.hasType(MinorType.INT));
Collection<MinorType> types = union.types();
assertNotNull(types);
assertEquals(2, types.size());
assertTrue(types.contains(MinorType.BIGINT));
assertTrue(types.contains(MinorType.VARCHAR));
BatchSchema batchSchema = ((TupleSchema) schema).toBatchSchema(SelectionVectorMode.NONE);
MaterializedField field = batchSchema.getColumn(0);
assertEquals("list", field.getName());
MajorType majorType = field.getType();
assertEquals(MinorType.LIST, majorType.getMinorType());
assertEquals(DataMode.OPTIONAL, majorType.getMode());
assertEquals(2, majorType.getSubTypeCount());
List<MinorType> subtypes = majorType.getSubTypeList();
assertEquals(2, subtypes.size());
assertTrue(subtypes.contains(MinorType.BIGINT));
assertTrue(subtypes.contains(MinorType.VARCHAR));
}
use of org.apache.drill.exec.record.metadata.TupleSchema in project drill by axbaretto.
the class TestTupleSchema method testUnionSchema.
@Test
public void testUnionSchema() {
TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.BIGINT).addType(MinorType.VARCHAR).resumeSchema().buildSchema();
assertEquals(1, schema.size());
ColumnMetadata col = schema.metadata(0);
assertTrue(col instanceof VariantColumnMetadata);
assertEquals(MinorType.UNION, col.type());
assertEquals(DataMode.OPTIONAL, col.mode());
assertTrue(col.isNullable());
assertFalse(col.isArray());
assertTrue(col.isVariant());
assertEquals(StructureType.VARIANT, col.structureType());
VariantMetadata union = col.variantSchema();
assertNotNull(union);
assertEquals(2, union.size());
assertTrue(union.hasType(MinorType.BIGINT));
assertTrue(union.hasType(MinorType.VARCHAR));
assertFalse(union.hasType(MinorType.INT));
Collection<MinorType> types = union.types();
assertNotNull(types);
assertEquals(2, types.size());
assertTrue(types.contains(MinorType.BIGINT));
assertTrue(types.contains(MinorType.VARCHAR));
BatchSchema batchSchema = ((TupleSchema) schema).toBatchSchema(SelectionVectorMode.NONE);
MaterializedField field = batchSchema.getColumn(0);
assertEquals("u", field.getName());
MajorType majorType = field.getType();
assertEquals(MinorType.UNION, majorType.getMinorType());
assertEquals(DataMode.OPTIONAL, majorType.getMode());
assertEquals(2, majorType.getSubTypeCount());
List<MinorType> subtypes = majorType.getSubTypeList();
assertEquals(2, subtypes.size());
assertTrue(subtypes.contains(MinorType.BIGINT));
assertTrue(subtypes.contains(MinorType.VARCHAR));
}
use of org.apache.drill.exec.record.metadata.TupleSchema in project drill by axbaretto.
the class TestTupleSchema method testMapTupleFromField.
@Test
public void testMapTupleFromField() {
// Create a materialized field with the desired structure.
MaterializedField fieldA = SchemaBuilder.columnSchema("a", MinorType.MAP, DataMode.REQUIRED);
MaterializedField fieldB = SchemaBuilder.columnSchema("b.x", MinorType.MAP, DataMode.REQUIRED);
fieldA.addChild(fieldB);
MaterializedField fieldC = SchemaBuilder.columnSchema("c.y", MinorType.MAP, DataMode.REQUIRED);
fieldB.addChild(fieldC);
MaterializedField fieldD = SchemaBuilder.columnSchema("d", MinorType.VARCHAR, DataMode.REQUIRED);
fieldC.addChild(fieldD);
MaterializedField fieldE = SchemaBuilder.columnSchema("e", MinorType.INT, DataMode.REQUIRED);
fieldC.addChild(fieldE);
// Create a metadata schema from the field.
TupleMetadata root = new TupleSchema();
ColumnMetadata colA = root.add(fieldA);
// Get the parts.
TupleMetadata mapA = colA.mapSchema();
ColumnMetadata colB = mapA.metadata("b.x");
TupleMetadata mapB = colB.mapSchema();
ColumnMetadata colC = mapB.metadata("c.y");
TupleMetadata mapC = colC.mapSchema();
ColumnMetadata colD = mapC.metadata("d");
ColumnMetadata colE = mapC.metadata("e");
// Validate. Should be same as earlier test that started
// with the metadata.
assertEquals(1, root.size());
assertEquals(1, mapA.size());
assertEquals(1, mapB.size());
assertEquals(2, mapC.size());
assertSame(colA, root.metadata("a"));
assertSame(colB, mapA.metadata("b.x"));
assertSame(colC, mapB.metadata("c.y"));
assertSame(colD, mapC.metadata("d"));
assertSame(colE, mapC.metadata("e"));
// The full name contains quoted names if the contain dots.
// This name is more for diagnostic than semantic purposes.
assertEquals("a", root.fullName(0));
assertEquals("a.`b.x`", mapA.fullName(0));
assertEquals("a.`b.x`.`c.y`", mapB.fullName(0));
assertEquals("a.`b.x`.`c.y`.d", mapC.fullName(0));
assertEquals("a.`b.x`.`c.y`.e", mapC.fullName(1));
assertEquals(1, colA.schema().getChildren().size());
assertEquals(1, colB.schema().getChildren().size());
assertEquals(2, colC.schema().getChildren().size());
assertTrue(colA.schema().isEquivalent(fieldA));
}
use of org.apache.drill.exec.record.metadata.TupleSchema in project drill by apache.
the class ScanTestUtils method schema.
public static TupleMetadata schema(ResolvedTuple output) {
final TupleMetadata schema = new TupleSchema();
for (final ResolvedColumn col : output.columns()) {
MaterializedField field = col.schema();
if (field.getType() == null) {
// Convert from internal format of null columns (unset type)
// to a usable form (explicit minor type of NULL.)
field = MaterializedField.create(field.getName(), Types.optional(MinorType.NULL));
}
schema.add(field);
}
return schema;
}
use of org.apache.drill.exec.record.metadata.TupleSchema in project drill by apache.
the class TestProjectionFilter method testSchemaFilter.
@Test
public void testSchemaFilter() {
TupleMetadata schema = new SchemaBuilder().add(A_COL.copy()).add(B_COL.copy()).addMap("m").add("a", MinorType.INT).resumeSchema().build();
ProjectionFilter filter = new SchemaProjectionFilter(schema, EmptyErrorContext.INSTANCE);
assertFalse(filter.isEmpty());
assertTrue(filter.isProjected("a"));
assertTrue(filter.projection(A_COL).isProjected);
assertTrue(filter.isProjected("b"));
assertTrue(filter.projection(B_COL).isProjected);
assertFalse(filter.isProjected("c"));
assertFalse(filter.projection(MetadataUtils.newScalar("c", Types.required(MinorType.BIGINT))).isProjected);
ColumnMetadata typeConflict = MetadataUtils.newScalar("a", Types.required(MinorType.BIGINT));
try {
filter.projection(typeConflict);
fail();
} catch (UserException e) {
assertTrue(e.getMessage().contains("conflict"));
}
ColumnMetadata modeConflict = MetadataUtils.newScalar("a", Types.optional(MinorType.INT));
try {
filter.projection(modeConflict);
fail();
} catch (UserException e) {
assertTrue(e.getMessage().contains("conflict"));
}
try {
ColumnMetadata aMap = MetadataUtils.newMap("a", new TupleSchema());
filter.projection(aMap);
fail();
} catch (UserException e) {
assertTrue(e.getMessage().contains("type conflict"));
}
ProjResult result = filter.projection(MAP_COL);
assertTrue(result.isProjected);
ProjectionFilter child = result.mapFilter;
assertTrue(child.isProjected("a"));
assertFalse(child.isProjected("b"));
}
Aggregations