vlemaistre
vlemaistre

Reputation: 3331

Equivalent of Presto's transform() function in hive

I can't seem to find an equivalent of Presto's transform() function in Hive.

TLDR: how do you apply a function to each element of an array in hive ?

More precisely I have an array containing zero or more structs. Each struct has the same schema and contains several variables including one is_done which is a boolean. What I want to do is to assess if there is at least one struct with the variable is_done as True in the array.

In Presto, we would use transform to extract the booleans then array_max to see if at least one is true:

array_max(transform(a.array_task, x -> x.is_done))

Upvotes: 2

Views: 1788

Answers (1)

leftjoin
leftjoin

Reputation: 38290

Unfortunately, Hive has no such functionality. Without using additional UDFs you can do it by exploding array and checking struct element like this:

with your_table as (select array(named_struct('is_done',true),named_struct('is_done',false)) initial_array) 

select t.initial_array, max(a.s.is_done) as is_done 
  from your_table t
       lateral view outer explode(initial_array) a as s --s is a struct
  group by  t.initial_array    
  ;

Result:

initial_array                           is_done 

[{"is_done":true},{"is_done":false}]    true

Upvotes: 1

Related Questions