Search in sources :

Example 6 with SchemaContainer

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

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

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

the class TestSchemaCommands method testSuccessfulCreateForPath.

@Test
public void testSuccessfulCreateForPath() throws Exception {
    File tmpDir = dirTestWatcher.getTmpDir();
    File schemaFile = new File(tmpDir, "schema_for_successful_create_for_path.schema");
    assertFalse(schemaFile.exists());
    try {
        testBuilder().sqlQuery("create schema (i int not null, v varchar) 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());
        assertNotNull(schemaContainer.getSchema());
        TupleMetadata schema = schemaContainer.getSchema();
        ColumnMetadata intColumn = schema.metadata("i");
        assertFalse(intColumn.isNullable());
        assertEquals(TypeProtos.MinorType.INT, intColumn.type());
        ColumnMetadata varcharColumn = schema.metadata("v");
        assertTrue(varcharColumn.isNullable());
        assertEquals(TypeProtos.MinorType.VARCHAR, varcharColumn.type());
    } 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) 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 SchemaContainer

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

the class TestSchemaCommands method testCreateWithoutSchemaProperties.

@Test
public void testCreateWithoutSchemaProperties() throws Exception {
    File tmpDir = dirTestWatcher.getTmpDir();
    File schemaFile = new File(tmpDir, "schema_for_create_without_properties.schema");
    assertFalse(schemaFile.exists());
    try {
        testBuilder().sqlQuery("create schema (i int not null) 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);
        assertNotNull(schema.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) 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 10 with SchemaContainer

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

the class TestSchemaCommands method testCreateUsingLoadEmptyFile.

@Test
public void testCreateUsingLoadEmptyFile() throws Exception {
    File tmpDir = dirTestWatcher.getTmpDir();
    File rawSchema = new File(tmpDir, "raw_empty.schema");
    File schemaFile = new File(tmpDir, "schema_for_create_using_load_empty_file.schema");
    try {
        assertTrue(rawSchema.createNewFile());
        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(0, schema.size());
        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)

Aggregations

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