use of org.apache.drill.exec.record.metadata.DictColumnMetadata in project drill by apache.
the class SchemaPathUtils method getColumnMetadata.
/**
* Returns {@link ColumnMetadata} instance obtained from specified {@code TupleMetadata schema} which corresponds to
* the specified column schema path.
*
* @param schemaPath schema path of the column which should be obtained
* @param schema tuple schema where column should be searched
* @return {@link ColumnMetadata} instance which corresponds to the specified column schema path
*/
public static ColumnMetadata getColumnMetadata(SchemaPath schemaPath, TupleMetadata schema) {
PathSegment.NameSegment colPath = schemaPath.getUnIndexed().getRootSegment();
ColumnMetadata colMetadata = schema.metadata(colPath.getPath());
while (!colPath.isLastPath() && colMetadata != null) {
if (colMetadata.isDict()) {
colMetadata = ((DictColumnMetadata) colMetadata).valueColumnMetadata();
break;
}
if (!colMetadata.isMap()) {
colMetadata = null;
break;
}
colPath = (PathSegment.NameSegment) colPath.getChild();
colMetadata = colMetadata.tupleSchema().metadata(colPath.getPath());
}
return colMetadata;
}
use of org.apache.drill.exec.record.metadata.DictColumnMetadata in project drill by apache.
the class TestSchemaParser method testModeForMapType.
@Test
public void testModeForMapType() throws Exception {
TupleMetadata schema = SchemaExprParser.parseSchema("m1 map<varchar, int>, m2 map<varchar not null, int not null>");
ColumnMetadata mapOptional = schema.metadata("m1");
assertTrue(mapOptional.isDict());
assertEquals(TypeProtos.DataMode.REQUIRED, mapOptional.mode());
DictColumnMetadata dictOptional = (DictColumnMetadata) mapOptional;
assertEquals(TypeProtos.DataMode.REQUIRED, dictOptional.keyColumnMetadata().mode());
assertEquals(TypeProtos.DataMode.OPTIONAL, dictOptional.valueColumnMetadata().mode());
ColumnMetadata mapRequired = schema.metadata("m2");
assertTrue(mapRequired.isDict());
assertEquals(TypeProtos.DataMode.REQUIRED, mapRequired.mode());
DictColumnMetadata dictRequired = (DictColumnMetadata) mapRequired;
assertEquals(TypeProtos.DataMode.REQUIRED, dictRequired.keyColumnMetadata().mode());
assertEquals(TypeProtos.DataMode.REQUIRED, dictRequired.valueColumnMetadata().mode());
}
use of org.apache.drill.exec.record.metadata.DictColumnMetadata in project drill by apache.
the class TestSchemaParser method testModeForRepeatedType.
@Test
public void testModeForRepeatedType() throws Exception {
TupleMetadata schema = SchemaExprParser.parseSchema("a array<int>" + ", aa array<array<int>>" + ", sa array<struct<s1 int not null, s2 varchar>>" + ", ma array<map<varchar, array<int>>>");
assertTrue(schema.metadata("a").isArray());
ColumnMetadata nestedArray = schema.metadata("aa");
assertTrue(nestedArray.isArray());
assertTrue(nestedArray.childSchema().isArray());
ColumnMetadata structArray = schema.metadata("sa");
assertTrue(structArray.isArray());
assertTrue(structArray.isMap());
TupleMetadata structSchema = structArray.tupleSchema();
assertFalse(structSchema.metadata("s1").isNullable());
assertTrue(structSchema.metadata("s2").isNullable());
ColumnMetadata mapArray = schema.metadata("ma");
assertTrue(mapArray.isArray());
assertTrue(mapArray.isDict());
DictColumnMetadata dictMetadata = (DictColumnMetadata) mapArray;
assertFalse(dictMetadata.keyColumnMetadata().isNullable());
assertTrue(dictMetadata.valueColumnMetadata().isArray());
}
use of org.apache.drill.exec.record.metadata.DictColumnMetadata in project drill by apache.
the class TestResultSetLoaderDicts method testDictAddition.
/**
* Test adding a dict to a loader after writing the first row.
*/
@Test
public void testDictAddition() {
final TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
assertEquals(1, rsLoader.schemaVersion());
final RowSetLoader rootWriter = rsLoader.writer();
// Start without the dict. Add then add a dict after the first row.
rsLoader.startBatch();
rootWriter.addRow(10);
MaterializedField dictField = SchemaBuilder.columnSchema("d", MinorType.DICT, DataMode.REQUIRED);
dictField.addChild(SchemaBuilder.columnSchema(DictVector.FIELD_KEY_NAME, MinorType.VARCHAR, DataMode.REQUIRED));
dictField.addChild(SchemaBuilder.columnSchema(DictVector.FIELD_VALUE_NAME, MinorType.VARCHAR, DataMode.REQUIRED));
DictColumnMetadata dictMetadata = MetadataUtils.newDict(dictField);
final int dictIndex = rootWriter.addColumn(dictMetadata);
final DictWriter dictWriter = rootWriter.dict(dictIndex);
// Ensure metadata was added
final TupleMetadata actualSchema = rootWriter.tupleSchema();
assertTrue(actualSchema.metadata(1).isDict());
assertEquals(2, actualSchema.metadata("d").tupleSchema().size());
assertEquals(2, actualSchema.column("d").getChildren().size());
assertEquals(2, actualSchema.size());
assertEquals(2, dictWriter.schema().tupleSchema().size());
rootWriter.addRow(20, map("name", "fred", "lastname", "smith")).addRow(30, map("name", "barney", "lastname", "johnson"));
final RowSet actual = fixture.wrap(rsLoader.harvest());
assertEquals(4, rsLoader.schemaVersion());
assertEquals(3, actual.rowCount());
final DictVector dictVector = (DictVector) actual.container().getValueVector(1).getValueVector();
assertEquals(2, dictVector.getField().getChildren().size());
// Validate first batch
final TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).addDict("d", MinorType.VARCHAR).value(MinorType.VARCHAR).resumeSchema().buildSchema();
final SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(10, map()).addRow(20, map("name", "fred", "lastname", "smith")).addRow(30, map("name", "barney", "lastname", "johnson")).build();
RowSetUtilities.verify(expected, actual);
rsLoader.close();
}
use of org.apache.drill.exec.record.metadata.DictColumnMetadata in project drill by apache.
the class ConvertMetadataAggregateToDirectScanRule method containsArrayColumn.
/**
* Checks whether schema path contains array segment.
*
* @param schema tuple schema
* @param schemaPath schema path
* @return {@code true} if any segment in the schema path is an array, {@code false} otherwise
*/
private static boolean containsArrayColumn(TupleMetadata schema, SchemaPath schemaPath) {
PathSegment currentPath = schemaPath.getRootSegment();
ColumnMetadata columnMetadata = schema.metadata(currentPath.getNameSegment().getPath());
while (columnMetadata != null) {
if (columnMetadata.isArray()) {
return true;
} else if (columnMetadata.isMap()) {
currentPath = currentPath.getChild();
columnMetadata = columnMetadata.tupleSchema().metadata(currentPath.getNameSegment().getPath());
} else if (columnMetadata.isDict()) {
currentPath = currentPath.getChild();
columnMetadata = ((DictColumnMetadata) columnMetadata).valueColumnMetadata();
} else {
return false;
}
}
return false;
}
Aggregations