0

I have a column features in a table with the follwing structure

 |-- features: struct (nullable = true)
 |    |-- tectonFeatures: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- featureName: string (nullable = true)
 |    |    |    |-- results: array (nullable = true)
 |    |    |    |    |-- element: string (containsNull = true)

How do I write a SQL query that gets me any row where featureName = 'a' and results is not empty ?

1 Answer 1

2

With this schema:

root
 |-- features: struct (nullable = true)
 |    |-- tectonFeatures: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- featureName: string (nullable = true)
 |    |    |    |-- results: array (nullable = true)
 |    |    |    |    |-- element: string (containsNull = true)

and these rows:

+--------------------+
|features            |
+--------------------+
|{[{a, [Something]}]}|
|{[{b, []}]}         |
+--------------------+

this SQL statement keeps any row that contains at least one pair of featureName = a and size(results) > 0:

select * from test where size(filter(features.tectonFeatures, x -> x.featureName = 'a' and size(x.results) > 0)) > 0

Final result:

+--------------------+
|features            |
+--------------------+
|{[{a, [Something]}]}|
+--------------------+

Good luck!

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

Comments

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.