Skip to content

[Bug]: JSON_KEYS wildcard paths return NULL instead of an error #28041

Closed

Description

@Ariznawlll

Description

JSON_KEYS(json_doc, path) silently returns SQL NULL when path contains * or **. MySQL defines these wildcard paths as invalid for JSON_KEYS() and raises an error instead; returning NULL makes invalid queries indistinguishable from a valid path that selects a non-object or no value.

Environment

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

Steps to reproduce

SELECT JSON_KEYS('{"a":{"x":1},"b":{"y":2}}', '$.*');
SELECT JSON_KEYS('{"a":{"x":1},"b":{"y":2}}', '$**.x');

Actual behavior

Both statements return SQL NULL without an error.

Expected behavior

MySQL requires JSON_KEYS() to raise an error when its path contains a * or ** wildcard. A non-wildcard control behaves correctly in MatrixOne:

SELECT JSON_KEYS('{"a":{"x":1},"b":{"y":2}}', '$.a');
-- ["x"]

Reference: https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-keys

Stability and controls

  • Reproducer: 3/3 for both $.* and *.x.
  • Control: root JSON_KEYS() returns ["a", "b"]; the ordinary object path $.a returns ["x"] in each run.
  • No DDL or DML is involved.

Code analysis

pkg/sql/plan/function/func_builtin_json.go:jsonKeysWithPath parses the supplied path and executes bj.Query(...), but does not reject a path containing wildcard legs before querying. A wildcard query returns a non-object aggregate value, and the following val.Type != bytejson.TpCodeObject branch appends SQL NULL. The function needs a dedicated wildcard-path validation before the query.

Regression coverage

After the fix, add deterministic coverage for JSON_KEYS() with root and ordinary object paths, a path selecting a non-object/no value (SQL NULL), and both */** paths (error).

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. changed the title [-][Compatibility]: JSON_KEYS wildcard paths return NULL instead of an error[/-] [+][Bug]: JSON_KEYS wildcard paths return NULL instead of an error[/+] on Sep 3, 2026
  5. Ariznawlll commented on Sep 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充 MySQL 8.0.45 实测:

    SELECT JSON_KEYS('{"a":{"x":1},"b":{"y":2}}', '$.*');

    MySQL 返回 ERROR 3149 (42000): In this situation, path expressions may not contain the * and ** tokens or an array range.。MatrixOne 当前 main 对 $.* 与 $**.x 均连续 3 次静默返回 SQL NULL。

  6. added
    severity/s1
    and removed on Sep 4, 2026
  7. VioletQwQ-0 commented on Sep 4, 2026

    @VioletQwQ-0
    Collaborator

    已完成 #28041 的源码级复核,当前结论如下:

    • 在 main@494e6a7723242947c73442528e5fbabe62144ec3 上,jsonKeysWithPath 仍未拒绝 wildcard/range path。
    • 根因是 path 成功解析后直接进入 ByteJson.Query;non-simple path 的查询结果会被包装为 JSON array,随后落入“非 object → SQL NULL”分支,因而把非法路径吞成了 NULL。
    • 修复限定在 JSON_KEYS(json_doc, path) 的函数边界:对 !path.IsSimple() 返回 MySQL ER_INVALID_JSON_PATH_WILDCARD(3149 / 42000)。共享 path parser 与 Query 行为不变,允许 wildcard 的其他 JSON 函数不受影响。
    • 回归将覆盖 $.*、$[*]、$**.x、array range,以及 $."*" literal key、普通对象路径、未命中、非对象和 NULL 控制,并扩展 canonical func_json_keys BVT。
    • 错误码实现依赖 fix: implement STATEMENT_DIGEST_TEXT #27990 先合入;修复分支将从其合入后的最新 main 创建,不堆叠到现有 JSON PR head。
    • 这是用户可见的 SQL 错误兼容性修复,QA required。PR 将先保持 Draft;合入后 Issue 在明确的 QA PASS 前保持 open。

    Triage decision: remove needs-triage and add severity/s1. Assignee and MO-202609 milestone remain unchanged.

    当前证据为源码级分析;PR-head UT、BVT、CI 和 QA 结果将在对应阶段补充。

  8. VioletQwQ-0 commented on Oct 15, 2026

    @VioletQwQ-0
    Collaborator

    转测交接:JSON_KEYS 通配/范围路径拒绝(PR #28328 已合并)

    @Ariznawlll 你好,#28041 的修复已通过 PR #28328 合入 main,现按流程转测试验收。

    合入信息

    修复范围(对应原 issue 症状)

    • JSON_KEYS(json_doc, path) 对 typed 通配 *、递归 ** 与数组范围路径按 MySQL 行为拒绝:ERROR 3149 / SQLSTATE 42000,消息 In this situation, path expressions may not contain the * and ** tokens or an array range.
    • 文档校验先于 NULL path;SQL NULL 文档直接返回 NULL;带引号的 * 字面量 key 仍合法
    • 简单路径查找禁用非标量 auto-wrap:对象标量 $[0]、$[last] 返回 NULL,真实数组下标不受影响
    • 共享路径解析接受 tab/newline/CR 范围分隔符,拒绝同源逆序范围;JSON_EXTRACT/JSON_LENGTH 保留合法范围行为,JSON_SET/JSON_REMOVE/JSON_CONTAINS 保留范围拒绝

    验证证据

    • exact-head CI:合并前 required 检查全绿(SUCCESS=6 含 Matrixone CI/UT/BVT 套件;唯一 FAILURE 为 MO Checkin Regression On TKE / BUILD MO DOCKER IMAGE 镜像构建基建步骤,与产品行为无关;Mergify 自动队列规则 CANCELLED 为出队重排所致)
    • BVT 覆盖:test/distributed/cases/function/func_json_keys.test / .result(生产 SQL 入口断言 3149 错误及上述终态)
    • 单元级:pkg/container/bytejson/path_test.go、pkg/sql/plan/function/func_json_valid_test.go 覆盖新增路径分类与错误码
    • 本地自测:local_self_test_blocked —— 本地磁盘余量约 1.8GiB,且受控远程 mo50 scratch 在活动期间被删除、边界待人工确认,本轮无法在本机/受控远端重建带修复实例;SQL 语义终态已由合并 head 的 required CI + BVT 覆盖

    请验收的场景(PASS/FAIL 预期)

    1. 复现原症状现已 FAIL(即修复生效):SELECT JSON_KEYS('{"a":{"x":1},"b":{"y":2}}', '$.*') 应返回 ERROR 3149(你 2026-09-03 在 MySQL 8.0.45 实测的同款报错)
    2. 递归/范围变体(**、[1 to 3] 及其 tab/换行分隔写法)同样 ERROR 3149
    3. 回归项 PASS:普通对象/数组路径 JSON_KEYS 正常;JSON_KEYS(doc, NULL) 返回 NULL;SELECT JSON_KEYS(NULL, '$.*') 返回 NULL;带引号 "*" 的 key 正常返回;JSON_EXTRACT/JSON_LENGTH 合法数组范围行为不变;JSON_SET/JSON_REMOVE/JSON_CONTAINS 范围拒绝不变
    4. 环境建议:包含 f2093ae4b 的 main 构建或 nightly

    转测依据:issue 标签 kind/bug + severity/s1,无 compatibility 类标签,按维护约定分配 default tester @Ariznawlll。

    验证通过后请在本 issue 回复 PASS(注明版本/环境与终态结果),我将据此清理标签并关闭;如 FAIL 请附复现语句与版本,我会加 test-verification-failed 并继续修复。

  9. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:461bc8e34920959e5fc141e3d1b37673a90894e1(隔离 worktree 源码构建;embedded 实例不提供有效 git_version(),故以构建源码 SHA 标识)
    • 修复:matrixone #28328,merge commit f2093ae4b3cf019802091f6309e31c9524829b70 已核对为该 main 的祖先
    • 部署方式:本地隔离 embedded 集群,1 CN / 1 TN / 1 LogService,MySQL 协议;只读 SQL,无共享测试库或外部集群
    • 测试日期:2026-10-15

    原问题验证

    连续三轮执行原始 JSON_KEYS('{"a":{"x":1},"b":{"y":2}}', '$.*'),均返回 3149 / SQLSTATE 42000 和 In this situation, path expressions may not contain the * and ** tokens or an array range.;旧版静默返回 SQL NULL 的现象未再出现。

    Happy Path 覆盖

    1. 无 path 的根对象结果为 ["a", "b"],普通对象路径 $.a 为 ["x"];3/3。另用 CAST(... AS CHAR) 等值断言精确 JSON 值,避免只检查查询完成。
    2. $."*" 中引号包围的字面量星号 key 合法,返回 ["x"];3/3,等值断言为真。
    3. 不受本修复限制的 JSON_EXTRACT('[10,20,30]', '$[0 to 1]') 返回 [10, 20];3/3,等值断言为真。每轮后 SELECT 1 返回 1,连接仍可用。
    4. 合法但未命中路径、路径选中非对象、NULL 文档、NULL path 各返回 SQL NULL;各 3/3。它们是合法输入对照,不归为异常路径。

    Unhappy Path 覆盖

    1. 原始 $.*、递归 $**.x、数组通配 $[*]、数组范围 $[0 to 1]:四类无效 path 各 3/3 返回 3149 / 42000;没有被吞成 NULL。错误后正常查询继续成功。
    2. 本 issue 只执行只读 SQL;无数据变更,因此写入原子性不适用。

    白盒验证

    本次独立验收为公开 SQL 黑盒测试;未运行 Go UT 或 -race。这些内部测试不替代上述 SQL 结果。

    回归测试

    • 已提交 MOTR Draft PR #244,配对文件 suites/14_issue_regression/t/issue_28041_json_keys_wildcard.test / r/issue_28041_json_keys_wildcard.result,含精确值与错误码断言。
    • 相同 main 的隔离集群中,MOTR 定向运行 3/3 成功、51/51 语句符合 golden。PR 尚未合入,完整 suite 和集成 CI 尚未执行,将继续跟踪,不能把 Draft PR 视为已合入回归。
    • 问题是快速确定性 SQL 语义,无规模阈值、长时并发或外部故障条件,故不另加 big-data / stability / chaos。

    测试证据

    • MOTR #244 的用例和精确预期。
    • 独立本地探针每轮摘要:wildcard_3149_42000=4 controls=9 extract_range=[10, 20] healthy=1,共三轮。
    • MOTR 定向摘要:total=3 success=3 failed=0 skipped=0 | stmts=51 ok=51 fail=0。原始旧版现象和 MySQL 对照见 issue 正文及早期评论。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugphase/testingseverity/s1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions