JSON path type annotation is not portable: `data.n:int` works on PostgreSQL, raises on MySQL
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 68/100
调研方向
Start with adapters/mysql.py around lines 872-874 and compare its return_type handling with the mapping in adapters/postgres.py. Reproduce the listed expressions against MySQL 8.0 and PostgreSQL 15, then check json-type.ipynb and reference/specs/type-system.md. Done means the accepted annotation vocabulary is consistent or clearly rejected across both adapters, with the canonical spelling documented.
由索引模型根据 Issue 内容生成。
描述
The attr.path:type annotation accepted by translate_attribute takes a different vocabulary on each backend, with no indication of which is canonical. The same expression works on one and raises a SQL syntax error on the other.
Unrelated to #1561 and #1562, which are about hidden attributes. This one is about an ordinary visible json column.
Repro
@schema
class Doc(dj.Manual):
definition = """
doc_id : int32
---
data : json
"""
Doc.insert1({"doc_id": 1, "data": {"system": "PyRat", "n": 7}})
| expression | MySQL 8.0 | postgres:15 |
|---|---|---|
Doc.proj(n="data.n") |
'7' — str |
'7' — str |
Doc.proj(n="data.n:int") |
QuerySyntaxError | 7 — int |
Doc.proj(n="data.n:unsigned") |
7 — int |
7 — int |
Doc.proj(n="data.n:decimal(5,1)") |
Decimal('7.0') |
Decimal('7.0') |
Cause
The two adapters treat return_type differently.
adapters/postgres.py maps the annotation onto a PostgreSQL type before casting:
pg_type = return_type.lower()
if pg_type in ("unsigned", "signed"):
pg_type = "integer"
so it accepts MySQL's vocabulary and PostgreSQL's own.
adapters/mysql.py:872-874 passes it through verbatim:
return_clause = f" returning {return_type}" if return_type else ""
return f"json_value({quoted_col}, _utf8mb4'$.{path}'{return_clause})"
MySQL's JSON_VALUE ... RETURNING accepts only a fixed set — SIGNED, UNSIGNED, DECIMAL, CHAR, DATE and so on — and int is not among them, so the statement is invalid.
The effect is that the portable vocabulary is MySQL's, PostgreSQL silently tolerates a superset, and a pipeline developed against PostgreSQL can carry an annotation that fails the first time it runs on MySQL. Nothing in the docs or the error says so.
Suggested fix
Normalize in the MySQL adapter as PostgreSQL already does — map int / integer / bigint and similar onto the RETURNING vocabulary — and reject an unmappable annotation with a clear error naming the accepted set, rather than emitting SQL the server will refuse.
Settle which spelling is canonical and document it. json-type.ipynb and reference/specs/type-system.md are the places that would say so; neither currently does.
Also worth noting
Without an annotation, a numeric JSON field comes back as a string on both backends ('7', not 7) — both extraction functions return text. That is defensible, since JSON has no fixed scalar type, but it is undocumented and the annotation is the only remedy. Mentioned because anyone hitting the portability problem above arrived there by trying to fix this.
- 主要语言
- Python
- 星标
- 197
- 派生
- 98
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 6
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
datajoint/datajoint-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
datajoint/datajoint-python#1539 · 3 条评论 ·
-
bug
难度 3/5 1-2 天 新手友好度 75/100
datajoint/datajoint-python#1564 ·
-
enhancement
难度 5/5 一周以上 新手友好度 35/100
datajoint/datajoint-python#1562 · 2 条评论 ·
-
bug
难度 3/5 1-2 天 新手友好度 75/100
datajoint/datajoint-python#1561 · 1 条评论 ·
-
enhancement
难度 4/5 3-5 天 新手友好度 45/100
datajoint/datajoint-python#1560 ·
查看 datajoint/datajoint-python 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
FuRongJun-1999/dsh-memory#56 ·
维护者通常 1 天内回复
-
Bug
难度 2/5 1-3 小时 新手友好度 76/100
pgadmin-org/pgadmin4#10503 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
521xueweihan/HelloGitHub#3857 ·
-
needs-ac
难度 2/5 1-3 小时 新手友好度 78/100
Ikalus1988/MisakaNet#2845 ·
维护者通常 1 天内回复
-
bug connectors operations
难度 2/5 1-3 小时 新手友好度 72/100
pyinfra-dev/pyinfra#1989 ·
维护者通常 3 天内回复