Skip to content

Commit 43bcd6c

Browse files
committed
Add number of max branches for FORK
1 parent 9cc386f commit 43bcd6c

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ public void testOneSubQuery() {
10071007
( WHERE content:"fox" )
10081008
""";
10091009
var e = expectThrows(ParsingException.class, () -> run(query));
1010-
assertTrue(e.getMessage().contains("Fork requires at least two branches"));
1010+
assertTrue(e.getMessage().contains("Fork requires at least 2 branches"));
10111011
}
10121012

10131013
public void testForkWithinFork() {
@@ -1047,6 +1047,17 @@ public void testProfile() {
10471047
}
10481048
}
10491049

1050+
public void testWithTooManySubqueries() {
1051+
var query = """
1052+
FROM test
1053+
| FORK (WHERE true) (WHERE true) (WHERE true) (WHERE true) (WHERE true)
1054+
(WHERE true) (WHERE true) (WHERE true) (WHERE true)
1055+
""";
1056+
var e = expectThrows(ParsingException.class, () -> run(query));
1057+
assertTrue(e.getMessage().contains("Fork requires less than 8 branches"));
1058+
1059+
}
1060+
10501061
private void createAndPopulateIndices() {
10511062
var indexName = "test";
10521063
var client = client().admin().indices();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/LogicalPlanBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,13 @@ private void checkForRemoteClusters(LogicalPlan plan, Source source, String comm
651651
@SuppressWarnings("unchecked")
652652
public PlanFactory visitForkCommand(EsqlBaseParser.ForkCommandContext ctx) {
653653
List<PlanFactory> subQueries = visitForkSubQueries(ctx.forkSubQueries());
654-
if (subQueries.size() < 2) {
655-
throw new ParsingException(source(ctx), "Fork requires at least two branches");
654+
if (subQueries.size() < Fork.MIN_BRANCHES) {
655+
throw new ParsingException(source(ctx), "Fork requires at least " + Fork.MIN_BRANCHES + " branches");
656656
}
657+
if (subQueries.size() > Fork.MAX_BRANCHES) {
658+
throw new ParsingException(source(ctx), "Fork requires less than " + Fork.MAX_BRANCHES + " branches");
659+
}
660+
657661
return input -> {
658662
checkForRemoteClusters(input, source(ctx), "FORK");
659663
List<LogicalPlan> subPlans = subQueries.stream().map(planFactory -> planFactory.apply(input)).toList();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/Fork.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@
3636
public class Fork extends LogicalPlan implements PostAnalysisPlanVerificationAware, TelemetryAware {
3737

3838
public static final String FORK_FIELD = "_fork";
39+
public static final int MAX_BRANCHES = 8;
40+
public static final int MIN_BRANCHES = 2;
3941
private final List<Attribute> output;
4042

4143
public Fork(Source source, List<LogicalPlan> children, List<Attribute> output) {
4244
super(source, children);
43-
if (children.size() < 2) {
44-
throw new IllegalArgumentException("requires more than two subqueries, got:" + children.size());
45+
if (children.size() < MIN_BRANCHES) {
46+
throw new IllegalArgumentException("FORK requires more than " + MIN_BRANCHES + " branches, got: " + children.size());
4547
}
48+
if (children.size() > MAX_BRANCHES) {
49+
throw new IllegalArgumentException("FORK requires less than " + MAX_BRANCHES + " subqueries, got: " + children.size());
50+
}
51+
4652
this.output = output;
4753
}
4854

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,11 +3403,18 @@ public void testForkAllCommands() {
34033403
}
34043404

34053405
public void testInvalidFork() {
3406-
expectError("FROM foo* | FORK (WHERE a:\"baz\")", "line 1:13: Fork requires at least two branches");
3407-
expectError("FROM foo* | FORK (LIMIT 10)", "line 1:13: Fork requires at least two branches");
3408-
expectError("FROM foo* | FORK (SORT a)", "line 1:13: Fork requires at least two branches");
3409-
expectError("FROM foo* | FORK (WHERE x>1 | LIMIT 5)", "line 1:13: Fork requires at least two branches");
3410-
expectError("FROM foo* | WHERE x>1 | FORK (WHERE a:\"baz\")", "Fork requires at least two branches");
3406+
expectError("FROM foo* | FORK (WHERE a:\"baz\")", "line 1:13: Fork requires at least 2 branches");
3407+
expectError("FROM foo* | FORK (LIMIT 10)", "line 1:13: Fork requires at least 2 branches");
3408+
expectError("FROM foo* | FORK (SORT a)", "line 1:13: Fork requires at least 2 branches");
3409+
expectError("FROM foo* | FORK (WHERE x>1 | LIMIT 5)", "line 1:13: Fork requires at least 2 branches");
3410+
expectError("FROM foo* | WHERE x>1 | FORK (WHERE a:\"baz\")", "Fork requires at least 2 branches");
3411+
3412+
expectError("""
3413+
FROM foo*
3414+
| FORK (where true) (where true) (where true) (where true)
3415+
(where true) (where true) (where true) (where true)
3416+
(where true)
3417+
""", "Fork requires less than 8 branches");
34113418

34123419
expectError("FROM foo* | FORK ( x+1 ) ( WHERE y>2 )", "line 1:20: mismatched input 'x+1'");
34133420
expectError("FROM foo* | FORK ( LIMIT 10 ) ( y+2 )", "line 1:33: mismatched input 'y+2'");

0 commit comments

Comments
 (0)