Repository: calcite Updated Branches: refs/heads/branch-1.14 c0adb86b0 -> 6fa983541 (forced update)
Various tweaks They are: * Plug some resource leaks * Add tests for SELECT DISTINCT ... GROUP BY (they already work) * Fix OsAdapterTest on Windows; the 'files', 'vmstat' and 'ps' tables do not work on Windows * In checkstyle, LICENSE and NOTICE don't need header A pull request we forgot to close in CALCITE-1959: Close apache/calcite#529 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/310eaa00 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/310eaa00 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/310eaa00 Branch: refs/heads/branch-1.14 Commit: 310eaa0016a21155fa3d504ba6028c81c49d0d60 Parents: 939c9a6 Author: Julian Hyde <[email protected]> Authored: Wed Aug 9 16:27:49 2017 -0700 Committer: Julian Hyde <[email protected]> Committed: Thu Sep 7 11:23:18 2017 -0700 ---------------------------------------------------------------------- .../apache/calcite/test/TableFunctionTest.java | 183 ++++++++++--------- core/src/test/resources/sql/agg.iq | 67 +++++++ .../org/apache/calcite/adapter/os/SqlShell.java | 15 +- .../calcite/adapter/os/OsAdapterTest.java | 16 +- src/main/config/checkstyle/suppressions.xml | 2 + 5 files changed, 185 insertions(+), 98 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/310eaa00/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java b/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java index 12392b3..fefd7d2 100644 --- a/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java +++ b/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java @@ -161,44 +161,44 @@ public class TableFunctionTest { /** As {@link #testScannableTableFunction()} but with named parameters. */ @Test public void testMultipleScannableTableFunctionWithNamedParameters() throws SQLException, ClassNotFoundException { - Connection connection = DriverManager.getConnection("jdbc:calcite:"); - CalciteConnection calciteConnection = - connection.unwrap(CalciteConnection.class); - SchemaPlus rootSchema = calciteConnection.getRootSchema(); - SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); - final TableFunction table1 = TableFunctionImpl.create(Smalls.MAZE_METHOD); - schema.add("Maze", table1); - final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE2_METHOD); - schema.add("Maze", table2); - final TableFunction table3 = TableFunctionImpl.create(Smalls.MAZE3_METHOD); - schema.add("Maze", table3); - final String sql = "select *\n" - + "from table(\"s\".\"Maze\"(5, 3, 1))"; - final Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(sql); - final String result = "S=abcde\n" - + "S=xyz\n"; - assertThat(CalciteAssert.toString(resultSet), - is(result + "S=generate(w=5, h=3, s=1)\n")); + try (Connection connection = DriverManager.getConnection("jdbc:calcite:"); + Statement statement = connection.createStatement()) { + CalciteConnection calciteConnection = + connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); + SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); + final TableFunction table1 = TableFunctionImpl.create(Smalls.MAZE_METHOD); + schema.add("Maze", table1); + final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE2_METHOD); + schema.add("Maze", table2); + final TableFunction table3 = TableFunctionImpl.create(Smalls.MAZE3_METHOD); + schema.add("Maze", table3); + final String sql = "select *\n" + + "from table(\"s\".\"Maze\"(5, 3, 1))"; + ResultSet resultSet = statement.executeQuery(sql); + final String result = "S=abcde\n" + + "S=xyz\n"; + assertThat(CalciteAssert.toString(resultSet), + is(result + "S=generate(w=5, h=3, s=1)\n")); - final String sql2 = "select *\n" - + "from table(\"s\".\"Maze\"(WIDTH => 5, HEIGHT => 3, SEED => 1))"; - resultSet = statement.executeQuery(sql2); - assertThat(CalciteAssert.toString(resultSet), - is(result + "S=generate2(w=5, h=3, s=1)\n")); + final String sql2 = "select *\n" + + "from table(\"s\".\"Maze\"(WIDTH => 5, HEIGHT => 3, SEED => 1))"; + resultSet = statement.executeQuery(sql2); + assertThat(CalciteAssert.toString(resultSet), + is(result + "S=generate2(w=5, h=3, s=1)\n")); - final String sql3 = "select *\n" - + "from table(\"s\".\"Maze\"(HEIGHT => 3, WIDTH => 5))"; - resultSet = statement.executeQuery(sql3); - assertThat(CalciteAssert.toString(resultSet), - is(result + "S=generate2(w=5, h=3, s=null)\n")); + final String sql3 = "select *\n" + + "from table(\"s\".\"Maze\"(HEIGHT => 3, WIDTH => 5))"; + resultSet = statement.executeQuery(sql3); + assertThat(CalciteAssert.toString(resultSet), + is(result + "S=generate2(w=5, h=3, s=null)\n")); - final String sql4 = "select *\n" - + "from table(\"s\".\"Maze\"(FOO => 'a'))"; - resultSet = statement.executeQuery(sql4); - assertThat(CalciteAssert.toString(resultSet), - is(result + "S=generate3(foo=a)\n")); - connection.close(); + final String sql4 = "select *\n" + + "from table(\"s\".\"Maze\"(FOO => 'a'))"; + resultSet = statement.executeQuery(sql4); + assertThat(CalciteAssert.toString(resultSet), + is(result + "S=generate3(foo=a)\n")); + } } /** @@ -264,31 +264,32 @@ public class TableFunctionTest { + "could not be implemented") @Test public void testTableFunctionCursorInputs() throws SQLException, ClassNotFoundException { - Connection connection = - DriverManager.getConnection("jdbc:calcite:"); - CalciteConnection calciteConnection = - connection.unwrap(CalciteConnection.class); - SchemaPlus rootSchema = calciteConnection.getRootSchema(); - SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); - final TableFunction table = - TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD); - schema.add("GenerateStrings", table); - final TableFunction add = - TableFunctionImpl.create(Smalls.PROCESS_CURSOR_METHOD); - schema.add("process", add); - final PreparedStatement ps = connection.prepareStatement("select *\n" - + "from table(\"s\".\"process\"(2,\n" - + "cursor(select * from table(\"s\".\"GenerateStrings\"(?)))\n" - + ")) as t(u)\n" - + "where u > 3"); - ps.setInt(1, 5); - ResultSet resultSet = ps.executeQuery(); - // GenerateStrings returns 0..4, then 2 is added (process function), - // thus 2..6, finally where u > 3 leaves just 4..6 - assertThat(CalciteAssert.toString(resultSet), - equalTo("u=4\n" - + "u=5\n" - + "u=6\n")); + try (Connection connection = + DriverManager.getConnection("jdbc:calcite:")) { + CalciteConnection calciteConnection = + connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); + SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); + final TableFunction table = + TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD); + schema.add("GenerateStrings", table); + final TableFunction add = + TableFunctionImpl.create(Smalls.PROCESS_CURSOR_METHOD); + schema.add("process", add); + final PreparedStatement ps = connection.prepareStatement("select *\n" + + "from table(\"s\".\"process\"(2,\n" + + "cursor(select * from table(\"s\".\"GenerateStrings\"(?)))\n" + + ")) as t(u)\n" + + "where u > 3"); + ps.setInt(1, 5); + ResultSet resultSet = ps.executeQuery(); + // GenerateStrings returns 0..4, then 2 is added (process function), + // thus 2..6, finally where u > 3 leaves just 4..6 + assertThat(CalciteAssert.toString(resultSet), + equalTo("u=4\n" + + "u=5\n" + + "u=6\n")); + } } /** @@ -298,37 +299,37 @@ public class TableFunctionTest { + "could not be implemented") @Test public void testTableFunctionCursorsInputs() throws SQLException, ClassNotFoundException { - Connection connection = - getConnectionWithMultiplyFunction(); - CalciteConnection calciteConnection = - connection.unwrap(CalciteConnection.class); - SchemaPlus rootSchema = calciteConnection.getRootSchema(); - SchemaPlus schema = rootSchema.getSubSchema("s"); - final TableFunction table = - TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD); - schema.add("GenerateStrings", table); - final TableFunction add = - TableFunctionImpl.create(Smalls.PROCESS_CURSORS_METHOD); - schema.add("process", add); - final PreparedStatement ps = connection.prepareStatement("select *\n" - + "from table(\"s\".\"process\"(2,\n" - + "cursor(select * from table(\"s\".\"multiplication\"(5,5,0))),\n" - + "cursor(select * from table(\"s\".\"GenerateStrings\"(?)))\n" - + ")) as t(u)\n" - + "where u > 3"); - ps.setInt(1, 5); - ResultSet resultSet = ps.executeQuery(); - // GenerateStrings produce 0..4 - // multiplication produce 1..5 - // process sums and adds 2 - // sum is 2 + 1..9 == 3..9 - assertThat(CalciteAssert.toString(resultSet), - equalTo("u=4\n" - + "u=5\n" - + "u=6\n" - + "u=7\n" - + "u=8\n" - + "u=9\n")); + try (Connection connection = getConnectionWithMultiplyFunction()) { + CalciteConnection calciteConnection = + connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); + SchemaPlus schema = rootSchema.getSubSchema("s"); + final TableFunction table = + TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD); + schema.add("GenerateStrings", table); + final TableFunction add = + TableFunctionImpl.create(Smalls.PROCESS_CURSORS_METHOD); + schema.add("process", add); + final PreparedStatement ps = connection.prepareStatement("select *\n" + + "from table(\"s\".\"process\"(2,\n" + + "cursor(select * from table(\"s\".\"multiplication\"(5,5,0))),\n" + + "cursor(select * from table(\"s\".\"GenerateStrings\"(?)))\n" + + ")) as t(u)\n" + + "where u > 3"); + ps.setInt(1, 5); + ResultSet resultSet = ps.executeQuery(); + // GenerateStrings produce 0..4 + // multiplication produce 1..5 + // process sums and adds 2 + // sum is 2 + 1..9 == 3..9 + assertThat(CalciteAssert.toString(resultSet), + equalTo("u=4\n" + + "u=5\n" + + "u=6\n" + + "u=7\n" + + "u=8\n" + + "u=9\n")); + } } @Test public void testUserDefinedTableFunction() { http://git-wip-us.apache.org/repos/asf/calcite/blob/310eaa00/core/src/test/resources/sql/agg.iq ---------------------------------------------------------------------- diff --git a/core/src/test/resources/sql/agg.iq b/core/src/test/resources/sql/agg.iq index ba97052..a56ca09 100755 --- a/core/src/test/resources/sql/agg.iq +++ b/core/src/test/resources/sql/agg.iq @@ -62,6 +62,73 @@ select count(deptno, ename, 1, deptno) as c from emp; !ok +# DISTINCT and GROUP BY +select distinct deptno, count(*) as c from emp group by deptno; ++--------+---+ +| DEPTNO | C | ++--------+---+ +| 10 | 2 | +| 20 | 1 | +| 30 | 2 | +| 50 | 2 | +| 60 | 1 | +| | 1 | ++--------+---+ +(6 rows) + +!ok + +select distinct deptno from emp group by deptno; ++--------+ +| DEPTNO | ++--------+ +| 10 | +| 20 | +| 30 | +| 50 | +| 60 | +| | ++--------+ +(6 rows) + +!ok + +select distinct count(*) as c from emp group by deptno; ++---+ +| C | ++---+ +| 1 | +| 2 | ++---+ +(2 rows) + +!ok + +select distinct count(*) as c from emp group by deptno having count(*) > 1; ++---+ +| C | ++---+ +| 2 | ++---+ +(1 row) + +!ok + +select distinct count(*) as c from emp group by deptno order by deptno desc; +Expression 'DEPTNO' is not in the select clause +!error + +select distinct count(*) as c from emp group by deptno order by 1 desc; ++---+ +| C | ++---+ +| 2 | +| 1 | ++---+ +(2 rows) + +!ok + # [CALCITE-998] Exception when calling STDDEV_SAMP, STDDEV_POP # stddev_samp select stddev_samp(deptno) as s from emp; http://git-wip-us.apache.org/repos/asf/calcite/blob/310eaa00/plus/src/main/java/org/apache/calcite/adapter/os/SqlShell.java ---------------------------------------------------------------------- diff --git a/plus/src/main/java/org/apache/calcite/adapter/os/SqlShell.java b/plus/src/main/java/org/apache/calcite/adapter/os/SqlShell.java index bf91b41..6cdbfab 100644 --- a/plus/src/main/java/org/apache/calcite/adapter/os/SqlShell.java +++ b/plus/src/main/java/org/apache/calcite/adapter/os/SqlShell.java @@ -121,12 +121,10 @@ public class SqlShell { + "default), 'csv',\n" + " 'headers', 'json', 'mysql'\n" + " -h --help Print this help"; - try (Connection connection = DriverManager.getConnection(url); - Statement s = connection.createStatement()) { - final StringBuilder b = new StringBuilder(); - final Enumerator<String> args = - Linq4j.asEnumerable(this.args).enumerator(); - Format format = Format.SPACED; + final StringBuilder b = new StringBuilder(); + Format format = Format.SPACED; + try (Enumerator<String> args = + Linq4j.asEnumerable(this.args).enumerator()) { while (args.moveNext()) { if (args.current().equals("-o")) { if (args.moveNext()) { @@ -150,6 +148,11 @@ public class SqlShell { b.append(args.current()); } } + } + try (Connection connection = DriverManager.getConnection(url); + Statement s = connection.createStatement(); + Enumerator<String> args = + Linq4j.asEnumerable(this.args).enumerator()) { final ResultSet r = s.executeQuery(b.toString()); format.output(out, r); r.close(); http://git-wip-us.apache.org/repos/asf/calcite/blob/310eaa00/plus/src/test/java/org/apache/calcite/adapter/os/OsAdapterTest.java ---------------------------------------------------------------------- diff --git a/plus/src/test/java/org/apache/calcite/adapter/os/OsAdapterTest.java b/plus/src/test/java/org/apache/calcite/adapter/os/OsAdapterTest.java index 69d142b..c1cd78e 100644 --- a/plus/src/test/java/org/apache/calcite/adapter/os/OsAdapterTest.java +++ b/plus/src/test/java/org/apache/calcite/adapter/os/OsAdapterTest.java @@ -22,10 +22,12 @@ import org.apache.calcite.runtime.Hook; import org.apache.calcite.sql.validate.SqlConformanceEnum; import org.apache.calcite.test.CalciteAssert; import org.apache.calcite.util.Holder; +import org.apache.calcite.util.Util; import com.google.common.base.Function; import org.hamcrest.CoreMatchers; +import org.junit.Assume; import org.junit.Test; import java.io.ByteArrayInputStream; @@ -58,6 +60,10 @@ import static org.junit.Assert.fail; * </ul> */ public class OsAdapterTest { + private static boolean isWindows() { + return System.getProperty("os.name").startsWith("Windows"); + } + @Test public void testDu() { sql("select * from du") .returns( @@ -98,12 +104,16 @@ public class OsAdapterTest { } @Test public void testFiles() { + Assume.assumeFalse("Skip: the 'files' table does not work on Windows", + isWindows()); sql("select distinct type from files") .returnsUnordered("type=d", "type=f"); } @Test public void testPs() { + Assume.assumeFalse("Skip: the 'ps' table does not work on Windows", + isWindows()); sql("select * from ps") .returns( new Function<ResultSet, Void>() { @@ -126,6 +136,8 @@ public class OsAdapterTest { } @Test public void testPsDistinct() { + Assume.assumeFalse("Skip: the 'ps' table does not work on Windows", + isWindows()); sql("select distinct `user` from ps") .returns( new Function<ResultSet, Void>() { @@ -166,6 +178,8 @@ public class OsAdapterTest { } @Test public void testVmstat() { + Assume.assumeFalse("Skip: the 'files' table does not work on Windows", + isWindows()); sql("select * from vmstat") .returns( new Function<ResultSet, Void>() { @@ -304,7 +318,7 @@ public class OsAdapterTest { final StringWriter errSw = new StringWriter(); final PrintWriter err = new PrintWriter(errSw); new SqlShell(in, out, err, args).run(); - return outSw.toString(); + return Util.toLinux(outSw.toString()); } @Test public void testSqlShellHelp() throws SQLException { http://git-wip-us.apache.org/repos/asf/calcite/blob/310eaa00/src/main/config/checkstyle/suppressions.xml ---------------------------------------------------------------------- diff --git a/src/main/config/checkstyle/suppressions.xml b/src/main/config/checkstyle/suppressions.xml index 2f55a52..b76afb7 100644 --- a/src/main/config/checkstyle/suppressions.xml +++ b/src/main/config/checkstyle/suppressions.xml @@ -21,6 +21,8 @@ limitations under the License. <suppressions> <!-- Suppress checks on generated files. --> <suppress checks="Header" files="CalciteResource.properties"/> + <suppress checks="Header" files="LICENSE"/> + <suppress checks="Header" files="NOTICE"/> <suppress checks=".*" files="org-apache-calcite-jdbc.properties"/> <suppress checks=".*" files="Foo.java"/> <suppress checks=".*" files=".*[/\\]target[/\\]maven-archiver[/\\]pom.properties"/>
