Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COALESCE with Hive SQL

Tags:

sql

hive

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?

like image 674
Parsa Avatar asked Nov 19 '12 20:11

Parsa


People also ask

What is the difference between NVL and Coalesce in Hive?

NVL and COALESCE are used to achieve the same functionality of providing a default value in case the column returns a NULL. The differences are: NVL accepts only 2 arguments whereas COALESCE can take multiple arguments. NVL evaluates both the arguments and COALESCE stops at first occurrence of a non-Null value.

Does NVL work in Hive?

The hive nvl function is one of the same functions. We can use the nvl function as the keyword in the hive query. It will update, we need to replace the null value in the table with the specific value.

How do you use coalesce in SQL?

The SQL COALESCE function can be syntactically represented using the CASE expression. For example, as we know, the Coalesce function returns the first non-NULL values. SELECT COALESCE (expression1, expression2, expression3) FROM TABLENAME; The above Coalesce SQL statement can be rewritten using the CASE statement.


7 Answers

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

COALESCE(column, 0L) 
like image 65
Ivan Klass Avatar answered Sep 20 '22 11:09

Ivan Klass


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

like image 32
Parsa Avatar answered Sep 16 '22 11:09

Parsa


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

like image 32
kanishka vatsa Avatar answered Sep 17 '22 11:09

kanishka vatsa


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

like image 24
Zorayr Avatar answered Sep 20 '22 11:09

Zorayr


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')))
like image 44
Amit_Hora Avatar answered Sep 19 '22 11:09

Amit_Hora


From Language DDL & UDF of Hive

NVL(value, default value) 

Returns default value if value is null else returns value

like image 33
staticor Avatar answered Sep 18 '22 11:09

staticor


nvl(value,defaultvalue) as Columnname

will set the missing values to defaultvalue specified

like image 33
Srikant Avatar answered Sep 17 '22 11:09

Srikant