Repository navigation
fix(batch): parse table and column names with quote awareness - #2031
Tyagiquamar wants to merge 4 commits into
Conversation
|
Hi team, following up to check if anyone has time to review this PR when convenient. Tests and checks are passing. Thanks! |
|
Hi team, following up to check if anyone has time to review this PR when convenient. Thanks! |
🤖 Claude reviewReplaces the regex-based Parser: traced the new scanner against the existing Key concern driving the verdict:
Blind spots:
Verdict: General findings
Inline comments are attached to the relevant lines. This summary updates in place on re-review. |
…integration tests
|
Update on the automated review findings (all addressed in b3d9f49, verified locally): gofmt clean on batch.go/conn_batch.go/tests/issues/issue_2012_test.go; INSERT/INTO matching now uses the boundary-aware isKeyword helper; parseInsertTableAndColumns has a doc comment; stale insertMatch/columnMatch regexes removed from conn_batch.go; regression test tests/issues/issue_2012_test.go added (native + HTTP paths, wider-table scenario and quoted table name with paren). Unit suite TestExtractNormalizedInsertQueryAndColumns passes; go vet clean on changed packages. Ready for human review. |
| require.NoError(t, err) | ||
| t.Cleanup(func() { db.ExecContext(ctx, "DROP TABLE IF EXISTS "+tableName) }) | ||
|
|
||
| stmt, err := db.PrepareContext(ctx, "INSERT INTO "+tableName+"(col1, col2)") |
There was a problem hiding this comment.
❌ Must fix — database/sql subtest never sends the batch, so the SELECT finds no row
A statement prepared via db.PrepareContext is a batch whose Send is only invoked by tx.Commit() (stdDriver.PrepareContext stores batch.Send in std.commit; stdBatch.Close is a no-op). Without a transaction the row appended at line 111 is never written, so the QueryRowContext(...).Scan at line 119 fails with sql: no rows in result set on both protocols — which matches the red integration jobs on this PR.
- Use the pattern from
issue_2067_test.go:BeginTx→tx.PrepareContext→Exec→tx.Commit(). - Consider also exercising one quoted-with-parentheses table name on the std path so this surface covers the second half of the fix.
- stmt, err := db.PrepareContext(ctx, "INSERT INTO "+tableName+"(col1, col2)")
+ tx, err := db.BeginTx(ctx, nil)
+ require.NoError(t, err)
+ stmt, err := tx.PrepareContext(ctx, "INSERT INTO "+tableName+"(col1, col2)")
require.NoError(t, err)
defer stmt.Close()
_, err = stmt.ExecContext(ctx, uint32(5), "std_sql")
require.NoError(t, err)
+ require.NoError(t, tx.Commit())
Summary
Fixes #2012
When parsing `INSERT INTO` queries in `extractInsertQueryComponents`, the parser previously used regular expressions that had edge cases with identifier formatting:
my_table(1)(col1, col2)`) were prematurely cut off by `([^(]+)`.This PR replaces regex-based table and column decomposition with `parseInsertTableAndColumns`, which:
Checklist
Note
Medium Risk
Touches core batch INSERT parsing used by PrepareBatch on all transports; behavior change is broad but heavily regression-tested.
Overview
Fixes PrepareBatch /
extractInsertQueryComponentswhenINSERT INTOuses awkward but valid ClickHouse syntax. Regex-based table/column extraction is replaced withparseInsertTableAndColumns, backed by quote-awarescanBalancedParensandfindUnquotedParen, so parsing no longer breaks on missing space before(col1, col2), parentheses inside quoted table names, orINSERT INTO function ...targets.extractInsertQueryComponentsnow builds the normalized statement from the full sanitized query and parses table/columns through the new scanner; duplicate INSERT regex inconn_batch.gois removed in favor of the shared helper.Unit cases and
TestIssue2012_PrepareBatch_WithoutSpaceBeforeColumnscover native/HTTP anddatabase/sqlfor the reported regression.Reviewed by Cursor Bugbot for commit 54d8ff6. Bugbot is set up for automated code reviews on this repo. Configure here.