Skip to content
Merged
Changes from all commits
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
28 changes: 17 additions & 11 deletions docs/stable/sql/expressions/star.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,11 @@ SELECT COLUMNS(['id', 'num']) FROM numbers;
| 2 | 20 |
| 3 | NULL |

## `*COLUMNS` Unpacked Columns
## Unpacking a `COLUMNS` Expression

The `*COLUMNS` clause is a variation of `COLUMNS`, which supports all of the previously mentioned capabilities.
The difference is in how the expression expands.
By wrapping a `COLUMN` expression in `UNPACK`, the columns expand into a parent expression, much like the [iterable unpacking behavior in Python](https://peps.python.org/pep-3132/).

`*COLUMNS` will expand in-place, much like the [iterable unpacking behavior in Python](https://peps.python.org/pep-3132/), which inspired the `*` syntax.
This implies that the expression expands into the parent expression.
An example that shows this difference between `COLUMNS` and `*COLUMNS`:

With `COLUMNS`:
Without `UNPACK`, operations on the `COLUMNS` expression are applied to each column separately:

```sql
SELECT coalesce(COLUMNS(['a', 'b', 'c'])) AS result
Expand All @@ -244,18 +239,18 @@ FROM (SELECT NULL a, 42 b, true c);
|--------|-------:|-------:|
| NULL | 42 | true |

With `*COLUMNS`, the expression expands in its parent expression `coalesce`, resulting in a single result column:
With `UNPACK`, the `COLUMNS` expression is expanded into its parent expression, `coalesce` in the example above, which results in a single column:

```sql
SELECT coalesce(*COLUMNS(['a', 'b', 'c'])) AS result
SELECT coalesce(UNPACK(COLUMNS(['a', 'b', 'c']))) AS result
FROM (SELECT NULL AS a, 42 AS b, true AS c);
```

| result |
|-------:|
| 42 |

`*COLUMNS` also works with the `(*)` argument:
The `UNPACK` keyword may be replaced by `*`, [matching Python syntax](https://peps.python.org/pep-3132/), when it is applied directly to the `COLUMNS` expression without any intermediate operations.

```sql
SELECT coalesce(*COLUMNS(*)) AS result
Expand All @@ -266,6 +261,17 @@ FROM (SELECT NULL a, 42 AS b, true AS c);
|-------:|
| 42 |

> Warning In the following example, replacing `UNPACK` by `*` results in a syntax error:
>
> ```sql
> SELECT greatest(UNPACK(COLUMNS(*) + 1)) AS result
> FROM (SELECT 1 AS a, 2 AS b, 3 AS c);
> ```
>
> | result |
> |-------:|
> | 4 |

## `STRUCT.*`

The `*` expression can also be used to retrieve all keys from a struct as separate columns.
Expand Down