Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Searched open issues for "REPLACE" "prepare", "COM_STMT_PREPARE", "binary protocol" "prepared". Related but not duplicate:
- [Bug]: Prepared REPLACE SELECT can leave duplicate composite unique-key rows after prepared ODKU #25420 reports wrong data for text-protocol
PREPARE ... FROM 'replace ...'(which parses fine via theFROM STRINGgrammar rule). This issue is about the binary protocol path failing to prepareREPLACEat all. - [Bug] REPLACE has multiple MySQL compatibility gaps #24918 tracks general
REPLACEcompatibility gaps but does not mention prepared statements. - [Bug]: Protocol-level prepared statements fail with mysql-connector-python prepared cursor #25423 reports mysql-connector-python
cursor(prepared=True)failing with "MySQL server has gone away" onselect ? + ?. That is a different symptom: binary-protocol prepared statements in general work with go-sql-driver (e.g. anINSERT ... SELECT ... ON DUPLICATE KEY UPDATEwith a?placeholder prepares and executes fine on the same build); onlyREPLACEis rejected by the parser.
Branch Name
main
Commit ID
2a8f3b884 (also reproduced on 51656deb6003)
Other Environment Information
- Hardware parameters: Apple Silicon (darwin/arm64)
- OS type: macOS
- Others: standalone launch (
etc/launch/launch.toml); client: go-sql-driver/mysql v1.10.0 (server-side prepared statements, i.e.interpolateParams=false)
Actual Behavior
Preparing a REPLACE statement through the MySQL binary protocol (COM_STMT_PREPARE) fails with a syntax error:
Error 1064 (HY000): SQL parser error: You have an error in your SQL syntax; check the manual that
corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 1
column 35 near " replace into dst (tenant,id,acct,amount,status)
select tenant,id,acct,amount,status from src where tenant = ?";
Any driver that uses server-side prepared statements hits this (go-sql-driver/mysql with default settings, JDBC with useServerPrepStmts=true, etc.). Connector/J silently falls back to client-side emulation, so the failure is easy to miss but the behavior still diverges from MySQL.
Note the text-protocol SQL form PREPARE s FROM 'replace into ...' works, because the quoted string goes through the FROM STRING grammar rule and the inner statement is parsed separately.
Expected Behavior
Same as MySQL: REPLACE is a preparable statement (it is in MySQL's documented list of statements permitted in prepared statements), so COM_STMT_PREPARE of a REPLACE ... VALUES / SELECT with ? placeholders should succeed and execute like the non-prepared statement.
Steps to Reproduce
create database repro_prepare_replace;
use repro_prepare_replace;
create table t (a int primary key, b int);Then from any client using server-side (binary protocol) prepared statements, e.g. Go:
db, _ := sql.Open("mysql", "root:111@tcp(127.0.0.1:6001)/repro_prepare_replace")
stmt, err := db.Prepare("replace into t values (?, ?)") // COM_STMT_PREPARE
// err: Error 1064 (HY000): SQL parser error: ... syntax error ... near " replace into t values (?, ?)"
_ = stmtThe same statement works when executed directly (db.Exec with no args) or via text-protocol PREPARE s FROM 'replace into t values (1, 2)'.
Additional information
Root cause: the frontend rewrites COM_STMT_PREPARE into prepare __mo_stmt_id_x from <raw sql> (pkg/frontend/mysql_cmd_executor.go, case COM_STMT_PREPARE), and the unquoted form is parsed via the prepareable_stmt grammar rule in pkg/sql/parsers/dialect/mysql/mysql_sql.y:
prepareable_stmt:
create_stmt
| alter_stmt
| insert_stmt
| delete_stmt
| drop_stmt
| show_stmt
| update_stmt
| select_stmt
replace_stmt is missing from this list (it is present in explainable_stmt), so the wrapped statement fails to parse. Adding replace_stmt to prepareable_stmt (and regenerating the parser) should close the gap; execute-side handling likely needs a matching check since tree.Replace must be accepted wherever prepared statement plans are built.
Activity
fix: allow REPLACE in binary-protocol COM_STMT_PREPARE (#25429) (#25453)
Ariznawlll commentedon Jul 7, 2026
Verified on latest main and added motr regression.
Validation:
Covered cases:
Regression added in matrixorigin/motr#39:
The motr case passed against latest main a0f1006.
fix: allow REPLACE in binary-protocol COM_STMT_PREPARE (matrixorigin#…
fix: allow REPLACE in binary-protocol COM_STMT_PREPARE (matrixorigin#…
fix: allow REPLACE in binary-protocol COM_STMT_PREPARE (matrixorigin#…