Skip to content

[jdbc-v2] Keep the INSERT target when the ANTLR4 parser sees source tables - #3352

Merged
chernser merged 1 commit into
ClickHouse:mainfrom
piyush15102003:fix/antlr-insert-target-table
Nov 17, 2026
Merged

chernser merged 1 commit into
ClickHouse:mainfrom
piyush15102003:fix/antlr-insert-target-table

Conversation

@piyush15102003

@piyush15102003 piyush15102003 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

With the ANTLR4 and ANTLR4_PARAMS_PARSER backends, ParsedPreparedStatement.getTable() of an INSERT returned the last table read anywhere in the statement (a CTE name, a FROM/JOIN table, or a table in a scalar subquery inside VALUES) instead of the insert target. With beta.row_binary_for_simple_insert=true, the RowBinary writer was then built against the source table and the row was written there.

enterInsertStmt sets the target, but ANTLR enters insertStmt before its nested tableExprIdentifier nodes, and enterTableExprIdentifier overwrote table/database for each of them.

Change:

  • enterTableExprIdentifier no longer sets the table once the statement is known to be an insert (setInsert(true) runs in enterInsertStmt, before the children are entered). Statements that are not inserts keep using it as the only source of the name.

Unchanged: INSERT INTO [TABLE] FUNCTION f(...) still sets useFunction, and the default JAVACC backend is not touched.

Closes #3129

Checklist

  • Closes jdbc-v2: ANTLR4 parser backends report a source table as the INSERT target, writing rows into the wrong table #3129
  • Unit tests covering the common scenarios were added (BaseSqlParserFacadeTest#testInsertTargetIsNotReplacedBySourceTable, run for all three backends: CTE, FROM, qualified db1.dst from db2.src, JOIN, scalar subquery in VALUES, plus inserts and selects without the problem). The five affected cases fail on main for both ANTLR4 backends and pass with the fix. The database assertion is skipped for JAVACC, which reports the database only as part of the table name.
  • A human-readable description of the changes was provided to include in CHANGELOG (history/latest/3129.md)

Note

Medium Risk
Incorrect insert targets could route RowBinary writes to source tables when beta.row_binary_for_simple_insert=true; the fix is narrow but touches prepared-statement parsing used for all INSERTs on ANTLR4 backends.

Overview
Fixes a jdbc-v2 parsing bug on the ANTLR4 and ANTLR4_PARAMS_PARSER backends where ParsedPreparedStatement.getTable() for INSERT statements was overwritten by the last table referenced in the query (CTEs, FROM/JOIN, or subqueries in VALUES). enterTableExprIdentifier now skips updating database/table when the statement is already marked as an insert, so the target set in enterInsertStmt is kept. Non-insert statements behave as before.

Regression coverage was added via testInsertTargetIsNotReplacedBySourceTable, and CHANGELOG entry history/latest/3129.md documents the fix (closes #3129).

Reviewed by Cursor Bugbot for commit 07bc0de. Bugbot is set up for automated code reviews on this repo. Configure here.

@chernser
chernser merged commit 3fba63a into ClickHouse:main Nov 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jdbc-v2: ANTLR4 parser backends report a source table as the INSERT target, writing rows into the wrong table

2 participants