Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
{
"title": "MAP_ALL",
"language": "en",
"description": "Tests whether every entry in a map satisfies a condition"
}
---

## Description

Tests every entry in a map with a lambda expression and returns whether all entries satisfy the specified condition. The lambda receives the key and value of each entry.

## Syntax

```sql
MAP_ALL((<key>, <value>) -> <predicate>, <map>)
```

## Parameters

| Parameter | Description |
| --- | --- |
| `<key>` | The lambda parameter representing the key of a map entry. |
| `<value>` | The lambda parameter representing the value of a map entry. |
| `<predicate>` | A Boolean expression evaluated for each map entry. |
| `<map>` | A `MAP<K, V>` expression. |

## Return Value

Returns a `BOOLEAN` value.

- Returns `FALSE` if the predicate returns `FALSE` for any entry.
- Returns `TRUE` if the predicate returns `TRUE` for every entry, including when the map is empty.
- Returns `NULL` if no entry returns `FALSE` and at least one entry returns `NULL`.
- If `<map>` is `NULL`, returns `NULL`.

## Examples

```sql
SELECT map_all((k, v) -> v >= 10, map(1, 10, 2, 20)) AS result;
```

```text
+--------+
| result |
+--------+
| 1 |
+--------+
```

```sql
SELECT map_all((k, v) -> v > 10, map(1, 10, 2, 20)) AS result;
```

```text
+--------+
| result |
+--------+
| 0 |
+--------+
```

```sql
SELECT map_all((k, v) -> CAST(NULL AS BOOLEAN), map(1, 10)) AS result;
```

```text
+--------+
| result |
+--------+
| NULL |
+--------+
```

```sql
SELECT map_all((k, v) -> v > 0, CAST(NULL AS MAP<INT, INT>)) AS result;
```

```text
+--------+
| result |
+--------+
| NULL |
+--------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
{
"title": "MAP_APPLY",
"language": "en",
"description": "Transforms the keys and values of a map"
}
---

## Description

Applies a lambda expression to every entry in a map and constructs a new map from the transformed keys and values. The lambda receives the key and value of each entry and returns a pair of expressions. The first expression is used as the new key and the second expression as the new value.

## Syntax

```sql
MAP_APPLY((<key>, <value>) -> (<new_key>, <new_value>), <map>)
```

## Parameters

| Parameter | Description |
| --- | --- |
| `<key>` | The lambda parameter representing the key of a map entry. |
| `<value>` | The lambda parameter representing the value of a map entry. |
| `<new_key>` | An expression that produces the key in the returned map. Its type must be supported by MAP keys. |
| `<new_value>` | An expression that produces the value in the returned map. |
| `<map>` | A `MAP<K, V>` expression. |

## Return Value

Returns a `MAP<K2, V2>`, where `K2` and `V2` are the types of the first and second fields returned by the lambda.

- If `<map>` is empty, returns an empty map.
- If `<map>` is `NULL`, returns `NULL`.
- `NULL` fields returned by the lambda are retained as `NULL` keys or values.
- If multiple entries are transformed to the same key, the value from the last occurrence is retained.
- If the lambda does not return exactly two expressions, an analysis error is reported.

## Examples

```sql
SELECT map_apply((k, v) -> (k + 10, v * 2), map(1, 10, 2, 20)) AS result;
```

```text
+------------------+
| result |
+------------------+
| {11:20, 12:40} |
+------------------+
```

```sql
SELECT map_apply((k, v) -> (1, v), map(1, 10, 2, 20)) AS result;
```

```text
+--------+
| result |
+--------+
| {1:20} |
+--------+
```

```sql
SELECT map_apply((k, v) -> (k, v), CAST(NULL AS MAP<INT, INT>)) AS result;
```

```text
+--------+
| result |
+--------+
| NULL |
+--------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
{
"title": "MAP_EXISTS",
"language": "en",
"description": "Tests whether any entry in a map satisfies a condition"
}
---

## Description

Tests every entry in a map with a lambda expression and returns whether at least one entry satisfies the specified condition. The lambda receives the key and value of each entry.

## Syntax

```sql
MAP_EXISTS((<key>, <value>) -> <predicate>, <map>)
```

## Parameters

| Parameter | Description |
| --- | --- |
| `<key>` | The lambda parameter representing the key of a map entry. |
| `<value>` | The lambda parameter representing the value of a map entry. |
| `<predicate>` | A Boolean expression evaluated for each map entry. |
| `<map>` | A `MAP<K, V>` expression. |

## Return Value

Returns a `BOOLEAN` value.

- Returns `TRUE` if the predicate returns `TRUE` for any entry.
- Returns `FALSE` if the predicate returns `FALSE` for every entry, including when the map is empty.
- Returns `NULL` if no entry returns `TRUE` and at least one entry returns `NULL`.
- If `<map>` is `NULL`, returns `NULL`.

## Examples

```sql
SELECT map_exists((k, v) -> v = 20, map(1, 10, 2, 20)) AS result;
```

```text
+--------+
| result |
+--------+
| 1 |
+--------+
```

```sql
SELECT map_exists((k, v) -> v > 20, map(1, 10, 2, 20)) AS result;
```

```text
+--------+
| result |
+--------+
| 0 |
+--------+
```

```sql
SELECT map_exists((k, v) -> CAST(NULL AS BOOLEAN), map(1, 10)) AS result;
```

```text
+--------+
| result |
+--------+
| NULL |
+--------+
```

```sql
SELECT map_exists((k, v) -> v > 0, CAST(NULL AS MAP<INT, INT>)) AS result;
```

```text
+--------+
| result |
+--------+
| NULL |
+--------+
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
{
"title": "MAP_FILTER",
"language": "en",
"description": "Filters map entries using a lambda expression or a Boolean array"
}
---

## Description

Filters entries in a map and returns a map containing only the selected entries.

The lambda form evaluates a Boolean expression for every entry. The lambda receives the key and value of each entry. The Boolean-array form selects entries by their corresponding positions.

## Syntax

```sql
MAP_FILTER((<key>, <value>) -> <predicate>, <map>)
MAP_FILTER(<map>, <filter_array>)
```

## Parameters

| Parameter | Description |
| --- | --- |
| `<key>` | The lambda parameter representing the key of a map entry. |
| `<value>` | The lambda parameter representing the value of a map entry. |
| `<predicate>` | A Boolean expression evaluated for each map entry. Entries for which it returns `TRUE` are retained. |
| `<map>` | A `MAP<K, V>` expression. |
| `<filter_array>` | An `ARRAY<BOOLEAN>` expression. Each element selects the map entry at the same position. The array length must equal the number of entries in the map. |

## Return Value

Returns a `MAP<K, V>` containing the selected entries.

- In the lambda form, entries for which the predicate returns `FALSE` or `NULL` are removed.
- In the Boolean-array form, entries corresponding to `FALSE` or `NULL` elements are removed.
- If `<map>` is empty, returns an empty map.
- If `<map>` is `NULL`, returns `NULL`.
- In the Boolean-array form, if `<filter_array>` is `NULL`, returns `NULL`.

## Examples

```sql
SELECT map_filter((k, v) -> v >= 20, map(1, 10, 2, 20, 3, 30)) AS result;
```

```text
+--------------+
| result |
+--------------+
| {2:20, 3:30} |
+--------------+
```

```sql
SELECT map_filter(
(k, v) -> if(k = 1, CAST(NULL AS BOOLEAN), TRUE),
map(1, 10, 2, 20)) AS result;
```

```text
+--------+
| result |
+--------+
| {2:20} |
+--------+
```

```sql
SELECT map_filter(map(1, 10, 2, 20), [TRUE, FALSE]) AS result;
```

```text
+--------+
| result |
+--------+
| {1:10} |
+--------+
```

```sql
SELECT map_filter((k, v) -> TRUE, CAST(NULL AS MAP<INT, INT>)) AS result;
```

```text
+--------+
| result |
+--------+
| NULL |
+--------+
```
Loading
Loading