34

Since there is no IFNULL, ISNULL, or NVL function supported on Hive, I'm having trouble converting NULL to 0. I tried COALESCE(*column name*, 0) but received the below error message:

Argument type mismatch 0: The expressions after COALESCE should all have the same type: "bigint" is expected but "int" is found

How to resolve this?

Bugs
  • 4,491
  • 9
  • 32
  • 41
Parsa
  • 1,137
  • 1
  • 11
  • 15

7 Answers7

48

Hive supports bigint literal since 0.8 version. So, additional "L" is enough:

COALESCE(column, 0L)
Ivan Klass
  • 6,407
  • 3
  • 30
  • 28
26

As Lamak pointed out in the comment, COALESCE(column, CAST(0 AS BIGINT)) resolves the error.

Lamak
  • 69,480
  • 12
  • 108
  • 116
Parsa
  • 1,137
  • 1
  • 11
  • 15
6

Since 0.11 hive has a NVL function nvl(T value, T default_value)

which says Returns default value if value is null else returns value

sai_PB
  • 40
  • 1
  • 9
kanishka vatsa
  • 2,074
  • 18
  • 8
5

From [Hive Language Manual][1]:

COALESCE (T v1, T v2, ...)

Will return the first value that is not NULL, or NULL if all values's are NULL

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-ConditionalFunctions

Srikant
  • 406
  • 4
  • 11
Zorayr
  • 23,770
  • 8
  • 136
  • 129
4

If customer primary contact medium is email, if email is null then phonenumber, and if phonenumber is also null then address. It would be written using COALESCE as

coalesce(email,phonenumber,address) 

while the same in hive can be achieved by chaining together nvl as

nvl(email,nvl(phonenumber,nvl(address,'n/a')))
Amit_Hora
  • 716
  • 1
  • 8
  • 27
3

From Language DDL & UDF of Hive

NVL(value, default value) 

Returns default value if value is null else returns value

staticor
  • 620
  • 9
  • 19
2
nvl(value,defaultvalue) as Columnname

will set the missing values to defaultvalue specified

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Srikant
  • 406
  • 4
  • 11