以下内容无效,尽管我认为可以正常使用
select contains(PARSE_JSON('{"id": 2, "bucket_type": 11}'), PARSE_JSON('{"id": 2}')) ;
上面的查询返回false。
有没有办法做到这一点?
答案 0 :(得分:2)
您可以创建一个UDF来解析是否为is_subset()
:
CREATE OR REPLACE FUNCTION is_subset("a" OBJECT, "b" OBJECT)
RETURNS STRING
LANGUAGE JAVASCRIPT
AS
$$
isSubset = (superObj, subObj) => {
return Object.keys(subObj).every(ele => {
if (typeof subObj[ele] == 'object') {
return isSubset(superObj[ele], subObj[ele]);
}
return subObj[ele] === superObj[ele]
});
// https://stackoverflow.com/a/61676007/132438
};
return isSubset(a,b);
$$;
现在我们可以对其进行测试:
SELECT is_subset(PARSE_JSON('{"id": 2, "bucket_type": 11}'), PARSE_JSON('{"id": 2}'));
-- true
SELECT is_subset(PARSE_JSON('{"id": 2}'), PARSE_JSON('{"id": 2, "bucket_type": 11}'));
-- false
答案 1 :(得分:0)
请查看以下内容是否可以与您配合
with first_json
as
(select parse_json(column1) as src, src:id as id
from values('{"id": 2, "bucket_type": 11}')),
second_json
as
(select parse_json(column1) as src, src:id as id
from values('{"id": 2}'))
select
case
when f.id in(select id from second_json)
then 'true'
else 'false'
end as result
from first_json f;