vvysotskyi commented on a change in pull request #1615: DRILL-6964: Implement CREATE / DROP SCHEMA commands URL: https://github.com/apache/drill/pull/1615#discussion_r252704706
########## File path: exec/java-exec/src/test/java/org/apache/drill/TestSchemaCommands.java ########## @@ -0,0 +1,501 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.drill; + +import org.apache.drill.categories.SqlTest; +import org.apache.drill.common.exceptions.UserException; +import org.apache.drill.common.exceptions.UserRemoteException; +import org.apache.drill.common.types.TypeProtos; +import org.apache.drill.exec.record.metadata.ColumnMetadata; +import org.apache.drill.exec.record.metadata.TupleMetadata; +import org.apache.drill.exec.record.metadata.schema.PathSchemaProvider; +import org.apache.drill.exec.record.metadata.schema.SchemaContainer; +import org.apache.drill.exec.record.metadata.schema.SchemaProvider; +import org.apache.drill.test.ClusterFixture; +import org.apache.drill.test.ClusterFixtureBuilder; +import org.apache.drill.test.ClusterTest; +import org.apache.hadoop.fs.Path; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.ExpectedException; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +@Category(SqlTest.class) +public class TestSchemaCommands extends ClusterTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @BeforeClass + public static void setup() throws Exception { + ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher); + startCluster(builder); + } + + @Test + public void testCreateWithoutSchema() throws Exception { + thrown.expect(UserException.class); + thrown.expectMessage("PARSE ERROR: Lexical error"); + + run("create schema for"); + } + + @Test + public void testCreateWithForAndPath() throws Exception { + thrown.expect(UserException.class); + thrown.expectMessage("PARSE ERROR: Encountered \"path\""); + + run("create schema ( col1 int, col2 int) for table tbl path '/tmp/schema.file'"); + } + + @Test + public void testCreateWithPathAndOrReplace() throws Exception { + thrown.expect(UserException.class); + thrown.expectMessage("PARSE ERROR: <OR REPLACE> cannot be used with <PATH> property"); + + run("create or replace schema (col1 int, col2 int) path '/tmp/schema.file'"); + } + + @Test + public void testCreateForMissingTable() throws Exception { + String table = "dfs.tmp.tbl"; + thrown.expect(UserException.class); + thrown.expectMessage("VALIDATION ERROR: Table [tbl] was not found"); + + run("create schema (col1 int, col2 int) for table %s", table); + } + + @Test + public void testCreateForTemporaryTable() throws Exception { + String table = "temp_create"; + try { + run("create temporary table %s as select 'a' as c from (values(1))", table); + thrown.expect(UserException.class); + thrown.expectMessage(String.format("VALIDATION ERROR: Indicated table [%s] is temporary table", table)); + + run("create schema (col1 int, col2 int) for table %s", table); + } finally { + run("drop table if exists %s", table); + } + } + + @Test + public void testCreateForImmutableSchema() throws Exception { + String table = "sys.version"; + thrown.expect(UserException.class); + thrown.expectMessage("VALIDATION ERROR: Unable to create or drop objects. Schema [sys] is immutable"); + + run("create schema (col1 int, col2 int) for table %s", table); + } + + @Test + public void testMissingDirectory() throws Exception { + File tmpDir = dirTestWatcher.getTmpDir(); + Path schema = new Path(Paths.get(tmpDir.getPath(), "missing_parent_directory", "file.schema").toFile().getPath()); + + thrown.expect(UserException.class); + thrown.expectMessage(String.format("RESOURCE ERROR: Parent path for schema file [%s] does not exist", schema.toUri().getPath())); + + run("create schema (col1 int, col2 int) path '%s'", schema.toUri().getPath()); + } + + @Test + public void testTableAsFile() throws Exception { + File tmpDir = dirTestWatcher.getDfsTestTmpDir(); + String table = "test_table_as_file.json"; + File tablePath = new File(tmpDir, table); + assertTrue(tablePath.createNewFile()); + + thrown.expect(UserException.class); + thrown.expectMessage(String.format("RESOURCE ERROR: Indicated table [%s] must be a directory", + String.format("dfs.tmp.%s", table))); + + try { + run("create schema (col1 int, col2 int) for table %s.`%s`", "dfs.tmp", table); + } finally { + assertTrue(tablePath.delete()); + } + } + + @Test + public void testCreateSimpleForPathWithExistingSchema() throws Exception { + File tmpDir = dirTestWatcher.getTmpDir(); + File schema = new File(tmpDir, "simple_for_path.schema"); + assertTrue(schema.createNewFile()); + + thrown.expect(UserException.class); + thrown.expectMessage(String.format("VALIDATION ERROR: Schema already exists for [%s]", schema.getPath())); + + try { + run("create schema (col1 int, col2 int) path '%s'", schema.getPath()); + } finally { + assertTrue(schema.delete()); + } + } + + @Test + public void testCreateSimpleForTableWithExistingSchema() throws Exception { + String table = "dfs.tmp.table_for_simple_existing_schema"; + try { + run("create table %s as select 'a' as c from (values(1))", table); + client.testBuilder() Review comment: `client.testBuilder()` -> `testBuilder()` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
