Presto array contains an element that likes some pattern

daydayup picture daydayup · Jun 8, 2018 · Viewed 19.6k times · Source

For example, one column in my table is an array, I want to check if that column contains an element that contains substring "denied" (so elements like "denied at 12:00 pm", "denied by admin" will all count, I believe I will have to use "like" to identify the pattern). How to write sql for this?

Answer

Bohemian picture Bohemian · Jun 8, 2018

Use presto's array functions:

  • filter(), which returns elements that satisfy the given condition
  • cardinality(), which returns the size of an array:

Like this:

where cardinality(filter(myArray, x -> x like '%denied%')) > 0