builder: Oracle Merge JSON_TABLE infers VARCHAR2(4000) by char length instead of UTF-8 bytes and omits ERROR ON ERROR

未关闭 适合新手
#946 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python, sql

调研方向

从 sqlspec/builder/_merge.py 开始,重点查看 _create_oracle_json_source() 和 _infer_oracle_type(),然后比较 sqlspec/adapters/oracledb/core.py 中对字节数限制的处理。检查 Oracle JSON_TABLE 投影,以及针对超过 4,000 个 UTF-8 字节的多字节字符串和转换错误生成的 SQL。完成标准是这些值被推断为 CLOB,且 JSON_TABLE 输出 ERROR ON ERROR。

由索引模型根据 Issue 内容生成。

描述

bug

Summary

The Oracle Merge builder's JSON_TABLE source generator infers VARCHAR2(4000) vs CLOB using Python character length (len(str(value))) instead of UTF-8 byte length, and omits ERROR ON ERROR on JSON_TABLE. Under Oracle's default NLS_LENGTH_SEMANTICS=BYTE, multi-byte UTF-8 strings between 2,001 and 4,000 characters exceed 4,000 bytes and are silently coerced to NULL during MERGE.

Details

  1. Character count vs. UTF-8 byte length (sqlspec/builder/_merge.py:375, 419-428):

    • _create_oracle_json_source() compares len(str(value)) > len(str(existing)) (_merge.py:375) and _infer_oracle_type() checks len(str(value)) > 4000 (_merge.py:426).
    • This is inconsistent with coerce_large_parameters_sync() / _string_exceeds_byte_limit() in sqlspec/adapters/oracledb/core.py, which correctly checks UTF-8 encoded byte length against varchar2_byte_limit=4000.
    • A 2,500-character multi-byte string (e.g. "é" * 2500, 5,000 UTF-8 bytes) is inferred as VARCHAR2(4000) instead of CLOB.
  2. Silent NULL ON ERROR default on JSON_TABLE (sqlspec/builder/_merge.py:380-397):

    • exp.JSONTable(...) emits JSON_TABLE(:json_payload, '$[*]' COLUMNS (...)) without an ERROR ON ERROR clause.
    • Oracle JSON_TABLE defaults to NULL ON ERROR, so any value overflowing VARCHAR2(4000) or failing JSON/type conversion is silently projected as NULL rather than raising an ORA error.

Reproduction (sqlspec==0.74.2)

from sqlspec.builder import Merge

notes = "é" * 2500
assert len(notes) == 2500
assert len(notes.encode("utf-8")) == 5000

stmt = (
    Merge(dialect="oracle")
    .into("target", alias="t")
    .using([{"id": 1, "notes": notes}], alias="s")
    .on("t.id = s.id")
    .when_matched_then_update(notes="s.notes")
    .when_not_matched_then_insert(id="s.id", notes="s.notes")
    .build()
)
print(stmt.sql)
# Emits:
# FROM JSON_TABLE(:json_payload, '$[*]' COLUMNS(id NUMBER PATH '$.id', notes VARCHAR2(4000) PATH '$.notes'))

Expected Behavior

  • Measure UTF-8 byte length (len(str(value).encode("utf-8"))) in _create_oracle_json_source() sample selection and _infer_oracle_type() so strings exceeding 4,000 UTF-8 bytes project as CLOB.
  • Emit ERROR ON ERROR in Oracle JSON_TABLE projections so overflow or type conversion errors fail explicitly instead of silently writing NULL.
主要语言
Python
星标
110
派生
11
平均合并
19 小时 9 分钟
30 天内合并 PR
37

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

litestar-org/sqlspec 的其他 Issue

查看 litestar-org/sqlspec 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。