Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Add unnest to SQL Reference #10839

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,7 @@ to_unixtime(expression[, ..., format_n])
- [string_to_array](#string_to_array)
- [string_to_list](#string_to_list)
- [trim_array](#trim_array)
- [unnest](#unnest)
- [range](#range)

### `array_append`
Expand Down Expand Up @@ -3346,6 +3347,30 @@ trim_array(array, n)
Can be a constant, column, or function, and any combination of array operators.
- **n**: Element to trim the array.

### `unnest`

Transforms an array into rows.

#### Arguments

- **array**: Array expression to unnest.
Can be a constant, column, or function, and any combination of array operators.

#### Example

```
> select unnest(make_array(1, 2, 3, 4, 5));
+------------------------------------------------------------------+
| unnest(make_array(Int64(1),Int64(2),Int64(3),Int64(4),Int64(5))) |
+------------------------------------------------------------------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------------------------------------------------------------------+
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnest also works for structs. Can you possibly add this example too?

> create table foo as values (named_struct('a', 5, 'b', 'a string'));
0 row(s) fetched.
Elapsed 0.008 seconds.

> select * from foo;
+---------------------+
| column1             |
+---------------------+
| {a: 5, b: a string} |
+---------------------+
1 row(s) fetched.
Elapsed 0.003 seconds.

> select unnest(column1) from foo;
+-----------------------+-----------------------+
| unnest(foo.column1).a | unnest(foo.column1).b |
+-----------------------+-----------------------+
| 5                     | a string              |
+-----------------------+-----------------------+
1 row(s) fetched.
Elapsed 0.003 seconds.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could merge this PR as is, as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure. This should be in Struct Functions right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also love to add a range example

select unnest(range(0, 100000))

Copy link
Contributor

@alamb alamb Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure. This should be in Struct Functions right?

I think that would make sense.

FWIW here is an example:

DataFusion CLI v39.0.0
> select unnest(range(0, 10));
+-----------------------------------+
| unnest(range(Int64(0),Int64(10))) |
+-----------------------------------+
| 0                                 |
| 1                                 |
| 2                                 |
| 3                                 |
| 4                                 |
| 5                                 |
| 6                                 |
| 7                                 |
| 8                                 |
| 9                                 |
+-----------------------------------+
10 row(s) fetched.
Elapsed 0.054 seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added docs for struct unnest and example this range for array.
Couldn't link to it because of the name.
Is there a way to assign custom id to a block with sphinx? Tried labels, didn't work, so named it unnest (struct).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to assign custom id to a block with sphinx? Tried labels, didn't work, so named it unnest (struct).

I am not sure to be honest

### `range`

Returns an Arrow array between start and stop with step. `SELECT range(2, 10, 3) -> [2, 5, 8]` or `SELECT range(DATE '1992-09-01', DATE '1993-03-01', INTERVAL '1' MONTH);`
Expand Down