Skip to content

Commit bcc2f23

Browse files
Refactor SQL analyzer and analyzer rules (elastic#97836)
1 parent 62ee45c commit bcc2f23

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/analyzer/AnalyzerRules.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ public static List<Attribute> maybeResolveAgainstList(
145145
UnresolvedAttribute u,
146146
Collection<Attribute> attrList,
147147
boolean allowCompound,
148-
boolean acceptPattern
148+
boolean acceptPattern,
149+
BiFunction<UnresolvedAttribute, Attribute, Attribute> specialFieldHandler
149150
) {
150151
List<Attribute> matches = new ArrayList<>();
151152

@@ -177,7 +178,7 @@ public static List<Attribute> maybeResolveAgainstList(
177178
// found exact match or multiple if pattern
178179
if (matches.size() == 1 || isPattern) {
179180
// only add the location if the match is univocal; b/c otherwise adding the location will overwrite any preexisting one
180-
matches.replaceAll(e -> handleSpecialFields(u, e.withLocation(u.source()), allowCompound));
181+
matches.replaceAll(e -> specialFieldHandler.apply(u, e.withLocation(u.source())));
181182
return matches;
182183
}
183184

@@ -203,7 +204,7 @@ public static List<Attribute> maybeResolveAgainstList(
203204
);
204205
}
205206

206-
private static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute named, boolean allowCompound) {
207+
public static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute named, boolean allowCompound) {
207208
// if it's a object/compound type, keep it unresolved with a nice error message
208209
if (named instanceof FieldAttribute fa) {
209210

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/EsRelation.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ public class EsRelation extends LeafPlan {
2727
private final boolean frozen;
2828

2929
public EsRelation(Source source, EsIndex index, boolean frozen) {
30+
this(source, index, flatten(source, index.mapping()), frozen);
31+
}
32+
33+
public EsRelation(Source source, EsIndex index, List<Attribute> attributes) {
34+
this(source, index, attributes, false);
35+
}
36+
37+
private EsRelation(Source source, EsIndex index, List<Attribute> attributes, boolean frozen) {
3038
super(source);
3139
this.index = index;
32-
this.attrs = flatten(source, index.mapping());
40+
this.attrs = attributes;
3341
this.frozen = frozen;
3442
}
3543

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public List<? extends NamedExpression> projections() {
4343
return projections;
4444
}
4545

46+
public Project withProjections(List<? extends NamedExpression> projections) {
47+
return new Project(source(), child(), projections);
48+
}
49+
4650
@Override
4751
public boolean resolved() {
4852
return super.resolved() && Expressions.anyMatch(projections, Functions::isAggregate) == false;

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/EsField.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ public DataType getDataType() {
4848
return esDataType;
4949
}
5050

51-
/**
52-
* Create a new {@link EsField} replacing the type.
53-
*/
54-
public EsField withType(DataType esDataType) {
55-
return new EsField(name, esDataType, properties, aggregatable, isAlias);
56-
}
57-
5851
/**
5952
* This field can be aggregated
6053
*/

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.elasticsearch.common.logging.LoggerMessageFormat;
1010
import org.elasticsearch.core.Tuple;
11+
import org.elasticsearch.xpack.ql.analyzer.AnalyzerRules;
1112
import org.elasticsearch.xpack.ql.analyzer.AnalyzerRules.AddMissingEqualsToBoolField;
1213
import org.elasticsearch.xpack.ql.analyzer.AnalyzerRules.ParameterizedAnalyzerRule;
1314
import org.elasticsearch.xpack.ql.capabilities.Resolvables;
@@ -166,7 +167,13 @@ private static Attribute resolveAgainstList(UnresolvedAttribute u, Collection<At
166167
}
167168

168169
private static Attribute resolveAgainstList(UnresolvedAttribute u, Collection<Attribute> attrList, boolean allowCompound) {
169-
var matches = maybeResolveAgainstList(u, attrList, allowCompound, false);
170+
var matches = maybeResolveAgainstList(
171+
u,
172+
attrList,
173+
allowCompound,
174+
false,
175+
(ua, na) -> AnalyzerRules.handleSpecialFields(ua, na, allowCompound)
176+
);
170177
return matches.isEmpty() ? null : matches.get(0);
171178
}
172179

0 commit comments

Comments
 (0)