当我尝试使用->>'elementName'访问JSON元素时,我会得到一个text类型。
SELECT pg_typeof(x1->>'a'), jsonb_typeof(x2)
FROM ( VALUES
('{"a":5}'::jsonb, '5'::jsonb)
) AS t(x1,x2);
pg_typeof | jsonb_typeof
-----------+--------------
text | number
(1 row)然而,jsonb表示它将数字映射到数字类型。..。
当将文本JSON输入转换为jsonb时,RFC 7159描述的原始类型有效地映射到本机PostgreSQL类型,如表8-23所示。
这是从文档中复制的表格,
JSON primitive type PostgreSQL type Notes
string text \u0000 is disallowed, as are non-ASCII Unicode escapes if database encoding is not UTF8
number numeric NaN and infinity values are disallowed
boolean boolean Only lowercase true and false spellings are accepted
null (none) SQL NULL is a different concept发布于 2017-03-23 19:34:18
目前无法访问内部JSON类型。上面引用的文档只提到了它们是如何存储的。有一个PostgreSQL中的伪类型anyelement,但是不能返回那个类型。函数是多态的,因为它们接受不同的类型,但它们必须返回指定的类型。
对于不同的类型,运算符可能会被重载,但目前情况并非如此。目前->>定义为
Operator Right Operand Type Description
->> text Get JSON object field as text这意味着无论如何存储类型,都必须通过text才能访问它。所有jsonb操作符都返回jsonb或text。
考虑到歧义,即使类型超载,这将如何处理。
SELECT pg_typeof(x1->>'a'), jsonb_typeof(x2)
FROM ( VALUES
('{"a":5}'::jsonb, '5'),
('{"a":true}'::jsonb, 'true'::jsonb)
) AS t(x1,x2);如果这有道理的话..。那这个能做什么..。
SELECT sum(x1->>'a')
FROM ( VALUES
('{"a":5}'::jsonb, '5'),
('{"a":true}'::jsonb, 'true'::jsonb)
) AS t(x1,x2);虽然重载->>可能会提高系统的效率,但也会使系统变得更加复杂。
https://dba.stackexchange.com/questions/168029
复制相似问题