Skip to content

Commit 5a64c9b

Browse files
committed
Fix field resolution for FORK
1 parent 4c0c9b6 commit 5a64c9b

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,6 @@ static PreAnalysisResult fieldNames(LogicalPlan parsed, Set<String> enrichPolicy
574574
return result.withFieldNames(IndexResolver.ALL_FIELDS);
575575
}
576576

577-
if (parsed.anyMatch(plan -> plan instanceof Fork)) {
578-
return result.withFieldNames(IndexResolver.ALL_FIELDS);
579-
}
580-
581577
Holder<Boolean> projectAll = new Holder<>(false);
582578
parsed.forEachExpressionDown(UnresolvedStar.class, us -> {// explicit "*" fields selection
583579
if (projectAll.get()) {

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,62 @@ public void testDropWildcardFields_WithLookupJoin() {
17491749
);
17501750
}
17511751

1752+
public void testForkFieldsWithKeep() {
1753+
assumeTrue("FORK available as snapshot only", EsqlCapabilities.Cap.FORK.isEnabled());
1754+
1755+
assertFieldNames("""
1756+
FROM test
1757+
| WHERE a > 2000
1758+
| EVAL b = a + 100
1759+
| FORK (WHERE c > 1 AND a < 10000 | EVAL d = a + 500)
1760+
(WHERE d > 1000 AND e == "aaa" | EVAL c = a + 200)
1761+
| WHERE x > y
1762+
| KEEP a, b, c, d, x
1763+
""", Set.of("a", "a.*", "c", "c.*", "d", "d.*", "e", "e.*", "x", "x.*", "y", "y.*"));
1764+
}
1765+
1766+
public void testForkFieldsWithNoProjection() {
1767+
assumeTrue("FORK available as snapshot only", EsqlCapabilities.Cap.FORK.isEnabled());
1768+
1769+
assertFieldNames("""
1770+
FROM test
1771+
| WHERE a > 2000
1772+
| EVAL b = a + 100
1773+
| FORK (WHERE c > 1 AND a < 10000 | EVAL d = a + 500)
1774+
(WHERE d > 1000 AND e == "aaa" | EVAL c = a + 200)
1775+
| WHERE x > y
1776+
""", ALL_FIELDS);
1777+
}
1778+
1779+
public void testForkFieldsWithStatsInOneBranch() {
1780+
assumeTrue("FORK available as snapshot only", EsqlCapabilities.Cap.FORK.isEnabled());
1781+
1782+
assertFieldNames("""
1783+
FROM test
1784+
| WHERE a > 2000
1785+
| EVAL b = a + 100
1786+
| FORK (WHERE c > 1 AND a < 10000 | EVAL d = a + 500)
1787+
(STATS x = count(*), y=min(z))
1788+
| WHERE x > y
1789+
""", Set.of("a", "a.*", "c", "c.*", "z", "z.*"));
1790+
}
1791+
1792+
public void testForkFieldsWithEnrichAndLookupJoins() {
1793+
assumeTrue("FORK available as snapshot only", EsqlCapabilities.Cap.FORK.isEnabled());
1794+
assumeTrue("LOOKUP JOIN available as snapshot only", EsqlCapabilities.Cap.JOIN_LOOKUP_V12.isEnabled());
1795+
1796+
assertFieldNames("""
1797+
FROM test
1798+
| ENRICH enrich_policy ON abc
1799+
| EVAL b = a + 100
1800+
| LOOKUP JOIN my_lookup_index ON def
1801+
| FORK (WHERE c > 1 AND a < 10000 | EVAL d = a + 500)
1802+
(STATS x = count(*), y=min(z))
1803+
| LOOKUP JOIN my_lookup_index ON xyz
1804+
| WHERE x > y
1805+
""", Set.of("a", "a.*", "abc", "abc.*", "c", "c.*", "def", "def.*", "x", "x.*", "xyz", "xyz.*", "y", "y.*", "z", "z.*"));
1806+
}
1807+
17521808
private Set<String> fieldNames(String query, Set<String> enrichPolicyMatchFields) {
17531809
var preAnalysisResult = new EsqlSession.PreAnalysisResult(null);
17541810
return EsqlSession.fieldNames(parser.createStatement(query), enrichPolicyMatchFields, preAnalysisResult).fieldNames();

0 commit comments

Comments
 (0)