Search in sources :

Example 86 with ColumnMetadata

use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.

the class TestSchemaCommands method testCreateWithVariousColumnProperties.

@Test
public void testCreateWithVariousColumnProperties() throws Exception {
    File tmpDir = dirTestWatcher.getTmpDir();
    File schemaFile = new File(tmpDir, "schema_for_create_with_various_column_properties.schema");
    assertFalse(schemaFile.exists());
    try {
        testBuilder().sqlQuery("create schema ( " + "a int not null default '10', " + "b date format 'yyyy-MM-dd' default '2017-01-31', " + "c varchar properties {'k1' = 'v1', 'k2' = 'v2'}) " + "path '%s'", schemaFile.getPath()).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Created schema for [%s]", schemaFile.getPath())).go();
        SchemaProvider schemaProvider = new PathSchemaProvider(new Path(schemaFile.getPath()));
        assertTrue(schemaProvider.exists());
        SchemaContainer schemaContainer = schemaProvider.read();
        assertNull(schemaContainer.getTable());
        TupleMetadata schema = schemaContainer.getSchema();
        assertNotNull(schema);
        assertEquals(3, schema.size());
        ColumnMetadata a = schema.metadata("a");
        assertTrue(a.decodeDefaultValue() instanceof Integer);
        assertEquals(10, a.decodeDefaultValue());
        assertEquals("10", a.defaultValue());
        ColumnMetadata b = schema.metadata("b");
        assertTrue(b.decodeDefaultValue() instanceof LocalDate);
        assertEquals("yyyy-MM-dd", b.format());
        assertEquals(LocalDate.parse("2017-01-31"), b.decodeDefaultValue());
        assertEquals("2017-01-31", b.defaultValue());
        ColumnMetadata c = schema.metadata("c");
        Map<String, String> properties = new LinkedHashMap<>();
        properties.put("k1", "v1");
        properties.put("k2", "v2");
        assertEquals(properties, c.properties());
        assertEquals(0, schema.properties().size());
    } finally {
        FileUtils.deleteQuietly(schemaFile);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaContainer(org.apache.drill.exec.record.metadata.schema.SchemaContainer) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaProvider(org.apache.drill.exec.record.metadata.schema.SchemaProvider) PathSchemaProvider(org.apache.drill.exec.record.metadata.schema.PathSchemaProvider) File(java.io.File) PathSchemaProvider(org.apache.drill.exec.record.metadata.schema.PathSchemaProvider) LocalDate(java.time.LocalDate) LinkedHashMap(java.util.LinkedHashMap) ClusterTest(org.apache.drill.test.ClusterTest) Test(org.junit.Test) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) SqlTest(org.apache.drill.categories.SqlTest)

Example 87 with ColumnMetadata

use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.

the class TestSchemaCommands method testSuccessfulCreateOrReplaceForTable.

@Test
public void testSuccessfulCreateOrReplaceForTable() throws Exception {
    String tableName = "table_for_successful_create_or_replace_for_table";
    String table = String.format("dfs.tmp.%s", tableName);
    try {
        run("create table %s as select 'a' as c from (values(1))", table);
        File schemaPath = Paths.get(dirTestWatcher.getDfsTestTmpDir().getPath(), tableName, SchemaProvider.DEFAULT_SCHEMA_NAME).toFile();
        assertFalse(schemaPath.exists());
        testBuilder().sqlQuery("create schema (c varchar not null) for table %s", table).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Created schema for [%s]", table)).go();
        SchemaProvider schemaProvider = new PathSchemaProvider(new Path(schemaPath.getPath()));
        assertTrue(schemaProvider.exists());
        SchemaContainer schemaContainer = schemaProvider.read();
        assertNotNull(schemaContainer.getTable());
        assertEquals(String.format("dfs.tmp.`%s`", tableName), schemaContainer.getTable());
        assertNotNull(schemaContainer.getSchema());
        ColumnMetadata column = schemaContainer.getSchema().metadata("c");
        assertFalse(column.isNullable());
        assertEquals(TypeProtos.MinorType.VARCHAR, column.type());
        testBuilder().sqlQuery("create or replace schema (c varchar) for table %s", table).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Created schema for [%s]", table)).go();
        assertTrue(schemaProvider.exists());
        SchemaContainer updatedSchemaContainer = schemaProvider.read();
        assertNotNull(updatedSchemaContainer.getTable());
        assertEquals(String.format("dfs.tmp.`%s`", tableName), updatedSchemaContainer.getTable());
        assertNotNull(updatedSchemaContainer.getSchema());
        ColumnMetadata updatedColumn = updatedSchemaContainer.getSchema().metadata("c");
        assertTrue(updatedColumn.isNullable());
        assertEquals(TypeProtos.MinorType.VARCHAR, updatedColumn.type());
    } finally {
        run("drop table if exists %s", table);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaContainer(org.apache.drill.exec.record.metadata.schema.SchemaContainer) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) SchemaProvider(org.apache.drill.exec.record.metadata.schema.SchemaProvider) PathSchemaProvider(org.apache.drill.exec.record.metadata.schema.PathSchemaProvider) File(java.io.File) PathSchemaProvider(org.apache.drill.exec.record.metadata.schema.PathSchemaProvider) ClusterTest(org.apache.drill.test.ClusterTest) Test(org.junit.Test) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) SqlTest(org.apache.drill.categories.SqlTest)

Example 88 with ColumnMetadata

use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.

the class TestSchemaTrackerInputSchema method doTestGenericMap.

private void doTestGenericMap(ProjectionSchemaTracker tracker, boolean mapAll) {
    assertTrue(tracker.isResolved());
    TupleMetadata schema = tracker.internalSchema().toSchema();
    assertEquals(MAP_SCHEMA, schema);
    ColumnMetadata mapCol = schema.metadata("m");
    assertEquals(mapAll, SchemaUtils.isProjectAll(mapCol.tupleSchema()));
    assertEquals(MOCK_VALUE, schema.metadata("m").property(MOCK_PROP));
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata)

Example 89 with ColumnMetadata

use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.

the class TestSchemaTrackerInputSchema method testStrictWithWildcardWithMap.

/**
 * Strict schema with a map and a wildcard: neither the top level
 * nor the map allow the reader to add members. Not applicable for
 * a reader schema.
 */
@Test
public void testStrictWithWildcardWithMap() {
    ProjectionSchemaTracker tracker = trackerFor(RowSetTestUtils.projectAll());
    // Mark schema as strict
    TupleMetadata provided = MAP_SCHEMA.copy();
    SchemaUtils.markStrict(provided);
    tracker.applyProvidedSchema(provided);
    assertTrue(tracker.isResolved());
    // Projection as without strict, except the map is "closed",
    // won't accept new members from the reader.
    TupleMetadata schema = tracker.internalSchema().toSchema();
    assertEquals(MAP_SCHEMA, schema);
    ColumnMetadata mapCol = schema.metadata("m");
    assertFalse(SchemaUtils.isProjectAll(mapCol.tupleSchema()));
}
Also used : ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) Test(org.junit.Test) EvfTest(org.apache.drill.categories.EvfTest)

Example 90 with ColumnMetadata

use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.

the class TestDynamicSchemaFilter method testMapProjectList.

@Test
public void testMapProjectList() {
    ProjectionParseResult parseResult = ScanProjectionParser.parse(RowSetTestUtils.projectList("m.x"));
    ProjectionFilter filter = new DynamicTupleFilter(parseResult.dynamicSchema, EmptyErrorContext.INSTANCE);
    assertFalse(filter.isEmpty());
    assertTrue(filter.isProjected(MAP_COL.name()));
    ProjResult result = filter.projection(MAP_COL);
    assertTrue(result.isProjected);
    assertNotNull(result.projection);
    assertTrue(result.projection.isDynamic());
    assertEquals(MAP_COL.name(), result.projection.name());
    assertTrue(result.mapFilter instanceof DynamicTupleFilter);
    ProjectionFilter mapFilter = result.mapFilter;
    ColumnMetadata x_col = MAP_COL.tupleSchema().metadata("x");
    assertTrue(mapFilter.isProjected("x"));
    result = mapFilter.projection(x_col);
    assertTrue(result.isProjected);
    assertNotNull(result.projection);
    assertTrue(result.projection.isDynamic());
    assertEquals(x_col.name(), result.projection.name());
    ColumnMetadata y_col = MAP_COL.tupleSchema().metadata("y");
    assertFalse(mapFilter.isProjected("y"));
    result = mapFilter.projection(y_col);
    assertFalse(result.isProjected);
}
Also used : ProjectionParseResult(org.apache.drill.exec.physical.impl.scan.v3.schema.ScanProjectionParser.ProjectionParseResult) ColumnMetadata(org.apache.drill.exec.record.metadata.ColumnMetadata) ProjectionFilter(org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter) DynamicTupleFilter(org.apache.drill.exec.physical.impl.scan.v3.schema.DynamicSchemaFilter.DynamicTupleFilter) ProjResult(org.apache.drill.exec.physical.resultSet.impl.ProjectionFilter.ProjResult) Test(org.junit.Test) BaseTest(org.apache.drill.test.BaseTest) EvfTest(org.apache.drill.categories.EvfTest)

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