Skip to content

add json_overlap - #25981

Merged
mergify[bot] merged 20 commits into
matrixorigin:mainfrom
daviszhen:0720-add-json-overlap
Jul 23, 2026
Merged

mergify[bot] merged 20 commits into
matrixorigin:mainfrom
daviszhen:0720-add-json-overlap

Conversation

@daviszhen

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_OVERLAPS,并补齐其性能、安全性和回归覆盖。

  • 新增 JSON 顶层 overlap 语义:array/scalar autowrap、object 同键同值、JSON null 与 SQL NULL 区分、数值精确比较。
  • 解决超范围 Decimal:Parse256/Scale 极端指数不 panic;JSON_OVERLAPS 对无法安全使用 Decimal256 的数值走 normalized-key 比较,不展开巨大指数。
  • 优化 const 大数组:索引和 fallback numeric key 按 batch 构建复用,避免逐行重复排序;普通数值数组保持低分配。
  • 修复 parser:完整根节点后出现 malformed suffix 时释放 parse tree。
  • 补充 UT、race 覆盖、性能 benchmark,以及 JSON_OVERLAPS 的 BVT 和 result。
  • 静态检查项已修复:布尔比较改为 !Sign()。

@daviszhen
daviszhen requested a review from a user July 21, 2026 11:01
@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 →

@mergify mergify Bot added the kind/bug Something isn't working label Jul 21, 2026
@matrix-meow matrix-meow added the size/XXL Denotes a PR that changes 2000+ lines label Jul 21, 2026

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

Codex automated review

Found two blocking compatibility/performance issues. GitHub CI is green; focused local tests were blocked because cgo/libmo.dylib is absent.

P1 - Accept all MySQL string storage types (pkg/sql/plan/function/func_builtin_json_overlap.go:89)

The checker accepts only JSON, CHAR, VARCHAR, and TEXT. MatrixOne's IsMySQLString(), used by sibling JSON functions, also includes BINARY, VARBINARY, and BLOB; MySQL likewise accepts these as JSON-text inputs. Consequently JSON_OVERLAPS over valid VARBINARY/BLOB JSON columns fails during function resolution instead of parsing and executing. Use the shared string-type predicate and cover these types.

P1 - Reuse constant-array indexes even when the constant is longer (pkg/sql/plan/function/func_builtin_json_overlap.go:332)

arraysOverlapViews first moves the shorter array to the left, then only reuses left.prepared. For JSON_OVERLAPS(, column_with_small_arrays), the constant is moved right and its cached index is ignored, so every row scans the entire constant. An 8192-element constant against 8192 one-element rows performs roughly 67 million comparisons instead of one index build plus one logarithmic probe per row. Select the prepared constant using batch-amortized cost before the size swap and add array-valued batch coverage.

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

I found one meaningful issue before this should merge.

pkg/sql/plan/function/func_builtin_json_overlap.go has a performance regression in arraysOverlapViews(). The function swaps the operands so that left is always the smaller array, but after that swap it only checks left.prepared:

if left.document.GetElemCnt() > right.document.GetElemCnt() {
    left, right = right, left
}
...
if left.prepared != nil {
    left.prepared.ensure(left.document)
    return jsonOverlapPreparedArrayOverlaps(left.document, left.prepared, right.document)
}
return w.arraysOverlap(left.document, right.document)

For the common shape json_overlaps(<large const array>, <smaller row array>), the const side starts with a prepared index, but the size swap moves it to right, so this branch is missed and the code falls back to rebuilding a transient sorted workspace for every row. That defeats the prepared-array optimization exactly for the query shape where it matters most.

Please check right.prepared after the size swap as well, e.g. by probing the smaller array against the prepared larger const array, and add a regression test that covers the swapped-operand case.

Non-blocking follow-ups I would still recommend:

  • add typed-JSON RHS / both-sides-typed coverage for json_overlaps;
  • add SQL-visible too-deep JSON and NULL-short-circuit cases;
  • add a partial-row skip unhappy-path test where skipped rows contain malformed/deep JSON.

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

I found two merge-blocking issues on the current head.

  1. pkg/sql/plan/function/func_builtin_json_overlap.go:89: the overload checker accepts JSON/CHAR/VARCHAR/TEXT but rejects BINARY, VARBINARY, and BLOB. Those are included by the shared IsMySQLString() contract used by the other JSON functions, so valid JSON text stored in those column types fails at function resolution. Use input.Oid.IsMySQLString() and add typed cases.

  2. pkg/sql/plan/function/func_builtin_json_overlap.go:332: arraysOverlapViews swaps by length and then only considers left.prepared. For json_overlaps(<large constant array>, small_array_column), the prepared constant is moved to the right and ignored, forcing a full transient sort/scan for every row. The work grows to rows × constant size despite the constant index. Preserve/probe either prepared operand after the swap and add a batch regression where the constant is the larger array.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Codex automated review

The earlier request to accept native SQL scalars is withdrawn as disproven. The latest change implementing it introduces one blocking compatibility defect; one non-blocking prepared-index cost issue also remains.

P1 - Do not reinterpret native SQL scalars as JSON text (pkg/sql/plan/function/func_builtin_json_overlap.go:92)

This implicit-cast branch was added after the latest discussion, but its premise is incorrect. MySQL requires json_doc arguments to be JSON-typed values or strings containing JSON, as documented by the JSON argument contract and JSON_OVERLAPS signature; native INT, BOOL, and DECIMAL arguments fail with error 3146. Here they instead become successful VARCHAR inputs. The new BVT demonstrates semantic corruption: json_overlaps(true, '1') returns 1 because SQL TRUE becomes text 1 and is parsed as JSON number 1, not JSON boolean true. Restore the JSON/MySQL-string/ANY-only checker and require explicit JSON or string conversion.

P2 - Stop charging index construction after the prepared index is ready (pkg/sql/plan/function/func_builtin_json_overlap.go:373)

Every row calls jsonOverlapPreparedArrayBatchCost, which always includes count*log(count) preparation cost even after prepared.ensure has set ready. For an 8192-element constant and two heterogeneous probe rows, an 8192-element first probe builds the index, but a subsequent one-element probe rejects that already-built index because the estimate charges its 114688-comparison construction again. The fallback then probes all 8192 constant elements instead of doing one logarithmic lookup. Account for prepared.ready in the cost or retain the batch decision; current tests only repeat uniform probe shapes.

• 针对 reviewer comment 的修改:

  • 禁止 INT、BOOL、DECIMAL、FLOAT 等 native SQL scalar 被隐式转成 JSON 文本;仅接受 JSON、MySQL 字符串类型和 ANY。
  • 非法 JSON 参数改为明确提示参数位置,例如 argument 1 / argument 2,文本接近 MySQL。
  • 保留 BINARY、VARBINARY、BLOB 作为 JSON 文本输入的支持。
  • prepared constant-array 索引会比较左右候选;索引 ready 后不再重复计入构建成本。
  • 更新 UT 与 BVT .test/.result:覆盖 native scalar 拒绝、参数序号、二进制字符串类型及 prepared-index 场景。

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

Codex automated review

One non-blocking MySQL wire-compatibility issue remains.

P2 - Return MySQL ER_INVALID_TYPE_FOR_JSON for rejected arguments (pkg/common/moerr/error.go:404)

The new checker correctly rejects JSON_OVERLAPS(1, '1'), but ErrInvalidTypeForJSON is mapped to ER_UNKNOWN_ERROR/HY000. The protocol therefore sends the internal error code 20316 (it falls back when the MySQL code is unknown), rather than MySQL's ER_INVALID_TYPE_FOR_JSON 3146 with SQLSTATE 22032, despite the exact 3146 definition already existing in mysql_error_define.go. Clients that branch on MySQL error codes or SQLSTATE receive an incompatible result. Map this error to ER_INVALID_TYPE_FOR_JSON and add a protocol-level code/state assertion.

@mergify mergify Bot added the queued label Jul 23, 2026
@mergify

mergify Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-23 11:56 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-07-23 13:06 UTC · at c818479940f471d3d6eec1efcb56ef3f58bc3455 · squash

This pull request spent 1 hour 9 minutes 43 seconds in the queue, including 59 minutes 33 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection] (documentation)
  • github-review-decision = APPROVED [🛡 GitHub branch protection] (documentation)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64

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 size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants