Skip to content

Commit cb412e6

Browse files
Return all fields
1 parent 917b273 commit cb412e6

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
7171
inlinestatsAggs.add(((InlineStats) i).aggregate());
7272
}
7373

74-
if (false == parsed.anyMatch(p -> shouldCollectReferencedFields(p, inlinestatsAggs))) {
75-
// no explicit columns selection, for example "from employees"
76-
// also, inlinestats only adds columns to the existent output, its Aggregate shouldn't interfere with potentially using "*"
77-
return new PreAnalysisResult(enrichResolution, IndexResolver.ALL_FIELDS, Set.of());
78-
}
79-
8074
Holder<Boolean> projectAll = new Holder<>(false);
8175
parsed.forEachExpressionDown(UnresolvedStar.class, us -> {// explicit "*" fields selection
8276
if (projectAll.get()) {
@@ -107,6 +101,7 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
107101
Set<String> wildcardJoinIndices = new java.util.HashSet<>();
108102

109103
var canRemoveAliases = new Holder<>(true);
104+
var needsAllFields = new Holder<>(parsed.anyMatch(p -> shouldCollectReferencedFields(p, inlinestatsAggs)) == false);
110105

111106
var processingLambda = new Holder<Function<LogicalPlan, Boolean>>();
112107
processingLambda.set((LogicalPlan p) -> {// go over each plan top-down
@@ -120,6 +115,10 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
120115
var return_result = child.forEachDownMayReturnEarly(processingLambda.get());
121116
// No nested Forks for now...
122117
assert return_result;
118+
if (referencesBuilder.get().isEmpty()) {
119+
needsAllFields.set(true);
120+
return true;
121+
}
123122
forkRefsResult.addAll(referencesBuilder.get());
124123
}
125124

@@ -210,6 +209,12 @@ public static PreAnalysisResult resolveFieldNames(LogicalPlan parsed, EnrichReso
210209
});
211210
parsed.forEachDownMayReturnEarly(processingLambda.get());
212211

212+
if (needsAllFields.get()) {
213+
// no explicit columns selection, for example "from employees"
214+
// also, inlinestats only adds columns to the existent output, its Aggregate shouldn't interfere with potentially using "*"
215+
return new PreAnalysisResult(enrichResolution, IndexResolver.ALL_FIELDS, Set.of());
216+
}
217+
213218
// Add JOIN ON column references afterward to avoid Alias removal
214219
referencesBuilder.get().addAll(joinRefs);
215220
// If any JOIN commands need wildcard field-caps calls, persist the index names

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public void testSimple1() {
5959
);
6060
}
6161

62+
public void testSimple2() {
63+
assertFieldNames("""
64+
FROM employees
65+
| WHERE emp_no == "2"
66+
""", IndexResolver.ALL_FIELDS);
67+
}
68+
6269
public void testDirectFilter() {
6370
assertFieldNames(
6471
"from employees | sort emp_no | where still_hired | keep emp_no | limit 3",
@@ -2162,7 +2169,7 @@ public void testForkWithStatsInAllBranches2() {
21622169
public void testForkWithStatsAndWhere() {
21632170
assertFieldNames(
21642171
" FROM employees | FORK ( WHERE true | stats min(salary) by gender) ( WHERE true | LIMIT 3 )",
2165-
Set.of("gender", "salary", "gender.*", "salary.*")
2172+
IndexResolver.ALL_FIELDS
21662173
);
21672174
}
21682175

@@ -2180,7 +2187,7 @@ public void testNullStringWithFork() {
21802187
| EVAL x = null::string
21812188
| STATS COUNT() BY category=CATEGORIZE(x)
21822189
| SORT category
2183-
| FORK (WHERE true) (WHERE true) | WHERE _fork == "fork1" | DROP _fork""", Set.of("_index"));
2190+
| FORK (WHERE true) (WHERE true) | WHERE _fork == "fork1" | DROP _fork""", IndexResolver.ALL_FIELDS);
21842191
}
21852192

21862193
public void testSingleFork() {
@@ -2189,7 +2196,7 @@ public void testSingleFork() {
21892196
| FORK
21902197
( STATS x = count(*))
21912198
( WHERE emp_no == "2" )
2192-
| SORT _fork""", Set.of("emp_no", "emp_no.*"));
2199+
| SORT _fork""", IndexResolver.ALL_FIELDS);
21932200
}
21942201

21952202
private void assertFieldNames(String query, Set<String> expected) {

0 commit comments

Comments
 (0)