forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyzer.java
776 lines (697 loc) · 34.6 KB
/
Analyzer.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/*
* 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.common.logging.HeaderWarning;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import org.elasticsearch.xpack.esql.enrich.EnrichPolicyResolution;
import org.elasticsearch.xpack.esql.expression.function.UnsupportedAttribute;
import org.elasticsearch.xpack.esql.plan.logical.Drop;
import org.elasticsearch.xpack.esql.plan.logical.Enrich;
import org.elasticsearch.xpack.esql.plan.logical.EsqlUnresolvedRelation;
import org.elasticsearch.xpack.esql.plan.logical.Eval;
import org.elasticsearch.xpack.esql.plan.logical.Keep;
import org.elasticsearch.xpack.esql.plan.logical.MvExpand;
import org.elasticsearch.xpack.esql.plan.logical.Rename;
import org.elasticsearch.xpack.esql.plan.logical.local.EsqlProject;
import org.elasticsearch.xpack.esql.stats.FeatureMetric;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.analyzer.AnalyzerRules;
import org.elasticsearch.xpack.ql.analyzer.AnalyzerRules.BaseAnalyzerRule;
import org.elasticsearch.xpack.ql.analyzer.AnalyzerRules.ParameterizedAnalyzerRule;
import org.elasticsearch.xpack.ql.common.Failure;
import org.elasticsearch.xpack.ql.expression.Alias;
import org.elasticsearch.xpack.ql.expression.Attribute;
import org.elasticsearch.xpack.ql.expression.EmptyAttribute;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Expressions;
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.expression.NamedExpression;
import org.elasticsearch.xpack.ql.expression.Nullability;
import org.elasticsearch.xpack.ql.expression.ReferenceAttribute;
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;
import org.elasticsearch.xpack.ql.expression.UnresolvedStar;
import org.elasticsearch.xpack.ql.expression.function.UnresolvedFunction;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparison;
import org.elasticsearch.xpack.ql.index.EsIndex;
import org.elasticsearch.xpack.ql.index.IndexResolution;
import org.elasticsearch.xpack.ql.plan.TableIdentifier;
import org.elasticsearch.xpack.ql.plan.logical.Aggregate;
import org.elasticsearch.xpack.ql.plan.logical.EsRelation;
import org.elasticsearch.xpack.ql.plan.logical.Limit;
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.ql.plan.logical.Project;
import org.elasticsearch.xpack.ql.rule.ParameterizedRule;
import org.elasticsearch.xpack.ql.rule.ParameterizedRuleExecutor;
import org.elasticsearch.xpack.ql.rule.Rule;
import org.elasticsearch.xpack.ql.rule.RuleExecutor;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.elasticsearch.xpack.ql.type.EsField;
import org.elasticsearch.xpack.ql.type.InvalidMappedField;
import org.elasticsearch.xpack.ql.type.UnsupportedEsField;
import org.elasticsearch.xpack.ql.util.CollectionUtils;
import org.elasticsearch.xpack.ql.util.StringUtils;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.Collections.singletonList;
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
import static org.elasticsearch.xpack.esql.stats.FeatureMetric.LIMIT;
import static org.elasticsearch.xpack.ql.analyzer.AnalyzerRules.resolveFunction;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.NESTED;
public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerContext> {
static final List<Attribute> NO_FIELDS = List.of(
new ReferenceAttribute(Source.EMPTY, "<no-fields>", DataTypes.NULL, null, Nullability.TRUE, null, false)
);
private static final Iterable<RuleExecutor.Batch<LogicalPlan>> rules;
static {
var resolution = new Batch<>(
"Resolution",
new ResolveTable(),
new ResolveEnrich(),
new ResolveRefs(),
new ResolveFunctions(),
new RemoveDuplicateProjections()
);
var finish = new Batch<>("Finish Analysis", Limiter.ONCE, new AddImplicitLimit(), new PromoteStringsInDateComparisons());
rules = List.of(resolution, finish);
}
private final Verifier verifier;
public Analyzer(AnalyzerContext context, Verifier verifier) {
super(context);
this.verifier = verifier;
}
public LogicalPlan analyze(LogicalPlan plan) {
BitSet partialMetrics = new BitSet(FeatureMetric.values().length);
return verify(execute(plan), gatherPreAnalysisMetrics(plan, partialMetrics));
}
public LogicalPlan verify(LogicalPlan plan, BitSet partialMetrics) {
Collection<Failure> failures = verifier.verify(plan, partialMetrics);
if (failures.isEmpty() == false) {
throw new VerificationException(failures);
}
return plan;
}
@Override
protected Iterable<RuleExecutor.Batch<LogicalPlan>> batches() {
return rules;
}
private static class ResolveTable extends ParameterizedAnalyzerRule<EsqlUnresolvedRelation, AnalyzerContext> {
@Override
protected LogicalPlan rule(EsqlUnresolvedRelation plan, AnalyzerContext context) {
if (context.indexResolution().isValid() == false) {
return plan.unresolvedMessage().equals(context.indexResolution().toString())
? plan
: new EsqlUnresolvedRelation(plan.source(), plan.table(), plan.metadataFields(), context.indexResolution().toString());
}
TableIdentifier table = plan.table();
if (context.indexResolution().matches(table.index()) == false) {
new EsqlUnresolvedRelation(
plan.source(),
plan.table(),
plan.metadataFields(),
"invalid [" + table + "] resolution to [" + context.indexResolution() + "]"
);
}
EsIndex esIndex = context.indexResolution().get();
var attributes = mappingAsAttributes(plan.source(), esIndex.mapping());
attributes.addAll(plan.metadataFields());
return new EsRelation(plan.source(), esIndex, attributes.isEmpty() ? NO_FIELDS : attributes);
}
}
/**
* Specific flattening method, different from the default EsRelation that:
* 1. takes care of data type widening (for certain types)
* 2. drops the object and keyword hierarchy
*/
private static List<Attribute> mappingAsAttributes(Source source, Map<String, EsField> mapping) {
var list = new ArrayList<Attribute>();
mappingAsAttributes(list, source, null, mapping);
list.sort(Comparator.comparing(Attribute::qualifiedName));
return list;
}
private static void mappingAsAttributes(List<Attribute> list, Source source, String parentName, Map<String, EsField> mapping) {
for (Map.Entry<String, EsField> entry : mapping.entrySet()) {
String name = entry.getKey();
EsField t = entry.getValue();
if (t != null) {
name = parentName == null ? name : parentName + "." + name;
var fieldProperties = t.getProperties();
// widen the data type
var type = EsqlDataTypes.widenSmallNumericTypes(t.getDataType());
// due to a bug also copy the field since the Attribute hierarchy extracts the data type
// directly even if the data type is passed explicitly
if (type != t.getDataType()) {
t = new EsField(t.getName(), type, t.getProperties(), t.isAggregatable(), t.isAlias());
}
// primitive branch
if (EsqlDataTypes.isPrimitive(type)) {
Attribute attribute;
if (t instanceof UnsupportedEsField uef) {
attribute = new UnsupportedAttribute(source, name, uef);
} else {
attribute = new FieldAttribute(source, null, name, t);
}
list.add(attribute);
}
// allow compound object even if they are unknown (but not NESTED)
if (type != NESTED && fieldProperties.isEmpty() == false) {
mappingAsAttributes(list, source, name, fieldProperties);
}
}
}
}
private static class ResolveEnrich extends ParameterizedAnalyzerRule<Enrich, AnalyzerContext> {
@Override
protected LogicalPlan rule(Enrich plan, AnalyzerContext context) {
if (plan.policyName().resolved() == false) {
// the policy does not exist
return plan;
}
String policyName = (String) plan.policyName().fold();
EnrichPolicyResolution policyRes = context.enrichResolution()
.resolvedPolicies()
.stream()
.filter(x -> x.policyName().equals(policyName))
.findFirst()
.orElse(new EnrichPolicyResolution(policyName, null, null));
IndexResolution idx = policyRes.index();
EnrichPolicy policy = policyRes.policy();
var policyNameExp = policy == null || idx == null
? new UnresolvedAttribute(
plan.policyName().source(),
policyName,
null,
unresolvedPolicyError(policyName, context.enrichResolution())
)
: plan.policyName();
var matchField = policy != null && (plan.matchField() == null || plan.matchField() instanceof EmptyAttribute)
? new UnresolvedAttribute(plan.source(), policy.getMatchField())
: plan.matchField();
List<NamedExpression> enrichFields = policy == null || idx == null
? (plan.enrichFields() == null ? List.of() : plan.enrichFields())
: calculateEnrichFields(
plan.source(),
policyName,
mappingAsAttributes(plan.source(), idx.get().mapping()),
plan.enrichFields(),
policy
);
return new Enrich(plan.source(), plan.child(), plan.mode(), policyNameExp, matchField, policyRes, enrichFields);
}
private String unresolvedPolicyError(String policyName, EnrichResolution enrichResolution) {
List<String> potentialMatches = StringUtils.findSimilar(policyName, enrichResolution.existingPolicies());
String msg = "unresolved enrich policy [" + policyName + "]";
if (CollectionUtils.isEmpty(potentialMatches) == false) {
msg += ", did you mean "
+ (potentialMatches.size() == 1 ? "[" + potentialMatches.get(0) + "]" : "any of " + potentialMatches)
+ "?";
}
return msg;
}
public static List<NamedExpression> calculateEnrichFields(
Source source,
String policyName,
List<Attribute> mapping,
List<NamedExpression> enrichFields,
EnrichPolicy policy
) {
Set<String> policyEnrichFieldSet = new HashSet<>(policy.getEnrichFields());
Map<String, Attribute> fieldMap = mapping.stream()
.filter(e -> policyEnrichFieldSet.contains(e.name()))
.collect(Collectors.toMap(NamedExpression::name, Function.identity()));
List<NamedExpression> result = new ArrayList<>();
if (enrichFields == null || enrichFields.isEmpty()) {
// use the policy to infer the enrich fields
for (String enrichFieldName : policy.getEnrichFields()) {
result.add(createEnrichFieldExpression(source, policyName, fieldMap, enrichFieldName));
}
} else {
for (NamedExpression enrichField : enrichFields) {
String enrichFieldName = Expressions.name(enrichField instanceof Alias a ? a.child() : enrichField);
NamedExpression field = createEnrichFieldExpression(source, policyName, fieldMap, enrichFieldName);
result.add(enrichField instanceof Alias a ? new Alias(a.source(), a.name(), field) : field);
}
}
return result;
}
private static NamedExpression createEnrichFieldExpression(
Source source,
String policyName,
Map<String, Attribute> fieldMap,
String enrichFieldName
) {
Attribute mappedField = fieldMap.get(enrichFieldName);
if (mappedField == null) {
String msg = "Enrich field [" + enrichFieldName + "] not found in enrich policy [" + policyName + "]";
List<String> similar = StringUtils.findSimilar(enrichFieldName, fieldMap.keySet());
if (CollectionUtils.isEmpty(similar) == false) {
msg += ", did you mean " + (similar.size() == 1 ? "[" + similar.get(0) + "]" : "any of " + similar) + "?";
}
return new UnresolvedAttribute(source, enrichFieldName, null, msg);
} else {
return new ReferenceAttribute(source, enrichFieldName, mappedField.dataType(), null, Nullability.TRUE, null, false);
}
}
}
private static class ResolveRefs extends BaseAnalyzerRule {
@Override
protected LogicalPlan doRule(LogicalPlan plan) {
final List<Attribute> childrenOutput = new ArrayList<>();
for (LogicalPlan child : plan.children()) {
var output = child.output();
childrenOutput.addAll(output);
}
if (plan instanceof Drop d) {
return resolveDrop(d, childrenOutput);
}
if (plan instanceof Rename r) {
return resolveRename(r, childrenOutput);
}
if (plan instanceof Keep p) {
return resolveKeep(p, childrenOutput);
}
if (plan instanceof Eval p) {
return resolveEval(p, childrenOutput);
}
if (plan instanceof Enrich p) {
return resolveEnrich(p, childrenOutput);
}
if (plan instanceof MvExpand p) {
return resolveMvExpand(p, childrenOutput);
}
return plan.transformExpressionsUp(UnresolvedAttribute.class, ua -> maybeResolveAttribute(ua, childrenOutput));
}
private LogicalPlan resolveMvExpand(MvExpand p, List<Attribute> childrenOutput) {
if (p.target() instanceof UnresolvedAttribute ua) {
Attribute resolved = maybeResolveAttribute(ua, childrenOutput);
if (resolved == ua) {
return p;
}
return new MvExpand(
p.source(),
p.child(),
resolved,
resolved.resolved()
? new ReferenceAttribute(
resolved.source(),
resolved.name(),
resolved.dataType(),
null,
resolved.nullable(),
null,
false
)
: resolved
);
}
return p;
}
private Attribute maybeResolveAttribute(UnresolvedAttribute ua, List<Attribute> childrenOutput) {
if (ua.customMessage()) {
return ua;
}
return resolveAttribute(ua, childrenOutput);
}
private Attribute resolveAttribute(UnresolvedAttribute ua, List<Attribute> childrenOutput) {
Attribute resolved = ua;
var named = resolveAgainstList(ua, childrenOutput);
// if resolved, return it; otherwise keep it in place to be resolved later
if (named.size() == 1) {
resolved = named.get(0);
if (log.isTraceEnabled() && resolved.resolved()) {
log.trace("Resolved {} to {}", ua, resolved);
}
} else {
if (named.size() > 0) {
resolved = ua.withUnresolvedMessage("Resolved [" + ua + "] unexpectedly to multiple attributes " + named);
}
}
return resolved;
}
private LogicalPlan resolveEval(Eval eval, List<Attribute> childOutput) {
List<Attribute> allResolvedInputs = new ArrayList<>(childOutput);
List<Alias> newFields = new ArrayList<>();
boolean changed = false;
for (Alias field : eval.fields()) {
Alias result = (Alias) field.transformUp(UnresolvedAttribute.class, ua -> resolveAttribute(ua, allResolvedInputs));
changed |= result != field;
newFields.add(result);
if (result.resolved()) {
// for proper resolution, duplicate attribute names are problematic, only last occurrence matters
Attribute existing = allResolvedInputs.stream()
.filter(attr -> attr.name().equals(result.name()))
.findFirst()
.orElse(null);
if (existing != null) {
allResolvedInputs.remove(existing);
}
allResolvedInputs.add(result.toAttribute());
}
}
return changed ? new Eval(eval.source(), eval.child(), newFields) : eval;
}
/**
* resolve each item manually.
*
* Fields are added in the order they appear.
*
* If one field matches multiple expressions, the following precedence rules apply (higher to lower):
* 1. complete field name (ie. no wildcards)
* 2. partial wildcard expressions (eg. fieldNam*)
* 3. wildcard only (ie. *)
*
* If a field name matches multiple expressions with the same precedence, last one is used.
*
* A few examples below:
*
* // full name
* row foo = 1, bar = 2 | keep foo, bar, foo -> bar, foo
*
* // the full name has precedence on wildcard expression
* row foo = 1, bar = 2 | keep foo, bar, foo* -> foo, bar
*
* // the two wildcard expressions have the same priority, even though the first one is more specific
* // so last one wins
* row foo = 1, bar = 2 | keep foo*, bar, fo* -> bar, foo
*
* // * has the lowest priority
* row foo = 1, bar = 2 | keep *, foo -> bar, foo
* row foo = 1, bar = 2 | keep foo, * -> foo, bar
* row foo = 1, bar = 2 | keep bar*, foo, * -> bar, foo
*
*
* @param p
* @param childOutput
* @return
*/
private LogicalPlan resolveKeep(Project p, List<Attribute> childOutput) {
List<NamedExpression> resolvedProjections = new ArrayList<>();
var projections = p.projections();
// start with projections
// no projection specified or just *
if (projections.isEmpty() || (projections.size() == 1 && projections.get(0) instanceof UnresolvedStar)) {
resolvedProjections.addAll(childOutput);
}
// otherwise resolve them
else {
Map<NamedExpression, Integer> priorities = new LinkedHashMap<>();
for (Attribute attribute : childOutput) {
for (var proj : projections) {
List<Attribute> resolved;
int priority;
if (proj instanceof UnresolvedStar) {
resolved = childOutput;
priority = 2;
} else if (proj instanceof UnresolvedAttribute ua) {
resolved = resolveAgainstList(ua, childOutput);
priority = Regex.isSimpleMatchPattern(ua.name()) ? 1 : 0;
} else {
resolved = List.of(attribute);
priority = 0;
}
for (Attribute attr : resolved) {
Integer previousPrio = priorities.get(attr);
if (previousPrio == null || previousPrio >= priority) {
priorities.remove(attr);
priorities.put(attr, priority);
}
}
}
}
resolvedProjections = new ArrayList<>(priorities.keySet());
}
return new EsqlProject(p.source(), p.child(), resolvedProjections);
}
private LogicalPlan resolveDrop(Drop drop, List<Attribute> childOutput) {
List<NamedExpression> resolvedProjections = new ArrayList<>(childOutput);
for (var ne : drop.removals()) {
var resolved = ne instanceof UnresolvedAttribute ua ? resolveAgainstList(ua, childOutput) : singletonList(ne);
// the return list might contain either resolved elements or unresolved ones.
// if things are resolved, remove them - if not add them to the list to trip the Verifier;
// thus make sure to remove the intersection but add the unresolved difference (if any).
// so, remove things that are in common
resolvedProjections.removeIf(resolved::contains);
// but add non-projected, unresolved extras to later trip the Verifier.
resolved.forEach(r -> {
if (r.resolved() == false && r instanceof UnsupportedAttribute == false) {
resolvedProjections.add(r);
}
});
}
return new EsqlProject(drop.source(), drop.child(), resolvedProjections);
}
private LogicalPlan resolveRename(Rename rename, List<Attribute> childrenOutput) {
List<NamedExpression> projections = new ArrayList<>(childrenOutput);
int renamingsCount = rename.renamings().size();
List<NamedExpression> unresolved = new ArrayList<>(renamingsCount);
Map<String, String> reverseAliasing = new HashMap<>(renamingsCount); // `| rename a as x` => map(a: x)
rename.renamings().forEach(alias -> {
// skip NOPs: `| rename a as a`
if (alias.child() instanceof UnresolvedAttribute ua && alias.name().equals(ua.name()) == false) {
// remove attributes overwritten by a renaming: `| keep a, b, c | rename a as b`
projections.removeIf(x -> x.name().equals(alias.name()));
var resolved = maybeResolveAttribute(ua, childrenOutput);
if (resolved instanceof UnsupportedAttribute || resolved.resolved()) {
var realiased = (NamedExpression) alias.replaceChildren(List.of(resolved));
projections.replaceAll(x -> x.equals(resolved) ? realiased : x);
childrenOutput.removeIf(x -> x.equals(resolved));
reverseAliasing.put(resolved.name(), alias.name());
} else { // remained UnresolvedAttribute
// is the current alias referencing a previously declared alias?
boolean updated = false;
if (reverseAliasing.containsValue(resolved.name())) {
for (var li = projections.listIterator(); li.hasNext();) {
// does alias still exist? i.e. it hasn't been renamed again (`| rename a as b, b as c, b as d`)
if (li.next() instanceof Alias a && a.name().equals(resolved.name())) {
reverseAliasing.put(resolved.name(), alias.name());
// update aliased projection in place
li.set((NamedExpression) alias.replaceChildren(a.children()));
updated = true;
break;
}
}
}
if (updated == false) {
var u = resolved;
var previousAliasName = reverseAliasing.get(resolved.name());
if (previousAliasName != null) {
String message = format(
null,
"Column [{}] renamed to [{}] and is no longer available [{}]",
resolved.name(),
previousAliasName,
alias.sourceText()
);
u = ua.withUnresolvedMessage(message);
}
unresolved.add(u);
}
}
}
});
// add unresolved renamings to later trip the Verifier.
projections.addAll(unresolved);
return new EsqlProject(rename.source(), rename.child(), projections);
}
private LogicalPlan resolveEnrich(Enrich enrich, List<Attribute> childrenOutput) {
if (enrich.matchField().toAttribute() instanceof UnresolvedAttribute ua) {
Attribute resolved = maybeResolveAttribute(ua, childrenOutput);
if (resolved.equals(ua)) {
return enrich;
}
if (resolved.resolved() && resolved.dataType() != KEYWORD) {
resolved = ua.withUnresolvedMessage(
"Unsupported type [" + resolved.dataType() + "] for enrich matching field [" + ua.name() + "]; only KEYWORD allowed"
);
}
return new Enrich(
enrich.source(),
enrich.child(),
enrich.mode(),
enrich.policyName(),
resolved,
enrich.policy(),
enrich.enrichFields()
);
}
return enrich;
}
}
private static List<Attribute> resolveAgainstList(UnresolvedAttribute u, Collection<Attribute> attrList) {
var matches = AnalyzerRules.maybeResolveAgainstList(u, attrList, false, true, Analyzer::handleSpecialFields);
// none found - add error message
if (matches.isEmpty()) {
UnresolvedAttribute unresolved;
var name = u.name();
if (Regex.isSimpleMatchPattern(name)) {
unresolved = u.withUnresolvedMessage(format(null, "No match found for [{}]", name));
} else {
Set<String> names = new HashSet<>(attrList.size());
for (var a : attrList) {
String nameCandidate = a.name();
if (EsqlDataTypes.isPrimitive(a.dataType())) {
names.add(nameCandidate);
}
}
unresolved = u.withUnresolvedMessage(UnresolvedAttribute.errorMessage(name, StringUtils.findSimilar(name, names)));
}
return singletonList(unresolved);
}
return matches;
}
private static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute named) {
if (named instanceof FieldAttribute fa) {
// incompatible mappings
var field = fa.field();
if (field instanceof InvalidMappedField imf) {
named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] due to ambiguities being " + imf.errorMessage());
}
}
return named;
}
private static class ResolveFunctions extends ParameterizedAnalyzerRule<LogicalPlan, AnalyzerContext> {
@Override
protected LogicalPlan rule(LogicalPlan plan, AnalyzerContext context) {
return plan.transformExpressionsUp(
UnresolvedFunction.class,
uf -> resolveFunction(uf, context.configuration(), context.functionRegistry())
);
}
}
/**
* Rule that removes duplicate projects - this is done as a separate rule to allow
* full validation of the node before looking at the duplication.
* The duplication needs to be addressed to avoid ambiguity errors from commands further down
* the line.
*/
private static class RemoveDuplicateProjections extends BaseAnalyzerRule {
@Override
protected boolean skipResolved() {
return false;
}
@Override
protected LogicalPlan doRule(LogicalPlan plan) {
if (plan.resolved()) {
if (plan instanceof Aggregate agg) {
plan = removeAggDuplicates(agg);
}
}
return plan;
}
private static LogicalPlan removeAggDuplicates(Aggregate agg) {
var groupings = agg.groupings();
var newGroupings = new LinkedHashSet<>(groupings);
// reuse existing objects
groupings = newGroupings.size() == groupings.size() ? groupings : new ArrayList<>(newGroupings);
var aggregates = agg.aggregates();
var newAggregates = new ArrayList<>(aggregates);
var nameSet = Sets.newHashSetWithExpectedSize(newAggregates.size());
// remove duplicates in reverse to preserve the last one appearing
for (int i = newAggregates.size() - 1; i >= 0; i--) {
var aggregate = newAggregates.get(i);
if (nameSet.add(aggregate.name()) == false) {
newAggregates.remove(i);
}
}
// reuse existing objects
aggregates = newAggregates.size() == aggregates.size() ? aggregates : newAggregates;
// replace aggregate if needed
agg = (groupings == agg.groupings() && newAggregates == agg.aggregates())
? agg
: new Aggregate(agg.source(), agg.child(), groupings, aggregates);
return agg;
}
}
private static class AddImplicitLimit extends ParameterizedRule<LogicalPlan, LogicalPlan, AnalyzerContext> {
@Override
public LogicalPlan apply(LogicalPlan logicalPlan, AnalyzerContext context) {
List<LogicalPlan> limits = logicalPlan.collectFirstChildren(Limit.class::isInstance);
int limit;
if (limits.isEmpty()) {
HeaderWarning.addWarning(
"No limit defined, adding default limit of [{}]",
context.configuration().resultTruncationDefaultSize()
);
limit = context.configuration().resultTruncationDefaultSize(); // user provided no limit: cap to a default
} else {
limit = context.configuration().resultTruncationMaxSize(); // user provided a limit: cap result entries to the max
}
return new Limit(Source.EMPTY, new Literal(Source.EMPTY, limit, DataTypes.INTEGER), logicalPlan);
}
}
private static class PromoteStringsInDateComparisons extends Rule<LogicalPlan, LogicalPlan> {
@Override
public LogicalPlan apply(LogicalPlan plan) {
return plan.transformExpressionsUp(BinaryComparison.class, PromoteStringsInDateComparisons::promote);
}
private static Expression promote(BinaryComparison cmp) {
if (cmp.resolved() == false) {
return cmp;
}
var left = cmp.left();
var right = cmp.right();
boolean modified = false;
if (left.dataType() == DATETIME) {
if (right.dataType() == KEYWORD && right.foldable()) {
right = stringToDate(right);
modified = true;
}
} else {
if (right.dataType() == DATETIME) {
if (left.dataType() == KEYWORD && left.foldable()) {
left = stringToDate(left);
modified = true;
}
}
}
return modified ? cmp.replaceChildren(List.of(left, right)) : cmp;
}
private static Expression stringToDate(Expression stringExpression) {
var str = stringExpression.fold().toString();
Long millis = null;
// TODO: better control over this string format - do we want this to be flexible or always redirect folks to use date parsing
try {
millis = str == null ? null : DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(str);
} catch (Exception ex) { // in case of exception, millis will be null which will trigger an error
}
var source = stringExpression.source();
Expression result;
if (millis == null) {
var errorMessage = format(null, "Invalid date [{}]", str);
result = new UnresolvedAttribute(source, source.text(), null, errorMessage);
} else {
result = new Literal(source, millis, DATETIME);
}
return result;
}
}
private BitSet gatherPreAnalysisMetrics(LogicalPlan plan, BitSet b) {
// count only the explicit "limit" the user added, otherwise all queries will have a "limit" and telemetry won't reflect reality
if (plan.collectFirstChildren(Limit.class::isInstance).isEmpty() == false) {
b.set(LIMIT.ordinal());
}
plan.forEachDown(p -> FeatureMetric.set(p, b));
return b;
}
}