所以执行此操作的 SQL,我知道你不想要:
with table1 AS (
select parse_json('{"list": "212=1.00,214"}') as payload
), table2 AS (
select parse_json(column1) as payload
,column2 as key
,column3 as value
from values ('{"id":"212"}', 'test13', 'success' ),
('{"id":"214"}', 'test15', 'impression' )
), table1_demunged AS (
select split(f.value,'=')[0] as id
,split(f.value,'=')[1] as val
from table1 t, lateral flatten(input=>split(t.payload:list,',')) f
), tables_joined as (
select t2.key as obj_key
,t1.id as code
,t2.value as desc
,t1.val as value
from table2 t2
join table1_demunged t1 on t2.payload:id = t1.id
), as_objects AS (
select obj_key, object_construct('code', code, 'desc', desc, 'value', coalesce(value,'')) as res
from tables_joined t
)
select object_agg(obj_key, res) object
from as_objects
group by true;
给出你想要的结果:
OBJECT
{ "test13": { "code": "212", "desc": "success", "value": "1.00" }, "test15": { "code": "214", "desc": "impression", "value": "" } }
但是我不明白你是否真的想要一个 UDF 来完成所有这些,因为它是一个 FLATTEN 然后是一个 JOIN 然后是一些 OBJECT_ 函数,或者如果你只是想避免 FALTTEN 因为它“棘手的 SQL 和你想在 UDF 后面找到它”,或者您使用的系统无法解析 =>,因此您需要将 flatten 隐藏在 UDF 后面,但在这种情况下,UDF 无法为您完成所有连接..
感觉这个问题比被问到的要多。