Skip to content

Commit 236ae2b

Browse files
authored
ES|QL: Add query generator for FORK (#129415)
1 parent a47e00f commit 236ae2b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/generative/EsqlQueryGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe.DropGenerator;
1414
import org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe.EnrichGenerator;
1515
import org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe.EvalGenerator;
16+
import org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe.ForkGenerator;
1617
import org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe.GrokGenerator;
1718
import org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe.KeepGenerator;
1819
import org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe.LimitGenerator;
@@ -53,6 +54,7 @@ public record QueryExecuted(String query, int depth, List<Column> outputSchema,
5354
DropGenerator.INSTANCE,
5455
EnrichGenerator.INSTANCE,
5556
EvalGenerator.INSTANCE,
57+
ForkGenerator.INSTANCE,
5658
GrokGenerator.INSTANCE,
5759
KeepGenerator.INSTANCE,
5860
LimitGenerator.INSTANCE,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe;
9+
10+
import org.elasticsearch.xpack.esql.qa.rest.generative.EsqlQueryGenerator;
11+
import org.elasticsearch.xpack.esql.qa.rest.generative.command.CommandGenerator;
12+
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static org.elasticsearch.test.ESTestCase.randomIntBetween;
17+
18+
public class ForkGenerator implements CommandGenerator {
19+
20+
public static final String FORK = "fork";
21+
public static final CommandGenerator INSTANCE = new ForkGenerator();
22+
23+
@Override
24+
public CommandDescription generate(
25+
List<CommandDescription> previousCommands,
26+
List<EsqlQueryGenerator.Column> previousOutput,
27+
QuerySchema schema
28+
) {
29+
// FORK can only be allowed once - so we skip adding another FORK if we already have one
30+
// otherwise, most generated queries would only result in a validation error
31+
for (CommandDescription command : previousCommands) {
32+
if (command.commandName().equals(FORK)) {
33+
return new CommandDescription(FORK, this, " ", Map.of());
34+
}
35+
}
36+
37+
int n = randomIntBetween(2, 10);
38+
39+
String cmd = " | FORK " + "( WHERE true ) ".repeat(n) + " | WHERE _fork == \"fork" + randomIntBetween(1, n) + "\" | DROP _fork";
40+
41+
return new CommandDescription(FORK, this, cmd, Map.of());
42+
}
43+
44+
@Override
45+
public ValidationResult validateOutput(
46+
List<CommandDescription> previousCommands,
47+
CommandDescription command,
48+
List<EsqlQueryGenerator.Column> previousColumns,
49+
List<List<Object>> previousOutput,
50+
List<EsqlQueryGenerator.Column> columns,
51+
List<List<Object>> output
52+
) {
53+
return CommandGenerator.expectSameRowCount(previousCommands, previousOutput, output);
54+
}
55+
}

0 commit comments

Comments
 (0)