http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java b/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java deleted file mode 100644 index 5795f0c..0000000 --- a/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java +++ /dev/null @@ -1,354 +0,0 @@ -/** - * 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.tajo.cli; - -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.PosixParser; -import org.apache.hadoop.fs.FileSystem; -import org.apache.hadoop.fs.Path; -import org.apache.tajo.ConfigKey; -import org.apache.tajo.SessionVars; -import org.apache.tajo.TajoTestingCluster; -import org.apache.tajo.TpchTestBase; -import org.apache.tajo.client.QueryStatus; -import org.apache.tajo.conf.TajoConf; -import org.apache.tajo.storage.StorageUtil; -import org.apache.tajo.util.FileUtil; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.PrintWriter; -import java.net.URL; - -import static org.junit.Assert.*; - -public class TestTajoCli { - protected static final TpchTestBase testBase; - protected static final TajoTestingCluster cluster; - - /** the base path of result directories */ - protected static final Path resultBasePath; - static { - testBase = TpchTestBase.getInstance(); - cluster = testBase.getTestingCluster(); - URL resultBaseURL = ClassLoader.getSystemResource("results"); - resultBasePath = new Path(resultBaseURL.toString()); - } - - private TajoCli tajoCli; - private Path currentResultPath; - private ByteArrayOutputStream out; - - @Rule - public TestName name = new TestName(); - - public TestTajoCli() { - String className = getClass().getSimpleName(); - currentResultPath = new Path(resultBasePath, className); - } - - @Before - public void setUp() throws Exception { - out = new ByteArrayOutputStream(); - tajoCli = new TajoCli(cluster.getConfiguration(), new String[]{}, System.in, out); - } - - @After - public void tearDown() { - if (tajoCli != null) { - tajoCli.close(); - } - } - - private static void setVar(TajoCli cli, ConfigKey key, String val) throws Exception { - cli.executeMetaCommand("\\set " + key.keyname() +" " + val); - } - - private static void assertSessionVar(TajoCli cli, String key, String expectedVal) { - assertEquals(cli.getContext().getCliSideVar(key), expectedVal); - } - - private void assertOutputResult(String actual) throws Exception { - assertOutputResult(name.getMethodName() + ".result", actual); - } - - private void assertOutputResult(String expectedResultFile, String actual) throws Exception { - assertOutputResult(expectedResultFile, actual, null, null); - } - - private void assertOutputResult(String expectedResultFile, String actual, String[] paramKeys, String[] paramValues) - throws Exception { - FileSystem fs = currentResultPath.getFileSystem(testBase.getTestingCluster().getConfiguration()); - Path resultFile = StorageUtil.concatPath(currentResultPath, expectedResultFile); - assertTrue(resultFile.toString() + " existence check", fs.exists(resultFile)); - - String expectedResult = FileUtil.readTextFile(new File(resultFile.toUri())); - - if (paramKeys != null) { - for (int i = 0; i < paramKeys.length; i++) { - if (i < paramValues.length) { - expectedResult = expectedResult.replace(paramKeys[i], paramValues[i]); - } - } - } - assertEquals(expectedResult.trim(), actual.trim()); - } - - @Test - public void testParseParam() throws Exception { - String[] args = new String[]{"-f", "test.sql", "--param", "test1=10", "--param", "test2=20"}; - - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(TajoCli.options, args); - - String fileName = cmd.getOptionValue("f"); - assertNotNull(fileName); - assertEquals(args[1], fileName); - - String[] paramValues = cmd.getOptionValues("param"); - - assertNotNull(paramValues); - assertEquals(2, paramValues.length); - - assertEquals("test1=10", paramValues[0]); - assertEquals("test2=20", paramValues[1]); - } - - @Test - public void testParseConf() throws Exception { - String[] args = new String[]{"--conf", "tajo.cli.print.pause=false", - "--conf", "tajo.executor.join.inner.in-memory-table-num=256"}; - - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(TajoCli.options, args); - String[] confValues = cmd.getOptionValues("conf"); - - assertNotNull(confValues); - assertEquals(2, confValues.length); - - assertEquals("tajo.cli.print.pause=false", confValues[0]); - assertEquals("tajo.executor.join.inner.in-memory-table-num=256", confValues[1]); - - TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); - TajoCli testCli = new TajoCli(tajoConf, args, System.in, System.out); - assertEquals("false", testCli.getContext().get(SessionVars.CLI_PAGING_ENABLED)); - assertEquals("256", testCli.getContext().getConf().get("tajo.executor.join.inner.in-memory-table-num")); - } - - @Test - public void testReplaceParam() throws Exception { - String sql = "select * from lineitem where l_tax > ${tax} and l_returnflag > '${returnflag}'"; - String[] params = new String[]{"tax=10", "returnflag=A"}; - - - String expected = "select * from lineitem where l_tax > 10 and l_returnflag > 'A'"; - assertEquals(expected, TajoCli.replaceParam(sql, params)); - } - - @Test - public void testLocalQueryWithoutFrom() throws Exception { - String sql = "select 'abc', '123'; select substr('123456', 1,3);"; - setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); - tajoCli.executeScript(sql); - String consoleResult = new String(out.toByteArray()); - - assertOutputResult(consoleResult); - } - - @Test - public void testConnectDatabase() throws Exception { - String databaseName; - - if (cluster.isHCatalogStoreRunning()) { - databaseName = "TEST_CONNECTION_DATABASE".toLowerCase(); - } else { - databaseName = "TEST_CONNECTION_DATABASE"; - } - String sql = "create database \"" + databaseName + "\";"; - - tajoCli.executeScript(sql); - - tajoCli.executeMetaCommand("\\c " + databaseName); - assertEquals(databaseName, tajoCli.getContext().getCurrentDatabase()); - - tajoCli.executeMetaCommand("\\c default"); - assertEquals("default", tajoCli.getContext().getCurrentDatabase()); - - tajoCli.executeMetaCommand("\\c \"" + databaseName + "\""); - assertEquals(databaseName, tajoCli.getContext().getCurrentDatabase()); - } - - @Test - public void testDescTable() throws Exception { - String tableName; - if (cluster.isHCatalogStoreRunning()) { - tableName = "TEST_DESC_TABLE".toLowerCase(); - } else { - tableName = "TEST_DESC_TABLE"; - } - - String sql = "create table \"" + tableName + "\" (col1 int4, col2 int4);"; - - setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); - tajoCli.executeScript(sql); - - tajoCli.executeMetaCommand("\\d " + tableName); - tajoCli.executeMetaCommand("\\d \"" + tableName + "\""); - - String consoleResult = new String(out.toByteArray()); - - FileSystem fs = FileSystem.get(testBase.getTestingCluster().getConfiguration()); - if (!cluster.isHCatalogStoreRunning()) { - assertOutputResult("testDescTable.result", consoleResult, new String[]{"${table.path}"}, - new String[]{fs.getUri() + "/tajo/warehouse/default/" + tableName}); - } - } - - @Test - public void testSelectResultWithNullFalse() throws Exception { - String sql = - "select\n" + - " c_custkey,\n" + - " orders.o_orderkey,\n" + - " orders.o_orderstatus \n" + - "from\n" + - " orders full outer join customer on c_custkey = o_orderkey\n" + - "order by\n" + - " c_custkey,\n" + - " orders.o_orderkey;\n"; - - setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); - tajoCli.executeScript(sql); - - String consoleResult = new String(out.toByteArray()); - assertOutputResult(consoleResult); - } - - private void verifySelectResultWithNullTrue() throws Exception { - String sql = - "select\n" + - " c_custkey,\n" + - " orders.o_orderkey,\n" + - " orders.o_orderstatus \n" + - "from\n" + - " orders full outer join customer on c_custkey = o_orderkey\n" + - "order by\n" + - " c_custkey,\n" + - " orders.o_orderkey;\n"; - - - setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); - assertSessionVar(tajoCli, SessionVars.CLI_NULL_CHAR.keyname(), "testnull"); - - tajoCli.executeScript(sql); - - String consoleResult = new String(out.toByteArray()); - assertOutputResult(consoleResult); - } - - @Test - public void testSelectResultWithNullTrueDeprecated() throws Exception { - setVar(tajoCli, TajoConf.ConfVars.$CLI_NULL_CHAR, "testnull"); - verifySelectResultWithNullTrue(); - } - - @Test - public void testSelectResultWithNullTrue() throws Exception { - setVar(tajoCli, SessionVars.CLI_NULL_CHAR, "testnull"); - verifySelectResultWithNullTrue(); - } - - private void verifyStopWhenError() throws Exception { - setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); - - assertSessionVar(tajoCli, SessionVars.ON_ERROR_STOP.keyname(), "true"); - - tajoCli.executeScript("select count(*) from lineitem; " + - "select count(*) from lineitem2; " + - "select count(*) from orders"); - - String consoleResult = new String(out.toByteArray()); - assertOutputResult(consoleResult); - } - - @Test - public void testGetConf() throws Exception { - TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); - setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tajoCli = new TajoCli(tajoConf, new String[]{}, System.in, out); - tajoCli.executeMetaCommand("\\getconf tajo.rootdir"); - - String consoleResult = new String(out.toByteArray()); - assertEquals(consoleResult, tajoCli.getContext().getConf().getVar(TajoConf.ConfVars.ROOT_DIR) + "\n"); - } - - @Test - public void testShowMasters() throws Exception { - TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); - setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tajoCli = new TajoCli(tajoConf, new String[]{}, System.in, out); - tajoCli.executeMetaCommand("\\admin -showmasters"); - - String consoleResult = new String(out.toByteArray()); - - String masterAddress = tajoCli.getContext().getConf().getVar(TajoConf.ConfVars.TAJO_MASTER_UMBILICAL_RPC_ADDRESS); - String host = masterAddress.split(":")[0]; - - assertEquals(consoleResult, host + "\n"); - } - - @Test - public void testStopWhenErrorDeprecated() throws Exception { - tajoCli.executeMetaCommand("\\set tajo.cli.error.stop true"); - verifyStopWhenError(); - } - - @Test - public void testStopWhenError() throws Exception { - tajoCli.executeMetaCommand("\\set ON_ERROR_STOP true"); - verifyStopWhenError(); - } - - @Test - public void testHelpSessionVars() throws Exception { - tajoCli.executeMetaCommand("\\help set"); - assertOutputResult(new String(out.toByteArray())); - } - - public static class TajoCliOutputTestFormatter extends DefaultTajoCliOutputFormatter { - @Override - protected String getResponseTimeReadable(float responseTime) { - return ""; - } - @Override - public void printProgress(PrintWriter sout, QueryStatus status) { - //nothing to do - } - } -}
http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestDDLBuilder.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestDDLBuilder.java b/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestDDLBuilder.java new file mode 100644 index 0000000..0ad3af1 --- /dev/null +++ b/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestDDLBuilder.java @@ -0,0 +1,133 @@ +/** + * 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.tajo.cli.tools; + +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.io.compress.GzipCodec; +import org.apache.tajo.catalog.*; +import org.apache.tajo.catalog.partition.PartitionMethodDesc; +import org.apache.tajo.catalog.proto.CatalogProtos; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.storage.StorageConstants; +import org.apache.tajo.util.FileUtil; +import org.junit.Test; + +import static org.junit.Assert.*; + + +public class TestDDLBuilder { + private static final Schema schema1; + private static final TableMeta meta1; + private static final PartitionMethodDesc partitionMethod1; + + static { + schema1 = new Schema(); + schema1.addColumn("name", TajoDataTypes.Type.BLOB); + schema1.addColumn("addr", TajoDataTypes.Type.TEXT); + + meta1 = CatalogUtil.newTableMeta(CatalogProtos.StoreType.CSV); + meta1.putOption(StorageConstants.CSVFILE_DELIMITER, StorageConstants.DEFAULT_FIELD_DELIMITER); + meta1.putOption(StorageConstants.COMPRESSION_CODEC, GzipCodec.class.getName()); + + Schema expressionSchema = new Schema(); + expressionSchema.addColumn("key", TajoDataTypes.Type.INT4); + expressionSchema.addColumn("key2", TajoDataTypes.Type.TEXT); + partitionMethod1 = new PartitionMethodDesc( + "db1", + "table1", + CatalogProtos.PartitionType.COLUMN, + "key,key2", + expressionSchema); + } + + @Test + public void testBuildDDLForExternalTable() throws Exception { + TableDesc desc = new TableDesc("db1.table1", schema1, meta1, new Path("/table1")); + desc.setPartitionMethod(partitionMethod1); + desc.setExternal(true); + assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLForExternalTable.result"), + DDLBuilder.buildDDLForExternalTable(desc)); + } + + @Test + public void testBuildDDLQuotedTableName() throws Exception { + Schema schema2 = new Schema(); + schema2.addColumn("name", TajoDataTypes.Type.BLOB); + schema2.addColumn("addr", TajoDataTypes.Type.TEXT); + schema2.addColumn("FirstName", TajoDataTypes.Type.TEXT); + schema2.addColumn("LastName", TajoDataTypes.Type.TEXT); + schema2.addColumn("with", TajoDataTypes.Type.TEXT); + + Schema expressionSchema2 = new Schema(); + expressionSchema2.addColumn("BirthYear", TajoDataTypes.Type.INT4); + + PartitionMethodDesc partitionMethod2 = new PartitionMethodDesc( + "db1", + "table1", + CatalogProtos.PartitionType.COLUMN, + "key,key2", + expressionSchema2); + + TableDesc desc = new TableDesc("db1.TABLE2", schema2, meta1, new Path("/table1")); + desc.setPartitionMethod(partitionMethod2); + desc.setExternal(true); + assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLQuotedTableName1.result"), + DDLBuilder.buildDDLForExternalTable(desc)); + + desc = new TableDesc("db1.TABLE1", schema2, meta1, new Path("/table1")); + desc.setPartitionMethod(partitionMethod2); + desc.setExternal(false); + assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLQuotedTableName2.result"), + DDLBuilder.buildDDLForBaseTable(desc)); + } + + @Test + public void testBuildDDLForBaseTable() throws Exception { + TableDesc desc = new TableDesc("db1.table2", schema1, meta1, new Path("/table1")); + assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLForBaseTable.result"), + DDLBuilder.buildDDLForBaseTable(desc)); + } + + @Test + public void testBuildColumn() throws Exception { + String [] tobeUnquoted = { + "column_name", + "columnname", + "column_1", + }; + + for (String columnName : tobeUnquoted) { + assertFalse(CatalogUtil.isShouldBeQuoted(columnName)); + } + + String [] quoted = { + "Column_Name", + "COLUMN_NAME", + "컬ë¼", + "$column_name", + "Column_Name1", + "with", + "when" + }; + + for (String columnName : quoted) { + assertTrue(CatalogUtil.isShouldBeQuoted(columnName)); + } + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestTajoDump.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestTajoDump.java b/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestTajoDump.java new file mode 100644 index 0000000..29b7d3b --- /dev/null +++ b/tajo-core/src/test/java/org/apache/tajo/cli/tools/TestTajoDump.java @@ -0,0 +1,47 @@ +/** + * 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.tajo.cli.tools; + +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.tajo.QueryTestCaseBase; +import org.apache.tajo.cli.tools.TajoDump; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; + +public class TestTajoDump extends QueryTestCaseBase { + + @Test + public void testDump1() throws Exception { + if (!testingCluster.isHCatalogStoreRunning()) { + executeString("CREATE TABLE \"" + getCurrentDatabase() + + "\".\"TableName1\" (\"Age\" int, \"FirstName\" TEXT, lastname TEXT)"); + + UserGroupInformation userInfo = UserGroupInformation.getCurrentUser(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + PrintWriter printWriter = new PrintWriter(bos); + TajoDump.dump(client, userInfo, getCurrentDatabase(), false, false, false, printWriter); + printWriter.flush(); + printWriter.close(); + assertStrings(new String(bos.toByteArray())); + bos.close(); + } + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestDefaultCliOutputFormatter.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestDefaultCliOutputFormatter.java b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestDefaultCliOutputFormatter.java new file mode 100644 index 0000000..df709c5 --- /dev/null +++ b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestDefaultCliOutputFormatter.java @@ -0,0 +1,180 @@ +/** + * 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.tajo.cli.tsql; + +import org.apache.hadoop.fs.Path; +import org.apache.tajo.TajoTestingCluster; +import org.apache.tajo.TpchTestBase; +import org.apache.tajo.catalog.TableDesc; +import org.apache.tajo.catalog.statistics.TableStats; +import org.apache.tajo.cli.tsql.DefaultTajoCliOutputFormatter; +import org.apache.tajo.cli.tsql.TajoCli; +import org.apache.tajo.common.TajoDataTypes; +import org.apache.tajo.conf.TajoConf; +import org.apache.tajo.datum.Float8Datum; +import org.apache.tajo.datum.Int4Datum; +import org.apache.tajo.datum.TextDatum; +import org.apache.tajo.jdbc.MetaDataTuple; +import org.apache.tajo.jdbc.TajoMetaDataResultSet; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.URL; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class TestDefaultCliOutputFormatter { + protected static final TpchTestBase testBase; + protected static final TajoTestingCluster cluster; + + /** the base path of result directories */ + protected static final Path resultBasePath; + static { + testBase = TpchTestBase.getInstance(); + cluster = testBase.getTestingCluster(); + URL resultBaseURL = ClassLoader.getSystemResource("results"); + resultBasePath = new Path(resultBaseURL.toString()); + } + + private TajoConf conf; + private TajoCli tajoCli; + private TajoCli.TajoCliContext cliContext; + + @Before + public void setUp() throws Exception { + conf = cluster.getConfiguration(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + tajoCli = new TajoCli(conf, new String[]{}, System.in, out); + cliContext = tajoCli.getContext(); + } + + @After + public void tearDown() { + if (tajoCli != null) { + tajoCli.close(); + } + } + + + @Test + public void testParseErrorMessage() { + String message = "java.sql.SQLException: ERROR: no such a table: table1"; + assertEquals("ERROR: no such a table: table1", DefaultTajoCliOutputFormatter.parseErrorMessage(message)); + + String multiLineMessage = + "ERROR: java.sql.SQLException: ERROR: no such a table: table1\n" + + "com.google.protobuf.ServiceException: java.sql.SQLException: ERROR: no such a table: table1\n" + + "\tat org.apache.tajo.rpc.ServerCallable.withRetries(ServerCallable.java:107)\n" + + "\tat org.apache.tajo.client.TajoClient.getTableDesc(TajoClient.java:777)\n" + + "\tat org.apache.tajo.cli.tsql.commands.DescTableCommand.invoke(DescTableCommand.java:43)\n" + + "\tat org.apache.tajo.cli.tsql.TajoCli.executeMetaCommand(TajoCli.java:300)\n" + + "\tat org.apache.tajo.cli.tsql.TajoCli.executeParsedResults(TajoCli.java:280)\n" + + "\tat org.apache.tajo.cli.tsql.TajoCli.runShell(TajoCli.java:271)\n" + + "\tat org.apache.tajo.cli.tsql.TajoCli.main(TajoCli.java:420)\n" + + "Caused by: java.sql.SQLException: ERROR: no such a table: table1\n" + + "\tat org.apache.tajo.client.TajoClient$22.call(TajoClient.java:791)\n" + + "\tat org.apache.tajo.client.TajoClient$22.call(TajoClient.java:778)\n" + + "\tat org.apache.tajo.rpc.ServerCallable.withRetries(ServerCallable.java:97)\n" + + "\t... 6 more"; + + assertEquals("ERROR: no such a table: table1", DefaultTajoCliOutputFormatter.parseErrorMessage(multiLineMessage)); + } + + @Test + public void testPrintResultInsertStatement() throws Exception { + + + DefaultTajoCliOutputFormatter outputFormatter = new DefaultTajoCliOutputFormatter(); + outputFormatter.init(cliContext); + + float responseTime = 10.1f; + long numBytes = 102; + long numRows = 30; + + TableDesc tableDesc = new TableDesc(); + TableStats stats = new TableStats(); + stats.setNumBytes(102); + stats.setNumRows(numRows); + tableDesc.setStats(stats); + + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + outputFormatter.printResult(writer, null, tableDesc, responseTime, null); + + String expectedOutput = "(" + numRows + " rows, " + responseTime + " sec, " + numBytes + " B inserted)\n"; + assertEquals(expectedOutput, stringWriter.toString()); + } + + @Test + public void testPrintResultSelectStatement() throws Exception { + DefaultTajoCliOutputFormatter outputFormatter = new DefaultTajoCliOutputFormatter(); + outputFormatter.init(cliContext); + + float responseTime = 10.1f; + long numBytes = 102; + long numRows = 30; + + TableDesc tableDesc = new TableDesc(); + TableStats stats = new TableStats(); + stats.setNumBytes(102); + stats.setNumRows(numRows); + tableDesc.setStats(stats); + + final List<MetaDataTuple> resultTables = new ArrayList<MetaDataTuple>(); + + String expectedOutput = "col1, col2, col3\n"; + expectedOutput += "-------------------------------\n"; + + String prefix = ""; + for (int i = 0; i < numRows; i++) { + MetaDataTuple tuple = new MetaDataTuple(3); + + int index = 0; + + tuple.put(index++, new TextDatum("row_" + i)); + tuple.put(index++, new Int4Datum(i)); + tuple.put(index++, new Float8Datum(i)); + + expectedOutput += prefix + "row_" + i + ", " + (new Int4Datum(i)) + ", " + (new Float8Datum(i)); + prefix = "\n"; + resultTables.add(tuple); + } + expectedOutput += "\n(" + numRows + " rows, " + responseTime + " sec, " + numBytes + " B selected)\n"; + + ResultSet resultSet = new TajoMetaDataResultSet( + Arrays.asList("col1", "col2", "col3"), + Arrays.asList(TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT4, TajoDataTypes.Type.FLOAT8), + resultTables); + + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + outputFormatter.printResult(writer, null, tableDesc, responseTime, resultSet); + + assertEquals(expectedOutput, stringWriter.toString()); + } + +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestSimpleParser.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestSimpleParser.java b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestSimpleParser.java new file mode 100644 index 0000000..33a5621 --- /dev/null +++ b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestSimpleParser.java @@ -0,0 +1,274 @@ +/** + * 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.tajo.cli.tsql; + +import org.apache.tajo.cli.tsql.InvalidStatementException; +import org.apache.tajo.cli.tsql.ParsedResult; +import org.apache.tajo.cli.tsql.SimpleParser; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestSimpleParser { + + @Test + public final void testSpecialCases() throws InvalidStatementException { + List<ParsedResult> res1 = SimpleParser.parseScript(""); + assertEquals(0, res1.size()); + + List<ParsedResult> res2 = SimpleParser.parseScript("a"); + assertEquals(1, res2.size()); + + List<ParsedResult> res3 = SimpleParser.parseScript("?"); + assertEquals(0, res3.size()); + + List<ParsedResult> res4 = SimpleParser.parseScript("\\"); + assertEquals(1, res4.size()); + } + + @Test + public final void testMetaCommands() throws InvalidStatementException { + List<ParsedResult> res1 = SimpleParser.parseScript("\\d"); + assertEquals(1, res1.size()); + assertEquals(ParsedResult.StatementType.META, res1.get(0).getType()); + assertEquals("\\d", res1.get(0).getHistoryStatement()); + + List<ParsedResult> res2 = SimpleParser.parseScript("\\d;\\c;\\f;"); + assertEquals(3, res2.size()); + assertEquals(ParsedResult.StatementType.META, res2.get(0).getType()); + assertEquals("\\d", res2.get(0).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.META, res2.get(1).getType()); + assertEquals("\\c", res2.get(1).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.META, res2.get(2).getType()); + assertEquals("\\f", res2.get(2).getHistoryStatement()); + + List<ParsedResult> res3 = SimpleParser.parseScript("\n\t\t \\d;\n\\c;\t\t\\f ;"); + assertEquals(3, res3.size()); + assertEquals(ParsedResult.StatementType.META, res3.get(0).getType()); + assertEquals("\\d", res3.get(0).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.META, res3.get(1).getType()); + assertEquals("\\c", res3.get(1).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.META, res3.get(2).getType()); + assertEquals("\\f", res3.get(2).getHistoryStatement()); + + List<ParsedResult> res4 = SimpleParser.parseScript("\\\td;"); + assertEquals(1, res4.size()); + assertEquals("\\\td", res4.get(0).getHistoryStatement()); + } + + @Test + public final void testParseScript() throws InvalidStatementException { + List<ParsedResult> res1 = SimpleParser.parseScript("select * from test;"); + assertEquals(1, res1.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res1.get(0).getType()); + assertEquals("select * from test", res1.get(0).getStatement()); + assertEquals("select * from test", res1.get(0).getHistoryStatement()); + + List<ParsedResult> res2 = SimpleParser.parseScript("select * from test;"); + assertEquals(1, res2.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res2.get(0).getType()); + assertEquals("select * from test", res2.get(0).getStatement()); + assertEquals("select * from test", res2.get(0).getHistoryStatement()); + + List<ParsedResult> res3 = SimpleParser.parseScript("select * from test1;select * from test2;"); + assertEquals(2, res3.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res3.get(0).getType()); + assertEquals("select * from test1", res3.get(0).getStatement()); + assertEquals("select * from test1", res3.get(0).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.STATEMENT, res3.get(1).getType()); + assertEquals("select * from test2", res3.get(1).getStatement()); + assertEquals("select * from test2", res3.get(1).getHistoryStatement()); + + List<ParsedResult> res4 = SimpleParser.parseScript("\t\t\n\rselect * from \ntest1;select * from test2\n;"); + assertEquals(2, res4.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res4.get(0).getType()); + assertEquals("select * from \ntest1", res4.get(0).getStatement()); + assertEquals("select * from test1", res4.get(0).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.STATEMENT, res4.get(1).getType()); + assertEquals("select * from test2", res4.get(1).getStatement()); + assertEquals("select * from test2", res4.get(1).getHistoryStatement()); + + List<ParsedResult> res5 = + SimpleParser.parseScript("\t\t\n\rselect * from \ntest1;\\d test;select * from test2;\n\nselect 1;"); + assertEquals(4, res5.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res5.get(0).getType()); + assertEquals("select * from \ntest1", res5.get(0).getStatement()); + assertEquals("select * from test1", res5.get(0).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.META, res5.get(1).getType()); + assertEquals("\\d test", res5.get(1).getStatement()); + assertEquals(ParsedResult.StatementType.STATEMENT, res5.get(2).getType()); + assertEquals("select * from test2", res5.get(2).getStatement()); + assertEquals("select * from test2", res5.get(2).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.STATEMENT, res5.get(3).getType()); + assertEquals("select 1", res5.get(3).getStatement()); + assertEquals("select 1", res5.get(3).getHistoryStatement()); + + List<ParsedResult> res6 = + SimpleParser.parseScript("select * from \n--test1; select * from test2;\ntest3;"); + assertEquals(1, res6.size()); + assertEquals("select * from test3", res6.get(0).getHistoryStatement()); + assertEquals("select * from \n--test1; select * from test2;\ntest3", res6.get(0).getStatement()); + + List<ParsedResult> res7 = + SimpleParser.parseScript("select * from --test1; select * from test2;\ntest3;"); + assertEquals(1, res7.size()); + assertEquals("select * from test3", res7.get(0).getHistoryStatement()); + assertEquals("select * from --test1; select * from test2;\ntest3", res7.get(0).getStatement()); + + List<ParsedResult> res8 = SimpleParser.parseScript("\\d test\nselect * \n--from test1;\nfrom test2;\\d test2;"); + assertEquals(3, res8.size()); + assertEquals(ParsedResult.StatementType.META, res8.get(0).getType()); + assertEquals("\\d test", res8.get(0).getStatement()); + assertEquals("\\d test", res8.get(0).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.STATEMENT, res8.get(1).getType()); + assertEquals("select * \n--from test1;\nfrom test2", res8.get(1).getStatement()); + assertEquals("select * from test2", res8.get(1).getHistoryStatement()); + assertEquals(ParsedResult.StatementType.META, res8.get(2).getType()); + assertEquals("\\d test2", res8.get(2).getStatement()); + assertEquals("\\d test2", res8.get(2).getHistoryStatement()); + } + + @Test + public final void testParseLines() throws InvalidStatementException { + SimpleParser simpleParser = new SimpleParser(); + List<ParsedResult> res1 = null; + + res1 = simpleParser.parseLines("select * from test1; select * from test2;"); + assertEquals(2, res1.size()); + assertEquals("select * from test1", res1.get(0).getStatement()); + assertEquals("select * from test2", res1.get(1).getStatement()); + assertEquals("select * from test1", res1.get(0).getHistoryStatement()); + assertEquals("select * from test2", res1.get(1).getHistoryStatement()); + + simpleParser = new SimpleParser(); + res1 = simpleParser.parseLines("select * from "); + assertEquals(0, res1.size()); + res1 = simpleParser.parseLines("test1; select * from test2;"); + assertEquals(2, res1.size()); + assertEquals("select * from \ntest1", res1.get(0).getStatement()); + assertEquals("select * from test2", res1.get(1).getStatement()); + assertEquals("select * from test1", res1.get(0).getHistoryStatement()); + assertEquals("select * from test2", res1.get(1).getHistoryStatement()); + + // select * from + // --test1; select * from test2; + // test3; + simpleParser = new SimpleParser(); + res1 = simpleParser.parseLines("select * from "); + assertEquals(0, res1.size()); + res1 = simpleParser.parseLines("--test1; select * from test2;"); + assertEquals(0, res1.size()); + res1 = simpleParser.parseLines("test3;"); + assertEquals(1, res1.size()); + assertEquals("select * from test3", res1.get(0).getHistoryStatement()); + assertEquals("select * from \n--test1; select * from test2;\ntest3", res1.get(0).getStatement()); + + + // select * from + // test1 --select * from test2; + // where col1 = '123'; + simpleParser = new SimpleParser(); + res1 = simpleParser.parseLines("select * from "); + assertEquals(0, res1.size()); + res1 = simpleParser.parseLines("test1 --select * from test2;"); + assertEquals(0, res1.size()); + res1 = simpleParser.parseLines("where col1 = '123';"); + assertEquals(1, res1.size()); + assertEquals("select * from test1 where col1 = '123'", res1.get(0).getHistoryStatement()); + assertEquals("select * from \ntest1 --select * from test2;\nwhere col1 = '123'", res1.get(0).getStatement()); + } + + @Test + public final void testQuoted() throws InvalidStatementException { + List<ParsedResult> res1 = SimpleParser.parseScript("select '\n;' from test;"); + assertEquals(1, res1.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res1.get(0).getType()); + assertEquals("select '\n;' from test", res1.get(0).getHistoryStatement()); + assertEquals("select '\n;' from test", res1.get(0).getStatement()); + + List<ParsedResult> res2 = SimpleParser.parseScript("select 'abc\nbbc\nddf' from test;"); + assertEquals(1, res2.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res2.get(0).getType()); + assertEquals("select 'abc\nbbc\nddf' from test", res2.get(0).getHistoryStatement()); + assertEquals("select 'abc\nbbc\nddf' from test", res2.get(0).getStatement()); + + List<ParsedResult> res3 = SimpleParser.parseScript("select '--test', \n'--test2' from test"); + assertEquals(1, res3.size()); + assertEquals(ParsedResult.StatementType.STATEMENT, res3.get(0).getType()); + assertEquals("select '--test', '--test2' from test", res3.get(0).getHistoryStatement()); + assertEquals("select '--test', \n'--test2' from test", res3.get(0).getStatement()); + + try { + SimpleParser.parseScript("select 'abc"); + assertTrue(false); + } catch (InvalidStatementException is) { + assertTrue(true); + } + } + + @Test + public final void testParseLines1() throws InvalidStatementException { + String [] lines = { + "select abc, ", + "bbc from test" + }; + SimpleParser parser = new SimpleParser(); + List<ParsedResult> result1 = parser.parseLines(lines[0]); + assertEquals(0, result1.size()); + List<ParsedResult> result2 = parser.parseLines(lines[1]); + assertEquals(0, result2.size()); + List<ParsedResult> result3 = parser.EOF(); + assertEquals(1, result3.size()); + assertEquals(lines[0] + lines[1], result3.get(0).getHistoryStatement()); + assertEquals(lines[0] + "\n" + lines[1], result3.get(0).getStatement()); + } + + @Test + public final void testParseLines2() throws InvalidStatementException { + String [] lines = { + "select abc, '", + "bbc' from test; select * from test3;" + }; + SimpleParser parser = new SimpleParser(); + List<ParsedResult> result1 = parser.parseLines(lines[0]); + assertEquals(0, result1.size()); + List<ParsedResult> result2 = parser.parseLines(lines[1]); + assertEquals(2, result2.size()); + assertEquals("select abc, 'bbc' from test", result2.get(0).getHistoryStatement()); + assertEquals("select * from test3", result2.get(1).getHistoryStatement()); + } + + @Test + public final void testParseLines3() throws InvalidStatementException { + String [] lines = { + "select abc, 'bbc", + "' from test; select * from test3;" + }; + SimpleParser parser = new SimpleParser(); + List<ParsedResult> result1 = parser.parseLines(lines[0]); + assertEquals(0, result1.size()); + List<ParsedResult> result2 = parser.parseLines(lines[1]); + assertEquals(2, result2.size()); + assertEquals("select abc, 'bbc' from test", result2.get(0).getHistoryStatement()); + assertEquals("select * from test3", result2.get(1).getHistoryStatement()); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestTajoCli.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestTajoCli.java b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestTajoCli.java new file mode 100644 index 0000000..c5ff00b --- /dev/null +++ b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/TestTajoCli.java @@ -0,0 +1,356 @@ +/** + * 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.tajo.cli.tsql; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.PosixParser; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.tajo.ConfigKey; +import org.apache.tajo.SessionVars; +import org.apache.tajo.TajoTestingCluster; +import org.apache.tajo.TpchTestBase; +import org.apache.tajo.cli.tsql.DefaultTajoCliOutputFormatter; +import org.apache.tajo.cli.tsql.TajoCli; +import org.apache.tajo.client.QueryStatus; +import org.apache.tajo.conf.TajoConf; +import org.apache.tajo.storage.StorageUtil; +import org.apache.tajo.util.FileUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintWriter; +import java.net.URL; + +import static org.junit.Assert.*; + +public class TestTajoCli { + protected static final TpchTestBase testBase; + protected static final TajoTestingCluster cluster; + + /** the base path of result directories */ + protected static final Path resultBasePath; + static { + testBase = TpchTestBase.getInstance(); + cluster = testBase.getTestingCluster(); + URL resultBaseURL = ClassLoader.getSystemResource("results"); + resultBasePath = new Path(resultBaseURL.toString()); + } + + private TajoCli tajoCli; + private Path currentResultPath; + private ByteArrayOutputStream out; + + @Rule + public TestName name = new TestName(); + + public TestTajoCli() { + String className = getClass().getSimpleName(); + currentResultPath = new Path(resultBasePath, className); + } + + @Before + public void setUp() throws Exception { + out = new ByteArrayOutputStream(); + tajoCli = new TajoCli(cluster.getConfiguration(), new String[]{}, System.in, out); + } + + @After + public void tearDown() { + if (tajoCli != null) { + tajoCli.close(); + } + } + + private static void setVar(TajoCli cli, ConfigKey key, String val) throws Exception { + cli.executeMetaCommand("\\set " + key.keyname() +" " + val); + } + + private static void assertSessionVar(TajoCli cli, String key, String expectedVal) { + assertEquals(cli.getContext().getCliSideVar(key), expectedVal); + } + + private void assertOutputResult(String actual) throws Exception { + assertOutputResult(name.getMethodName() + ".result", actual); + } + + private void assertOutputResult(String expectedResultFile, String actual) throws Exception { + assertOutputResult(expectedResultFile, actual, null, null); + } + + private void assertOutputResult(String expectedResultFile, String actual, String[] paramKeys, String[] paramValues) + throws Exception { + FileSystem fs = currentResultPath.getFileSystem(testBase.getTestingCluster().getConfiguration()); + Path resultFile = StorageUtil.concatPath(currentResultPath, expectedResultFile); + assertTrue(resultFile.toString() + " existence check", fs.exists(resultFile)); + + String expectedResult = FileUtil.readTextFile(new File(resultFile.toUri())); + + if (paramKeys != null) { + for (int i = 0; i < paramKeys.length; i++) { + if (i < paramValues.length) { + expectedResult = expectedResult.replace(paramKeys[i], paramValues[i]); + } + } + } + assertEquals(expectedResult.trim(), actual.trim()); + } + + @Test + public void testParseParam() throws Exception { + String[] args = new String[]{"-f", "test.sql", "--param", "test1=10", "--param", "test2=20"}; + + CommandLineParser parser = new PosixParser(); + CommandLine cmd = parser.parse(TajoCli.options, args); + + String fileName = cmd.getOptionValue("f"); + assertNotNull(fileName); + assertEquals(args[1], fileName); + + String[] paramValues = cmd.getOptionValues("param"); + + assertNotNull(paramValues); + assertEquals(2, paramValues.length); + + assertEquals("test1=10", paramValues[0]); + assertEquals("test2=20", paramValues[1]); + } + + @Test + public void testParseConf() throws Exception { + String[] args = new String[]{"--conf", "tajo.cli.print.pause=false", + "--conf", "tajo.executor.join.inner.in-memory-table-num=256"}; + + CommandLineParser parser = new PosixParser(); + CommandLine cmd = parser.parse(TajoCli.options, args); + String[] confValues = cmd.getOptionValues("conf"); + + assertNotNull(confValues); + assertEquals(2, confValues.length); + + assertEquals("tajo.cli.print.pause=false", confValues[0]); + assertEquals("tajo.executor.join.inner.in-memory-table-num=256", confValues[1]); + + TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); + TajoCli testCli = new TajoCli(tajoConf, args, System.in, System.out); + assertEquals("false", testCli.getContext().get(SessionVars.CLI_PAGING_ENABLED)); + assertEquals("256", testCli.getContext().getConf().get("tajo.executor.join.inner.in-memory-table-num")); + } + + @Test + public void testReplaceParam() throws Exception { + String sql = "select * from lineitem where l_tax > ${tax} and l_returnflag > '${returnflag}'"; + String[] params = new String[]{"tax=10", "returnflag=A"}; + + + String expected = "select * from lineitem where l_tax > 10 and l_returnflag > 'A'"; + assertEquals(expected, TajoCli.replaceParam(sql, params)); + } + + @Test + public void testLocalQueryWithoutFrom() throws Exception { + String sql = "select 'abc', '123'; select substr('123456', 1,3);"; + setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); + tajoCli.executeScript(sql); + String consoleResult = new String(out.toByteArray()); + + assertOutputResult(consoleResult); + } + + @Test + public void testConnectDatabase() throws Exception { + String databaseName; + + if (cluster.isHCatalogStoreRunning()) { + databaseName = "TEST_CONNECTION_DATABASE".toLowerCase(); + } else { + databaseName = "TEST_CONNECTION_DATABASE"; + } + String sql = "create database \"" + databaseName + "\";"; + + tajoCli.executeScript(sql); + + tajoCli.executeMetaCommand("\\c " + databaseName); + assertEquals(databaseName, tajoCli.getContext().getCurrentDatabase()); + + tajoCli.executeMetaCommand("\\c default"); + assertEquals("default", tajoCli.getContext().getCurrentDatabase()); + + tajoCli.executeMetaCommand("\\c \"" + databaseName + "\""); + assertEquals(databaseName, tajoCli.getContext().getCurrentDatabase()); + } + + @Test + public void testDescTable() throws Exception { + String tableName; + if (cluster.isHCatalogStoreRunning()) { + tableName = "TEST_DESC_TABLE".toLowerCase(); + } else { + tableName = "TEST_DESC_TABLE"; + } + + String sql = "create table \"" + tableName + "\" (col1 int4, col2 int4);"; + + setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); + tajoCli.executeScript(sql); + + tajoCli.executeMetaCommand("\\d " + tableName); + tajoCli.executeMetaCommand("\\d \"" + tableName + "\""); + + String consoleResult = new String(out.toByteArray()); + + FileSystem fs = FileSystem.get(testBase.getTestingCluster().getConfiguration()); + if (!cluster.isHCatalogStoreRunning()) { + assertOutputResult("testDescTable.result", consoleResult, new String[]{"${table.path}"}, + new String[]{fs.getUri() + "/tajo/warehouse/default/" + tableName}); + } + } + + @Test + public void testSelectResultWithNullFalse() throws Exception { + String sql = + "select\n" + + " c_custkey,\n" + + " orders.o_orderkey,\n" + + " orders.o_orderstatus \n" + + "from\n" + + " orders full outer join customer on c_custkey = o_orderkey\n" + + "order by\n" + + " c_custkey,\n" + + " orders.o_orderkey;\n"; + + setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); + tajoCli.executeScript(sql); + + String consoleResult = new String(out.toByteArray()); + assertOutputResult(consoleResult); + } + + private void verifySelectResultWithNullTrue() throws Exception { + String sql = + "select\n" + + " c_custkey,\n" + + " orders.o_orderkey,\n" + + " orders.o_orderstatus \n" + + "from\n" + + " orders full outer join customer on c_custkey = o_orderkey\n" + + "order by\n" + + " c_custkey,\n" + + " orders.o_orderkey;\n"; + + + setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); + assertSessionVar(tajoCli, SessionVars.CLI_NULL_CHAR.keyname(), "testnull"); + + tajoCli.executeScript(sql); + + String consoleResult = new String(out.toByteArray()); + assertOutputResult(consoleResult); + } + + @Test + public void testSelectResultWithNullTrueDeprecated() throws Exception { + setVar(tajoCli, TajoConf.ConfVars.$CLI_NULL_CHAR, "testnull"); + verifySelectResultWithNullTrue(); + } + + @Test + public void testSelectResultWithNullTrue() throws Exception { + setVar(tajoCli, SessionVars.CLI_NULL_CHAR, "testnull"); + verifySelectResultWithNullTrue(); + } + + private void verifyStopWhenError() throws Exception { + setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); + + assertSessionVar(tajoCli, SessionVars.ON_ERROR_STOP.keyname(), "true"); + + tajoCli.executeScript("select count(*) from lineitem; " + + "select count(*) from lineitem2; " + + "select count(*) from orders"); + + String consoleResult = new String(out.toByteArray()); + assertOutputResult(consoleResult); + } + + @Test + public void testGetConf() throws Exception { + TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); + setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + tajoCli = new TajoCli(tajoConf, new String[]{}, System.in, out); + tajoCli.executeMetaCommand("\\getconf tajo.rootdir"); + + String consoleResult = new String(out.toByteArray()); + assertEquals(consoleResult, tajoCli.getContext().getConf().getVar(TajoConf.ConfVars.ROOT_DIR) + "\n"); + } + + @Test + public void testShowMasters() throws Exception { + TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); + setVar(tajoCli, SessionVars.CLI_FORMATTER_CLASS, TajoCliOutputTestFormatter.class.getName()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + tajoCli = new TajoCli(tajoConf, new String[]{}, System.in, out); + tajoCli.executeMetaCommand("\\admin -showmasters"); + + String consoleResult = new String(out.toByteArray()); + + String masterAddress = tajoCli.getContext().getConf().getVar(TajoConf.ConfVars.TAJO_MASTER_UMBILICAL_RPC_ADDRESS); + String host = masterAddress.split(":")[0]; + + assertEquals(consoleResult, host + "\n"); + } + + @Test + public void testStopWhenErrorDeprecated() throws Exception { + tajoCli.executeMetaCommand("\\set tajo.cli.error.stop true"); + verifyStopWhenError(); + } + + @Test + public void testStopWhenError() throws Exception { + tajoCli.executeMetaCommand("\\set ON_ERROR_STOP true"); + verifyStopWhenError(); + } + + @Test + public void testHelpSessionVars() throws Exception { + tajoCli.executeMetaCommand("\\help set"); + assertOutputResult(new String(out.toByteArray())); + } + + public static class TajoCliOutputTestFormatter extends DefaultTajoCliOutputFormatter { + @Override + protected String getResponseTimeReadable(float responseTime) { + return ""; + } + @Override + public void printProgress(PrintWriter sout, QueryStatus status) { + //nothing to do + } + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestExecExternalShellCommand.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestExecExternalShellCommand.java b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestExecExternalShellCommand.java new file mode 100644 index 0000000..ed7ee4a --- /dev/null +++ b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestExecExternalShellCommand.java @@ -0,0 +1,45 @@ +/** + * 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.tajo.cli.tsql.commands; + +import org.apache.tajo.TpchTestBase; +import org.apache.tajo.cli.tsql.TajoCli; +import org.apache.tajo.conf.TajoConf; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; + +import static org.junit.Assert.assertEquals; + +public class TestExecExternalShellCommand { + @Test + public void testCommand() throws Exception { + TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + TajoCli cli = new TajoCli(tajoConf, new String[]{}, null, out); + + cli.executeMetaCommand("\\! echo \"this is test\""); + String consoleResult = new String(out.toByteArray()); + assertEquals("this is test\n", consoleResult); + + assertEquals(-1, cli.executeMetaCommand("\\! error_command")); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestHdfsCommand.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestHdfsCommand.java b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestHdfsCommand.java new file mode 100644 index 0000000..496c7e3 --- /dev/null +++ b/tajo-core/src/test/java/org/apache/tajo/cli/tsql/commands/TestHdfsCommand.java @@ -0,0 +1,47 @@ +/** + * 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.tajo.cli.tsql.commands; + +import org.apache.tajo.TpchTestBase; +import org.apache.tajo.cli.tsql.TajoCli; +import org.apache.tajo.conf.TajoConf; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertEquals; + +public class TestHdfsCommand { + @Test + public void testHdfCommand() throws Exception { + TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + System.setOut(new PrintStream(out)); + System.setErr(new PrintStream(out)); + TajoCli cli = new TajoCli(tajoConf, new String[]{}, null, out); + + cli.executeMetaCommand("\\dfs -test"); + String consoleResult = new String(out.toByteArray()); + assertEquals("-test: Not enough arguments: expected 1 but got 0\n" + + "Usage: hadoop fs [generic options] -test -[defsz] <path>\n", consoleResult); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/client/TestDDLBuilder.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/client/TestDDLBuilder.java b/tajo-core/src/test/java/org/apache/tajo/client/TestDDLBuilder.java deleted file mode 100644 index 1855217..0000000 --- a/tajo-core/src/test/java/org/apache/tajo/client/TestDDLBuilder.java +++ /dev/null @@ -1,133 +0,0 @@ -/** - * 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.tajo.client; - -import org.apache.hadoop.fs.Path; -import org.apache.hadoop.io.compress.GzipCodec; -import org.apache.tajo.catalog.*; -import org.apache.tajo.catalog.partition.PartitionMethodDesc; -import org.apache.tajo.catalog.proto.CatalogProtos; -import org.apache.tajo.common.TajoDataTypes; -import org.apache.tajo.storage.StorageConstants; -import org.apache.tajo.util.FileUtil; -import org.junit.Test; - -import static org.junit.Assert.*; - - -public class TestDDLBuilder { - private static final Schema schema1; - private static final TableMeta meta1; - private static final PartitionMethodDesc partitionMethod1; - - static { - schema1 = new Schema(); - schema1.addColumn("name", TajoDataTypes.Type.BLOB); - schema1.addColumn("addr", TajoDataTypes.Type.TEXT); - - meta1 = CatalogUtil.newTableMeta(CatalogProtos.StoreType.CSV); - meta1.putOption(StorageConstants.CSVFILE_DELIMITER, StorageConstants.DEFAULT_FIELD_DELIMITER); - meta1.putOption(StorageConstants.COMPRESSION_CODEC, GzipCodec.class.getName()); - - Schema expressionSchema = new Schema(); - expressionSchema.addColumn("key", TajoDataTypes.Type.INT4); - expressionSchema.addColumn("key2", TajoDataTypes.Type.TEXT); - partitionMethod1 = new PartitionMethodDesc( - "db1", - "table1", - CatalogProtos.PartitionType.COLUMN, - "key,key2", - expressionSchema); - } - - @Test - public void testBuildDDLForExternalTable() throws Exception { - TableDesc desc = new TableDesc("db1.table1", schema1, meta1, new Path("/table1")); - desc.setPartitionMethod(partitionMethod1); - desc.setExternal(true); - assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLForExternalTable.result"), - DDLBuilder.buildDDLForExternalTable(desc)); - } - - @Test - public void testBuildDDLQuotedTableName() throws Exception { - Schema schema2 = new Schema(); - schema2.addColumn("name", TajoDataTypes.Type.BLOB); - schema2.addColumn("addr", TajoDataTypes.Type.TEXT); - schema2.addColumn("FirstName", TajoDataTypes.Type.TEXT); - schema2.addColumn("LastName", TajoDataTypes.Type.TEXT); - schema2.addColumn("with", TajoDataTypes.Type.TEXT); - - Schema expressionSchema2 = new Schema(); - expressionSchema2.addColumn("BirthYear", TajoDataTypes.Type.INT4); - - PartitionMethodDesc partitionMethod2 = new PartitionMethodDesc( - "db1", - "table1", - CatalogProtos.PartitionType.COLUMN, - "key,key2", - expressionSchema2); - - TableDesc desc = new TableDesc("db1.TABLE2", schema2, meta1, new Path("/table1")); - desc.setPartitionMethod(partitionMethod2); - desc.setExternal(true); - assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLQuotedTableName1.result"), - DDLBuilder.buildDDLForExternalTable(desc)); - - desc = new TableDesc("db1.TABLE1", schema2, meta1, new Path("/table1")); - desc.setPartitionMethod(partitionMethod2); - desc.setExternal(false); - assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLQuotedTableName2.result"), - DDLBuilder.buildDDLForBaseTable(desc)); - } - - @Test - public void testBuildDDLForBaseTable() throws Exception { - TableDesc desc = new TableDesc("db1.table2", schema1, meta1, new Path("/table1")); - assertEquals(FileUtil.readTextFileFromResource("results/testDDLBuilder/testBuildDDLForBaseTable.result"), - DDLBuilder.buildDDLForBaseTable(desc)); - } - - @Test - public void testBuildColumn() throws Exception { - String [] tobeUnquoted = { - "column_name", - "columnname", - "column_1", - }; - - for (String columnName : tobeUnquoted) { - assertFalse(CatalogUtil.isShouldBeQuoted(columnName)); - } - - String [] quoted = { - "Column_Name", - "COLUMN_NAME", - "컬ë¼", - "$column_name", - "Column_Name1", - "with", - "when" - }; - - for (String columnName : quoted) { - assertTrue(CatalogUtil.isShouldBeQuoted(columnName)); - } - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/client/TestTajoDump.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/client/TestTajoDump.java b/tajo-core/src/test/java/org/apache/tajo/client/TestTajoDump.java deleted file mode 100644 index e0d745e..0000000 --- a/tajo-core/src/test/java/org/apache/tajo/client/TestTajoDump.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * 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.tajo.client; - -import org.apache.hadoop.security.UserGroupInformation; -import org.apache.tajo.QueryTestCaseBase; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; - -public class TestTajoDump extends QueryTestCaseBase { - - @Test - public void testDump1() throws Exception { - if (!testingCluster.isHCatalogStoreRunning()) { - executeString("CREATE TABLE \"" + getCurrentDatabase() + - "\".\"TableName1\" (\"Age\" int, \"FirstName\" TEXT, lastname TEXT)"); - - UserGroupInformation userInfo = UserGroupInformation.getCurrentUser(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - PrintWriter printWriter = new PrintWriter(bos); - TajoDump.dump(client, userInfo, getCurrentDatabase(), false, false, false, printWriter); - printWriter.flush(); - printWriter.close(); - assertStrings(new String(bos.toByteArray())); - bos.close(); - } - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java b/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java index b00ab32..4287191 100644 --- a/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java +++ b/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java @@ -25,9 +25,9 @@ import org.apache.tajo.TajoTestingCluster; import org.apache.tajo.algebra.Expr; import org.apache.tajo.catalog.*; import org.apache.tajo.catalog.proto.CatalogProtos; -import org.apache.tajo.cli.InvalidStatementException; -import org.apache.tajo.cli.ParsedResult; -import org.apache.tajo.cli.SimpleParser; +import org.apache.tajo.cli.tsql.InvalidStatementException; +import org.apache.tajo.cli.tsql.ParsedResult; +import org.apache.tajo.cli.tsql.SimpleParser; import org.apache.tajo.common.TajoDataTypes.Type; import org.apache.tajo.conf.TajoConf; import org.apache.tajo.datum.*;
