Skip to content

add missing functions - #24509

Merged
mergify[bot] merged 49 commits into
matrixorigin:3.0-devfrom
daviszhen:0520-add-funcs
May 26, 2026
Merged

mergify[bot] merged 49 commits into
matrixorigin:3.0-devfrom
daviszhen:0520-add-funcs

Conversation

@daviszhen

@daviszhen daviszhen commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #24481 #24486

What this PR does / why we need it:

补充缺失的json函数.

JSON_ARRAY               = 359
JSON_OBJECT              = 360
JSON_TYPE                = 361
JSON_VALID               = 362
JSON_LENGTH              = 363
JSON_KEYS                = 364
JSON_PRETTY              = 365
JSON_SCHEMA_VALID        = 366
JSON_SCHEMA_VALID_REPORT = 367
JSON_VALUE              = 368

json_value 还缺少returning等语法.后续pr加

@daviszhen
daviszhen requested a review from a user May 20, 2026 10:53
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found two blocking issues in the new JSON functions:

  1. json_schema_valid / json_schema_validation_report only register (VARCHAR, VARCHAR) and (JSON, JSON) overloads in pkg/sql/plan/function/list_builtIn.go, but the implementations already handle schemaIsStr and docIsStr independently in pkg/sql/plan/function/func_builtin_json.go. Mixed calls like json_schema_valid('{\"type\":\"number\"}', json_col) and json_schema_validation_report(json_schema_col, '{\"a\":1}') currently fail during function resolution. I verified GetFunctionByName(..., [VARCHAR, JSON]) and [JSON, VARCHAR] both return invalid argument function ... bad value [...]. Please add the mixed overloads and cover them with tests.\n\n2. JsonValue currently returns JSON text for non-scalar matches. In pkg/sql/plan/function/func_builtin_json.go, after val := bj.Query(...), the code only checks val.IsNull() and otherwise always calls val.Unquote(). When the path resolves to an object/array, Unquote() falls back to bj.String(), so JSON_VALUE('{\"a\":[1]}', '$.a') returns [1] instead of SQL NULL. JSON_VALUE should stay scalar-only here, so object/array matches need to return NULL (and ideally get a regression test too).

@daviszhen

Copy link
Copy Markdown
Contributor Author

Found two blocking issues in the new JSON functions:

  1. json_schema_valid / json_schema_validation_report only register (VARCHAR, VARCHAR) and (JSON, JSON) overloads in pkg/sql/plan/function/list_builtIn.go, but the implementations already handle schemaIsStr and docIsStr independently in pkg/sql/plan/function/func_builtin_json.go. Mixed calls like json_schema_valid('{\"type\":\"number\"}', json_col) and json_schema_validation_report(json_schema_col, '{\"a\":1}') currently fail during function resolution. I verified GetFunctionByName(..., [VARCHAR, JSON]) and [JSON, VARCHAR] both return invalid argument function ... bad value [...]. Please add the mixed overloads and cover them with tests.\n\n2. JsonValue currently returns JSON text for non-scalar matches. In pkg/sql/plan/function/func_builtin_json.go, after val := bj.Query(...), the code only checks val.IsNull() and otherwise always calls val.Unquote(). When the path resolves to an object/array, Unquote() falls back to bj.String(), so JSON_VALUE('{\"a\":[1]}', '$.a') returns [1] instead of SQL NULL. JSON_VALUE should stay scalar-only here, so object/array matches need to return NULL (and ideally get a regression test too).
0bb8bb555a024122b476d3b01bd7f3d0

@daviszhen

Copy link
Copy Markdown
Contributor Author

已继续把之前列出的每个函数问题修掉,并补了测试/结果文件。 16:13:22 [12/971]

改动内容:

  • pkg/sql/plan/function/func_builtin_json.go
    • 修复 schema $ref 检测:
      • 不再用 ByteJson.HasRef() 递归拒绝所有名为 "$ref" 的 key。
      • 改为按 JSON Schema 关键字上下文检测真正的 $ref keyword。
      • 允许 property 名、enum/const 数据值里出现 "$ref"。
      • 仍拒绝实际 schema $ref keyword。
    • 修复 json_array / json_object 的 temporal 格式化:
      • TIMESTAMP 使用 session timezone + scale:Timestamp.String2(jsonSessionTimeZone(proc), scale)。
      • TIME / DATETIME 使用 String2(scale),保留 scale。
    • json_object 复用同一转换逻辑,所以 value 侧也一起修复。
  • pkg/sql/plan/function/list_builtIn.go
    • 给 json_array / json_object 增加注册期类型检查。
    • 不再对所有输入类型无条件通过,unsupported 类型会在 function resolution 阶段失败。
  • pkg/sql/plan/function/func_json_valid_test.go
    • 新增:
      • TestJsonSchemaRefKeywordDetection
      • TestJsonConstructorTypeChecking
      • TestJsonConstructorsTimestampUseSessionTimeZone
  • 分布式预期结果同步更新:
    • test/distributed/cases/function/func_json_array.result
    • test/distributed/cases/function/func_json_object.result
    • timestamp 输出从 UTC 后缀形式改为 session timezone 展示形式。
  • 已更新问题记录:
    • matrixone-pr24509-json-review-issues.md
    • 路径:/home/pengzhen/Documents/matrixone-pr24509-json-review-issues.md
    • 已追加 fix status。
    • 已保存到 memoria:019e539ebad87a12ac1376ea4aff5463

已通过测试:

@daviszhen

Copy link
Copy Markdown
Contributor Author

• 审查结论

这轮按你要求把 PR 24509 的 10 个目标函数逐个过了一遍,并且遵守了“当前函数彻底处理完再看下一个”。

最终结论是:当前分支上这 10 个函数没有剩余确认问题了。
这次实际新增修改只落在 5 个还残留 selectList.AllNull 执行顺序问题的函数上:

  • JSON_ARRAY
  • JSON_OBJECT
  • JSON_KEYS
  • JSON_PRETTY
  • JSON_VALUE

这 5 个函数原先都会在“整列都应直接返回 SQL NULL”时,仍然先尝试解析输入;我已经在 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_builtin_json.go 里补了 fast path,并在 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_json_valid_test.go 里加了对应回归测试。

其余 5 个函数在当前分支里已经是修好的状态,我逐个确认后没有再改代码:

  • JSON_TYPE
  • JSON_VALID
  • JSON_LENGTH
  • JSON_SCHEMA_VALID
  • JSON_SCHEMA_VALID_REPORT

其中也一并确认了这几类更早的问题在当前分支都已经到位:

  • JSON_ARRAY / JSON_OBJECT 的类型检查收紧
  • JSON_ARRAY / JSON_OBJECT 的 TIMESTAMP 时区与 scale 输出
  • JSON_SCHEMA_VALID / JSON_SCHEMA_VALID_REPORT 的混合重载
  • schema $ref 的上下文化检测
  • JSON_VALUE 命中 object/array 时返回 NULL

验证

我跑了每个函数对应的定向 go test,以及最后一轮聚合验证,全部通过。
验证范围主要是 pkg/sql/plan/function 的单测;我没有重跑整套分布式 SQL 用例。

如果你要,我下一步可以把这次审查整理成一份适合直接发 PR review comment 的精简版结论。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current head still has several blocking JSON semantic issues:

  1. JSON_KEYS is still registered and materialized as VARCHAR, not JSON.

    • pkg/sql/plan/function/list_builtIn.go registers all JSON_KEYS overloads with retType = types.T_varchar.
    • pkg/sql/plan/function/func_builtin_json.go builds a JSON array text (buildJsonKeysArray) and appends it via AppendMustBytesValue, so downstream composition treats it as a string, not a JSON value.
    • This breaks nesting semantics, e.g. JSON_ARRAY(JSON_KEYS('{"a":1}')) becomes an array containing the string "["a"]" instead of a nested JSON array.
  2. JSON_SCHEMA_VALIDATION_REPORT is also exposed as VARCHAR, and its payload shape is not the MySQL-compatible report contract.

    • pkg/sql/plan/function/list_builtIn.go registers JSON_SCHEMA_VALID_REPORT with retType = types.T_varchar.
    • pkg/sql/plan/function/func_builtin_json.go returns a custom payload like {"valid": false, "errors": [...]} from buildSchemaValidationReport().
    • This locks in the wrong contract and the wrong type for a function that should produce a JSON report document.
  3. JSON_VALUE still accepts non-simple / wildcard paths and silently turns multi-match cases into NULL.

    • pkg/sql/plan/function/func_builtin_json.go only parses the path, but never rejects wildcard / non-simple paths before bj.Query(...).
    • When such a path yields an object/array, the implementation falls into the object/array => NULL branch instead of rejecting the path / multiple-match case.

These are all correctness / contract issues for newly added builtins, so I’m requesting changes.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Current head still has several blocking JSON semantic issues:

  1. JSON_KEYS is still registered and materialized as VARCHAR, not JSON.

    • pkg/sql/plan/function/list_builtIn.go registers all JSON_KEYS overloads with retType = types.T_varchar.
    • pkg/sql/plan/function/func_builtin_json.go builds a JSON array text (buildJsonKeysArray) and appends it via AppendMustBytesValue, so downstream composition treats it as a string, not a JSON value.
    • This breaks nesting semantics, e.g. JSON_ARRAY(JSON_KEYS('{"a":1}')) becomes an array containing the string "["a"]" instead of a nested JSON array.
  2. JSON_SCHEMA_VALIDATION_REPORT is also exposed as VARCHAR, and its payload shape is not the MySQL-compatible report contract.

    • pkg/sql/plan/function/list_builtIn.go registers JSON_SCHEMA_VALID_REPORT with retType = types.T_varchar.
    • pkg/sql/plan/function/func_builtin_json.go returns a custom payload like {"valid": false, "errors": [...]} from buildSchemaValidationReport().
    • This locks in the wrong contract and the wrong type for a function that should produce a JSON report document.
  3. JSON_VALUE still accepts non-simple / wildcard paths and silently turns multi-match cases into NULL.

    • pkg/sql/plan/function/func_builtin_json.go only parses the path, but never rejects wildcard / non-simple paths before bj.Query(...).
    • When such a path yields an object/array, the implementation falls into the object/array => NULL branch instead of rejecting the path / multiple-match case.

These are all correctness / contract issues for newly added builtins, so I’m requesting changes.

• 继续这轮后,我把你补充指出的 3 个漏审点都落成了代码和测试,并把分布式基线文件也一并补了。

这次新增的核心修复是:

  • JSON_KEYS
    • 返回类型改成 JSON,注册位置在 /mnt/fastdata/matrixone/pkg/sql/plan/function/list_builtIn.go:985。
    • 结果不再是 VARCHAR 文本,而是用真实 ByteJson 物化,位置在 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_builtin_json.go:1224 和 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_builtin_json.go:1333。
    • 新增了返回类型测试和组合语义测试,见 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_json_valid_test.go:145 和 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_json_valid_test.go:498。
    • 分布式用例补了 json_array(json_keys(...)),见 /mnt/fastdata/matrixone/test/distributed/cases/function/func_json_keys.test 和 /mnt/fastdata/matrixone/test/distributed/cases/function/func_json_keys.result。
  • JSON_SCHEMA_VALIDATION_REPORT
    • 返回类型改成 JSON,注册位置在 /mnt/fastdata/matrixone/pkg/sql/plan/function/list_builtIn.go:1118。
    • 执行层改成按 JSON 值写入,位置在 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_builtin_json.go:1552。
    • report payload 从自定义 errors 列表改成 MySQL 风格字段集合:valid、reason、schema-location、document-location、schema-failed-keyword,位置在 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_builtin_json.go:1631。
    • 新增返回类型和 contract 测试,见 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_json_valid_test.go:471。
    • 分布式 func_json_schema.result 已改到新 contract,见 /mnt/fastdata/matrixone/test/distributed/cases/function/func_json_schema.result。
  • JSON_VALUE
    • 现在显式拒绝 non-simple path,wildcard / multi-match 不再静默变 NULL,位置在 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_builtin_json.go:1881。
    • 新增 wildcard path 必须报错的回归测试,见 /mnt/fastdata/matrixone/pkg/sql/plan/function/func_json_valid_test.go:698。
    • 分布式用例补了 '$.*' 报错 case,见 /mnt/fastdata/matrixone/test/distributed/cases/function/func_json_value.test 和 /mnt/fastdata/matrixone/test/distributed/cases/function/func_json_value.result。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed from multiple angles, including function registration, JSON constructor/extractor semantics, schema-validation paths, typed-scalar bytejson behavior, cross-type numeric comparison, test coverage, and current unresolved threads.

I did not find any remaining blocking issues on the current head. Approving.

@mergify

mergify Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-26 03:00 UTC · Rule: release-3.0
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-05-26 03:01 UTC · at 9070340a2fd9773efdcefa85b11a312446ad2ecb · squash

This pull request spent 43 seconds in the queue, including 4 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI (3.0) / Coverage
    • check-neutral = Matrixone Utils CI (3.0) / Coverage
    • check-skipped = Matrixone Utils CI (3.0) / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI (3.0) / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI (3.0) / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI (3.0) / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/feature size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants