-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EQL: Add field resolution and verification (#51872)
Add basic field resolution inside the Analyzer and a basic Verifier to check for any unresolved fields.
- Loading branch information
Showing
15 changed files
with
381 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.analysis; | ||
|
||
import org.elasticsearch.xpack.ql.expression.Attribute; | ||
import org.elasticsearch.xpack.ql.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute; | ||
import org.elasticsearch.xpack.ql.type.DataTypes; | ||
import org.elasticsearch.xpack.ql.type.InvalidMappedField; | ||
import org.elasticsearch.xpack.ql.type.UnsupportedEsField; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
public final class AnalysisUtils { | ||
|
||
private AnalysisUtils() {} | ||
|
||
// | ||
// Shared methods around the analyzer rules | ||
// | ||
static Attribute resolveAgainstList(UnresolvedAttribute u, Collection<Attribute> attrList) { | ||
return resolveAgainstList(u, attrList, false); | ||
} | ||
|
||
static Attribute resolveAgainstList(UnresolvedAttribute u, Collection<Attribute> attrList, boolean allowCompound) { | ||
List<Attribute> matches = new ArrayList<>(); | ||
|
||
// first take into account the qualified version | ||
boolean qualified = u.qualifier() != null; | ||
|
||
for (Attribute attribute : attrList) { | ||
if (!attribute.synthetic()) { | ||
boolean match = qualified ? Objects.equals(u.qualifiedName(), attribute.qualifiedName()) : | ||
// if the field is unqualified | ||
// first check the names directly | ||
(Objects.equals(u.name(), attribute.name()) | ||
// but also if the qualifier might not be quoted and if there's any ambiguity with nested fields | ||
|| Objects.equals(u.name(), attribute.qualifiedName())); | ||
if (match) { | ||
matches.add(attribute.withLocation(u.source())); | ||
} | ||
} | ||
} | ||
|
||
// none found | ||
if (matches.isEmpty()) { | ||
return null; | ||
} | ||
|
||
if (matches.size() == 1) { | ||
return handleSpecialFields(u, matches.get(0), allowCompound); | ||
} | ||
|
||
return u.withUnresolvedMessage( | ||
"Reference [" + u.qualifiedName() + "] is ambiguous (to disambiguate use quotes or qualifiers); matches any of " | ||
+ matches.stream().map(a -> "\"" + a.qualifier() + "\".\"" + a.name() + "\"").sorted().collect(toList())); | ||
} | ||
|
||
private static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute named, boolean allowCompound) { | ||
// if it's a object/compound type, keep it unresolved with a nice error message | ||
if (named instanceof FieldAttribute) { | ||
FieldAttribute fa = (FieldAttribute) named; | ||
|
||
// incompatible mappings | ||
if (fa.field() instanceof InvalidMappedField) { | ||
named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] due to ambiguities being " | ||
+ ((InvalidMappedField) fa.field()).errorMessage()); | ||
} | ||
// unsupported types | ||
else if (DataTypes.isUnsupported(fa.dataType())) { | ||
UnsupportedEsField unsupportedField = (UnsupportedEsField) fa.field(); | ||
if (unsupportedField.hasInherited()) { | ||
named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] with unsupported type [" | ||
+ unsupportedField.getOriginalType() + "] " + "in hierarchy (field [" + unsupportedField.getInherited() + "])"); | ||
} else { | ||
named = u.withUnresolvedMessage( | ||
"Cannot use field [" + fa.name() + "] with unsupported type [" + unsupportedField.getOriginalType() + "]"); | ||
} | ||
} | ||
// compound fields | ||
else if (allowCompound == false && DataTypes.isPrimitive(fa.dataType()) == false) { | ||
named = u.withUnresolvedMessage( | ||
"Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] only its subfields"); | ||
} | ||
} | ||
return named; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalyzerRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.analysis; | ||
|
||
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.ql.rule.Rule; | ||
|
||
public abstract class AnalyzerRule<SubPlan extends LogicalPlan> extends Rule<SubPlan, LogicalPlan> { | ||
|
||
// transformUp (post-order) - that is first children and then the node | ||
// but with a twist; only if the tree is not resolved or analyzed | ||
@Override | ||
public final LogicalPlan apply(LogicalPlan plan) { | ||
return plan.transformUp(t -> t.analyzed() || skipResolved() && t.resolved() ? t : rule(t), typeToken()); | ||
} | ||
|
||
@Override | ||
protected abstract LogicalPlan rule(SubPlan plan); | ||
|
||
protected boolean skipResolved() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.expression.function; | ||
|
||
import org.elasticsearch.xpack.ql.expression.function.FunctionRegistry; | ||
|
||
public class EqlFunctionRegistry extends FunctionRegistry { | ||
|
||
public EqlFunctionRegistry() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql; | ||
|
||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.xpack.eql.session.Configuration; | ||
|
||
import static org.elasticsearch.test.ESTestCase.randomAlphaOfLength; | ||
import static org.elasticsearch.test.ESTestCase.randomBoolean; | ||
import static org.elasticsearch.test.ESTestCase.randomNonNegativeLong; | ||
import static org.elasticsearch.test.ESTestCase.randomZone; | ||
|
||
public final class EqlTestUtils { | ||
|
||
private EqlTestUtils() {} | ||
|
||
public static final Configuration TEST_CFG = new Configuration(new String[] { "none" }, org.elasticsearch.xpack.ql.util.DateUtils.UTC, | ||
"nobody", "cluster", null, TimeValue.timeValueSeconds(30), false, ""); | ||
|
||
public static Configuration randomConfiguration() { | ||
return new Configuration(new String[] {randomAlphaOfLength(16)}, | ||
randomZone(), | ||
randomAlphaOfLength(16), | ||
randomAlphaOfLength(16), | ||
null, | ||
new TimeValue(randomNonNegativeLong()), | ||
randomBoolean(), | ||
randomAlphaOfLength(16)); | ||
} | ||
} |
Oops, something went wrong.