Skip to content

Commit

Permalink
QL: EQL and ESQL to use only the necessary fields in the internal fie…
Browse files Browse the repository at this point in the history
…ld_caps calls (#98987)

* Use only the necessary fields in field_caps calls from QL
  • Loading branch information
astefan committed Aug 30, 2023
1 parent 8ea41dd commit 291ecc5
Show file tree
Hide file tree
Showing 12 changed files with 1,800 additions and 33 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/98987.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 98987
summary: EQL and ESQL to use only the necessary fields in the internal `field_caps`
calls
area: EQL
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,27 @@ setup:
body:
query: 'sequence with maxspan=10d [network where user == "ADMIN"] ![network where user == "SYSTEM"] [network where user == "ADMIN"]'
- match: {hits.total.value: 0}

---
"Error message missing column - no suggestion":

- do:
catch: "bad_request"
eql.search:
index: eql_test
body:
query: 'sequence with maxspan=10d [network where used == "ADMIN"] [network where id == 123]'
- match: { error.root_cause.0.type: "verification_exception" }
- match: { error.root_cause.0.reason: "Found 1 problem\nline 1:42: Unknown column [used]" }

---
"Error message missing column - did you mean functionality":

- do:
catch: "bad_request"
eql.search:
index: eql_test
body:
query: 'sequence with maxspan=10d [network where user == "ADMIN"] ![network where used == "SYSTEM"]'
- match: { error.root_cause.0.type: "verification_exception" }
- match: { error.root_cause.0.reason: "Found 1 problem\nline 1:75: Unknown column [used], did you mean [user]?" }
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import org.elasticsearch.xpack.eql.parser.ParserParams;
import org.elasticsearch.xpack.eql.plan.physical.PhysicalPlan;
import org.elasticsearch.xpack.eql.planner.Planner;
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;
import org.elasticsearch.xpack.ql.expression.function.FunctionRegistry;
import org.elasticsearch.xpack.ql.index.IndexResolver;
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan;

import java.util.LinkedHashSet;
import java.util.Set;

import static org.elasticsearch.xpack.ql.util.ActionListeners.map;
import static org.elasticsearch.xpack.ql.util.StringUtils.WILDCARD;

public class EqlSession {

Expand Down Expand Up @@ -116,14 +121,27 @@ private <T> void preAnalyze(LogicalPlan parsed, ActionListener<LogicalPlan> list
listener.onFailure(new TaskCancelledException("cancelled"));
return;
}
Set<String> fieldNames = fieldNames(parsed);
indexResolver.resolveAsMergedMapping(
indexWildcard,
fieldNames,
configuration.indicesOptions(),
configuration.runtimeMappings(),
map(listener, r -> preAnalyzer.preAnalyze(parsed, r))
);
}

static Set<String> fieldNames(LogicalPlan parsed) {
Set<String> fieldNames = new LinkedHashSet<>();
parsed.forEachExpressionDown(UnresolvedAttribute.class, ua -> {
fieldNames.add(ua.name());
if (ua.name().endsWith(WILDCARD) == false) {
fieldNames.add(ua.name() + ".*");
}
});
return fieldNames.isEmpty() ? IndexResolver.ALL_FIELDS : fieldNames;
}

private LogicalPlan postAnalyze(LogicalPlan verified) {
return postAnalyzer.postAnalyze(verified, configuration);
}
Expand Down

0 comments on commit 291ecc5

Please sign in to comment.