Skip to content

Commit b83ba21

Browse files
committed
Fix issue of parsing insert with settings
1 parent 925fde5 commit b83ba21

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

clickhouse-jdbc/src/main/javacc/ClickHouseSqlParser.jj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ void insertStmt(): {} {
512512
LOOKAHEAD({ getToken(1).kind == FUNCTION }) <FUNCTION> functionExpr()
513513
| (LOOKAHEAD(2) <TABLE>)? tableIdentifier(true)
514514
)
515+
( settingsPart() )?
515516
dataClause()
516517
}
517518

@@ -528,7 +529,6 @@ void dataClause(): {} {
528529
columnExprList()
529530
<RPAREN> { token_source.removePosition(ClickHouseSqlStatement.KEYWORD_VALUES_END); }
530531
)*
531-
( settingsPart() )?
532532
| (LOOKAHEAD(2) ((withClause())? <SELECT> { token_source.addPosition(token); } columnExprList() <FROM>
533533
<INPUT> <LPAREN> <STRING_LITERAL> { token_source.input = ClickHouseSqlUtils.unescape(token.image); } <RPAREN>)?
534534
<FORMAT> <IDENTIFIER> { token_source.format = token.image; } )? (anyExprList())?

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.clickhouse.client.config.ClickHouseClientOption;
4343
import com.clickhouse.client.data.ClickHouseBitmap;
4444
import com.clickhouse.client.data.ClickHouseExternalTable;
45-
import com.clickhouse.client.data.UnsignedByte;
4645
import com.clickhouse.client.data.UnsignedInteger;
4746
import com.clickhouse.client.data.UnsignedLong;
4847
import com.clickhouse.jdbc.internal.InputBasedPreparedStatement;
@@ -1580,6 +1579,29 @@ public void testInsertWithNullDateTime() throws SQLException {
15801579
}
15811580
}
15821581

1582+
@Test(groups = "integration")
1583+
public void testInsertWithSettings() throws SQLException {
1584+
Properties props = new Properties();
1585+
try (ClickHouseConnection conn = newConnection(props);
1586+
Statement s = conn.createStatement()) {
1587+
s.execute("drop table if exists test_insert_with_settings; "
1588+
+ "CREATE TABLE test_insert_with_settings(i Int32, s String) ENGINE=Memory");
1589+
try (PreparedStatement ps = conn
1590+
.prepareStatement(
1591+
"INSERT INTO test_insert_with_settings SETTINGS async_insert=1,wait_for_async_insert=1 values(?, ?)")) {
1592+
ps.setInt(1, 1);
1593+
ps.setString(2, "1");
1594+
ps.addBatch();
1595+
ps.executeBatch();
1596+
}
1597+
1598+
try (ResultSet rs = s.executeQuery("select * from test_insert_with_settings order by i")) {
1599+
Assert.assertTrue(rs.next());
1600+
Assert.assertFalse(rs.next());
1601+
}
1602+
}
1603+
}
1604+
15831605
@Test(groups = "integration")
15841606
public void testGetParameterMetaData() throws SQLException {
15851607
try (Connection conn = newConnection(new Properties());

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/parser/ClickHouseSqlParserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ public void testInsertStatement() throws ParseException {
238238
checkSingleStatement(parse(sql = "INSERT INTO insert_select_testtable (* EXCEPT(b)) Values (2, 2)"),
239239
sql, StatementType.INSERT, "system", "insert_select_testtable");
240240
checkSingleStatement(
241-
parse(sql = "insert into `test` (num) values (?) SETTINGS input_format_null_as_default = 1"),
241+
parse(sql = "insert into `test` (num)SETTINGS input_format_null_as_default = 1 values (?)"),
242242
sql, StatementType.INSERT, "system", "test");
243243
checkSingleStatement(
244-
parse(sql = "insert into `test` (id, name) values (1,2)(3,4),(5,6) SETTINGS input_format_null_as_default = 1"),
244+
parse(sql = "insert into `test` (id, name) SETTINGS input_format_null_as_default = 1 values (1,2)(3,4),(5,6)"),
245245
sql, StatementType.INSERT, "system", "test");
246246
s = checkSingleStatement(
247247
parse(sql = "insert into `test`"), sql, StatementType.INSERT, "system", "test");

0 commit comments

Comments
 (0)