我有以下带有复杂数据类型STRUCT的配置单元表。您可以帮忙为特定城市的where子句编写蜂巢查询吗?
CREATE EXTERNAL TABLE user_t (
name STRING,
id BIGINT,
isFTE BOOLEAN,
role VARCHAR(64),
salary DECIMAL(8,2),
phones ARRAY<INT>,
deductions MAP<STRING, FLOAT>,
address ARRAY<STRUCT<street:STRING, city:STRING, state:STRING, zip:INT>>,
others UNIONTYPE<FLOAT,BOOLEAN,STRING>,
misc BINARY
)
Run Code Online (Sandbox Code Playgroud)
我可以在select子句中使用STRUCT数据类型,但不能在where子句中使用相同的数据类型。
工作方式:
select address.city from user_t;
Run Code Online (Sandbox Code Playgroud)
无法运作:
select address.city from user_t where address.city = 'XYZ'
Run Code Online (Sandbox Code Playgroud)
文档说它在使用group by或where子句时有局限性,并且也给出了解决方案。但是我不清楚。
链接:文档
请提出建议。谢谢。
create table user_t
(
id bigint
,address array<struct<street:string, city:string, state:string, zip:int>>
)
;
insert into user_t
select 1
,array
(
named_struct('street','street_1','city','city_1','state','state_1','zip',11111)
,named_struct('street','street_2','city','city_1','state','state_1','zip',11111)
,named_struct('street','street_3','city','city_3','state','state_3','zip',33333)
)
union all
select 2
,array
(
named_struct('street','street_4','city','city_4','state','state_4','zip',44444)
,named_struct('street','street_5','city','city_5','state','state_5','zip',55555)
)
;
Run Code Online (Sandbox Code Playgroud)
select u.id
,a.*
from user_t as u
lateral view explode(address) a as details
where details.city = 'city_1'
;
Run Code Online (Sandbox Code Playgroud)
+----+---------------------------------------------------------------------+
| id | details |
+----+---------------------------------------------------------------------+
| 1 | {"street":"street_1","city":"city_1","state":"state_1","zip":11111} |
| 1 | {"street":"street_2","city":"city_1","state":"state_1","zip":11111} |
+----+---------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
select u.id
,a.*
from user_t as u
lateral view inline(address) a
where a.city = 'city_1'
;
Run Code Online (Sandbox Code Playgroud)
+----+----------+--------+---------+-------+
| id | street | city | state | zip |
+----+----------+--------+---------+-------+
| 1 | street_1 | city_1 | state_1 | 11111 |
| 1 | street_2 | city_1 | state_1 | 11111 |
+----+----------+--------+---------+-------+
Run Code Online (Sandbox Code Playgroud)
select u.*
from user_t as u
join (select distinct
u.id
from user_t as u
lateral view inline(address) a
where a.city = 'city_1'
) as u2
on u2.id = u.id
;
Run Code Online (Sandbox Code Playgroud)
+----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | address |
+----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | [{"street":"street_1","city":"city_1","state":"state_1","zip":11111},{"street":"street_2","city":"city_1","state":"state_1","zip":11111},{"street":"street_3","city":"city_3","state":"state_3","zip":33333}] |
+----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3275 次 |
| 最近记录: |