use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestMockPlugin method testBoolean.
@Test
public void testBoolean() throws RpcException {
String sql = "SELECT active_b FROM `mock`.`employee_100`";
RowSet result = client.queryBuilder().sql(sql).rowSet();
TupleMetadata schema = result.schema();
assertEquals(1, schema.size());
ColumnMetadata col = schema.metadata(0);
assertEquals("active_b", col.name());
assertEquals(MinorType.BIT, col.type());
assertEquals(DataMode.REQUIRED, col.mode());
assertEquals(100, result.rowCount());
result.clear();
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class StreamingHttpConnection method writeColTypes.
private void writeColTypes(JsonOutput gen, TupleMetadata rowSchema) throws IOException {
gen.writeStartArray();
for (ColumnMetadata col : rowSchema) {
gen.writeVarChar(webDataType(col.majorType()));
}
gen.writeEndArray();
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class StreamingHttpConnection method writeColNames.
private void writeColNames(JsonOutput gen, TupleMetadata rowSchema) throws IOException {
gen.writeStartArray();
for (ColumnMetadata col : rowSchema) {
gen.writeVarChar(col.name());
}
gen.writeEndArray();
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata 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.ColumnMetadata in project drill by apache.
the class SchemaPathUtils method addColumnMetadata.
/**
* Adds column with specified schema path and type into specified {@code TupleMetadata schema}.
* For the case when specified {@link SchemaPath} has children, corresponding maps will be created
* in the {@code TupleMetadata schema} and the last child of the map will have specified type.
*
* @param schema tuple schema where column should be added
* @param schemaPath schema path of the column which should be added
* @param type type of the column which should be added
* @param types list of column's parent types
*/
public static void addColumnMetadata(TupleMetadata schema, SchemaPath schemaPath, TypeProtos.MajorType type, Map<SchemaPath, TypeProtos.MajorType> types) {
PathSegment.NameSegment colPath = schemaPath.getUnIndexed().getRootSegment();
List<String> names = new ArrayList<>(types.size());
// Used in case of LIST; defined here to avoid many instantiations inside while-loop
List<String> nextNames = new ArrayList<>(names.size());
ColumnMetadata colMetadata;
while (!colPath.isLastPath()) {
names.add(colPath.getPath());
colMetadata = schema.metadata(colPath.getPath());
TypeProtos.MajorType pathType = types.get(SchemaPath.getCompoundPath(names.toArray(new String[0])));
// The following types, DICT and LIST, contain a nested segment in Parquet representation
// (see ParquetReaderUtility#isLogicalListType(GroupType) and ParquetReaderUtility#isLogicalMapType(GroupType))
// which we should skip when creating corresponding TupleMetadata representation. Additionally,
// there is a need to track if the field is LIST to create appropriate column metadata based
// on the info: whether to create singular MAP/DICT or MAP/DICT array.
boolean isDict = pathType != null && pathType.getMinorType() == TypeProtos.MinorType.DICT;
boolean isList = pathType != null && pathType.getMinorType() == TypeProtos.MinorType.LIST;
String name = colPath.getPath();
if (isList) {
nextNames.clear();
nextNames.addAll(names);
// Parquet's LIST group (which represents an array) has
// an inner group (bagSegment) which we want to skip here
PathSegment.NameSegment bagSegment = colPath.getChild().getNameSegment();
PathSegment.NameSegment elementSegment = bagSegment.getChild().getNameSegment();
nextNames.add(bagSegment.getPath());
nextNames.add(elementSegment.getPath());
pathType = types.get(SchemaPath.getCompoundPath(nextNames.toArray(new String[0])));
if (pathType == null && colPath.getChild().getChild().isLastPath()) {
// will be handled after the while statement
break;
}
colPath = elementSegment;
names.add(bagSegment.getPath());
names.add(elementSegment.getPath());
// Check whether LIST's element type is DICT
isDict = pathType != null && pathType.getMinorType() == TypeProtos.MinorType.DICT;
}
if (colMetadata == null) {
if (isDict) {
colMetadata = isList ? MetadataUtils.newDictArray(name) : MetadataUtils.newDict(name);
} else {
colMetadata = isList ? MetadataUtils.newMapArray(name, null) : MetadataUtils.newMap(name, null);
}
schema.addColumn(colMetadata);
}
if (isDict) {
// Parquet's MAP (which corresponds to DICT in Drill) has
// an inner group which we want to skip here
colPath = (PathSegment.NameSegment) colPath.getChild();
names.add(colPath.getPath());
}
if (!colMetadata.isMap() && !colMetadata.isDict()) {
throw new DrillRuntimeException(String.format("Expected map or dict, but was %s", colMetadata.majorType()));
}
schema = colMetadata.tupleSchema();
colPath = (PathSegment.NameSegment) colPath.getChild();
}
colMetadata = schema.metadata(colPath.getPath());
if (colMetadata == null) {
schema.addColumn(new PrimitiveColumnMetadata(MaterializedField.create(colPath.getPath(), type)));
} else if (!colMetadata.majorType().equals(type)) {
throw new DrillRuntimeException(String.format("Types mismatch: existing type: %s, new type: %s", colMetadata.majorType(), type));
}
}
Aggregations