Skip to content

[Bug]: JSON boolean sort and comparison precedence is incorrect #28039

Done

Description

@Ariznawlll

Description

MatrixOne orders JSON boolean literals below JSON numeric and string scalars. This makes direct JSON comparisons and ascending ORDER BY on JSON scalar columns differ from MySQL.

Environment

  • Branch: main
  • Commit: 17c161a31b47a22e71ca222839143f66717ac365
  • Deployment: local single-CN test service
  • Test date: 2026-09-03
  • MySQL control: isolated MySQL 8.0.45

Steps to reproduce

SELECT
  CAST('true' AS JSON) > CAST('[]' AS JSON) AS bool_gt_array,
  CAST('true' AS JSON) > CAST('{}' AS JSON) AS bool_gt_object,
  CAST('true' AS JSON) > CAST('\"a\"' AS JSON) AS bool_gt_string,
  CAST('true' AS JSON) > CAST('1' AS JSON) AS bool_gt_number;

CREATE TABLE t (id INT PRIMARY KEY, j JSON);
INSERT INTO t VALUES
  (1, CAST('null' AS JSON)), (2, CAST('false' AS JSON)),
  (3, CAST('true' AS JSON)), (4, CAST('0' AS JSON)),
  (5, CAST('\"a\"' AS JSON));
SELECT JSON_TYPE(j) FROM t ORDER BY j, id;

Actual behavior

Three independent MatrixOne executions return:

bool_gt_array  bool_gt_object  bool_gt_string  bool_gt_number
0              0               0               0

NULL
BOOLEAN
BOOLEAN
INTEGER
STRING

Expected behavior

The isolated MySQL 8.0.45 control returns 1,1,1,1 for the pairwise predicates and orders the scalar JSON column as:

NULL
INTEGER
STRING
BOOLEAN
BOOLEAN

This matches MySQL JSON type precedence: booleans compare above arrays, objects, strings, and numbers; JSON null is lowest. Reference: https://dev.mysql.com/doc/refman/8.0/en/json.html#json-comparison

Stability and controls

  • MatrixOne reproducer: 3/3.
  • MySQL 8.0.45 control: all pairwise predicates return 1; scalar ordering is as shown above.
  • Controls in MatrixOne: ARRAY > OBJECT, OBJECT > STRING, STRING > INTEGER, and INTEGER > NULL each return 1, isolating the problem to the literal/boolean precedence category.
  • No DDL or DML persists after the probe.

Code analysis

pkg/container/bytejson/types.go assigns every LITERAL (JSON null, false, and true) one jsonTpOrder value of -11, below numeric and container types. CompareByteJson uses that map for cross-type comparisons; it is used by SQL JSON comparisons and the JSON sort comparator in pkg/sort/sort.go. This collapses JSON null and JSON booleans into one low-precedence category, whereas MySQL requires JSON null to be lowest and JSON booleans to have high precedence.

Regression coverage

After the fix, add deterministic JSON comparison/sort coverage for JSON null, booleans, numeric scalars, and strings. Include pairwise boolean comparisons with arrays/objects, but do not assert an order for nonscalar ORDER BY, because MySQL documents that nonscalar JSON sorting is not supported.

Activity

  1. added
    kind/bug
    on Sep 3, 2026
  2. added this to the MO-202609 milestone on Sep 3, 2026
  3. added theissue type on Sep 3, 2026
  4. Ariznawlll commented on Sep 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充范围:该排序问题不止 JSON boolean。

    在同一 main commit 的三次独立执行中,先把 SQL DATE、TIME、DATETIME、BLOB 放入 JSON_ARRAY(),再用 JSON_EXTRACT(..., $[0]) 取回 JSON 标量:JSON_TYPE() 分别为 DATE、TIME、DATETIME、BLOB,确认比较的是带类型的 JSON 值。

    SELECT
      date_json < time_json,
      time_json < datetime_json;
    -- MO: 0, 0
    
    SELECT GROUP_CONCAT(JSON_TYPE(j) ORDER BY j SEPARATOR ,) ...;
    -- MO: BLOB,DATETIME,TIME,DATE

    MySQL 的 JSON 预期升序为 DATE,TIME,DATETIME,BLOB(其优先级列表从高到低为 BLOB ... DATETIME, TIME, DATE ...)。因此当前实现不仅将 boolean 放错位置,也把 BLOB/DATETIME/TIME/DATE 的相对顺序完全反转。

    这与 pkg/container/bytejson/types.go:jsonTpOrder 的负序映射一致;该映射同时被比较和排序路径使用。

  5. changed the title [-][Compatibility]: JSON boolean sort and comparison precedence differs from MySQL[/-] [+][Bug]: JSON boolean sort and comparison precedence is incorrect[/+] on Sep 3, 2026
  6. added
    severity/s0
    and removed on Sep 3, 2026
  7. Ariznawlll commented on Oct 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    Fix verification on dev build 3a1e42b430 (v1.3.0, 2026-10-22): JSON boolean precedence now matches MySQL.

    SELECT CAST('true' AS JSON) > CAST('[]' AS JSON)   AS bool_gt_array, -- 1 (MySQL 8.0.45: 1)
           CAST('true' AS JSON) > CAST('"s"' AS JSON)  AS bool_gt_str,  -- 1 (MySQL: 1)
           CAST('true' AS JSON) > CAST('1' AS JSON)    AS bool_gt_num;  -- 1 (MySQL: 1)

    Full-spectrum ORDER BY on a mixed JSON column (null/number/string/object/array/false/true) now produces null < 1 < "s" < {"a":1} < [1] < false < true, identical to MySQL 8.0.45 verified side-by-side on the same fixture. The scalar type-major ordering is fully fixed on main.

    Remaining divergences observed during the same sweep are cosmetic and outside this issue's scope: (1) MySQL emits Warning 1235 ("sorting of non-scalar JSON values") while MatrixOne is silent, and (2) inter-object ordering differs — but MySQL's own manual states that ordering of non-equal objects is "unspecified but deterministic", so both are allowed.

  8. Ariznawlll commented on Nov 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。JSON boolean 与带类型日期/时间/BLOB 标量的比较、排序已与 MySQL 对照一致。

    关键证据

    版本与环境

    • 此项按 main 验收。本地隔离双 CN 实测 922815d74fe664dc9f4ea7bfc4c4e8ac3b279cd0;本次 Linux CI 从官方 main 检出的实际版本为 231b68c2d6421e9d212538866e0f825afd6c13e8,两处分别核对完整 git_version()。CI 比本地多了 Release merge spill buffers on panic #32160,核对差异仅为 spill sinker 及其两个测试文件。以下 CI 的正式 SQL、SDK 和源码测试确实在 CI 版本上重新执行;不把 MOTR commit 当作 MO 版本,也不将 main 结果外推为 4.2-dev 通过。
    • 原生源码测试使用该版本;[Bug]: PREPARE'd LOAD DATA re-bind drops its column list and SET/NULLIF — rows load into the wrong columns #31315 的测试 overlay 只增加测试与适配 BuildPlan 调用签名,没有修改产品实现。SQL 与协议测试连接实际启动的服务。

    步骤、断言与实际结果

    覆盖 断言
    原 boolean 反例 true 相对 array/object/string/number 的四个比较均为 1;false < true
    存储列与 ORDER BY JSON null、number、string、false、true 的标量顺序正确;SQL NULL 单独处理
    类型扩展 用 JSON_ARRAY/JSON_EXTRACT 得到 DATE/TIME/DATETIME/BLOB,先断言 JSON_TYPE,再验证 DATE < TIME < DATETIME < BLOB
    重复与控制 本地三轮,与独立 MySQL 8.0.45 对照;反向比较、相等值及准备语句控制

    未把 MySQL 不保证的非标量对象内部排序当作通过标准。源码 TestCompareByteJson* 三轮覆盖比较器路径;没有仅凭旧 boolean 修复遗漏后续 typed scalar 报告。

    回归与 CI

    • 回归追加现有 MOTR main PR #374,未向 MatrixOne 主库提测试 PR。
    • 本项固定回归文件,本地三轮通过,Linux 正式 MOTR 本项 3/3。
    • Linux CI 46298275529 SUCCESS;已回读 qa-acceptance-main 中版本、正式结果、原生测试和 SDK 日志。本轮整体正式 MOTR 63/63(21 个用例各三轮)、0 失败;63 次不全属于本 Issue。
    • 同轮原生日志有 398 个顶层 PASS,没有顶层 FAIL/SKIP 或 DATA RACE;其中 saved-result projection 为普通20次、race3次。真实 SDK Arrow 48项通过,真实二进制 prepared 探针三轮、共24次执行通过。每项证据归属以上具体步骤,不将组合 CI 的总数重复算作本 Issue 的独立测试数量。
    • 汇总 PR 尚待合入;合入默认分支后相关每日调度才会生效,不声称尚未合入的 schedule 已在每天执行。

    待解决点

    本 Issue 上述原始问题与相关边界没有未通过项。该结论不扩展为其他 Issue、大数据性能或未执行的生产拓扑验收。MOTR 回归已经提交,按用户允许先验收关闭、随后合入 PR 的流程处理。

    最终结论:测试通过。原始复现、上述扩展、对应 MOTR 回归及 Linux CI 均已完成;本 Issue 验收范围内未通过项:无。按授权关闭 #28039。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions