的雪花,我有以下:基於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它應該只調用一次。 –