Skip to content

Commit

Permalink
Merge pull request #817 from zhicwu/fix-parser-issue
Browse files Browse the repository at this point in the history
Consider CTE when parsing insert statement
  • Loading branch information
zhicwu authored Jan 19, 2022
2 parents d42ede2 + 47bf6b0 commit fd3d923
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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 @@ -529,7 +529,7 @@ void dataClause(): {} {
<RPAREN> { token_source.removePosition(ClickHouseSqlStatement.KEYWORD_VALUES_END); }
)*
( settingsPart() )?
| (LOOKAHEAD(2) (<SELECT> { token_source.addPosition(token); } columnExprList() <FROM>
| (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())?
} catch (ParseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,30 @@ public void testQueryWithNamedParameter() throws SQLException {
Assert.assertFalse(rs.next());
}
}

@Test(groups = "integration")
public void testInsertWithAndSelect() throws Exception {
try (ClickHouseConnection conn = newConnection(new Properties());
Statement s = conn.createStatement()) {
s.execute("drop table if exists test_insert_with_and_select; "
+ "CREATE TABLE test_insert_with_and_select(value String) ENGINE=Memory");
try (PreparedStatement ps = conn.prepareStatement(
"INSERT INTO test_insert_with_and_select(value) WITH t as ( SELECT 'testValue1') SELECT * FROM t")) {
ps.executeUpdate();
}

try (PreparedStatement ps = conn.prepareStatement(
"INSERT INTO test_insert_with_and_select(value) WITH t as ( SELECT 'testValue2' as value) SELECT * FROM t WHERE value != ?")) {
ps.setString(1, "");
ps.executeUpdate();
}

ResultSet rs = s.executeQuery("select * from test_insert_with_and_select order by value");
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString("Value"), "testValue1");
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getString("VALUE"), "testValue2");
Assert.assertFalse(rs.next());
}
}
}

0 comments on commit fd3d923

Please sign in to comment.