forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Verifier.java
300 lines (278 loc) · 12.5 KB
/
Verifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.analysis;
import org.elasticsearch.xpack.esql.expression.function.UnsupportedAttribute;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.NotEquals;
import org.elasticsearch.xpack.esql.plan.logical.Dissect;
import org.elasticsearch.xpack.esql.plan.logical.Eval;
import org.elasticsearch.xpack.esql.plan.logical.Grok;
import org.elasticsearch.xpack.esql.plan.logical.RegexExtract;
import org.elasticsearch.xpack.esql.plan.logical.Row;
import org.elasticsearch.xpack.esql.stats.FeatureMetric;
import org.elasticsearch.xpack.esql.stats.Metrics;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.capabilities.Unresolvable;
import org.elasticsearch.xpack.ql.common.Failure;
import org.elasticsearch.xpack.ql.expression.Alias;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.expression.MetadataAttribute;
import org.elasticsearch.xpack.ql.expression.ReferenceAttribute;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
import org.elasticsearch.xpack.ql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.ql.expression.predicate.BinaryOperator;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparison;
import org.elasticsearch.xpack.ql.plan.logical.Aggregate;
import org.elasticsearch.xpack.ql.plan.logical.Filter;
import org.elasticsearch.xpack.ql.plan.logical.Limit;
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.ql.plan.logical.OrderBy;
import org.elasticsearch.xpack.ql.plan.logical.Project;
import org.elasticsearch.xpack.ql.type.DataType;
import org.elasticsearch.xpack.ql.type.DataTypes;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.DISSECT;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.EVAL;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.GROK;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.LIMIT;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.SORT;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.STATS;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.WHERE;
import static org.elasticsearch.xpack.ql.common.Failure.fail;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FIRST;
public class Verifier {
private final Metrics metrics;
public Verifier(Metrics metrics) {
this.metrics = metrics;
}
Collection<Failure> verify(LogicalPlan plan) {
Set<Failure> failures = new LinkedHashSet<>();
// quick verification for unresolved attributes
plan.forEachUp(p -> {
// if the children are unresolved, so will this node; counting it will only add noise
if (p.childrenResolved() == false) {
return;
}
if (p instanceof Unresolvable u) {
failures.add(fail(p, u.unresolvedMessage()));
}
// p is resolved, skip
else if (p.resolved()) {
return;
}
p.forEachExpression(e -> {
// everything is fine, skip expression
if (e.resolved()) {
return;
}
e.forEachUp(ae -> {
// we're only interested in the children
if (ae.childrenResolved() == false) {
return;
}
if (ae instanceof Unresolvable u) {
// special handling for Project and unsupported types
if (p instanceof Project == false || u instanceof UnsupportedAttribute == false) {
failures.add(fail(ae, u.unresolvedMessage()));
}
}
if (ae.typeResolved().unresolved()) {
failures.add(fail(ae, ae.typeResolved().message()));
}
});
});
});
// in case of failures bail-out as all other checks will be redundant
if (failures.isEmpty() == false) {
return failures;
}
// Concrete verifications
plan.forEachDown(p -> {
if (p instanceof Aggregate agg) {
agg.aggregates().forEach(e -> {
var exp = e instanceof Alias ? ((Alias) e).child() : e;
if (exp instanceof AggregateFunction aggFunc) {
aggFunc.arguments().forEach(a -> {
// TODO: allow an expression?
if ((a instanceof FieldAttribute
|| a instanceof MetadataAttribute
|| a instanceof ReferenceAttribute
|| a instanceof Literal) == false) {
failures.add(
fail(
e,
"aggregate function's parameters must be an attribute or literal; found ["
+ a.sourceText()
+ "] of type ["
+ a.nodeName()
+ "]"
)
);
}
});
} else if (agg.groupings().contains(exp) == false) { // TODO: allow an expression?
failures.add(
fail(
exp,
"expected an aggregate function or group but got ["
+ exp.sourceText()
+ "] of type ["
+ exp.nodeName()
+ "]"
)
);
}
});
} else if (p instanceof RegexExtract re) {
Expression expr = re.input();
DataType type = expr.dataType();
if (EsqlDataTypes.isString(type) == false) {
failures.add(
fail(
expr,
"{} only supports KEYWORD or TEXT values, found expression [{}] type [{}]",
re.getClass().getSimpleName(),
expr.sourceText(),
type
)
);
}
} else if (p instanceof Row row) {
failures.addAll(validateRow(row));
}
p.forEachExpression(BinaryOperator.class, bo -> {
Failure f = validateUnsignedLongOperator(bo);
if (f != null) {
failures.add(f);
}
});
p.forEachExpression(BinaryComparison.class, bc -> {
Failure f = validateBinaryComparison(bc);
if (f != null) {
failures.add(f);
}
});
});
// gather metrics
if (failures.isEmpty()) {
gatherMetrics(plan);
}
return failures;
}
private void gatherMetrics(LogicalPlan plan) {
BitSet b = new BitSet(FeatureMetric.values().length);
plan.forEachDown(p -> {
if (p instanceof Dissect) {
b.set(DISSECT.ordinal());
} else if (p instanceof Eval) {
b.set(EVAL.ordinal());
} else if (p instanceof Grok) {
b.set(GROK.ordinal());
} else if (p instanceof Limit) {
b.set(LIMIT.ordinal());
} else if (p instanceof OrderBy) {
b.set(SORT.ordinal());
} else if (p instanceof Aggregate) {
b.set(STATS.ordinal());
} else if (p instanceof Filter) {
b.set(WHERE.ordinal());
}
});
for (int i = b.nextSetBit(0); i >= 0; i = b.nextSetBit(i + 1)) {
metrics.inc(FeatureMetric.values()[i]);
}
}
public static Collection<Failure> validateRow(Row row) {
List<Failure> failures = new ArrayList<>(row.fields().size());
row.fields().forEach(o -> {
if (EsqlDataTypes.isRepresentable(o.dataType()) == false && o instanceof Alias a) {
failures.add(fail(o, "cannot use [{}] directly in a row assignment", a.child().sourceText()));
}
});
return failures;
}
/**
* Limit QL's comparisons to types we support.
*/
public static Failure validateBinaryComparison(BinaryComparison bc) {
if (bc.left().dataType().isNumeric()) {
if (false == bc.right().dataType().isNumeric()) {
return fail(
bc,
"first argument of [{}] is [numeric] so second argument must also be [numeric] but was [{}]",
bc.sourceText(),
bc.right().dataType().typeName()
);
}
return null;
}
List<DataType> allowed = new ArrayList<>();
allowed.add(DataTypes.KEYWORD);
allowed.add(DataTypes.TEXT);
allowed.add(DataTypes.IP);
allowed.add(DataTypes.DATETIME);
allowed.add(DataTypes.VERSION);
if (bc instanceof Equals || bc instanceof NotEquals) {
allowed.add(DataTypes.BOOLEAN);
}
Expression.TypeResolution r = TypeResolutions.isType(
bc.left(),
allowed::contains,
bc.sourceText(),
FIRST,
Stream.concat(Stream.of("numeric"), allowed.stream().map(DataType::typeName)).toArray(String[]::new)
);
if (false == r.resolved()) {
return fail(bc, r.message());
}
if (DataTypes.isString(bc.left().dataType()) && DataTypes.isString(bc.right().dataType())) {
return null;
}
if (bc.left().dataType() != bc.right().dataType()) {
return fail(
bc,
"first argument of [{}] is [{}] so second argument must also be [{}] but was [{}]",
bc.sourceText(),
bc.left().dataType().typeName(),
bc.left().dataType().typeName(),
bc.right().dataType().typeName()
);
}
return null;
}
// Ensure that UNSIGNED_LONG types are not implicitly converted when used in arithmetic binary operator, as this cannot be done since:
// - unsigned longs are passed through the engine as longs, so/and
// - negative values cannot be represented (i.e. range [Long.MIN_VALUE, "abs"(Long.MIN_VALUE) + Long.MAX_VALUE] won't fit on 64 bits);
// - a conversion to double isn't possible, since upper range UL values can no longer be distinguished
// ex: (double) 18446744073709551615 == (double) 18446744073709551614
// - the implicit ESQL's Cast doesn't currently catch Exception and nullify the result.
// Let the user handle the operation explicitly.
public static Failure validateUnsignedLongOperator(BinaryOperator<?, ?, ?, ?> bo) {
DataType leftType = bo.left().dataType();
DataType rightType = bo.right().dataType();
if ((leftType == DataTypes.UNSIGNED_LONG || rightType == DataTypes.UNSIGNED_LONG) && leftType != rightType) {
return fail(
bo,
"first argument of [{}] is [{}] and second is [{}]. [{}] can only be operated on together with another [{}]",
bo.sourceText(),
leftType.typeName(),
rightType.typeName(),
DataTypes.UNSIGNED_LONG.typeName(),
DataTypes.UNSIGNED_LONG.typeName()
);
}
return null;
}
}