我可以在 Hive 中执行 LEFT JOIN LATERAL 吗?

Can I do a LEFT JOIN LATERAL in Hive?

我想在 Hive 中进行横向连接。 有什么办法可以支持这个吗?本质上,我想使用 LHS 行中的值作为 RHS 上任意 SQL 的参数。

这是一个来自 Postgres 的例子:(请原谅我粗略的例子):

create table lhs (
    subject_id integer,
    date_time  BIGINT );

create table events (
    subject_id  integer,
    date_time   BIGINT,
    event_val   integer );

SELECT * from lhs LEFT JOIN LATERAL ( select SUM(event_val) as val_sum, count(event_val) as ecnt from events WHERE date_time < lhs.date_time and subject_id = lhs.subject_id ) rhs1 ON true;

Hive 不支持 LEFT JOIN LATERAL,使用下面的查询,这等同于您的 query.I 已经用示例数据测试过,它产生相同的结果。

select subject_id,date_time,SUM(event_val) as val_sum,COUNT(event_val) as ecnt 
from (SELECT a.subject_id as subject_id ,
      a.date_time as date_time, b.date_time as bdate , b.event_val as event_val
      FROM events b LEFT OUTER JOIN lhs a 
      ON b.subject_id = a.subject_id) abc 
where bdate < date_time group by subject_id,date_time;

希望我能帮助您制定如何在 hive 中实现相同的目标。