Skip to content

Commit 93e54fb

Browse files
committed
Add docs for FORK
1 parent fe75f8d commit 93e54fb

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 runs multiple execution branches and outputs the
9+
results back into a single 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 feeds the input rows into multiple execution
20+
branches and outputs the results into a single table, enhanced with a
21+
discriminator column called `_fork`.
22+
23+
The values of the discriminator column `_fork` are `fork1`, `fork2`, ... and
24+
they designate which `FORK` branch the current row is coming from.
25+
The values of the `_fork` column always start with `fork1`, which indicates that
26+
the row is coming from the first branch.
27+
28+
The `FORK` branches can output different columns as long as there exists no
29+
column with the same name and different data types into two different `FORK`
30+
branches.
31+
32+
When a column does not exist in a `FORK` branch, but it exists in the output of
33+
other branches, `FORK` will add `null` values to the rows that have missing
34+
columns.
35+
36+
`FORK` preserves the order of the rows from each subset, but it does not
37+
guarantee that the rows will follow the same order in which the `FORK` branches
38+
are defined. The rows from the first branch can be interleaved with the rows
39+
from subsequent branches. Use `SORT _fork` after `FORK` if you need to change
40+
this behaviour.
41+
42+
`FORK` branches receive an implicit `LIMIT 1000` if no `LIMIT` is provided.
43+
44+
::::{note}
45+
`FORK` supports at most 8 execution branches.
46+
::::
47+
48+
::::{note}
49+
Using remote cluster references and `FORK` is not supported.
50+
::::
51+
52+
::::{note}
53+
Using more than one `FORK` command in a query is not supported.
54+
::::
55+
56+
**Examples**
57+
58+
In the following example, each `FORK` branch returns one row.
59+
Notice how `FORK` adds a `_fork` that indicates from which row the branch is
60+
coming:
61+
62+
:::{include} ../examples/fork.csv-spec/simpleFork.md
63+
64+
The next example, returns total number of rows that match the query along with
65+
the top five rows sorted by score.
66+
67+
:::{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`](...)
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)