如果X使用Hive为空,如何在表X上执行INSERT OVERWRITE?

时间:2018-10-22 15:05:19

标签: hive hiveql cloudera

如果X为空,我正在尝试将一些值插入到具有基于列cx的分区的表X中。我正在尝试使用这样的查询:

INSERT OVERWRITE TABLE X PARITION(cx) 
SELECT DISTINCT ... 
WHERE ... OR NOT EXISTS(SELECT cx2 FROM X LIMIT 1)

但是运行该代码时出现以下错误:

Error while compiling statement: FAILED: SemanticException [Error 10249]:
line 18:131 Unsupported SubQuery Expression '1': Only SubQuery expressions
that are top level conjuncts are allowed

是否可以使用Hive的SQL获得相同的预期行为?

1 个答案:

答案 0 :(得分:0)

蜂巢中的

EXISTS应该相关。像这样

select * from a
where EXISTS (select 1 from x where x.col=a.col) --correlated condition

使用联接可以实现的目的(假设联接键没有重复):

--checking NOT EXISTS
select * from a
         left join X on x.col=a.col  --same condition like in correlated EXISTS  query
where x.col is null --not exists

--checking EXISTS can be done using INNER JOIN:
select * from a 
         inner join x on x.col=a.col --same condition like in correlated EXISTS query

--checking EXISTS can be done using LEFT SEMI JOIN:
select * from a 
         left semi join x on x.col=a.col

--Insert into table X only if partition is empty (JOIN):
insert overwrite table X partition(p)
select col1, col2, p--partition col
  from Y 
        left join (select distinct p from X) x1 on y.p=x1.p
 where x1.p is null

--Insert into table X only if partition is empty (EXISTS):
insert overwrite table X partition(p)
select col1, col2, p--partition col
  from Y 
        WHERE NOT EXISTS (select 1 from X x1 where x1.p=Y.p)