Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the replacement of NULLIF in Hive?

I would like to know what is the replacement of NULLIF in Hive? I am using COALESCE but its not serving my requirement. My query statement is something like :

COALESCE(A,B,C) AS D

COALESCE will return first NOT NULL value. But my A/B/C contain blank values so COALESCE is not assigning that value to D as it is considering blank as NOT NULL. But I want the correct value to be get assign to D.

In SQL I could have use COALESCE(NULLIF(A,'')......) so it will check for blank as well. I tried CASE but it's not working.

like image 655
Shan Avatar asked Oct 09 '15 18:10

Shan


People also ask

How do I replace nulls with 0 in hive?

You can use coalesce() to replace null values with 0 s: select t1. *, coalesce(t2. Apple, 0) as apple, coalesce(t3.

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.

Can we use NVL in hive?

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. With the help of the nvl keyword, we can easily replace the null values from the hive table.

IS NULL function Hive SQL?

2.1 isnull( a ) This returns a true when the value of a (column) is NULL otherwise it returns false. Above example column _C1 is derived based on salary column, if you notice isnull() function return true for value NULL and false for non NULL values.


2 Answers

Just use case:

select (case when A is null or A = '' then . . . end)

This is standard SQL, so it will work in Hive and elsewhere.

For your particular problem:

select (case when A is not null and A <> '' then A
             when B is not null and B <> '' then B
             else C
        end)

You can actually shorten this to:

select (case when A <> '' then A
             when B <> '' then B
             else C
        end)

because NULL values fail comparisons. I would use this version but often people learning SQL prefer the more explicit version with the not null comparison.

like image 129
Gordon Linoff Avatar answered Sep 20 '22 10:09

Gordon Linoff


Another HiveQL specific option is here:

create temporary macro nullify(s string) if(s = '', null, s);
--
select coalesce(nullify(A), nullify(B), nullify(C)) as D ...
like image 27
GoodDok Avatar answered Sep 21 '22 10:09

GoodDok