Skip to content

Commit 965069f

Browse files
committed
Support STATS after FORK
1 parent abf5f00 commit 965069f

File tree

6 files changed

+116
-17
lines changed

6 files changed

+116
-17
lines changed

x-pack/plugin/esql/qa/server/multi-clusters/src/javaRestTest/java/org/elasticsearch/xpack/esql/ccq/MultiClusterSpecIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import static org.elasticsearch.xpack.esql.CsvTestUtils.isEnabled;
4747
import static org.elasticsearch.xpack.esql.CsvTestsDataLoader.ENRICH_SOURCE_INDICES;
4848
import static org.elasticsearch.xpack.esql.EsqlTestUtils.classpathResources;
49-
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.FORK_V5;
49+
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.FORK_V6;
5050
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.INLINESTATS;
5151
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.INLINESTATS_V2;
5252
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.INLINESTATS_V7;
@@ -132,7 +132,7 @@ protected void shouldSkipTest(String testName) throws IOException {
132132
assumeFalse("LOOKUP JOIN not yet supported in CCS", testCase.requiredCapabilities.contains(JOIN_LOOKUP_V12.capabilityName()));
133133
// Unmapped fields require a coorect capability response from every cluster, which isn't currently implemented.
134134
assumeFalse("UNMAPPED FIELDS not yet supported in CCS", testCase.requiredCapabilities.contains(UNMAPPED_FIELDS.capabilityName()));
135-
assumeFalse("FORK not yet supported in CCS", testCase.requiredCapabilities.contains(FORK_V5.capabilityName()));
135+
assumeFalse("FORK not yet supported in CCS", testCase.requiredCapabilities.contains(FORK_V6.capabilityName()));
136136
}
137137

138138
@Override

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

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44

55
simpleFork
6-
required_capability: fork_v5
6+
required_capability: fork_v6
77

88
FROM employees
99
| FORK ( WHERE emp_no == 10001 )
@@ -18,7 +18,7 @@ emp_no:integer | _fork:keyword
1818
;
1919

2020
forkWithWhereSortAndLimit
21-
required_capability: fork_v5
21+
required_capability: fork_v6
2222

2323
FROM employees
2424
| FORK ( WHERE hire_date < "1985-03-01T00:00:00Z" | SORT first_name | LIMIT 5 )
@@ -38,7 +38,7 @@ emp_no:integer | first_name:keyword | _fork:keyword
3838
;
3939

4040
fiveFork
41-
required_capability: fork_v5
41+
required_capability: fork_v6
4242

4343
FROM employees
4444
| FORK ( WHERE emp_no == 10005 )
@@ -59,7 +59,7 @@ fork5 | 10001
5959
;
6060

6161
forkWithWhereSortDescAndLimit
62-
required_capability: fork_v5
62+
required_capability: fork_v6
6363

6464
FROM employees
6565
| FORK ( WHERE hire_date < "1985-03-01T00:00:00Z" | SORT first_name DESC | LIMIT 2 )
@@ -76,7 +76,7 @@ fork2 | 10087 | Xinglin
7676
;
7777

7878
forkWithCommonPrefilter
79-
required_capability: fork_v5
79+
required_capability: fork_v6
8080

8181
FROM employees
8282
| WHERE emp_no > 10050
@@ -94,7 +94,7 @@ fork2 | 10100
9494
;
9595

9696
forkWithSemanticSearchAndScore
97-
required_capability: fork_v5
97+
required_capability: fork_v6
9898
required_capability: semantic_text_field_caps
9999
required_capability: metadata_score
100100

@@ -114,7 +114,7 @@ fork2 | 6.093784261960139E18 | 2 | all we have to decide is w
114114
;
115115

116116
forkWithEvals
117-
required_capability: fork_v5
117+
required_capability: fork_v6
118118

119119
FROM employees
120120
| FORK (WHERE emp_no == 10048 OR emp_no == 10081 | EVAL x = "abc" | EVAL y = 1)
@@ -131,7 +131,7 @@ fork2 | 10087 | def | null | 2
131131
;
132132

133133
forkWithStats
134-
required_capability: fork_v5
134+
required_capability: fork_v6
135135

136136
FROM employees
137137
| FORK (WHERE emp_no == 10048 OR emp_no == 10081)
@@ -152,7 +152,7 @@ fork4 | null | 100 | 10001 | null
152152
;
153153

154154
forkWithDissect
155-
required_capability: fork_v5
155+
required_capability: fork_v6
156156

157157
FROM employees
158158
| WHERE emp_no == 10048 OR emp_no == 10081
@@ -172,7 +172,7 @@ fork2 | 10081 | Rosen | 10081 | null | Zhongwei
172172
;
173173

174174
forkWithMixOfCommands
175-
required_capability: fork_v5
175+
required_capability: fork_v6
176176

177177
FROM employees
178178
| WHERE emp_no == 10048 OR emp_no == 10081
@@ -197,7 +197,7 @@ fork4 | 10081 | abc | aaa | null | null
197197
;
198198

199199
forkWithFiltersOnConstantValues
200-
required_capability: fork_v5
200+
required_capability: fork_v6
201201

202202
FROM employees
203203
| EVAL z = 1
@@ -218,7 +218,7 @@ fork3 | null | 100 | 10100 | 10001
218218
;
219219

220220
forkWithUnsupportedAttributes
221-
required_capability: fork_v5
221+
required_capability: fork_v6
222222

223223
FROM heights
224224
| FORK (SORT description DESC | LIMIT 1 | EVAL x = length(description) )
@@ -230,3 +230,63 @@ description:keyword | height_range:unsupported | x:integer | _fork:keyword
230230
Very Tall | null | 9 | fork1
231231
Medium Height | null | null | fork2
232232
;
233+
234+
forkBeforeStats
235+
required_capability: fork_v6
236+
237+
FROM employees
238+
| WHERE emp_no == 10048 OR emp_no == 10081
239+
| FORK ( EVAL a = CONCAT(first_name, " ", emp_no::keyword, " ", last_name)
240+
| DISSECT a "%{x} %{y} %{z}"
241+
| EVAL y = y::keyword )
242+
( STATS x = COUNT(*)::keyword, y = MAX(emp_no)::keyword, z = MIN(emp_no)::keyword )
243+
( SORT emp_no ASC | LIMIT 2 | EVAL x = last_name )
244+
( EVAL x = "abc" | EVAL y = "aaa" )
245+
| STATS c = count(*), m = max(_fork)
246+
;
247+
248+
c:long | m:keyword
249+
7 | fork4
250+
;
251+
252+
forkBeforeStatsWithWhere
253+
required_capability: fork_v6
254+
255+
FROM employees
256+
| WHERE emp_no == 10048 OR emp_no == 10081
257+
| FORK ( EVAL a = CONCAT(first_name, " ", emp_no::keyword, " ", last_name)
258+
| DISSECT a "%{x} %{y} %{z}"
259+
| EVAL y = y::keyword )
260+
( STATS x = COUNT(*)::keyword, y = MAX(emp_no)::keyword, z = MIN(emp_no)::keyword )
261+
( SORT emp_no ASC | LIMIT 2 | EVAL x = last_name )
262+
( EVAL x = "abc" | EVAL y = "aaa" )
263+
| STATS a = count(*) WHERE _fork == "fork1",
264+
b = max(_fork)
265+
;
266+
267+
a:long | b:keyword
268+
2 | fork4
269+
;
270+
271+
forkBeforeStatsByWithWhere
272+
required_capability: fork_v6
273+
274+
FROM employees
275+
| WHERE emp_no == 10048 OR emp_no == 10081
276+
| FORK ( EVAL a = CONCAT(first_name, " ", emp_no::keyword, " ", last_name)
277+
| DISSECT a "%{x} %{y} %{z}"
278+
| EVAL y = y::keyword )
279+
( STATS x = COUNT(*)::keyword, y = MAX(emp_no)::keyword, z = MIN(emp_no)::keyword )
280+
( SORT emp_no ASC | LIMIT 2 | EVAL x = last_name )
281+
( EVAL x = "abc" | EVAL y = "aaa" )
282+
| STATS a = count(*) WHERE emp_no > 10000,
283+
b = max(x) WHERE _fork == "fork1" BY _fork
284+
| SORT _fork
285+
;
286+
287+
a:long | b:keyword | _fork:keyword
288+
2 | Zhongwei | fork1
289+
0 | null | fork2
290+
2 | null | fork3
291+
2 | null | fork4
292+
;

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ForkIT.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,43 @@ public void testWithStatsSimple() {
588588
}
589589
}
590590

591+
public void testWithStatsAfterFork() {
592+
var query = """
593+
FROM test
594+
| FORK ( WHERE content:"fox" | EVAL a = 1)
595+
( WHERE content:"cat" | EVAL b = 2 )
596+
( WHERE content:"dog" | EVAL c = 3 )
597+
| STATS c = count(*)
598+
""";
599+
try (var resp = run(query)) {
600+
assertColumnNames(resp.columns(), List.of("c"));
601+
assertColumnTypes(resp.columns(), List.of("long"));
602+
Iterable<Iterable<Object>> expectedValues = List.of(
603+
List.of(7L)
604+
);
605+
assertValues(resp.values(), expectedValues);
606+
}
607+
}
608+
609+
public void testWithStatsWithWhereAfterFork() {
610+
var query = """
611+
FROM test
612+
| FORK ( WHERE content:"fox" | EVAL a = 1)
613+
( WHERE content:"cat" | EVAL b = 2 )
614+
( WHERE content:"dog" | EVAL c = 3 )
615+
| STATS c = count(*) WHERE _fork == "fork1"
616+
""";
617+
try (var resp = run(query)) {
618+
assertColumnNames(resp.columns(), List.of("c"));
619+
assertColumnTypes(resp.columns(), List.of("long"));
620+
621+
Iterable<Iterable<Object>> expectedValues = List.of(
622+
List.of(2L)
623+
);
624+
assertValues(resp.values(), expectedValues);
625+
}
626+
}
627+
591628
public void testWithConditionOnForkField() {
592629
var query = """
593630
FROM test

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ public enum Cap {
10191019
/**
10201020
* Support streaming of sub plan results
10211021
*/
1022-
FORK_V5(Build.current().isSnapshot()),
1022+
FORK_V6(Build.current().isSnapshot()),
10231023

10241024
/**
10251025
* Support for the {@code leading_zeros} named parameter.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/AbstractPhysicalOperationProviders.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public final PhysicalOperation groupingPhysicalOperation(
7474
if (aggregatorMode != AggregatorMode.INITIAL && aggregatorMode != AggregatorMode.FINAL) {
7575
assert false : "Invalid aggregator mode [" + aggregatorMode + "]";
7676
}
77-
if (aggregatorMode == AggregatorMode.INITIAL && aggregateExec.child() instanceof ExchangeSourceExec) {
77+
if (aggregatorMode == AggregatorMode.INITIAL
78+
&& aggregateExec.child() instanceof ExchangeSourceExec exchangeSourceExec
79+
&& exchangeSourceExec.isIntermediateAgg()) {
7880
// the reducer step at data node (local) level
7981
aggregatorMode = AggregatorMode.INTERMEDIATE;
8082
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public final void test() throws Throwable {
310310
);
311311
assumeFalse(
312312
"CSV tests cannot currently handle FORK",
313-
testCase.requiredCapabilities.contains(EsqlCapabilities.Cap.FORK_V5.capabilityName())
313+
testCase.requiredCapabilities.contains(EsqlCapabilities.Cap.FORK_V6.capabilityName())
314314
);
315315
assumeFalse(
316316
"CSV tests cannot currently handle multi_match function that depends on Lucene",

0 commit comments

Comments
 (0)