前言
临时结果集(会把查询的表数据放到内存中,供其他查询随时使用),该子句紧跟在SELECT或INSERT关键字之前。CTE仅在单个语句的执行范围内定义。可以在Hive SELECT,INSERT, CREATE TABLE AS SELECT或CREATE VIEW AS SELECT语句中使用一个或多个CTE 。
使用HIVE-1180在Hive 0.13.0中添加了通用表格表达式。
语法
withClause: cteClause (, cteClause)*
cteClause: cte_name AS (select statment)
- 注意
SubQuery Blocks中不支持WITH子句
Views,CTAS和INSERT语句支持CTE。
不支持递归查询。
举例
1、在 select 中使用 CTE
with q1 as ( select key from src where key = '5')
select *
from q1;
-- from style
with q1 as (select * from src where key= '5')
from q1
select *;
-- chaining CTEs
with q1 as ( select key from q2 where key = '5'),
q2 as ( select key from src where key = '5')
select * from (select key from q1) a;
-- union example
with q1 as (select * from src where key= '5'),
q2 as (select * from src s2 where key = '4')
select * from q1 union all select * from q2;
2、CTE in Views, CTAS, and Insert Statements
-- insert example
create table s1;
with q1 as ( select key, value from src where key = '5')
from q1
insert overwrite table s1
select *;
-- insert
create table tb_h_test_insert;
WITH TABLE1 AS
(
SELECT
cod_index,
CAST(test_1 AS VARCHAR(200)),
CAST(test_2 AS VARCHAR(200)),
CAST(test_3 AS VARCHAR(200))
FROM db_h_gss.tb_h_test_orig
)
INSERT INTO TABLE tb_h_test_insert PARTITION (cod_index = 1)
SELECT
test_1,
test_2,
test_3
FROM TABLE1 WHERE cod_index = 1;
-- ctas example
create table s2 as
with q1 as ( select key from src where key = '4')
select * from q1;
-- view example
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
select * from v1;
-- view example, name collision
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
with q1 as ( select key from src where key = '4')
select * from v1;
在第二个View示例中,查询的CTE与创建视图时使用的CTE不同。结果将包含key ='5'的行,因为在视图的查询语句中,视图定义中定义的CTE生效。
另见JIRA:
- HIV-1180 支持Hive中的公用表表达式(CTE)
参考: