Skip to content

[Bug]: Binary-protocol COM_STMT_PREPARE rejects REPLACE (missing replace_stmt in prepareable_stmt grammar) #25429

Closed

Description

@ck89119

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:

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 (?, ?)"
_ = stmt

The 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

  1. added
    kind/bug
    on Jul 3, 2026
  2. added theissue type on Jul 3, 2026
  3. added this to the MO-202607 milestone on Jul 3, 2026
  4. added
    severity/s0
    and removed on Jul 4, 2026
  5. assigned and unassigned on Jul 7, 2026
  6. added
    ai-easy
    on Jul 7, 2026
  7. Repository owner unassigned on Jul 7, 2026
  8. Ariznawlll commented on Jul 7, 2026

    @Ariznawlll
    Collaborator

    Verified on latest main and added motr regression.

    Validation:

    Covered cases:

    • db.Prepare("replace into t values (?, ?)") succeeds.
    • Executing the prepared REPLACE twice replaces the primary-key row, final result: 1|20.
    • db.Prepare("replace into dst (...) select ... from src where tenant = ?") succeeds.
    • Executing prepared REPLACE SELECT for tenant=1 and tenant=2 handles unique-key replacement correctly, final result: 1|101|a1|10.25|new;2|201|b1|20.50|paid.

    Regression added in matrixorigin/motr#39:

    • suites/14_issue_regression/t/issue_25429_protocol_prepare_replace.test
    • suites/scenarios/14_issue_regression/issue_25429_protocol_prepare_replace.go
    • suites/scenarios/14_issue_regression/issue_25429_protocol_prepare_replace.sh
    • suites/scenarios/14_issue_regression/out/issue_25429_protocol_prepare_replace.out

    The motr case passed against latest main a0f1006.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions