Skip to content

fix(#828): backtick-quote PK column names in chunker and checksum SQL#829

Merged
morgo merged 6 commits into
block:mainfrom
morgo:fix-issue-828-reserved-word-pk-columns
May 8, 2026
Merged

fix(#828): backtick-quote PK column names in chunker and checksum SQL#829
morgo merged 6 commits into
block:mainfrom
morgo:fix-issue-828-reserved-word-pk-columns

Conversation

@morgo
Copy link
Copy Markdown
Collaborator

@morgo morgo commented May 8, 2026

Fixes #828.

Summary

  • Migrating or moving a table whose primary key contains MySQL reserved words (e.g. key, value) failed immediately with Error 1064 because several SQL builders interpolated bare PK column names.
  • Affected sites:
    • pkg/table/chunker_composite.go::Next() — composite prefetch query (the user-reported failure path).
    • pkg/table/chunker_optimistic.go::nextChunkByPrefetching — single-key prefetch query.
    • pkg/table/tableinfo.go::setMinMax — min/max query that runs during SetInfo.
    • pkg/checksum/single.go::inspectDifferences — PK list passed into queryTemplate.
  • All four sites now backtick-quote columns. The composite chunker uses the existing table.QuoteColumns helper.

Note on origin

This is not a regression from #701/#702. The unquoted strings.Join in the composite chunker has been there since 36877bb (2023-08-02), when composite-key chunking was first introduced. PR #702 doesn't touch the prefetch query. The bug surfaced now only because no existing test covered a reserved-word PK column.

Test plan

  • pkg/table regression test: composite chunker walks a table with PK (osm_id, \key`)` to completion.
  • pkg/table regression test: optimistic chunker prefetch path with a single-column auto-inc PK named \key``.
  • pkg/migration regression test: end-to-end ALTER through the copy path (the user's reported scenario).
  • pkg/move regression test: end-to-end move with a reserved-word composite PK.
  • Existing pkg/table, pkg/checksum, pkg/copier test suites still pass locally.

🤖 Generated with Claude Code

morgo and others added 2 commits May 7, 2026 17:29
…m SQL

The chunker_composite prefetch query, the chunker_optimistic prefetch
query, the single-checker inspectDifferences query, and the table-info
min/max query all built SQL with bare PK column names. When a primary
key column is a MySQL reserved word (e.g. `key`, `value`), the migration
fails immediately with Error 1064.

Quote PK columns at every SQL composition site, and add regression tests
exercising both chunkers, the migrate path, and the move path with a
reserved-word composite PK.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@morgo morgo marked this pull request as ready for review May 8, 2026 00:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes migrations/moves/checksums failing with MySQL Error 1064 when primary key columns are reserved words (e.g. key, value) by consistently backtick-quoting PK column identifiers in generated SQL.

Changes:

  • Backtick-quote PK column identifiers in chunker prefetch queries (composite + optimistic) and TableInfo min/max query.
  • Backtick-quote PK column list passed into checksum row inspection query template.
  • Add regression tests covering reserved-word PKs across chunkers, migration E2E, and move E2E.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/table/tableinfo.go Quotes PK column in min/max SQL used during table info setup.
pkg/table/chunker_optimistic.go Quotes single-column auto-inc PK in optimistic chunker prefetch SQL.
pkg/table/chunker_composite.go Uses QuoteColumns for SELECT/ORDER BY lists in composite chunker prefetch SQL.
pkg/checksum/single.go Uses table.QuoteColumns for PK list in queryTemplate used by row inspection.
pkg/table/chunker_optimistic_test.go Adds regression test for optimistic prefetch path with reserved-word PK column.
pkg/table/chunker_composite_test.go Adds regression test walking composite chunker on reserved-word composite PK.
pkg/migration/e2e_test.go Adds E2E migration regression test for reserved-word composite PK.
pkg/move/move_test.go Adds E2E move regression test for reserved-word composite PK.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/table/tableinfo.go Outdated
Comment thread pkg/table/chunker_optimistic.go Outdated
Address Copilot feedback on block#829 — replace hand-rolled backtick
concatenation with the existing QuoteColumns helper for consistency
with the rest of the codebase.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Kiran01bm
Copy link
Copy Markdown
Collaborator

these needs fixing as well ?

and ?

Address Kiran's review comment on block#829 — t.keyName was passed into
FORCE INDEX(...) unquoted, so a secondary index whose name is a MySQL
reserved word (e.g. `key`) would also break the prefetch query.
Extend the regression test to cover this case via SetKey.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@morgo
Copy link
Copy Markdown
Collaborator Author

morgo commented May 8, 2026

Good catch — yes, the index name passed into FORCE INDEX(...) had the same issue. A secondary index whose name is a reserved word (e.g. key) would break the prefetch query. Fixed in 44eb1cd by quoting t.keyName via QuoteColumns and extending the regression test to cover the path via SetKey("key", ""). Verified locally that FORCE INDEX (\key`)` is accepted by MySQL.

… move

QuotedTableName is already backtick-wrapped at construction, so these
pass without a code change — they guard against any future regression
where a SQL builder uses the unquoted TableName.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@morgo morgo enabled auto-merge May 8, 2026 10:50
@morgo morgo merged commit 06471d8 into block:main May 8, 2026
11 of 12 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.

SQL syntax error when primary key contains reserved-word columns (e.g. key, value)

4 participants