Skip to content

Commit 9f24a18

Browse files
Initial
1 parent 9db4361 commit 9f24a18

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,6 @@ static PreAnalysisResult fieldNames(LogicalPlan parsed, Set<String> enrichPolicy
836836
return result.withFieldNames(IndexResolver.ALL_FIELDS);
837837
}
838838

839-
// TODO: Improve field resolution for FORK - right now we request all fields
840-
if (parsed.anyMatch(p -> p instanceof Fork)) {
841-
return result.withFieldNames(IndexResolver.ALL_FIELDS);
842-
}
843-
844839
Holder<Boolean> projectAll = new Holder<>(false);
845840
parsed.forEachExpressionDown(UnresolvedStar.class, us -> {// explicit "*" fields selection
846841
if (projectAll.get()) {
@@ -853,6 +848,32 @@ static PreAnalysisResult fieldNames(LogicalPlan parsed, Set<String> enrichPolicy
853848
return result.withFieldNames(IndexResolver.ALL_FIELDS);
854849
}
855850

851+
Holder<Boolean> projectAfterFork = new Holder<>(false);
852+
Holder<Boolean> hasFork = new Holder<>(false);
853+
854+
parsed.forEachDown(plan -> {
855+
if (projectAll.get()) {
856+
return;
857+
}
858+
859+
if (hasFork.get() == false && shouldCollectReferencedFields(plan, inlinestatsAggs)) {
860+
projectAfterFork.set(true);
861+
}
862+
863+
if (plan instanceof Fork fork && projectAfterFork.get() == false) {
864+
hasFork.set(true);
865+
fork.children().forEach(child -> {
866+
if (child.anyMatch(p -> shouldCollectReferencedFields(p, inlinestatsAggs)) == false) {
867+
projectAll.set(true);
868+
}
869+
});
870+
}
871+
});
872+
873+
if (projectAll.get()) {
874+
return result.withFieldNames(IndexResolver.ALL_FIELDS);
875+
}
876+
856877
var referencesBuilder = AttributeSet.builder();
857878
// "keep" and "drop" attributes are special whenever a wildcard is used in their name, as the wildcard can cover some
858879
// attributes ("lookup join" generated columns among others); steps like removal of Aliases should ignore fields matching the

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,7 +2080,7 @@ public void testForkFieldsWithKeepAfterFork() {
20802080
(WHERE d > 1000 AND e == "aaa" | EVAL c = a + 200)
20812081
| WHERE x > y
20822082
| KEEP a, b, c, d, x
2083-
""", ALL_FIELDS);
2083+
""", Set.of("a", "x", "y", "d", "e", "e.*", "d.*", "y.*", "x.*", "a.*"));
20842084
}
20852085

20862086
public void testForkFieldsWithKeepBeforeFork() {
@@ -2092,7 +2092,7 @@ public void testForkFieldsWithKeepBeforeFork() {
20922092
| FORK (WHERE c > 1 AND a < 10000 | EVAL d = a + 500)
20932093
(WHERE d > 1000 AND e == "aaa" | EVAL c = a + 200)
20942094
| WHERE x > y
2095-
""", ALL_FIELDS);
2095+
""", Set.of("x", "y", "a", "d", "e", "b", "c", "e.*", "d.*", "y.*", "x.*", "a.*", "c.*", "b.*"));
20962096
}
20972097

20982098
public void testForkFieldsWithNoProjection() {
@@ -2118,17 +2118,41 @@ public void testForkFieldsWithStatsInOneBranch() {
21182118
}
21192119

21202120
public void testForkFieldsWithEnrichAndLookupJoins() {
2121-
assertFieldNames("""
2122-
FROM test
2123-
| KEEP a, b, abc, def, z, xyz
2124-
| ENRICH enrich_policy ON abc
2125-
| EVAL b = a + 100
2126-
| LOOKUP JOIN my_lookup_index ON def
2127-
| FORK (WHERE c > 1 AND a < 10000 | EVAL d = a + 500)
2128-
(STATS x = count(*), y=min(z))
2129-
| LOOKUP JOIN my_lookup_index ON xyz
2130-
| WHERE x > y OR _fork == "fork1"
2131-
""", ALL_FIELDS);
2121+
assertFieldNames(
2122+
"""
2123+
FROM test
2124+
| KEEP a, b, abc, def, z, xyz
2125+
| ENRICH enrich_policy ON abc
2126+
| EVAL b = a + 100
2127+
| LOOKUP JOIN my_lookup_index ON def
2128+
| FORK (WHERE c > 1 AND a < 10000 | EVAL d = a + 500)
2129+
(STATS x = count(*), y=min(z))
2130+
| LOOKUP JOIN my_lookup_index ON xyz
2131+
| WHERE x > y OR _fork == "fork1"
2132+
""",
2133+
Set.of(
2134+
"x",
2135+
"y",
2136+
"_fork",
2137+
"a",
2138+
"c",
2139+
"abc",
2140+
"b",
2141+
"def",
2142+
"z",
2143+
"xyz",
2144+
"def.*",
2145+
"_fork.*",
2146+
"y.*",
2147+
"x.*",
2148+
"xyz.*",
2149+
"z.*",
2150+
"abc.*",
2151+
"a.*",
2152+
"c.*",
2153+
"b.*"
2154+
)
2155+
);
21322156
}
21332157

21342158
public void testForkWithStatsInAllBranches() {
@@ -2140,7 +2164,7 @@ public void testForkWithStatsInAllBranches() {
21402164
(EVAL z = a * b | STATS m = max(z))
21412165
(STATS x = count(*), y=min(z))
21422166
| WHERE x > y
2143-
""", ALL_FIELDS);
2167+
""", Set.of("c", "a", "z", "z.*", "a.*", "c.*"));
21442168
}
21452169

21462170
public void testForkWithStatsAndWhere() {

0 commit comments

Comments
 (0)