Skip to content

add json_contains_path - #25672

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
daviszhen:0713-fix-json-contains-path
Jul 13, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
daviszhen:0713-fix-json-contains-path

Conversation

@daviszhen

@daviszhen daviszhen commented Jul 13, 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 #24738

What this PR does / why we need it:

• json_contains_path 的实现分成三层:

  1. 函数注册和类型检查
    在 matrixone/pkg/sql/plan/function/list_builtIn.go:1379 注册 json_contains_path,返回类型是 int64。
    在 matrixone/pkg/sql/plan/function/func_builtin_json.go:139 做参数检查:至少 3 个参数,doc 可以是 json 或 MySQL string,其他参数要求字符串或可隐式 cast 到 varchar。

  2. 执行逻辑
    主执行函数在 matrixone/pkg/sql/plan/function/func_builtin_json.go:322。执行顺序是:

    • 先解析 doc,doc 为 SQL NULL 直接返回 NULL;
    • 再解析 one/all,大小写不敏感,非法值报错;
    • 再按参数顺序逐个解析 path;
    • one 模式遇到第一个存在 path 就返回 1;
    • all 模式遇到第一个不存在 path 就返回 0;
    • path 为 SQL NULL 时返回 NULL;
    • 短路后不会继续解析后面的 path,所以后面的 NULL 或非法 path 不会覆盖已确定结果。
  3. 路径存在性判断
    JSON path 是否存在由 matrixone/pkg/container/bytejson/bytejson.go:449 的 ByteJson.PathExists 负责。这里和 json_extract 不同,只关心 path 是否选中至少一个 JSON 值;JSON literal null 也算 path 存在。还补了 scalar array autowrap 语义,比如 '1' 的 $[0] 算存在。

另外实现里给 const mode/path 做了缓存:matrixone/pkg/sql/plan/function/func_builtin_json.go:422,避免每行重复解析常量 one/all 和 JSON path。BVT 覆盖在 func_json_contains_path.test/result,UT 覆盖在 func_json_valid_test.go。

@daviszhen
daviszhen requested a review from a user July 13, 2026 10:36
@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 →

@daviszhen daviszhen changed the title update add json_contains_path Jul 13, 2026
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Jul 13, 2026

@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.

LGTM after reviewing this from the function semantics, bytejson path-existence, and unhappy-path angles.

  • correctness: json_contains_path now follows the intended one/all and NULL semantics, and PathExists checks “does the path select any JSON value?” rather than reusing extract-style result shaping
  • behavior fidelity: the implementation preserves the important edge cases called out in the PR description — JSON literal null counts as existing, scalar $[0] autowrap works, and one/all short-circuiting prevents later NULL/invalid paths from overriding an already-settled result
  • unhappy path coverage: the new UT/BVT coverage exercises invalid mode/path/document inputs, recursive descent/wildcards, short-circuit order, dynamic mode/path columns, and the bytejson existence helper directly

I did not find a remaining correctness, leak, or hang issue in the latest diff, so approving.

@XuPeng-SH
XuPeng-SH merged commit b6b91bc into matrixorigin:main Jul 13, 2026
21 of 23 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#24738

## What this PR does / why we need it:

• json_contains_path 的实现分成三层:

  1. 函数注册和类型检查
在 matrixone/pkg/sql/plan/function/list_builtIn.go:1379 注册
json_contains_path,返回类型是 int64。
在 matrixone/pkg/sql/plan/function/func_builtin_json.go:139 做参数检查:至少 3
个参数,doc 可以是 json 或 MySQL string,其他参数要求字符串或可隐式 cast 到 varchar。

  2. 执行逻辑
主执行函数在 matrixone/pkg/sql/plan/function/func_builtin_json.go:322。执行顺序是:
      - 先解析 doc,doc 为 SQL NULL 直接返回 NULL;
      - 再解析 one/all,大小写不敏感,非法值报错;
      - 再按参数顺序逐个解析 path;
      - one 模式遇到第一个存在 path 就返回 1;
      - all 模式遇到第一个不存在 path 就返回 0;
      - path 为 SQL NULL 时返回 NULL;
      - 短路后不会继续解析后面的 path,所以后面的 NULL 或非法 path 不会覆盖已确定结果。

  3. 路径存在性判断
JSON path 是否存在由 matrixone/pkg/container/bytejson/bytejson.go:449 的
ByteJson.PathExists 负责。这里和 json_extract 不同,只关心 path 是否选中至少一个 JSON 值;JSON
literal null 也算 path 存在。还补了 scalar array autowrap 语义,比如 '1' 的 $[0] 算存在。

另外实现里给 const mode/path
做了缓存:matrixone/pkg/sql/plan/function/func_builtin_json.go:422,避免每行重复解析常量
one/all 和 JSON path。BVT 覆盖在 func_json_contains_path.test/result,UT 覆盖在
func_json_valid_test.go。

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#24738

## What this PR does / why we need it:

• json_contains_path 的实现分成三层:

  1. 函数注册和类型检查
在 matrixone/pkg/sql/plan/function/list_builtIn.go:1379 注册
json_contains_path,返回类型是 int64。
在 matrixone/pkg/sql/plan/function/func_builtin_json.go:139 做参数检查:至少 3
个参数,doc 可以是 json 或 MySQL string,其他参数要求字符串或可隐式 cast 到 varchar。

  2. 执行逻辑
主执行函数在 matrixone/pkg/sql/plan/function/func_builtin_json.go:322。执行顺序是:
      - 先解析 doc,doc 为 SQL NULL 直接返回 NULL;
      - 再解析 one/all,大小写不敏感,非法值报错;
      - 再按参数顺序逐个解析 path;
      - one 模式遇到第一个存在 path 就返回 1;
      - all 模式遇到第一个不存在 path 就返回 0;
      - path 为 SQL NULL 时返回 NULL;
      - 短路后不会继续解析后面的 path,所以后面的 NULL 或非法 path 不会覆盖已确定结果。

  3. 路径存在性判断
JSON path 是否存在由 matrixone/pkg/container/bytejson/bytejson.go:449 的
ByteJson.PathExists 负责。这里和 json_extract 不同,只关心 path 是否选中至少一个 JSON 值;JSON
literal null 也算 path 存在。还补了 scalar array autowrap 语义,比如 '1' 的 $[0] 算存在。

另外实现里给 const mode/path
做了缓存:matrixone/pkg/sql/plan/function/func_builtin_json.go:422,避免每行重复解析常量
one/all 和 JSON path。BVT 覆盖在 func_json_contains_path.test/result,UT 覆盖在
func_json_valid_test.go。

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#24738

## What this PR does / why we need it:

• json_contains_path 的实现分成三层:

  1. 函数注册和类型检查
在 matrixone/pkg/sql/plan/function/list_builtIn.go:1379 注册
json_contains_path,返回类型是 int64。
在 matrixone/pkg/sql/plan/function/func_builtin_json.go:139 做参数检查:至少 3
个参数,doc 可以是 json 或 MySQL string,其他参数要求字符串或可隐式 cast 到 varchar。

  2. 执行逻辑
主执行函数在 matrixone/pkg/sql/plan/function/func_builtin_json.go:322。执行顺序是:
      - 先解析 doc,doc 为 SQL NULL 直接返回 NULL;
      - 再解析 one/all,大小写不敏感,非法值报错;
      - 再按参数顺序逐个解析 path;
      - one 模式遇到第一个存在 path 就返回 1;
      - all 模式遇到第一个不存在 path 就返回 0;
      - path 为 SQL NULL 时返回 NULL;
      - 短路后不会继续解析后面的 path,所以后面的 NULL 或非法 path 不会覆盖已确定结果。

  3. 路径存在性判断
JSON path 是否存在由 matrixone/pkg/container/bytejson/bytejson.go:449 的
ByteJson.PathExists 负责。这里和 json_extract 不同,只关心 path 是否选中至少一个 JSON 值;JSON
literal null 也算 path 存在。还补了 scalar array autowrap 语义,比如 '1' 的 $[0] 算存在。

另外实现里给 const mode/path
做了缓存:matrixone/pkg/sql/plan/function/func_builtin_json.go:422,避免每行重复解析常量
one/all 和 JSON path。BVT 覆盖在 func_json_contains_path.test/result,UT 覆盖在
func_json_valid_test.go。

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: XuPeng-SH <[email protected]>

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/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants