Skip to content

[Bug]: Protocol prepared JSON_EXTRACT comparison with parameter fails at prepare time #25502

Closed

Description

@Ariznawlll

Summary

On latest main (a03bfc1b6), a protocol-level prepared statement that compares JSON_EXTRACT(...) with a parameter fails at prepare time. MySQL 8.0.45 accepts the same prepared statement and returns the expected result.

The same expression with a SQL literal works in MatrixOne, so this appears specific to driver / binary-protocol prepared parameters.

Duplicate check

Searched before filing:

  • protocol prepared JSON compare parameter
  • invalid argument operator >= JSON TEXT
  • prepared JSON_EXTRACT parameter

Related but not duplicate:

Reproduce

Go driver repro using github.com/go-sql-driver/mysql:

stmt, err := db.Prepare(`select json_extract('{"a":1}', '$.a') >= ? as cmp`)
if err != nil {
    return err
}
var cmp string
err = stmt.QueryRow(1).Scan(&cmp)

Equivalent direct SQL literal works in MatrixOne:

select json_extract('{"a":1}', '$.a') >= 1 as cmp_json;
-- returns 1

MySQL 8.0.45 result

RESULT: 1

MatrixOne result

PREPARE_ERROR: Error 20203 (HY000): invalid argument operator >=, bad value [JSON TEXT]

Expected behavior

MatrixOne should accept the protocol-level prepared statement and evaluate the JSON comparison consistently with MySQL and with MatrixOne's own literal-SQL path.

Additional information

Found while exploring protocol-level prepared statements containing window queries. The window protocol-prepared path passed after rewriting the JSON predicate to JSON_UNQUOTE(JSON_EXTRACT(...)) >= ?, but the direct JSON_EXTRACT(...) >= ? prepared form remains incompatible with MySQL.

Activity

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

    @LeftHandCold
    Collaborator

    fixed

  6. Ariznawlll commented on Jul 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 验证(协议级场景,未加入本 SQL PR)

    • MatrixOne 基线:origin/main@f1c0a92d882dbdad469ffd2c96f607796207c797。
    • 该问题必须通过真实 MySQL COM_STMT_PREPARE / COM_STMT_EXECUTE 二进制协议验证 JSON 与 signed、unsigned、decimal、NULL、非法 JSON及重复参数绑定。
    • 定向 Go MySQL driver 场景在本地最新 main 服务上执行通过,没有出现 panic 或错误复用参数。

    PR 范围修正

    matrixorigin/motr#48 按要求只保留 SQL .test/.result。本场景依赖 Go driver、Shell launcher、go.mod/go.sum 和 scenario output,不是纯 SQL,因此相关文件已从 PR 删除。

    结论:协议级定向验证结果保留为 issue 记录,但本 issue 当前未纳入该 MOTR SQL PR。

  7. Ariznawlll commented on Jul 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 单元测试验证(协议层 → Binder → 执行层 → 比较函数)

    本次按单元测试方式验证 issue #25502,测试的是 prepared statement 参数参与 JSON 排序比较(<、>、<=、>=)的完整关键路径。

    测试基线与环境

    执行命令

    go test -race ./pkg/frontend \
      -run '^TestParseExecuteDataPreservesExactJsonOrderingParams$' \
      -count=10 -v
    
    go test -race ./pkg/sql/plan \
      -run '^TestBindFuncExprImplByPlanExpr_JsonOrderingWithDynamicParam$' \
      -count=10 -v
    
    go test -race ./pkg/sql/colexec \
      -run '^TestJsonOrderingWithTextPrepareParamExact$' \
      -count=10 -v
    
    go test -race ./pkg/sql/plan/function \
      -run '^TestJsonOrderingOperatorsUseExactComparison$' \
      -count=10 -v

    测试过程与覆盖用例

    1. MySQL 二进制协议参数解析:验证 COM_STMT_EXECUTE 参数进入 prepared statement 后,仍以精确文本保存,而不是先转成 float64 导致精度丢失。

      • 9007199254740993(超过 IEEE-754 可精确表示边界的相邻整数)
      • 18446744073709551615(uint64 最大值)
      • 0.123456789123456789(高精度小数)
      • 非法 JSON 字符串保留给后续 cast 校验
      • NULL
    2. Plan Binder:验证 JSON 与动态参数出现在比较符两侧时,均插入 json_ordering_param 归一化函数;同时验证普通字符串字面量仍被拒绝,非排序比较及非二元表达式不被误处理。

    3. 表达式执行层:验证 prepared text 参数与 JSON 的真实比较结果。

      • 大整数相邻值,JSON 分别位于左、右侧
      • uint64 最大值
      • 高精度小数
      • NULL 传播
      • 非法参数返回错误
    4. JSON 比较函数:直接验证 <、>、<=、>= 使用精确比较,并验证 NULL 传播。

    实际结果

    ok  github.com/matrixorigin/matrixone/pkg/frontend          2.828s
    ok  github.com/matrixorigin/matrixone/pkg/sql/plan          1.944s
    ok  github.com/matrixorigin/matrixone/pkg/sql/colexec       2.152s
    ok  github.com/matrixorigin/matrixone/pkg/sql/plan/function 1.911s
    combined_exit_status=0
    race_report=none
    
    • 4 个顶层测试 × 10 次:40/40 PASS
    • 子测试实例:210/210 PASS
      • frontend:50/50
      • plan binder:50/50
      • colexec:60/60
      • comparison function:50/50
    • -race:未发现数据竞争

    结论

    issue #25502 描述的 prepared JSON 排序比较问题在最新 main 上已通过上述四层定向单元测试,未复现 prepare 阶段的类型错误,也未发现数值精度、NULL、非法参数处理或并发安全方面的回归。该场景依赖 MySQL protocol prepared statement 参数类型,适合保留在 Go 单元测试中,不加入仅执行 SQL 文本的 MOTR 用例。

  8. Ariznawlll commented on Jul 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    已确认该问题的修复和定向 UT 均已合入 MatrixOne 最新 main。

    最新代码中包含以下四层回归 UT:

    1. pkg/frontend/mysql_protocol_test.go
      • TestParseExecuteDataPreservesExactJsonOrderingParams
    2. pkg/sql/plan/base_binder_test.go
      • TestBindFuncExprImplByPlanExpr_JsonOrderingWithDynamicParam
    3. pkg/sql/colexec/evalExpression_test.go
      • TestJsonOrderingWithTextPrepareParamExact
    4. pkg/sql/plan/function/func_compare_test.go
      • TestJsonOrderingOperatorsUseExactComparison

    覆盖 MySQL prepared 参数解析、Binder 动态参数处理、JSON 排序比较执行、精确数值比较、NULL 传播和非法参数错误路径。

    结论:issue #25502 已有正式 Go UT 回归覆盖,不需要依赖 MOTR 的纯 SQL case。

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