Search in sources :

Example 6 with PathSchemaProvider

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

the class TestSchemaCommands method testAlterAddSuccess.

@Test
public void testAlterAddSuccess() throws Exception {
    File tmpDir = dirTestWatcher.getTmpDir();
    File schemaFile = new File(tmpDir, "alter_schema_add_success.schema");
    assertFalse(schemaFile.exists());
    try {
        run("create schema (col1 int) path '%s' properties ('prop1' = 'a')", schemaFile.getPath());
        testBuilder().sqlQuery("alter schema path '%s' add " + "columns (col2 varchar, col3 boolean) " + "properties ('prop2' = 'b', 'prop3' = 'c')", schemaFile.getPath()).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Schema for [%s] was updated", schemaFile.getPath())).go();
        SchemaProvider schemaProvider = new PathSchemaProvider(new Path(schemaFile.getPath()));
        TupleMetadata schema = schemaProvider.read().getSchema();
        assertEquals(3, schema.size());
        assertEquals("col1", schema.fullName(0));
        assertEquals("col2", schema.fullName(1));
        assertEquals("col3", schema.fullName(2));
        Map<String, String> expectedProperties = new HashMap<>();
        expectedProperties.put("prop1", "a");
        expectedProperties.put("prop2", "b");
        expectedProperties.put("prop3", "c");
        assertEquals(expectedProperties, schema.properties());
    } finally {
        FileUtils.deleteQuietly(schemaFile);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) ClusterTest(org.apache.drill.test.ClusterTest) Test(org.junit.Test) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) SqlTest(org.apache.drill.categories.SqlTest)

Example 7 with PathSchemaProvider

use of org.apache.drill.exec.record.metadata.schema.PathSchemaProvider 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 8 with PathSchemaProvider

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

the class TestSchemaCommands method testCreateUsingLoad.

@Test
public void testCreateUsingLoad() throws Exception {
    File tmpDir = dirTestWatcher.getTmpDir();
    File rawSchema = new File(tmpDir, "raw.schema");
    File schemaFile = new File(tmpDir, "schema_for_create_using_load.schema");
    try {
        Files.write(rawSchema.toPath(), Arrays.asList("i int,", "v varchar"));
        assertTrue(rawSchema.exists());
        testBuilder().sqlQuery("create schema load '%s' path '%s' properties ('k1'='v1', 'k2' = 'v2')", rawSchema.getPath(), 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(schemaFile.exists());
        SchemaContainer schemaContainer = schemaProvider.read();
        assertNull(schemaContainer.getTable());
        TupleMetadata schema = schemaContainer.getSchema();
        assertNotNull(schema);
        assertEquals(2, schema.size());
        assertEquals(TypeProtos.MinorType.INT, schema.metadata("i").type());
        assertEquals(TypeProtos.MinorType.VARCHAR, schema.metadata("v").type());
        assertEquals(2, schema.properties().size());
    } finally {
        FileUtils.deleteQuietly(rawSchema);
        FileUtils.deleteQuietly(schemaFile);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaContainer(org.apache.drill.exec.record.metadata.schema.SchemaContainer) 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) ClusterTest(org.apache.drill.test.ClusterTest) Test(org.junit.Test) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) SqlTest(org.apache.drill.categories.SqlTest)

Example 9 with PathSchemaProvider

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

the class TestSchemaCommands method testDescribeJson.

@Test
public void testDescribeJson() throws Exception {
    String tableName = "table_describe_json";
    String table = String.format("dfs.tmp.%s", tableName);
    try {
        run("create table %s as select 'a' as c from (values(1))", table);
        run("create schema (col int) for table %s", table);
        File schemaFile = Paths.get(dirTestWatcher.getDfsTestTmpDir().getPath(), tableName, SchemaProvider.DEFAULT_SCHEMA_NAME).toFile();
        SchemaProvider schemaProvider = new PathSchemaProvider(new Path(schemaFile.getPath()));
        SchemaContainer schemaContainer = schemaProvider.read();
        String schema = PathSchemaProvider.WRITER.writeValueAsString(schemaContainer);
        testBuilder().sqlQuery("describe schema for table %s as json", table).unOrdered().baselineColumns("schema").baselineValues(schema).go();
    } 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) 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 10 with PathSchemaProvider

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

the class TestSchemaCommands method testAlterRemoveProperties.

@Test
public void testAlterRemoveProperties() throws Exception {
    File tmpDir = dirTestWatcher.getTmpDir();
    File schemaFile = new File(tmpDir, "alter_schema_remove_success.schema");
    assertFalse(schemaFile.exists());
    try {
        run("create schema (col1 int, col2 varchar, col3 boolean, col4 int) path '%s' " + "properties ('prop1' = 'a', 'prop2' = 'b', 'prop3' = 'c', 'prop4' = 'd')", schemaFile.getPath());
        testBuilder().sqlQuery("alter schema path '%s' remove " + "properties ('prop2', 'prop4')", schemaFile.getPath()).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Schema for [%s] was updated", schemaFile.getPath())).go();
        SchemaProvider schemaProvider = new PathSchemaProvider(new Path(schemaFile.getPath()));
        TupleMetadata schema = schemaProvider.read().getSchema();
        assertEquals(4, schema.size());
        Map<String, String> expectedProperties = new HashMap<>();
        expectedProperties.put("prop1", "a");
        expectedProperties.put("prop3", "c");
        assertEquals(expectedProperties, schema.properties());
    } finally {
        FileUtils.deleteQuietly(schemaFile);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) ClusterTest(org.apache.drill.test.ClusterTest) Test(org.junit.Test) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) SqlTest(org.apache.drill.categories.SqlTest)

Aggregations

File (java.io.File)19 SqlTest (org.apache.drill.categories.SqlTest)19 UnlikelyTest (org.apache.drill.categories.UnlikelyTest)19 PathSchemaProvider (org.apache.drill.exec.record.metadata.schema.PathSchemaProvider)19 SchemaProvider (org.apache.drill.exec.record.metadata.schema.SchemaProvider)19 ClusterTest (org.apache.drill.test.ClusterTest)19 Path (org.apache.hadoop.fs.Path)19 Test (org.junit.Test)19 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)16 SchemaContainer (org.apache.drill.exec.record.metadata.schema.SchemaContainer)10 LinkedHashMap (java.util.LinkedHashMap)7 HashMap (java.util.HashMap)5 ColumnMetadata (org.apache.drill.exec.record.metadata.ColumnMetadata)3 LocalDate (java.time.LocalDate)1