Search in sources :

Example 1 with DictColumnMetadata

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;
}
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 2 with DictColumnMetadata

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());
}
Also used : DictColumnMetadata(org.apache.drill.exec.record.metadata.DictColumnMetadata) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) DictColumnMetadata(org.apache.drill.exec.record.metadata.DictColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) Test(org.junit.Test) BaseTest(org.apache.drill.test.BaseTest)

Example 3 with DictColumnMetadata

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());
}
Also used : DictColumnMetadata(org.apache.drill.exec.record.metadata.DictColumnMetadata) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) DictColumnMetadata(org.apache.drill.exec.record.metadata.DictColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) Test(org.junit.Test) BaseTest(org.apache.drill.test.BaseTest)

Example 4 with DictColumnMetadata

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();
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) DictWriter(org.apache.drill.exec.vector.accessor.DictWriter) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) MaterializedField(org.apache.drill.exec.record.MaterializedField) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) DictColumnMetadata(org.apache.drill.exec.record.metadata.DictColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 5 with DictColumnMetadata

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

Aggregations

DictColumnMetadata (org.apache.drill.exec.record.metadata.DictColumnMetadata)5 ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)4 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)3 Test (org.junit.Test)3 PathSegment (org.apache.drill.common.expression.PathSegment)2 BaseTest (org.apache.drill.test.BaseTest)2 ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)1 RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)1 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)1 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)1 MaterializedField (org.apache.drill.exec.record.MaterializedField)1 PrimitiveColumnMetadata (org.apache.drill.exec.record.metadata.PrimitiveColumnMetadata)1 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)1 DictWriter (org.apache.drill.exec.vector.accessor.DictWriter)1 DictVector (org.apache.drill.exec.vector.complex.DictVector)1 SubOperatorTest (org.apache.drill.test.SubOperatorTest)1