简体   繁体   English

Hive case when 子句效率

[英]Hive case when clause efficiency

Is there difference between:有没有区别:

select
  case some_calculation() 
  when 'a' then 1
  when 'b' then 2
  else 0
  end

and

select
  case
  when some_calculation() ='a' then 1
  when some_calculation() ='b' then 2
  else 0
  end

I assume in the second version, the some_calculation() function will be evaluated twice.我假设在第二个版本中, some_calculation() function 将被评估两次。 I don't know how to verify that.我不知道如何验证。 Any input will be appreciated.任何输入将不胜感激。

This is not well-documented in Hive.这在 Hive 中没有详细记录。 But it is well understood in the standard and in other databases.但它在标准和其他数据库中得到了很好的理解。

In the second version, some_calculation() will definitely be evaluated multiple times.在第二个版本中, some_calculation()肯定会被计算多次。 A case expression is evaluated sequentially, so the second when is not evaluated until the first evaluates to false. case表达式是按顺序计算的,因此第二个when不会被计算,直到第一个计算结果为 false。

In the first version, the value should be evaluated once.在第一个版本中,值应该被评估一次。

You can get a flavor for the difference by using a volatile functions, such as random() .您可以通过使用 volatile 函数(例如random()来了解差异。 This db<>fiddle illustrates the difference between the two approaches. 这个 db<>fiddle说明了这两种方法之间的区别。 The first version never returns NULL .第一个版本永远不会返回NULL The second returns lots of them.第二个返回很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM