Hive Where 子句 'And'/'Or' 操作顺序

Hive Where Clause 'And'/'Or' Order of Operations

我正在使用 Hadoop 2.7.3。当你 运行 一个 hiveql 命令并且有一个带有 'and' 和 'or' 的 where 子句时,它如何分配条件?

例如 假设我有以下查询:

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

是否return以下之一:

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

我知道我可以使用括号来具体说明使用了上面的哪一个,但默认情况下它是做什么的?

这是操作的优先级。 ANDOR 结合得更紧密,所以:

A and B or C and D

解释为:

(A and B) or (C and D)

@GordonLinoff 的回答是正确的。您可以使用以下查询构造事实 table 来验证这一点:

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)