From d1d228fb136ef2dd4986f9c31bacbbacabe0e701 Mon Sep 17 00:00:00 2001 From: penghuo Date: Wed, 23 Feb 2022 10:22:06 -0800 Subject: [PATCH] Version Bump: H2 1.x -> 2.x Signed-off-by: penghuo --- integ-test/build.gradle | 2 +- .../runner/connection/JDBCConnection.java | 11 +++- .../runner/resultset/DBResult.java | 12 +++- .../sql/correctness/runner/resultset/Row.java | 2 + .../correctness/tests/JDBCConnectionTest.java | 14 ++--- .../opensearch/sql/sql/SQLCorrectnessIT.java | 4 ++ .../correctness/expressions/cast.txt | 12 ++-- .../resources/correctness/queries/window.txt | 60 +++++++++---------- 8 files changed, 72 insertions(+), 45 deletions(-) diff --git a/integ-test/build.gradle b/integ-test/build.gradle index fc87f88a7c..6079eede9f 100644 --- a/integ-test/build.gradle +++ b/integ-test/build.gradle @@ -72,7 +72,7 @@ dependencies { include '*.jar' builtBy 'compileJdbc' } - testCompile group: 'com.h2database', name: 'h2', version: '1.4.200' + testCompile group: 'com.h2database', name: 'h2', version: '2.1.210' testCompile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.28.0' testCompile group: 'com.google.code.gson', name: 'gson', version: '2.8.9' } diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/JDBCConnection.java b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/JDBCConnection.java index 520bc52f79..d2d7d2aee6 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/JDBCConnection.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/JDBCConnection.java @@ -30,6 +30,7 @@ public class JDBCConnection implements DBConnection { private static final String SINGLE_QUOTE = "'"; private static final String DOUBLE_QUOTE = "''"; + private static final String BACKTICK = "`"; /** * Database name for display @@ -103,7 +104,8 @@ public void drop(String tableName) { @Override public void insert(String tableName, String[] columnNames, List batch) { try (Statement stmt = connection.createStatement()) { - String names = String.join(",", columnNames); + String names = + Arrays.stream(columnNames).map(this::delimited).collect(joining(",")); for (Object[] fieldValues : batch) { stmt.addBatch(StringUtils.format( "INSERT INTO %s(%s) VALUES (%s)", tableName, names, getValueList(fieldValues))); @@ -143,7 +145,8 @@ public void close() { private String parseColumnNameAndTypesInSchemaJson(String schema) { JSONObject json = (JSONObject) new JSONObject(schema).query("/mappings/properties"); return json.keySet().stream(). - map(colName -> colName + " " + mapToJDBCType(json.getJSONObject(colName).getString("type"))) + map(colName -> delimited(colName) + " " + mapToJDBCType(json.getJSONObject(colName) + .getString("type"))) .collect(joining(",")); } @@ -212,4 +215,8 @@ private boolean isOrderByQuery(String query) { public void setConnection(Connection connection) { this.connection = connection; } + + private String delimited(String columnName) { + return BACKTICK + columnName + BACKTICK; + } } diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/DBResult.java b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/DBResult.java index dfb0f66406..0899a6e2c4 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/DBResult.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/DBResult.java @@ -30,8 +30,16 @@ public class DBResult { /** * Possible types for floating point number + * H2 2.x use DOUBLE PRECISION instead of DOUBLE. */ - private static final Set FLOAT_TYPES = ImmutableSet.of("FLOAT", "DOUBLE", "REAL"); + private static final Set FLOAT_TYPES = + ImmutableSet.of("FLOAT", "DOUBLE", "REAL", "DOUBLE PRECISION", "DECFLOAT"); + + /** + * Possible types for varchar. + * H2 2.x use CHARACTER VARYING instead of VARCHAR. + */ + private static final Set VARCHAR = ImmutableSet.of("CHARACTER VARYING", "VARCHAR"); /** * Database name for display @@ -81,6 +89,8 @@ public void addColumn(String name, String type) { // Ignore float type by assigning all type names string to it. if (FLOAT_TYPES.contains(type)) { type = FLOAT_TYPES.toString(); + } else if (VARCHAR.contains(type)) { + type = "VARCHAR"; } schema.add(new Type(StringUtils.toUpper(name), type)); } diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java index 5dfe2c197b..da08487a10 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java @@ -44,6 +44,8 @@ private Object roundFloatNum(Object value) { } else if (value instanceof Double) { BigDecimal decimal = BigDecimal.valueOf((Double) value).setScale(2, RoundingMode.CEILING); value = decimal.doubleValue(); + } else if (value instanceof BigDecimal) { + value = ((BigDecimal) value).setScale(2, RoundingMode.CEILING).doubleValue(); } return value; } diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/tests/JDBCConnectionTest.java b/integ-test/src/test/java/org/opensearch/sql/correctness/tests/JDBCConnectionTest.java index 966bfe7692..1c253c9d82 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/tests/JDBCConnectionTest.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/tests/JDBCConnectionTest.java @@ -67,7 +67,7 @@ public void testCreateTable() throws SQLException { verify(statement).executeUpdate(argCap.capture()); String actual = argCap.getValue(); - assertEquals("CREATE TABLE test(name VARCHAR,age INT)", actual); + assertEquals("CREATE TABLE test(`name` VARCHAR,`age` INT)", actual); } @Test @@ -92,8 +92,8 @@ public void testInsertData() throws SQLException { assertEquals( Arrays.asList( - "INSERT INTO test(name,age) VALUES ('John','25')", - "INSERT INTO test(name,age) VALUES ('Hank','30')" + "INSERT INTO test(`name`,`age`) VALUES ('John','25')", + "INSERT INTO test(`name`,`age`) VALUES ('Hank','30')" ), actual ); } @@ -112,9 +112,9 @@ public void testInsertNullData() throws SQLException { assertEquals( Arrays.asList( - "INSERT INTO test(name,age) VALUES ('John',NULL)", - "INSERT INTO test(name,age) VALUES (NULL,'25')", - "INSERT INTO test(name,age) VALUES ('Hank','30')" + "INSERT INTO test(`name`,`age`) VALUES ('John',NULL)", + "INSERT INTO test(`name`,`age`) VALUES (NULL,'25')", + "INSERT INTO test(`name`,`age`) VALUES ('Hank','30')" ), actual ); } @@ -178,7 +178,7 @@ public void testSelectQueryWithFloatInResultSet() throws SQLException { assertEquals( Arrays.asList( new Type("NAME", "VARCHAR"), - new Type("BALANCE", "[FLOAT, DOUBLE, REAL]") + new Type("BALANCE", "[FLOAT, DOUBLE, REAL, DOUBLE PRECISION, DECFLOAT]") ), result.getSchema() ); diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java index 89763e87e6..30f23547ec 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java @@ -48,9 +48,13 @@ private void verifyQueries(String[] dirs, Function converter) th } } + /** + * Comment start with # + */ private void verifyQueries(Path file, Function converter) { try { String[] queries = Files.lines(file) + .filter(line -> !line.startsWith("#")) .map(converter) .toArray(String[]::new); verify(queries); diff --git a/integ-test/src/test/resources/correctness/expressions/cast.txt b/integ-test/src/test/resources/correctness/expressions/cast.txt index 2d313203b4..a0936170d1 100644 --- a/integ-test/src/test/resources/correctness/expressions/cast.txt +++ b/integ-test/src/test/resources/correctness/expressions/cast.txt @@ -17,10 +17,14 @@ cast('01:01:01' as time) as castTime cast('true' as boolean) as castBool cast(1 as boolean) as castBool cast(cast(1 as string) as int) castCombine -false = 'False' as implicitCast -false = 'true' as implicitCast -'TRUE' = true as implicitCast -'false' = true as implicitCast +# disable, H2 2.x does not support 'False' literal +#false = 'False' as implicitCast +# disable, H2 2.x does not support 'true' literal +#false = 'true' as implicitCast +# disable, H2 2.x does not support 'TRUE' literal +#'TRUE' = true as implicitCast +# disable, H2 2.x does not support 'false' literal +#'false' = true as implicitCast CAST('2021-06-17 00:00:00' AS TIMESTAMP) = '2021-06-17 00:00:00' as implicitCast '2021-06-18' < CAST('2021-06-17' AS DATE) as implicitCast '10:20:00' <= CAST('11:00:00' AS TIME) as implicitCast diff --git a/integ-test/src/test/resources/correctness/queries/window.txt b/integ-test/src/test/resources/correctness/queries/window.txt index c3f2715322..2d0d9d3490 100644 --- a/integ-test/src/test/resources/correctness/queries/window.txt +++ b/integ-test/src/test/resources/correctness/queries/window.txt @@ -21,33 +21,33 @@ SELECT FlightDelayMin, AvgTicketPrice, STDDEV_POP(AvgTicketPrice) OVER (ORDER BY SELECT FlightDelayMin, AvgTicketPrice, STDDEV_SAMP(AvgTicketPrice) OVER (ORDER BY FlightDelayMin) AS num FROM opensearch_dashboards_sample_data_flights ORDER BY FlightDelayMin SELECT FlightDelayMin, AvgTicketPrice, VAR_POP(AvgTicketPrice) OVER (ORDER BY FlightDelayMin) AS num FROM opensearch_dashboards_sample_data_flights ORDER BY FlightDelayMin SELECT FlightDelayMin, AvgTicketPrice, VAR_SAMP(AvgTicketPrice) OVER (ORDER BY FlightDelayMin) AS num FROM opensearch_dashboards_sample_data_flights ORDER BY FlightDelayMin -SELECT user, RANK() OVER (ORDER BY user) AS rnk FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, DENSE_RANK() OVER (ORDER BY user) AS rnk FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, COUNT(day_of_week_i) OVER (ORDER BY user) AS cnt FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, SUM(day_of_week_i) OVER (ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, AVG(day_of_week_i) OVER (ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, MAX(day_of_week_i) OVER (ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, MIN(day_of_week_i) OVER (ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, STDDEV_POP(day_of_week_i) OVER (ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY user -SELECT user, VAR_POP(day_of_week_i) OVER (ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY user -SELECT user, RANK() OVER (ORDER BY user DESC) AS rnk FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, DENSE_RANK() OVER (ORDER BY user DESC) AS rnk FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, COUNT(day_of_week_i) OVER (PARTITION BY user ORDER BY order_id) AS cnt FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, SUM(day_of_week_i) OVER (PARTITION BY user ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, AVG(day_of_week_i) OVER (PARTITION BY user ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, MAX(day_of_week_i) OVER (PARTITION BY user ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, MIN(day_of_week_i) OVER (PARTITION BY user ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT user, STDDEV_POP(day_of_week_i) OVER (PARTITION BY user ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY user -SELECT user, VAR_POP(day_of_week_i) OVER (PARTITION BY user ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY user -SELECT customer_gender, user, ROW_NUMBER() OVER (PARTITION BY customer_gender ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, RANK() OVER (PARTITION BY customer_gender ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, DENSE_RANK() OVER (PARTITION BY customer_gender ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, customer_id, ROW_NUMBER() OVER (PARTITION BY customer_gender, user ORDER BY customer_id) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, customer_id, RANK() OVER (PARTITION BY customer_gender, user ORDER BY customer_id) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, customer_id, DENSE_RANK() OVER (PARTITION BY customer_gender, user ORDER BY customer_id) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, ROW_NUMBER() OVER (ORDER BY customer_gender, user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, RANK() OVER (ORDER BY customer_gender, user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, DENSE_RANK() OVER (ORDER BY customer_gender, user) AS num FROM opensearch_dashboards_sample_data_ecommerce -SELECT customer_gender, user, SUM(total_quantity), ROW_NUMBER() OVER (PARTITION BY customer_gender ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce GROUP BY customer_gender, user -SELECT customer_gender, user, AVG(total_quantity), RANK() OVER (PARTITION BY customer_gender ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce GROUP BY customer_gender, user -SELECT customer_gender, user, DENSE_RANK() OVER (PARTITION BY customer_gender ORDER BY user) AS num FROM opensearch_dashboards_sample_data_ecommerce GROUP BY customer_gender, user \ No newline at end of file +SELECT `user`, RANK() OVER (ORDER BY `user`) AS rnk FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, DENSE_RANK() OVER (ORDER BY `user`) AS rnk FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, COUNT(day_of_week_i) OVER (ORDER BY `user`) AS cnt FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, SUM(day_of_week_i) OVER (ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, AVG(day_of_week_i) OVER (ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, MAX(day_of_week_i) OVER (ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, MIN(day_of_week_i) OVER (ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, STDDEV_POP(day_of_week_i) OVER (ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY `user` +SELECT `user`, VAR_POP(day_of_week_i) OVER (ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY `user` +SELECT `user`, RANK() OVER (ORDER BY `user` DESC) AS rnk FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, DENSE_RANK() OVER (ORDER BY `user` DESC) AS rnk FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, COUNT(day_of_week_i) OVER (PARTITION BY `user` ORDER BY order_id) AS cnt FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, SUM(day_of_week_i) OVER (PARTITION BY `user` ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, AVG(day_of_week_i) OVER (PARTITION BY `user` ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, MAX(day_of_week_i) OVER (PARTITION BY `user` ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, MIN(day_of_week_i) OVER (PARTITION BY `user` ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT `user`, STDDEV_POP(day_of_week_i) OVER (PARTITION BY `user` ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY `user` +SELECT `user`, VAR_POP(day_of_week_i) OVER (PARTITION BY `user` ORDER BY order_id) AS num FROM opensearch_dashboards_sample_data_ecommerce ORDER BY `user` +SELECT customer_gender, `user`, ROW_NUMBER() OVER (PARTITION BY customer_gender ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, RANK() OVER (PARTITION BY customer_gender ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, DENSE_RANK() OVER (PARTITION BY customer_gender ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, customer_id, ROW_NUMBER() OVER (PARTITION BY customer_gender, `user` ORDER BY customer_id) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, customer_id, RANK() OVER (PARTITION BY customer_gender, `user` ORDER BY customer_id) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, customer_id, DENSE_RANK() OVER (PARTITION BY customer_gender, `user` ORDER BY customer_id) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, ROW_NUMBER() OVER (ORDER BY customer_gender, `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, RANK() OVER (ORDER BY customer_gender, `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, DENSE_RANK() OVER (ORDER BY customer_gender, `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce +SELECT customer_gender, `user`, SUM(total_quantity), ROW_NUMBER() OVER (PARTITION BY customer_gender ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce GROUP BY customer_gender, `user` +SELECT customer_gender, `user`, AVG(total_quantity), RANK() OVER (PARTITION BY customer_gender ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce GROUP BY customer_gender, `user` +SELECT customer_gender, `user`, DENSE_RANK() OVER (PARTITION BY customer_gender ORDER BY `user`) AS num FROM opensearch_dashboards_sample_data_ecommerce GROUP BY customer_gender, `user`