Hive子句“和//或”操作顺序

时间:2018-07-25 19:38:58

标签: hive hiveql where-clause

我正在使用Hadoop 2.7.3。当您运行hiveql命令并在其中包含带有“和”和“或”的where子句时,它如何分配条件?

例如 说我有以下查询:

... where A and B or C and D.

它是否返回以下之一:

A or B or C or D
((A and B) or C) and D
(A and B and D) or C
A and (B or C) and D

我知道我可以使用括号来确切指定使用了以上哪个,但是默认情况下它会做什么?

2 个答案:

答案 0 :(得分:3)

这是操作的优先级。 AND的绑定比OR更紧密,因此:

A and B or C and D

被解释为:

(A and B) or (C and D)

答案 1 :(得分:1)

@GordonLinoff的回答是正确的。您可以通过使用以下查询构造真值表来验证这一点:

SELECT *, A and B or C and D AS result
FROM
  (SELECT TRUE AS a
   UNION ALL SELECT FALSE AS a) A
CROSS JOIN
  (SELECT TRUE AS b
   UNION ALL SELECT FALSE AS b) B
CROSS JOIN
  (SELECT TRUE AS c
   UNION ALL SELECT FALSE AS c) C
CROSS JOIN
  (SELECT TRUE AS d
   UNION ALL SELECT FALSE AS d) D

哪个输出:

+-----+-----+-----+-----+-------+
|  a.a|  b.b|  c.c|  d.d| result|
+-----+-----+-----+-----+-------+
| true| true| true|false|   true|
| true| true| true| true|   true|
| true| true|false|false|   true|
| true| true|false| true|   true|
|false| true| true|false|  false|
|false| true| true| true|   true|
|false| true|false|false|  false|
|false| true|false| true|  false|
| true|false| true|false|  false|
| true|false| true| true|   true|
| true|false|false|false|  false|
| true|false|false| true|  false|
|false|false| true|false|  false|
|false|false| true| true|   true|
|false|false|false|false|  false|
|false|false|false| true|  false|
+-----+-----+-----+-----+-------+

因此,我们可以凭经验得出结论,这确实等于(A and B) or (C and D)

相关问题