Skip to content

Commit 3f3222a

Browse files
committed
[jep-018] Grouping.
1 parent 1d9ff9f commit 3f3222a

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

jep-018-grouping.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# group_by function
2+
3+
|||
4+
|---|---
5+
| **JEP** | 18
6+
| **Author** | Maxime Labelle
7+
| **Created**| 05-August-2022
8+
| **SemVer** | MINOR
9+
| **Status**| Draft
10+
11+
## Abstract
12+
13+
This JEP introduces a new `group_by()` function.
14+
15+
## Specification
16+
17+
### group_by
18+
19+
```
20+
object group_by(array[object] $elements, expression->expression->string) $expr)
21+
```
22+
23+
Groups an array of objects `$elements` using an expression `$expr` as the group key.
24+
The `$expr` expression is applied to each element in the array `$elements` and the
25+
resulting value is used as a group key.
26+
27+
The result is an object whose keys are the unique set of string keys and whose respective values are an array of objects array matching the group criteria.
28+
29+
Objects that do not match the group criteria are discarded from the output.
30+
This includes objects for which applying the `$expr` expression evaluates to `null`.
31+
32+
If the result of applying the `$expr` expression against the current array element
33+
results in type other than `string` or `null`, a type error MUST be raised.
34+
35+
### Examples
36+
37+
Given the following input JSON document:
38+
39+
```json
40+
{
41+
"items": [
42+
{ "spec": { "nodeName": "node_01", "other": "values_01" } },
43+
{ "spec": { "nodeName": "node_02", "other": "values_02" } },
44+
{ "spec": { "nodeName": "node_03", "other": "values_03" } },
45+
{ "spec": { "nodeName": "node_01", "other": "values_04" } }
46+
]
47+
}
48+
```
49+
50+
Example:
51+
52+
|Expression|Result
53+
|---|---
54+
|`` group_by(items, &spec.nodeName) ``| ` {"node_01": [{"spec": {"nodeName": "node_01", "other": "values_01"}}, {"spec": {"nodeName": "node_01", "other": "values_04"}}], "node_02": [{"spec": {"nodeName": "node_02", "other": "values_02"}}], "node_03": [{"spec": {"nodeName": "node_03", "other": "values_03"}}]} `
55+
56+
Given the following input JSON document:
57+
58+
```json
59+
{
60+
"array": [
61+
{ "name": "one", "b": true },
62+
{ "name": "two", "b": false },
63+
{ "b": false }
64+
]
65+
}
66+
```
67+
68+
Here are a couple of other examples:
69+
70+
|Expression|Result
71+
|---|---
72+
|`` group_by(array, &name) `` | ` {"one":[{"name":"one","b":true}],"two":[{"name":"two","b":false}]} `
73+
|`` group_by(array, &b) `` | `invalid-type`

0 commit comments

Comments
 (0)