Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

check if all elements in hive array contain a string pattern

avatar

Hello hortonworks family!

I have two columns in a hive table that look something like this:

codecodeset
ABAB123,MU124
LMLM123,LM234

I need to verify that all elements in codeset column contain the value in code column so in the above example the first row would be false and the second row would be true.

Is there a simple way to do this that I am missing? I already read about array_contains but that returns true if just one element matches, I need all elements to contain what's in the code column.

Thanks in advance.

1 REPLY 1

avatar
New Contributor

Not very clean - but you can try something like this -


with src as (select 'AB' as code, ARRAY('AB123','MU124') as codeset
      union all
      select 'LM' as code, ARRAY('LM123','LM234') as codeset)
select a.code, CASE WHEN e.c is null THEN False ELSE TRUE END as condition
from (select code, size(codeset) c from src) as a left join
  (select code, count(*) c
  from (select code, explode(codeset) as val from src)
  where instr(val, code) > 0
  group by code) e on (a.code=e.code and a.c=e.c)