Skip to content

[Bug]: UNNEST rejects prepared JSON text placeholders as TEXT #29307

Closed

Description

@Ariznawlll

问题描述

UNNEST(?,?,?) 通过 SQL PREPARE 接收 JSON 文本、JSON path 和 outer 参数时,第一个字符串变量在运行时以 TEXT 类型进入表函数。UNNEST 的执行检查只接受 JSON 或 VARCHAR,因此在解析 JSON 前直接返回:

invalid input: unnest: first argument must be json or string, but got TEXT

相同 JSON 使用字面量能够正常展开;给占位符显式添加 CAST(? AS VARCHAR)/CAST(? AS BOOL) 后也能得到相同的三行结果。因此 JSON 内容和 path 均有效,差异来自 prepared placeholder 的类型绑定。

环境

  • MatrixOne commit: 95b1d2bfd9d756d08320569858080cb16f230fff
  • Branch: main
  • Date: 2026-09-24
  • Deployment: local launch, single CN

最小复现

prepare p_plain from
  'select u.seq,u.path,u.value from unnest(?,?,?) u order by u.seq';
set @j='{"a":[1,null,3]}',@p='$.a',@o=false;
execute p_plain using @j,@p,@o;

实际结果:

ERROR 20301 (HY000): invalid input: unnest: first argument must be json or string, but got TEXT

对照

字面量调用:

select u.seq,u.path,u.value
from unnest('{"a":[1,null,3]}','$.a',false) u order by u.seq;

显式转换的 prepared 调用:

prepare p_cast from
  'select u.seq,u.path,u.value
   from unnest(cast(? as varchar),cast(? as varchar),cast(? as bool)) u
   order by u.seq';
execute p_cast using @j,@p,@o;

两种对照均返回:

0  $.a[0]  1
1  $.a[1]  null
2  $.a[2]  3

未显式转换的调用重复 3 次均返回相同 TEXT 类型错误;显式转换的对照重复 3 次均返回相同三行。错误后同一会话仍可继续执行普通查询。

代码定位

pkg/sql/plan/unnest.go 的 buildUnnest 原样保留参数表达式,没有为未定型的占位符建立函数参数域;pkg/sql/colexec/table_function/unnest.go 在执行阶段又精确限制第一个参数只能是 T_json 或 T_varchar。SQL PREPARE 的字符串变量以 TEXT 到达该检查,因此被拒绝。

期望行为

有效 JSON 文本和 path 通过 prepared placeholders 传入时,应与相同字面量调用一致地展开,不应要求调用方额外添加类型转换。

Activity

  1. added this to the MO-202609 milestone on Sep 24, 2026
  2. added
    kind/bug
    on Sep 24, 2026
  3. added theissue type on Sep 24, 2026
  4. self-assigned this
    on Sep 24, 2026
  5. added a commit that references this issue on Sep 24, 2026
    5d80e0b
  6. Ariznawlll commented on Sep 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5be6cd90b501da225be591ffffe6000bf01f515e
    • 部署方式:本地单机(1 CN / 1 TN / 1 LogService)
    • 测试日期:2026-09-27

    原问题验证

    在上述最新 main 上按 issue 原始复现步骤执行,并独立重复 3 次;3/3 均满足修复后的预期。当前 main 已包含修复提交 5d80e0bc0348c31325c9a54488561c567076abb0。

    Happy Path 覆盖

    1. prepared UNNEST 正确接收 JSON 文本、路径与 outer 参数,返回序号、路径和值。

    Unhappy Path 覆盖

    1. JSON 数组包含 NULL,并连续执行两次,未被错误绑定为普通 TEXT。

    白盒验证

    确认修复提交 5d80e0bc0348c31325c9a54488561c567076abb0 是当前 main 的祖先,并核对对应 MatrixOne BVT/实现路径;本次另以公开 SQL 协议执行黑盒验收。

    回归测试

    已加入 MOTR PR #204 的 issue_29306_29307_29308_29310_table_functions.test,MOTR 提交 2b4462f2;本地基线比对连续 3 次通过。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions