From b83ba218dc9c4070e70c7b5f2894d1365df4aa56 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Fri, 16 Dec 2022 11:08:39 +0800 Subject: [PATCH 1/2] Fix issue of parsing insert with settings --- .../src/main/javacc/ClickHouseSqlParser.jj | 2 +- .../jdbc/ClickHousePreparedStatementTest.java | 24 ++++++++++++++++++- .../jdbc/parser/ClickHouseSqlParserTest.java | 4 ++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/clickhouse-jdbc/src/main/javacc/ClickHouseSqlParser.jj b/clickhouse-jdbc/src/main/javacc/ClickHouseSqlParser.jj index 27a84c036..561121c92 100644 --- a/clickhouse-jdbc/src/main/javacc/ClickHouseSqlParser.jj +++ b/clickhouse-jdbc/src/main/javacc/ClickHouseSqlParser.jj @@ -512,6 +512,7 @@ void insertStmt(): {} { LOOKAHEAD({ getToken(1).kind == FUNCTION }) functionExpr() | (LOOKAHEAD(2) )? tableIdentifier(true) ) + ( settingsPart() )? dataClause() } @@ -528,7 +529,6 @@ void dataClause(): {} { columnExprList() { token_source.removePosition(ClickHouseSqlStatement.KEYWORD_VALUES_END); } )* - ( settingsPart() )? | (LOOKAHEAD(2) ((withClause())? { token_source.input = ClickHouseSqlUtils.unescape(token.image); } )? { token_source.format = token.image; } )? (anyExprList())? diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java index e12a50bb0..048e3240e 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -42,7 +42,6 @@ import com.clickhouse.client.config.ClickHouseClientOption; import com.clickhouse.client.data.ClickHouseBitmap; import com.clickhouse.client.data.ClickHouseExternalTable; -import com.clickhouse.client.data.UnsignedByte; import com.clickhouse.client.data.UnsignedInteger; import com.clickhouse.client.data.UnsignedLong; import com.clickhouse.jdbc.internal.InputBasedPreparedStatement; @@ -1580,6 +1579,29 @@ public void testInsertWithNullDateTime() throws SQLException { } } + @Test(groups = "integration") + public void testInsertWithSettings() throws SQLException { + Properties props = new Properties(); + try (ClickHouseConnection conn = newConnection(props); + Statement s = conn.createStatement()) { + s.execute("drop table if exists test_insert_with_settings; " + + "CREATE TABLE test_insert_with_settings(i Int32, s String) ENGINE=Memory"); + try (PreparedStatement ps = conn + .prepareStatement( + "INSERT INTO test_insert_with_settings SETTINGS async_insert=1,wait_for_async_insert=1 values(?, ?)")) { + ps.setInt(1, 1); + ps.setString(2, "1"); + ps.addBatch(); + ps.executeBatch(); + } + + try (ResultSet rs = s.executeQuery("select * from test_insert_with_settings order by i")) { + Assert.assertTrue(rs.next()); + Assert.assertFalse(rs.next()); + } + } + } + @Test(groups = "integration") public void testGetParameterMetaData() throws SQLException { try (Connection conn = newConnection(new Properties()); diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/parser/ClickHouseSqlParserTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/parser/ClickHouseSqlParserTest.java index 02675e67b..b47726710 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/parser/ClickHouseSqlParserTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/parser/ClickHouseSqlParserTest.java @@ -238,10 +238,10 @@ public void testInsertStatement() throws ParseException { checkSingleStatement(parse(sql = "INSERT INTO insert_select_testtable (* EXCEPT(b)) Values (2, 2)"), sql, StatementType.INSERT, "system", "insert_select_testtable"); checkSingleStatement( - parse(sql = "insert into `test` (num) values (?) SETTINGS input_format_null_as_default = 1"), + parse(sql = "insert into `test` (num)SETTINGS input_format_null_as_default = 1 values (?)"), sql, StatementType.INSERT, "system", "test"); checkSingleStatement( - parse(sql = "insert into `test` (id, name) values (1,2)(3,4),(5,6) SETTINGS input_format_null_as_default = 1"), + parse(sql = "insert into `test` (id, name) SETTINGS input_format_null_as_default = 1 values (1,2)(3,4),(5,6)"), sql, StatementType.INSERT, "system", "test"); s = checkSingleStatement( parse(sql = "insert into `test`"), sql, StatementType.INSERT, "system", "test"); From 55061aebd6f35221bca451c1852abf986352a0a1 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Fri, 16 Dec 2022 11:42:32 +0800 Subject: [PATCH 2/2] Skip the new test before 22.5 --- .../clickhouse/jdbc/ClickHousePreparedStatementTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java index 048e3240e..f798ba500 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -1582,8 +1582,12 @@ public void testInsertWithNullDateTime() throws SQLException { @Test(groups = "integration") public void testInsertWithSettings() throws SQLException { Properties props = new Properties(); - try (ClickHouseConnection conn = newConnection(props); - Statement s = conn.createStatement()) { + try (ClickHouseConnection conn = newConnection(props); Statement s = conn.createStatement()) { + if (!conn.getServerVersion().check("[22.5,)")) { + throw new SkipException( + "Skip due to breaking change introduced by https://github.com/ClickHouse/ClickHouse/pull/35883"); + } + s.execute("drop table if exists test_insert_with_settings; " + "CREATE TABLE test_insert_with_settings(i Int32, s String) ENGINE=Memory"); try (PreparedStatement ps = conn