question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Support "unnesting" Array columns in the select clause

See original GitHub issue

Use case: Aggregate queries on Array columns

Feature description: We need to be able to run aggregations on array columns. For example, if we have a table which looks like this…

+----+---------------+
| id | tags          |
+----+---------------+
| 1  | ["abc", "def"]|
| 2  | ["abc", "xyz"]|
+----+---------------+

…we need to be able to find counts for the unique values found in the tags column. For example (using UNNEST as a placeholder for this functionality):

select count(id) as tag_count, UNNEST(tags) as tag
from my_table group by tag order by tag_count desc;

Which would produce:

+----+---------------+
| tag_count | tag    |
+----+---------------+
| 2  | "abc"         |
| 1  | "def"         |
| 1  | "xyz"         |
+----+---------------+

Other products typically provide an UNNEST function which turns an array column into a set of rows so that aggregates over the array column will work. For example:

https://www.postgresql.org/docs/9.2/static/functions-array.html https://www.mapd.com/docs/latest/mapd-core-guide/dml/#array-support https://docs.snowflake.net/manuals/user-guide/querying-semistructured.html#using-the-flatten-function-to-parse-arrays

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mfusseneggercommented, Oct 2, 2018

Would something like the following work? If not, why?

Currently it doesn’t, but you’re right that we could make it work. We’ll have a look at enabling that as well.

1reaction
mfusseneggercommented, Oct 1, 2018

We’ve just merged support for table functions in the select list (https://github.com/crate/crate/pull/7716). This will be available in 3.2 and can be used for your use case:

create table t (id int primary key, tags array(string));
insert into t (id, tags) values (1, ['abc', 'def']);
insert into t (id, tags) values (2, ['abc', 'xyz']);

select count(*) as tag_count, tag from (
    select unnest(tags) as tag from t
) tt group by tag order by 1 desc;
+-----------+-----+
| tag_count | tag |
+-----------+-----+
|         2 | abc |
|         1 | xyz |
|         1 | def |
+-----------+-----+
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use SELECT FROM UNNEST to analyze multiple ...
Next, I'm going to ask BigQuery to SELECT the value.int_value column from our UNNEST ed event_params array, where the key of the event ......
Read more >
Work with arrays | BigQuery - Google Cloud
The query itself contains a subquery. This subquery selects each row in the some_numbers column and uses UNNEST to return the array as...
Read more >
Unnest Operator Performance Enhancement with Dictionary ...
Consider the following CROSS JOIN UNNEST query on a table with one VARCHAR type and one ARRAY(VARCHAR) type columns. SELECT T.name, U.
Read more >
Zipping unnest on arrays from views - Cloudera Documentation
You can use UNNEST() on array columns in two ways. Using one of these two ways results in the items of the arrays...
Read more >
Re: UNNEST() operation for arrays-Apache Mail Archives
The whole idea of having multiple UNNEST() functions in the select list (and zipping the results) came from a user as a direct...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found