Skip to content

Fix issue of parsing insert with settings #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clickhouse-jdbc/src/main/javacc/ClickHouseSqlParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ void insertStmt(): {} {
LOOKAHEAD({ getToken(1).kind == FUNCTION }) <FUNCTION> functionExpr()
| (LOOKAHEAD(2) <TABLE>)? tableIdentifier(true)
)
( settingsPart() )?
dataClause()
}

Expand All @@ -528,7 +529,6 @@ void dataClause(): {} {
columnExprList()
<RPAREN> { token_source.removePosition(ClickHouseSqlStatement.KEYWORD_VALUES_END); }
)*
( settingsPart() )?
| (LOOKAHEAD(2) ((withClause())? <SELECT> { token_source.addPosition(token); } columnExprList() <FROM>
<INPUT> <LPAREN> <STRING_LITERAL> { token_source.input = ClickHouseSqlUtils.unescape(token.image); } <RPAREN>)?
<FORMAT> <IDENTIFIER> { token_source.format = token.image; } )? (anyExprList())?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1580,6 +1579,33 @@ 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()) {
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
.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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down