1

I'm just curious about filtering my query with a where statement in a table with array column

So for example, I have a column of username and usertype, then each username might have more than one type

So when I use

 select username, collect_set(usertype) as type from table group by username

Then I will have something like: user1,[1,2,3], user2,[3,4,5] and so on. The problem is when I want to filter the result using "where usertype = [3,4,5]". I am not sure how to construct an array to do the filtering and right now I have been using "where usertype[0] = 3 and usertype[1] = 4 " and so on. Anyone has any advise or idea on this matter?

Thanks

1 Answer 1

2

You can't compare non-primitive types using =.

One solution is to cast type to string so that the collection would be array<string>, then use concat_ws to compare to a string:

select * from table where concat_ws(' ', usertype) = "1 2 3";

Another option it to write a custom UDF that compares two array<int> arguments.

I found an implementation of several array UDFs, including arrayEquals here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.