Skip to content

[Bug]: JSON_UNQUOTE drops boundary quotes from JSON string values #27427

Closed

Description

@Lundomn

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

ae01f96c4d5c02c973f5bac9e8c4ab82b5dac3a7 (latest origin/main tested on 2026-08-21)

Other Environment Information

- Hardware parameters: local development machine
- OS type: macOS
- Others: local single-process MatrixOne; MySQL text protocol on 127.0.0.1:6001. A deterministic Kafka/GH Archive stream was used for impact validation.

Actual Behavior

JSON-to-string conversion loses data when the JSON string payload itself begins and ends with a double quote.

SELECT
  json_object('v', concat(char(34), 'x', char(34))) AS doc,
  json_extract(json_object('v', concat(char(34), 'x', char(34))), '$.v') AS extracted,
  json_unquote(json_extract(json_object('v', concat(char(34), 'x', char(34))), '$.v')) AS actual,
  hex(json_unquote(json_extract(json_object('v', concat(char(34), 'x', char(34))), '$.v'))) AS actual_hex,
  hex('"x"') AS expected_hex;

doc is {"v": "\\"x\\""}, and JSON_EXTRACT correctly represents the value as "\\"x\\"". But MatrixOne returns:

actual       x
actual_hex   78
expected_hex 227822

The payload should be "x", including the two quote characters.

The same data loss occurs through all of these paths:

  • JSON_UNQUOTE(JSON_EXTRACT(...))
  • JSON_EXTRACT_STRING(...)
  • CAST(JSON_EXTRACT(...) AS CHAR)
  • JSON_UNQUOTE(JSON_QUOTE(...)) (the quote/unquote round trip is not lossless)

A read-only boundary matrix covered 8 values (ordinary string; interior, leading, trailing and paired quotes; one/two quote-only strings; paired-quote Unicode string), all 4 paths above, and 10 repetitions. It executed 320 assertions; 120 failed identically. Only values whose payload begins and ends with " fail. For example, a payload of "" becomes an empty string and "你好" becomes 你好.

Expected Behavior

JSON string conversion must preserve every character in the JSON string payload.

For the query above, expected output is:

actual       "x"
actual_hex   227822
expected_hex 227822

Likewise, JSON_UNQUOTE(JSON_QUOTE(value)) must round-trip the original string, including leading/trailing quote characters.

Steps to Reproduce

Run this SQL only; no table or DML is required:

SELECT
  hex(json_unquote(json_extract(
    json_object('v', concat(char(34), 'x', char(34))), '$.v'
  ))) AS actual_hex,
  hex('"x"') AS expected_hex,
  hex(json_extract_string(
    json_object('v', concat(char(34), 'x', char(34))), '$.v'
  )) AS extract_string_hex,
  hex(cast(json_extract(
    json_object('v', concat(char(34), 'x', char(34))), '$.v'
  ) AS char)) AS cast_hex,
  hex(json_unquote(json_quote(concat(char(34), 'x', char(34))))) AS roundtrip_hex;

Actual output:

actual_hex          78
expected_hex        227822
extract_string_hex  78
cast_hex            78
roundtrip_hex       78

Additional information

The deterministic 590,187-event GH Archive Kafka stream contained two affected payloads (seq 258021 and 258650), both exactly "branch_name_with_quotes". In the loaded MatrixOne table, the scalar reference column had 25 bytes while conversion from the identical JSON field had 23 bytes. This silently discarded two valid rows:

SELECT count(*) FROM github_events WHERE ref_name IS NOT NULL;
-- 446383

SELECT count(*) FROM github_events
WHERE ref_name = json_unquote(json_extract(meta, '$.ref'));
-- 446381

Source inspection points to pkg/container/bytejson/bytejson.go, ByteJson.Unquote(): it examines the first and last byte of bj.GetString() and strips them as JSON delimiters. For an extracted JSON string, GetString() is already the payload, so a payload such as "x" is incorrectly sliced to x. Existing tests cover an interior escaped quote but not a payload whose content itself has quote boundaries.

Existing-issue/PR searches covered json_unquote, json_quote, json_extract_string, ByteJson.Unquote, escaped quotes, and JSON boundary quotes. Related JSON reports concern comparison/cast behavior or missing functions; none describes loss of payload boundary quotes.

Activity

  1. self-assigned this
    on Aug 21, 2026
  2. added
    kind/bug
    on Aug 21, 2026
  3. added a commit that references this issue on Aug 21, 2026
    73a7667
  4. reopened this on Sep 7, 2026
  5. assigned and unassigned on Sep 7, 2026
  6. Lundomn commented on Sep 7, 2026

    @Lundomn
    ContributorAuthor

    QA 转测(2026-09-07)

    • 修复 PR:PR #27449(已 merged)
    • 根因与修复范围:JSON_UNQUOTE 等路径把 binary JSON 已解码 payload 的首尾双引号误当 delimiters,错误删除边界字符;PR 直接返回 TpCodeString payload。
    • 开发侧验证:bytejson/plan/function 单测、race、go vet、build 通过;原始失败复现 20/20,四个 API 的边界矩阵 320/320 通过。
    • QA 请重点覆盖:VARCHAR 与 binary JSON、首尾/嵌入/仅双引号、Unicode、NULL、JSON_UNQUOTE 与 JSON_EXTRACT_STRING,以及 malformed JSON。

    请在本 Issue 记录实际测试版本/环境、通过或失败结论、未覆盖项及补测要求。按流程,QA 明确通过后再关闭 Issue;如发现失败或回归请保持 open 并反馈复现信息。

  7. slyunnn commented on Sep 8, 2026

    @slyunnn

    Regression and extended verification for #27427 (2026-09-08)

    Verification Environment

    • MatrixOne source: latest origin/main at f72ca9efbeb3a7c673701e4a135cc6670c33cfde — fix(function): preserve row-local FROM_BASE64 decoding semantics (#28360).
    • Related fix: PR #27449, merge commit 73a766720ce06589a1272b7d77c4cfa7e0e8f756 — fix(json): preserve boundary quotes in unquote; it is included in the verified origin/main.
    • Executed topology: locally built LOG + TN + one CN; SQL used MySQL/TCP through MOTR at CN endpoint 127.0.0.1:26001.
    • Standard three test endpoints were not covered: CN-1 127.0.0.1:16001, CN-2 127.0.0.1:16002, and Proxy 127.0.0.1:6001. The issue is a local JSON conversion contract and does not require multi-CN or Proxy routing to reproduce.
    • make build completed, and focused CGo tests for pkg/container/bytejson and pkg/sql/plan/function passed.

    What Was Covered

    • OK — Original JSON_UNQUOTE(JSON_EXTRACT(...)) reproduction with payload "x": JSON_UNQUOTE, JSON_EXTRACT_STRING, CAST(... AS CHAR), and JSON_UNQUOTE(JSON_QUOTE(...)) all returned hex 227822.
    • OK — Happy path: VARCHAR JSON text and binary JSON column inputs preserved ordinary text and embedded, leading, trailing, paired, one-quote, and two-quote payloads.
    • OK — Boundary and compatibility: all four conversion paths preserved paired quote payload "both" and Unicode payload "你好", with every equality oracle returning 1.
    • OK — Unhappy path: JSON_UNQUOTE(NULL) and JSON_EXTRACT_STRING(NULL, '$.v') returned NULL; malformed JSON produced JSON_VALID(...) = 0. The focused TestJsonUnquoteRejectsInvalidFramedStringAndUTF8 unit test also passed.
    • OK — Scalar control: JSON_UNQUOTE(JSON_QUOTE('plain')) returned plain, and the session remained usable.
    • OK — Cleanup: the MOTR case dropped its test database and verified cleanup_database_count = 0 after each run.
    • Not covered — Multi-CN execution through 127.0.0.1:16001 / 127.0.0.1:16002, Proxy execution through 127.0.0.1:6001, Kafka/GH Archive ingestion, and workload-scale validation.

    Findings

    The fix returns decoded TpCodeString payload bytes directly, so JSON string payload delimiters are no longer inferred from content. Boundary quotes are therefore retained rather than being removed by ByteJson.Unquote().

    CHAR(34) is binary-typed in this SQL context; the direct JSON_OBJECT reproduction therefore explicitly casts the CONCAT(...) payload to CHAR text. The VARCHAR and binary JSON column matrix independently proves the affected conversion contract without relying on that constructor typing detail.

    Large-data regression is not needed. The defect depends on JSON string payload boundaries, not row count, block/batch boundaries, memory or spill, concurrency, skew, network transfer, or resource scale. The minimal deterministic matrix covers the relevant string-content boundary.

    MOTR / BVT Coverage

    • Added suites/14_issue_regression/t/issue_27427_json_unquote_boundary.test and suites/14_issue_regression/r/issue_27427_json_unquote_boundary.result.
    • Recorded once and replayed twice against the same locally built latest-main instance: each run passed 13/13 statements.
    • Independent MOTR branch: test/issue-27427-json-unquote-boundary; commit b6c44ba (test: add MOTR regression for #27427), pushed by SSH.
    • No issue-specific PR was created. The commit is ready for the next daily MOTR regression summary.

    Final Conclusion

    Fixed. Latest origin/main contains the related fix and passes the original reproduction, happy-path, boundary, Unicode/binary-JSON compatibility, NULL, malformed-input validation, cleanup, and focused unit coverage. Large-data regression is not required for the reasons above.

  8. added 3 commits that reference this issue on Sep 17, 2026
    e064ee9
    494e63d
    c65fc89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugphase/testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions