Skip to content

Commit f158400

Browse files
Better explain STATS on multivalued fields (elastic#135109) (elastic#135704)
Updates the docs for multivalued fields to make it clear that grouping puts the entire *row* in each group. Relates to elastic#134792 Co-authored-by: Craig Taverner <[email protected]>
1 parent a17d9a2 commit f158400

File tree

6 files changed

+127
-38
lines changed

6 files changed

+127
-38
lines changed
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
% This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it.
22

33
```esql
4-
ROW i=1, a=["a", "b"], b=[2, 3] | STATS MIN(i) BY a, b | SORT a ASC, b ASC
4+
ROW price = 10, color = ["blue", "pink", "yellow"], size = ["s", "m", "l"]
5+
| STATS SUM(price) BY color, size
56
```
67

7-
| MIN(i):integer | a:keyword | b:integer |
8+
| SUM(price):long | color:keyword | size:keyword |
89
| --- | --- | --- |
9-
| 1 | a | 2 |
10-
| 1 | a | 3 |
11-
| 1 | b | 2 |
12-
| 1 | b | 3 |
10+
| 10 | blue | l |
11+
| 10 | blue | m |
12+
| 10 | blue | s |
13+
| 10 | pink | l |
14+
| 10 | pink | m |
15+
| 10 | pink | s |
16+
| 10 | yellow | l |
17+
| 10 | yellow | m |
18+
| 10 | yellow | s |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
% This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it.
2+
3+
```esql
4+
ROW color = ["blue", "pink", "yellow"]
5+
| MV_EXPAND color
6+
| STATS VALUES(color) BY color
7+
```
8+
9+
| VALUES(color):keyword | color:keyword |
10+
| --- | --- |
11+
| blue | blue |
12+
| pink | pink |
13+
| yellow | yellow |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
% This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it.
2+
3+
```esql
4+
ROW color = ["blue", "pink", "yellow"]
5+
| STATS VALUES(color) BY color
6+
```
7+
8+
| VALUES(color):keyword | color:keyword |
9+
| --- | --- |
10+
| [blue, pink, yellow] | blue |
11+
| [blue, pink, yellow] | pink |
12+
| [blue, pink, yellow] | yellow |
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
% This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it.
22

33
```esql
4-
ROW i=1, a=["a", "b"] | STATS MIN(i) BY a | SORT a ASC
4+
ROW price = 10, color = ["blue", "pink", "yellow"]
5+
| STATS SUM(price) BY color
56
```
67

7-
| MIN(i):integer | a:keyword |
8+
| SUM(price):long | color:keyword |
89
| --- | --- |
9-
| 1 | a |
10-
| 1 | b |
10+
| 10 | blue |
11+
| 10 | pink |
12+
| 10 | yellow |

docs/reference/query-languages/esql/_snippets/commands/layout/stats-by.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,24 @@ It’s also possible to group by multiple values:
110110

111111
:::{include} ../examples/stats.csv-spec/statsGroupByMultipleValues.md
112112
:::
113+
113114
If all the grouping keys are multivalued then the input row is in all groups:
114115

115116
:::{include} ../examples/stats.csv-spec/multi-mv-group.md
116117
:::
117118

119+
The input **ROW** is in all groups. The entire row. All the values. Even group
120+
keys. That means that:
121+
122+
:::{include} ../examples/stats.csv-spec/mv-group-values.md
123+
:::
124+
125+
The `VALUES` function above sees the whole row - all of the values of the group
126+
key. If you want to send the group key to the function then `MV_EXPAND` first:
127+
128+
:::{include} ../examples/stats.csv-spec/mv-group-values-expand.md
129+
:::
130+
118131
Both the aggregating functions and the grouping expressions accept other
119132
functions. This is useful for using `STATS` on multivalue columns.
120133
For example, to calculate the average salary change, you can use `MV_AVG` to

x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,34 +2276,6 @@ M
22762276
null
22772277
;
22782278

2279-
docsStatsMvGroup
2280-
// tag::mv-group[]
2281-
ROW i=1, a=["a", "b"] | STATS MIN(i) BY a | SORT a ASC
2282-
// end::mv-group[]
2283-
;
2284-
2285-
// tag::mv-group-result[]
2286-
MIN(i):integer | a:keyword
2287-
1 | a
2288-
1 | b
2289-
// end::mv-group-result[]
2290-
;
2291-
2292-
docsStatsMultiMvGroup
2293-
// tag::multi-mv-group[]
2294-
ROW i=1, a=["a", "b"], b=[2, 3] | STATS MIN(i) BY a, b | SORT a ASC, b ASC
2295-
// end::multi-mv-group[]
2296-
;
2297-
2298-
// tag::multi-mv-group-result[]
2299-
MIN(i):integer | a:keyword | b:integer
2300-
1 | a | 2
2301-
1 | a | 3
2302-
1 | b | 2
2303-
1 | b | 3
2304-
// end::multi-mv-group-result[]
2305-
;
2306-
23072279
statsByConstant#[skip:-8.14.1,reason:implemented in 8.14]
23082280
from employees
23092281
| stats m = max(salary), a = round(avg(salary)) by 0
@@ -3301,3 +3273,74 @@ FROM employees
33013273
min1:integer | min2:integer | max1:integer | max2:integer
33023274
10011 | [10011, 10012] | 10079 | [10079, 10078]
33033275
;
3276+
3277+
sumRowMany
3278+
// tag::mv-group[]
3279+
ROW price = 10, color = ["blue", "pink", "yellow"]
3280+
| STATS SUM(price) BY color
3281+
// end::mv-group[]
3282+
| SORT color ASC
3283+
;
3284+
3285+
// tag::mv-group-result[]
3286+
SUM(price):long | color:keyword
3287+
10 | blue
3288+
10 | pink
3289+
10 | yellow
3290+
// end::mv-group-result[]
3291+
;
3292+
3293+
sumRowManyTwo
3294+
// tag::multi-mv-group[]
3295+
ROW price = 10, color = ["blue", "pink", "yellow"], size = ["s", "m", "l"]
3296+
| STATS SUM(price) BY color, size
3297+
// end::multi-mv-group[]
3298+
| SORT color ASC, size ASC
3299+
;
3300+
3301+
// tag::multi-mv-group-result[]
3302+
SUM(price):long | color:keyword | size:keyword
3303+
10 | blue | l
3304+
10 | blue | m
3305+
10 | blue | s
3306+
10 | pink | l
3307+
10 | pink | m
3308+
10 | pink | s
3309+
10 | yellow | l
3310+
10 | yellow | m
3311+
10 | yellow | s
3312+
// end::multi-mv-group-result[]
3313+
;
3314+
3315+
valuesRowMany
3316+
// tag::mv-group-values[]
3317+
ROW color = ["blue", "pink", "yellow"]
3318+
| STATS VALUES(color) BY color
3319+
// end::mv-group-values[]
3320+
| SORT color ASC
3321+
;
3322+
3323+
// tag::mv-group-values-result[]
3324+
VALUES(color):keyword | color:keyword
3325+
[blue, pink, yellow] | blue
3326+
[blue, pink, yellow] | pink
3327+
[blue, pink, yellow] | yellow
3328+
// end::mv-group-values-result[]
3329+
;
3330+
3331+
valuesRowManyExpand
3332+
// tag::mv-group-values-expand[]
3333+
ROW color = ["blue", "pink", "yellow"]
3334+
| MV_EXPAND color
3335+
| STATS VALUES(color) BY color
3336+
// end::mv-group-values-expand[]
3337+
| SORT color ASC
3338+
;
3339+
3340+
// tag::mv-group-values-expand-result[]
3341+
VALUES(color):keyword | color:keyword
3342+
blue | blue
3343+
pink | pink
3344+
yellow | yellow
3345+
// end::mv-group-values-expand-result[]
3346+
;

0 commit comments

Comments
 (0)