Search in sources :

Example 41 with ColumnMetadata

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();
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) ClusterTest(org.apache.drill.test.ClusterTest) Test(org.junit.Test) UnlikelyTest(org.apache.drill.categories.UnlikelyTest)

Example 42 with ColumnMetadata

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();
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata)

Example 43 with ColumnMetadata

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();
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata)

Example 44 with ColumnMetadata

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;
}
Also used : DictColumnMetadata(org.apache.drill.exec.record.metadata.DictColumnMetadata) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) PrimitiveColumnMetadata(org.apache.drill.exec.record.metadata.PrimitiveColumnMetadata) PathSegment(org.apache.drill.common.expression.PathSegment)

Example 45 with ColumnMetadata

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));
    }
}
Also used : DictColumnMetadata(org.apache.drill.exec.record.metadata.DictColumnMetadata) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) PrimitiveColumnMetadata(org.apache.drill.exec.record.metadata.PrimitiveColumnMetadata) PrimitiveColumnMetadata(org.apache.drill.exec.record.metadata.PrimitiveColumnMetadata) ArrayList(java.util.ArrayList) PathSegment(org.apache.drill.common.expression.PathSegment) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) TypeProtos(org.apache.drill.common.types.TypeProtos)

Aggregations

ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)195 Test (org.junit.Test)104 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)98 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)50 DrillTest (org.apache.drill.test.DrillTest)37 PrimitiveColumnMetadata (org.apache.drill.exec.record.metadata.PrimitiveColumnMetadata)31 SubOperatorTest (org.apache.drill.test.SubOperatorTest)31 BaseTest (org.apache.drill.test.BaseTest)26 MapColumnMetadata (org.apache.drill.exec.record.metadata.MapColumnMetadata)20 SchemaBuilder (org.apache.drill.test.rowSet.schema.SchemaBuilder)20 VariantColumnMetadata (org.apache.drill.exec.record.metadata.VariantColumnMetadata)19 VariantMetadata (org.apache.drill.exec.record.metadata.VariantMetadata)19 AbstractColumnMetadata (org.apache.drill.exec.record.metadata.AbstractColumnMetadata)17 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)17 DictColumnMetadata (org.apache.drill.exec.record.metadata.DictColumnMetadata)15 EvfTest (org.apache.drill.categories.EvfTest)13 ProjectionFilter (org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter)13 TupleSchema (org.apache.drill.exec.record.metadata.TupleSchema)11 ArrayList (java.util.ArrayList)10 ProjResult (org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter.ProjResult)10