을 하위 쿼리 횡 방향 : 여기 눈송이 JSON 내가 눈송이에 다음 한
+-------+---------+-----+-----+
| db_id | json_id | a | b |
+-------+---------+-----+-----+
+-------+---------+-----+-----+
| 1 | 0x1 | foo | bar |
+-------+---------+-----+-----+
쿼리 I입니다 :
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;
FLATTEN
docs을 바탕으로,이처럼 보이는 테이블에 다음을 설정하는 희망 시도했다; 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;
정확히 여기에 오류가 있습니까? 이 변환을 수행하는 더 좋은 방법이 있습니까?
select json_tmp.id as dbid,
json_tmp.c:id as json_id,
a.value:value a,
b.value:value b
from
json_tmp,
lateral flatten(input => json_tmp.c, path => 'custom_vars') a,
lateral flatten(input => json_tmp.c, path => 'custom_vars') b
where a.value:key = 'a' and b.value:key = 'b'
;
차라리 하위 쿼리보다는 조인을 필터링 할 것, 그래서 나는 아직도 다른 답변을보고에 관심이 있어요 :
이것은 매우 도움이됩니다. 저는 UDF를 자바 스크립트로 작성할 수 있다는 것을 알지 못했습니다. 나는 그것이 나를위한 최선의 해결책이라고 생각한다. 배열에는 5 개의 맞춤 변수가 있습니다. – 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) : , my_func (C) : b 한 번만 호출하면됩니다. –