的雪花,我有以下:基于FLATTEN
docs雪花JSON横向子查询
create or replace table json_tmp as select column1 as id, parse_json(column2) as c
from VALUES (1,
'{"id": "0x1",
"custom_vars": [
{ "key": "a", "value": "foo" },
{ "key": "b", "value": "bar" }
] }') v;
,我希望把这些到表看起来像这样:
+-------+---------+-----+-----+
| db_id | json_id | a | b |
+-------+---------+-----+-----+
+-------+---------+-----+-----+
| 1 | 0x1 | foo | bar |
+-------+---------+-----+-----+
下面是该查询我审判;它导致SQL编译错误:“对象'CUSTOM_VARS'不存在。”
select json_tmp.id as dbid,
f.value:id as json_id,
a.v,
b.v
from json_tmp,
lateral flatten(input => json_tmp.c) as f,
lateral flatten(input => f.value:custom_vars) as custom_vars,
lateral (select value:value as v from custom_vars where value:key = 'a') as a,
lateral (select value:value as v from custom_vars where value:key = 'b') as b;
这里的错误究竟是什么?有没有更好的方法来做这种转变?
这是非常有用的,我没有意识到我可以在javascript中编写UDF。我怀疑这对我来说是最好的解决方案;该数组将有五个自定义变量。 – jsharp
如果我使用这样的UDF:var obj = {}; customData.forEach((item)=> {obj [item.key] = item.value;});返回obj; ',有什么办法,除了IMMUTABLE(根据文档不能保证memoization),以确保它只被调用一次?想要做一些类似'select db_id as id,c:id,custom_vars:a,custom_vars:b' – jsharp
这个系统通常足够聪明,可以优化常见的子表达式,所以如果你像'select my_func(C):a ,my_func(C):b它应该只调用一次。 –