Presto array contains an element that likes some pattern
Asked Answered
S

4

26

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?

Suez answered 8/6, 2018 at 19:0 Comment(1)
Please add sample data and expected outcomeOviduct
H
36

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
Hydrophyte answered 8/6, 2018 at 20:48 Comment(1)
You could reduce(myArray, false, (a, x -> a OR x like '%denied%')) to avoid copying an array for every row.Tribble
S
19

In newer versions of PrestoSQL (now known as Trino), you can use the any_match function:

WHERE any_match(column, e -> e like '%denied%')
Shoop answered 30/4, 2021 at 16:47 Comment(0)
B
1

See array operator docs here

contains(array_column,'denied')

Bethanybethe answered 20/1, 2020 at 20:57 Comment(1)
this does not answer the question. According to the doc, the CONTAINS operator only looks for exact matches.Cigar
C
0

We can use strpos(returns starting position of substring and 0 if not found) here. (documentation)

where strpos(array_column,'denied')>0
Clackmannan answered 29/8, 2022 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.