Skip to content
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
7 changes: 7 additions & 0 deletions history/latest/3129.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Bug Fixes

- **[jdbc-v2]** With the `ANTLR4` and `ANTLR4_PARAMS_PARSER` SQL parser backends, the target of an `INSERT` is no
longer replaced by a table the statement reads (a CTE name, a `FROM` or `JOIN` table, or a table in a scalar
subquery in `VALUES`). The parsed target is used to pick the table for the RowBinary writer, so with
`beta.row_binary_for_simple_insert=true` rows could be written into the source table instead of the target.
The default `JAVACC` backend was not affected. (https://github.com/ClickHouse/clickhouse-java/issues/3129)
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ public void enterAssignmentValuesList(ClickHouseParser.AssignmentValuesListConte

@Override
public void enterTableExprIdentifier(ClickHouseParser.TableExprIdentifierContext ctx) {
if (ctx.tableIdentifier() != null) {
// In an INSERT these are the tables the statement reads (CTE, FROM, subquery). The target
// is set by enterInsertStmt, which is entered first, and must not be overwritten.
if (ctx.tableIdentifier() != null && !parsedStatement.isInsert()) {
extractAndSetDatabaseAndTable(ctx.tableIdentifier());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,33 @@ private void testCase(String sql, String expectedTableName) {
Assert.assertEquals(stmt.getTable(), expectedTableName, "Table name mismatch for: " + sql);
}

@DataProvider(name = "insertTargetWithSourceTablesDP")
public static Object[][] insertTargetWithSourceTablesDP() {
return new Object[][] {
{"INSERT INTO dst WITH r AS (SELECT 1 AS n) SELECT n FROM r", null, "dst"},
{"INSERT INTO dst SELECT * FROM src", null, "dst"},
{"INSERT INTO db1.dst SELECT * FROM db2.src", "db1", "db1.dst"},
{"INSERT INTO dst WITH r AS (SELECT 1 AS n) SELECT n FROM r JOIN other USING (n)", null, "dst"},
{"INSERT INTO dst VALUES (?, (SELECT max(x) FROM src))", null, "dst"},
{"INSERT INTO dst (a) VALUES (?)", null, "dst"},
{"INSERT INTO dst SELECT 1", null, "dst"},
// not an insert: the table read is the only table name
{"SELECT * FROM src", null, "src"},
{"WITH r AS (SELECT 1 AS n) SELECT n FROM r", null, "r"},
};
}

@Test(dataProvider = "insertTargetWithSourceTablesDP")
public void testInsertTargetIsNotReplacedBySourceTable(String sql, String expectedDatabase, String expectedTable) {
ParsedPreparedStatement stmt = parser.parsePreparedStatement(sql);
Assert.assertFalse(stmt.isHasErrors(), "Query should parse without errors: " + sql);
Assert.assertEquals(stmt.getTable(), expectedTable, "Table name mismatch for: " + sql);
if (!javaCcBackend) {
// JavaCC reports the database only as part of the table name
Assert.assertEquals(stmt.getDatabase(), expectedDatabase, "Database mismatch for: " + sql);
}
}

@Test(dataProvider = "antlr4HeredocStatementsDP")
public void testHeredocStatementsAntlr4Only(String sql, boolean insert, String expectedTable,
String expectedValuesList, int expectedArgCount) {
Expand Down
Loading