使用 Between 运算符的 Hive 不等式连接

问题描述 投票:0回答:2

我们有一个类似于以下的查询:(partition_date 是我们的表分区)

SELECT * FROM A
JOIN B 
where partition_date > B.last_runtime;

我们意识到,将条件放在

where
子句中会导致全表扫描,因此我们需要将其作为
JOIN
放在
ON
中。

问题在于 Hive 不支持不等式连接,因此正在考虑使用

BETWEEN
运算符,如下所示:

Select * from A
JOIN B ON par_date between B.last_runtime and '99999999';

这给了我们错误:左右别名都遇到了 加入“99999999”

如果我将 B.last_runtime 替换为实际值,请说“20160310”,它可以正常工作...

有什么想法吗?预先感谢

sql hadoop join hive
2个回答
0
投票

A BETWEEN B AND C
翻译为 A 大于或等于 B 并且 A 小于或等于 C,所以我认为它仍然是非等值连接。

但是,我无法解释错误消息的含义。如果你想分析源代码,它会被抛出here

private static boolean hasTableAlias(JoinTypeCheckCtx ctx, String tabName, ASTNode expr)
    throws SemanticException {
  int tblAliasCnt = 0;
  for (RowResolver rr : ctx.getInputRRList()) {
    if (rr.hasTableAlias(tabName))
      tblAliasCnt++;
  }

  if (tblAliasCnt > 1) {
    throw new SemanticException(ErrorMsg.INVALID_JOIN_CONDITION_1.getMsg(expr));
  }

  return (tblAliasCnt == 1) ? true : false;
}

0
投票

Hive 在连接条件下不支持任何类似

>
<
<=
>=
的操作。可能是
left
right
加入。这是一个例子:

select A.Name, A.Address, B.salary from Person_details as A left join Person_earnings as B on (B.salary > 15000) 

相反

select A.Name, A.Address, B.salary from Person_details as A left join Person_earnings as B on (A.Id=B.Id) where B.salary > 15000

首先应该进行相等运算,然后可以应用其他条件。由于 Hive 用于大型数据集,因此它仅首先支持相等条件。

© www.soinside.com 2019 - 2024. All rights reserved.