Skip to content

Commit a239306

Browse files
ioanatialeemthompo
andauthored
ES|QL: Add docs for FORK (#130314)
Co-authored-by: Liam Thompson <[email protected]>
1 parent d69a282 commit a239306

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
% This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it.
2+
3+
```esql
4+
FROM employees
5+
| FORK ( WHERE emp_no == 10001 )
6+
( WHERE emp_no == 10002 )
7+
| KEEP emp_no, _fork
8+
| SORT emp_no
9+
```
10+
11+
| emp_no:integer | _fork:keyword |
12+
| --- | --- |
13+
| 10001 | fork1 |
14+
| 10002 | fork2 |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
% This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it.
2+
3+
```esql
4+
FROM books METADATA _score
5+
| WHERE author:"Faulkner"
6+
| EVAL score = round(_score, 2)
7+
| FORK (SORT score DESC, author | LIMIT 5 | KEEP author, score)
8+
(STATS total = COUNT(*))
9+
| SORT _fork, score DESC, author
10+
```
11+
12+
| author:text | score:double | _fork:keyword | total:long |
13+
| --- | --- | --- | --- |
14+
| William Faulkner | 2.39 | fork1 | null |
15+
| William Faulkner | 2.39 | fork1 | null |
16+
| Colleen Faulkner | 1.59 | fork1 | null |
17+
| Danny Faulkner | 1.59 | fork1 | null |
18+
| Keith Faulkner | 1.59 | fork1 | null |
19+
| null | null | fork2 | 18 |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## `FORK` [esql-fork]
2+
3+
```yaml {applies_to}
4+
serverless: preview
5+
stack: preview 9.1.0
6+
```
7+
8+
The `FORK` processing command creates multiple execution branches to operate
9+
on the same input data and combines the results in a single output table.
10+
11+
**Syntax**
12+
13+
```esql
14+
FORK ( <processing_commands> ) ( <processing_commands> ) ... ( <processing_commands> )
15+
```
16+
17+
**Description**
18+
19+
The `FORK` processing command creates multiple execution branches to operate
20+
on the same input data and combines the results in a single output table. A discriminator column (`_fork`) is added to identify which branch each row came from.
21+
22+
**Branch identification:**
23+
- The `_fork` column identifies each branch with values like `fork1`, `fork2`, `fork3`
24+
- Values correspond to the order branches are defined
25+
- `fork1` always indicates the first branch
26+
27+
**Column handling:**
28+
- `FORK` branches can output different columns
29+
- Columns with the same name must have the same data type across all branches
30+
- Missing columns are filled with `null` values
31+
32+
**Row ordering:**
33+
- `FORK` preserves row order within each branch
34+
- Rows from different branches may be interleaved
35+
- Use `SORT _fork` to group results by branch
36+
37+
::::{note}
38+
`FORK` branches default to `LIMIT 1000` if no `LIMIT` is provided.
39+
::::
40+
41+
**Limitations**
42+
43+
- `FORK` supports at most 8 execution branches.
44+
- Using remote cluster references and `FORK` is not supported.
45+
- Using more than one `FORK` command in a query is not supported.
46+
47+
**Examples**
48+
49+
In the following example, each `FORK` branch returns one row.
50+
Notice how `FORK` adds a `_fork` column that indicates which row the branch originates from:
51+
52+
:::{include} ../examples/fork.csv-spec/simpleFork.md
53+
54+
The next example, returns total number of rows that match the query along with
55+
the top five rows sorted by score.
56+
57+
:::{include} ../examples/fork.csv-spec/simpleForkWithStats.md

docs/reference/query-languages/esql/_snippets/lists/processing-commands.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* [`ENRICH`](../../commands/processing-commands.md#esql-enrich)
55
* [`EVAL`](../../commands/processing-commands.md#esql-eval)
66
* [`GROK`](../../commands/processing-commands.md#esql-grok)
7+
* [preview] [`FORK`](../../commands/processing-commands.md#esql-fork)
78
* [`KEEP`](../../commands/processing-commands.md#esql-keep)
89
* [`LIMIT`](../../commands/processing-commands.md#esql-limit)
910
* [`LOOKUP JOIN`](../../commands/processing-commands.md#esql-lookup-join)

docs/reference/query-languages/esql/commands/processing-commands.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ mapped_pages:
3232
:::{include} ../_snippets/commands/layout/eval.md
3333
:::
3434

35+
:::{include} ../_snippets/commands/layout/fork.md
36+
:::
37+
3538
:::{include} ../_snippets/commands/layout/grok.md
3639
:::
3740

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,44 @@
55
simpleFork
66
required_capability: fork_v9
77

8+
// tag::simpleFork[]
89
FROM employees
910
| FORK ( WHERE emp_no == 10001 )
1011
( WHERE emp_no == 10002 )
1112
| KEEP emp_no, _fork
1213
| SORT emp_no
14+
// end::simpleFork[]
1315
;
1416

17+
// tag::simpleFork-result-[]
1518
emp_no:integer | _fork:keyword
1619
10001 | fork1
1720
10002 | fork2
21+
// end::simpleFork-result[]
22+
;
23+
24+
simpleForkWithStats
25+
required_capability: fork_v9
26+
27+
// tag::simpleForkWithStats[]
28+
FROM books METADATA _score
29+
| WHERE author:"Faulkner"
30+
| EVAL score = round(_score, 2)
31+
| FORK (SORT score DESC, author | LIMIT 5 | KEEP author, score)
32+
(STATS total = COUNT(*))
33+
| SORT _fork, score DESC, author
34+
// end::simpleForkWithStats[]
35+
;
36+
37+
// tag::simpleForkWithStats-result[]
38+
author:text | score:double | _fork:keyword | total:long
39+
William Faulkner | 2.39 | fork1 | null
40+
William Faulkner | 2.39 | fork1 | null
41+
Colleen Faulkner | 1.59 | fork1 | null
42+
Danny Faulkner | 1.59 | fork1 | null
43+
Keith Faulkner | 1.59 | fork1 | null
44+
null | null | fork2 | 18
45+
// end::simpleForkWithStats-result[]
1846
;
1947

2048
forkWithWhereSortAndLimit

0 commit comments

Comments
 (0)