-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
Verifier.java
911 lines (799 loc) · 41.6 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
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/*
* 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.sql.analysis.analyzer;
import org.elasticsearch.common.collect.Tuple;
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.Attribute;
import org.elasticsearch.xpack.ql.expression.AttributeMap;
import org.elasticsearch.xpack.ql.expression.AttributeSet;
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.ReferenceAttribute;
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;
import org.elasticsearch.xpack.ql.expression.function.Function;
import org.elasticsearch.xpack.ql.expression.function.Functions;
import org.elasticsearch.xpack.ql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.ql.expression.function.grouping.GroupingFunction;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.predicate.fulltext.FullTextPredicate;
import org.elasticsearch.xpack.ql.expression.predicate.logical.BinaryLogic;
import org.elasticsearch.xpack.ql.expression.predicate.logical.Not;
import org.elasticsearch.xpack.ql.expression.predicate.nulls.IsNotNull;
import org.elasticsearch.xpack.ql.expression.predicate.nulls.IsNull;
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.tree.Node;
import org.elasticsearch.xpack.ql.type.DataType;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.elasticsearch.xpack.ql.type.EsField;
import org.elasticsearch.xpack.ql.util.Holder;
import org.elasticsearch.xpack.ql.util.StringUtils;
import org.elasticsearch.xpack.sql.expression.Exists;
import org.elasticsearch.xpack.sql.expression.function.Score;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Kurtosis;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Max;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Min;
import org.elasticsearch.xpack.sql.expression.function.aggregate.NumericAggregate;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Skewness;
import org.elasticsearch.xpack.sql.expression.function.aggregate.TopHits;
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;
import org.elasticsearch.xpack.sql.plan.logical.Distinct;
import org.elasticsearch.xpack.sql.plan.logical.LocalRelation;
import org.elasticsearch.xpack.sql.plan.logical.Pivot;
import org.elasticsearch.xpack.sql.plan.logical.command.Command;
import org.elasticsearch.xpack.sql.stats.FeatureMetric;
import org.elasticsearch.xpack.sql.stats.Metrics;
import org.elasticsearch.xpack.sql.type.SqlDataTypes;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import static java.util.stream.Collectors.toMap;
import static org.elasticsearch.xpack.ql.analyzer.VerifierChecks.checkFilterConditionType;
import static org.elasticsearch.xpack.ql.common.Failure.fail;
import static org.elasticsearch.xpack.ql.type.DataTypes.BINARY;
import static org.elasticsearch.xpack.ql.util.CollectionUtils.combine;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.COMMAND;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.GROUPBY;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.HAVING;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.LIMIT;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.LOCAL;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.ORDERBY;
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.WHERE;
import static org.elasticsearch.xpack.sql.type.SqlDataTypes.GEO_SHAPE;
import static org.elasticsearch.xpack.sql.type.SqlDataTypes.SHAPE;
/**
* The verifier has the role of checking the analyzed tree for failures and build a list of failures following this check.
* It is created in the plan executor along with the metrics instance passed as constructor parameter.
*/
public final class Verifier {
private final Metrics metrics;
public Verifier(Metrics metrics) {
this.metrics = metrics;
}
public Map<Node<?>, String> verifyFailures(LogicalPlan plan) {
Collection<Failure> failures = verify(plan);
return failures.stream().collect(toMap(Failure::node, Failure::message));
}
Collection<Failure> verify(LogicalPlan plan) {
Set<Failure> failures = new LinkedHashSet<>();
// start bottom-up
plan.forEachUp(p -> {
if (p.analyzed()) {
return;
}
// if the children are unresolved, so will this node; counting it will only add noise
if (p.childrenResolved() == false) {
return;
}
Set<Failure> localFailures = new LinkedHashSet<>();
//
// First handle usual suspects
//
if (p instanceof Unresolvable) {
localFailures.add(fail(p, ((Unresolvable) p).unresolvedMessage()));
} else if (p instanceof Distinct) {
localFailures.add(fail(p, "SELECT DISTINCT is not yet supported"));
} else {
// then take a look at the expressions
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;
}
// again the usual suspects
if (ae instanceof Unresolvable) {
// handle Attributes different to provide more context
if (ae instanceof UnresolvedAttribute) {
UnresolvedAttribute ua = (UnresolvedAttribute) ae;
// only work out the synonyms for raw unresolved attributes
if (ua.customMessage() == false) {
boolean useQualifier = ua.qualifier() != null;
List<String> potentialMatches = new ArrayList<>();
for (Attribute a : p.inputSet()) {
String nameCandidate = useQualifier ? a.qualifiedName() : a.name();
// add only primitives (object types would only result in another error)
if (DataTypes.isUnsupported(a.dataType()) == false && DataTypes.isPrimitive(a.dataType())) {
potentialMatches.add(nameCandidate);
}
}
List<String> matches = StringUtils.findSimilar(ua.qualifiedName(), potentialMatches);
if (matches.isEmpty() == false) {
ae = ua.withUnresolvedMessage(UnresolvedAttribute.errorMessage(ua.qualifiedName(), matches));
}
}
}
localFailures.add(fail(ae, ((Unresolvable) ae).unresolvedMessage()));
return;
}
// type resolution
if (ae.typeResolved().unresolved()) {
localFailures.add(fail(ae, ae.typeResolved().message()));
} else if (ae instanceof Exists) {
localFailures.add(fail(ae, "EXISTS is not yet supported"));
}
});
});
}
failures.addAll(localFailures);
});
// Concrete verifications
// if there are no (major) unresolved failures, do more in-depth analysis
if (failures.isEmpty()) {
Set<Failure> localFailures = new LinkedHashSet<>();
AttributeMap.Builder<Expression> collectRefs = AttributeMap.builder();
checkFullTextSearchInSelect(plan, localFailures);
// collect Attribute sources
// only Aliases are interesting since these are the only ones that hide expressions
// FieldAttribute for example are self replicating.
plan.forEachExpressionUp(Alias.class, a -> collectRefs.put(a.toAttribute(), a.child()));
AttributeMap<Expression> attributeRefs = collectRefs.build();
// for filtering out duplicated errors
final Set<LogicalPlan> groupingFailures = new LinkedHashSet<>();
plan.forEachDown(p -> {
if (p.analyzed()) {
return;
}
// if the children are unresolved, so will this node; counting it will only add noise
if (p.childrenResolved() == false) {
return;
}
checkFilterConditionType(p, localFailures);
checkGroupingFunctionInGroupBy(p, localFailures);
checkFilterOnAggs(p, localFailures, attributeRefs);
checkFilterOnGrouping(p, localFailures, attributeRefs);
if (groupingFailures.contains(p) == false) {
checkGroupBy(p, localFailures, attributeRefs, groupingFailures);
}
checkForScoreInsideFunctions(p, localFailures);
checkNestedUsedInGroupByOrHavingOrWhereOrOrderBy(p, localFailures, attributeRefs);
checkForGeoFunctionsOnDocValues(p, localFailures);
checkPivot(p, localFailures, attributeRefs);
checkMatrixStats(p, localFailures);
checkCastOnInexact(p, localFailures);
checkBinaryHasDocValues(p, localFailures);
// everything checks out
// mark the plan as analyzed
if (localFailures.isEmpty()) {
p.setAnalyzed();
}
failures.addAll(localFailures);
});
}
// gather metrics
if (failures.isEmpty()) {
BitSet b = new BitSet(FeatureMetric.values().length);
plan.forEachDown(p -> {
if (p instanceof Aggregate) {
b.set(GROUPBY.ordinal());
} else if (p instanceof OrderBy) {
b.set(ORDERBY.ordinal());
} else if (p instanceof Filter) {
if (((Filter) p).child() instanceof Aggregate) {
b.set(HAVING.ordinal());
} else {
b.set(WHERE.ordinal());
}
} else if (p instanceof Limit) {
b.set(LIMIT.ordinal());
} else if (p instanceof LocalRelation) {
b.set(LOCAL.ordinal());
} else if (p instanceof Command) {
b.set(COMMAND.ordinal());
}
});
for (int i = b.nextSetBit(0); i >= 0; i = b.nextSetBit(i + 1)) {
metrics.inc(FeatureMetric.values()[i]);
}
}
return failures;
}
private void checkFullTextSearchInSelect(LogicalPlan plan, Set<Failure> localFailures) {
plan.forEachUp(Project.class, p -> {
for (NamedExpression ne : p.projections()) {
ne.forEachUp(FullTextPredicate.class, (e) ->
localFailures.add(fail(e, "Cannot use MATCH() or QUERY() full-text search " +
"functions in the SELECT clause"))
);
}
});
}
/**
* Check validity of Aggregate/GroupBy.
* This rule is needed for multiple reasons:
* 1. a user might specify an invalid aggregate (SELECT foo GROUP BY bar)
* 2. the ORDER BY/HAVING might contain a non-grouped attribute. This is typically
* caught by the Analyzer however if wrapped in a function (ABS()) it gets resolved
* (because the expression gets resolved little by little without being pushed down,
* without the Analyzer modifying anything.
* 2a. HAVING also requires an Aggregate function
* 3. composite agg (used for GROUP BY) allows ordering only on the group keys
*/
private static boolean checkGroupBy(LogicalPlan p, Set<Failure> localFailures, AttributeMap<Expression> attributeRefs,
Set<LogicalPlan> groupingFailures) {
return checkGroupByInexactField(p, localFailures)
&& checkGroupByAgg(p, localFailures, attributeRefs)
&& checkGroupByOrder(p, localFailures, groupingFailures, attributeRefs)
&& checkGroupByHaving(p, localFailures, groupingFailures, attributeRefs)
&& checkGroupByTime(p, localFailures);
}
// check whether an orderBy failed or if it occurs on a non-key
private static boolean checkGroupByOrder(LogicalPlan p, Set<Failure> localFailures, Set<LogicalPlan> groupingFailures,
AttributeMap<Expression> attributeRefs) {
if (p instanceof OrderBy) {
OrderBy o = (OrderBy) p;
LogicalPlan child = o.child();
if (child instanceof Project) {
child = ((Project) child).child();
}
if (child instanceof Filter) {
child = ((Filter) child).child();
}
if (child instanceof Aggregate) {
Aggregate a = (Aggregate) child;
Map<Expression, Node<?>> missing = new LinkedHashMap<>();
o.order().forEach(oe -> {
final Expression e = oe.child();
final Expression resolvedE = attributeRefs.resolve(e, e);
// aggregates are allowed
if (Functions.isAggregate(resolvedE)) {
return;
}
// take aliases declared inside the aggregates which point to the grouping (but are not included in there)
// to correlate them to the order
List<Expression> groupingAndMatchingAggregatesAliases = new ArrayList<>(a.groupings());
a.aggregates().forEach(as -> {
if (as instanceof Alias) {
Alias al = (Alias) as;
if (Expressions.anyMatch(a.groupings(), g -> Expressions.equalsAsAttribute(al.child(), g))) {
groupingAndMatchingAggregatesAliases.add(al);
}
}
});
// Make sure you can apply functions on top of the grouped by expressions in the ORDER BY:
// e.g.: if "GROUP BY f2(f1(field))" you can "ORDER BY f4(f3(f2(f1(field))))"
//
// Also, make sure to compare attributes directly
if (resolvedE.anyMatch(expression -> Expressions.anyMatch(groupingAndMatchingAggregatesAliases,
g -> {
Expression resolvedG = attributeRefs.resolve(g, g);
resolvedG = expression instanceof Attribute ? Expressions.attribute(resolvedG) : resolvedG;
return expression.semanticEquals(resolvedG);
}))) {
return;
}
// nothing matched, cannot group by it
missing.put(e, oe);
});
if (missing.isEmpty() == false) {
String plural = missing.size() > 1 ? "s" : StringUtils.EMPTY;
// get the location of the first missing expression as the order by might be on a different line
localFailures.add(
fail(missing.values().iterator().next(),
"Cannot order by non-grouped column" + plural + " {}, expected {} or an aggregate function",
Expressions.names(missing.keySet()),
Expressions.names(a.groupings())));
groupingFailures.add(a);
return false;
}
}
}
return true;
}
private static boolean checkGroupByHaving(LogicalPlan p, Set<Failure> localFailures,
Set<LogicalPlan> groupingFailures, AttributeMap<Expression> attributeRefs) {
if (p instanceof Filter) {
Filter f = (Filter) p;
if (f.child() instanceof Aggregate) {
Aggregate a = (Aggregate) f.child();
Set<Expression> missing = new LinkedHashSet<>();
Set<Expression> unsupported = new LinkedHashSet<>();
Expression condition = f.condition();
// variation of checkGroupMatch customized for HAVING, which requires just aggregations
condition.collectFirstChildren(c -> checkGroupByHavingHasOnlyAggs(c, missing, unsupported, attributeRefs));
if (missing.isEmpty() == false) {
String plural = missing.size() > 1 ? "s" : StringUtils.EMPTY;
localFailures.add(
fail(condition, "Cannot use HAVING filter on non-aggregate" + plural + " {}; use WHERE instead",
Expressions.names(missing)));
groupingFailures.add(a);
return false;
}
if (unsupported.isEmpty() == false) {
String plural = unsupported.size() > 1 ? "s" : StringUtils.EMPTY;
localFailures.add(
fail(condition, "HAVING filter is unsupported for function" + plural + " {}",
Expressions.names(unsupported)));
groupingFailures.add(a);
return false;
}
}
}
return true;
}
private static boolean checkGroupByHavingHasOnlyAggs(Expression e, Set<Expression> missing,
Set<Expression> unsupported, AttributeMap<Expression> attributeRefs) {
// resolve FunctionAttribute to backing functions
if (e instanceof ReferenceAttribute) {
e = attributeRefs.resolve(e);
}
// scalar functions can be a binary tree
// first test the function against the grouping
// and if that fails, start unpacking hoping to find matches
if (e instanceof ScalarFunction) {
ScalarFunction sf = (ScalarFunction) e;
// unwrap function to find the base
for (Expression arg : sf.arguments()) {
arg.collectFirstChildren(c -> checkGroupByHavingHasOnlyAggs(c, missing, unsupported, attributeRefs));
}
return true;
} else if (e instanceof Score) {
// Score can't be used in having
unsupported.add(e);
return true;
} else if (e instanceof TopHits) {
// First and Last cannot be used in having
unsupported.add(e);
return true;
} else if (e instanceof Min || e instanceof Max) {
if (DataTypes.isString(((AggregateFunction) e).field().dataType())) {
// Min & Max on a Keyword field will be translated to First & Last respectively
unsupported.add(e);
return true;
}
}
// skip literals / foldable
if (e.foldable()) {
return true;
}
// skip aggs (allowed to refer to non-group columns)
if (Functions.isAggregate(e) || Functions.isGrouping(e)) {
return true;
}
// left without leaves which have to match; that's a failure since everything should be based on an agg
if (e instanceof Attribute) {
missing.add(e);
return true;
}
return false;
}
private static boolean checkGroupByInexactField(LogicalPlan p, Set<Failure> localFailures) {
if (p instanceof Aggregate) {
return onlyExactFields(((Aggregate) p).groupings(), localFailures);
}
return true;
}
// The grouping can not be an aggregate function or an inexact field (e.g. text without a keyword)
private static boolean onlyExactFields(List<Expression> expressions, Set<Failure> localFailures) {
Holder<Boolean> onlyExact = new Holder<>(Boolean.TRUE);
expressions.forEach(e -> e.forEachUp(FieldAttribute.class, c -> {
EsField.Exact exact = c.getExactInfo();
if (exact.hasExact() == false) {
localFailures.add(fail(c, "Field [{}] of data type [{}] cannot be used for grouping; {}", c.sourceText(),
c.dataType().typeName(), exact.errorMsg()));
onlyExact.set(Boolean.FALSE);
}
}));
return onlyExact.get();
}
private static boolean onlyRawFields(Iterable<? extends Expression> expressions, Set<Failure> localFailures,
AttributeMap<Expression> attributeRefs) {
Holder<Boolean> onlyExact = new Holder<>(Boolean.TRUE);
expressions.forEach(e -> e.forEachDown(c -> {
if (c instanceof ReferenceAttribute) {
c = attributeRefs.resolve(c, c);
}
if (c instanceof Function) {
localFailures.add(fail(c, "No functions allowed (yet); encountered [{}]", c.sourceText()));
onlyExact.set(Boolean.FALSE);
}
}));
return onlyExact.get();
}
private static boolean checkGroupByTime(LogicalPlan p, Set<Failure> localFailures) {
if (p instanceof Aggregate) {
Aggregate a = (Aggregate) p;
// TIME data type is not allowed for grouping key
// https://github.com/elastic/elasticsearch/issues/40639
a.groupings().forEach(f -> {
if (f.dataType() == SqlDataTypes.TIME) {
localFailures.add(fail(f, "Function [" + f.sourceText() + "] with data type [" + f.dataType().typeName() +
"] " + "cannot be used for grouping"));
}
});
}
return true;
}
// check whether plain columns specified in an agg are mentioned in the group-by
private static boolean checkGroupByAgg(LogicalPlan p, Set<Failure> localFailures, AttributeMap<Expression> attributeRefs) {
if (p instanceof Aggregate) {
Aggregate a = (Aggregate) p;
// The grouping can not be an aggregate function
a.groupings().forEach(e -> e.forEachUp(c -> {
if (Functions.isAggregate(c)) {
localFailures.add(fail(c, "Cannot use an aggregate [" + c.nodeName().toUpperCase(Locale.ROOT) + "] for grouping"));
}
if (c instanceof Score) {
localFailures.add(fail(c, "Cannot use [SCORE()] for grouping"));
}
}));
a.groupings().forEach(e -> {
if (Functions.isGrouping(e) == false) {
e.collectFirstChildren(c -> {
if (Functions.isGrouping(c)) {
localFailures.add(fail(c,
"Cannot combine [{}] grouping function inside GROUP BY, found [{}];"
+ " consider moving the expression inside the histogram",
Expressions.name(c), Expressions.name(e)));
return true;
}
return false;
});
}
});
if (localFailures.isEmpty() == false) {
return false;
}
// The agg can be:
// 1. plain column - in which case, there should be an equivalent in groupings
// 2. aggregate over non-grouped column
// 3. scalar function on top of 1 and/or 2. the function needs unfolding to make sure
// the 'source' is valid.
// Note that grouping can be done by a function (GROUP BY YEAR(date)) which means date
// cannot be used as a plain column, only YEAR(date) or aggs(?) on top of it
Map<Expression, Node<?>> missing = new LinkedHashMap<>();
a.aggregates().forEach(ne ->
ne.collectFirstChildren(c -> checkGroupMatch(c, ne, a.groupings(), missing, attributeRefs)));
if (missing.isEmpty() == false) {
String plural = missing.size() > 1 ? "s" : StringUtils.EMPTY;
localFailures.add(fail(missing.values().iterator().next(), "Cannot use non-grouped column" + plural + " {}, expected {}",
Expressions.names(missing.keySet()),
Expressions.names(a.groupings())));
return false;
}
}
return true;
}
private static boolean checkGroupMatch(Expression e, Node<?> source, List<Expression> groupings,
Map<Expression, Node<?>> missing, AttributeMap<Expression> attributeRefs) {
// 1:1 match
if (Expressions.match(groupings, e::semanticEquals)) {
return true;
}
// resolve FunctionAttribute to backing functions
if (e instanceof ReferenceAttribute) {
e = attributeRefs.resolve(e);
}
// scalar functions can be a binary tree
// first test the function against the grouping
// and if that fails, start unpacking hoping to find matches
if (e instanceof ScalarFunction) {
ScalarFunction sf = (ScalarFunction) e;
// found group for the expression
if (Expressions.anyMatch(groupings, e::semanticEquals)) {
return true;
}
// unwrap function to find the base
for (Expression arg : sf.arguments()) {
arg.collectFirstChildren(c -> checkGroupMatch(c, source, groupings, missing, attributeRefs));
}
return true;
} else if (e instanceof Score) {
// Score can't be an aggregate function
missing.put(e, source);
return true;
}
// skip literals / foldable
if (e.foldable()) {
return true;
}
// skip aggs (allowed to refer to non-group columns)
// TODO: need to check whether it's possible to agg on a field used inside a scalar for grouping
if (Functions.isAggregate(e)) {
return true;
}
// left without leaves which have to match; if not there's a failure
// make sure to match directly on the expression and not on the tree
// (since otherwise exp might match the function argument which would be incorrect)
final Expression exp = e;
if (e.children().isEmpty()) {
if (Expressions.match(groupings, c -> exp.semanticEquals(exp instanceof Attribute ? Expressions.attribute(c) : c)) == false) {
missing.put(exp, source);
}
return true;
}
return false;
}
private static void checkGroupingFunctionInGroupBy(LogicalPlan p, Set<Failure> localFailures) {
// check if the query has a grouping function (Histogram) but no GROUP BY
if (p instanceof Project) {
Project proj = (Project) p;
proj.projections().forEach(e -> e.forEachDown(GroupingFunction.class, f ->
localFailures.add(fail(f, "[{}] needs to be part of the grouping", Expressions.name(f)))));
}
// if it does have a GROUP BY, check if the groupings contain the grouping functions (Histograms)
else if (p instanceof Aggregate) {
Aggregate a = (Aggregate) p;
a.aggregates().forEach(agg -> agg.forEachDown(GroupingFunction.class, e -> {
if (a.groupings().size() == 0
|| Expressions.anyMatch(a.groupings(), g -> g instanceof Function && e.equals(g)) == false) {
localFailures.add(fail(e, "[{}] needs to be part of the grouping", Expressions.name(e)));
} else {
checkGroupingFunctionTarget(e, localFailures);
}
}));
a.groupings().forEach(g -> g.forEachDown(GroupingFunction.class, e -> {
checkGroupingFunctionTarget(e, localFailures);
}));
}
}
private static void checkGroupingFunctionTarget(GroupingFunction f, Set<Failure> localFailures) {
f.field().forEachDown(e -> {
if (e instanceof GroupingFunction) {
localFailures.add(fail(f.field(), "Cannot embed grouping functions within each other, found [{}] in [{}]",
Expressions.name(f.field()), Expressions.name(f)));
}
});
}
private static void checkFilterOnAggs(LogicalPlan p, Set<Failure> localFailures, AttributeMap<Expression> attributeRefs) {
if (p instanceof Filter) {
Filter filter = (Filter) p;
LogicalPlan filterChild = filter.child();
if (filterChild instanceof Aggregate == false) {
filter.condition().forEachDown(Expression.class, e -> {
if (Functions.isAggregate(attributeRefs.resolve(e, e))) {
if (filterChild instanceof Project) {
filter.condition().forEachDown(FieldAttribute.class,
f -> localFailures.add(fail(e, "[{}] field must appear in the GROUP BY clause or in an aggregate function",
Expressions.name(f)))
);
} else {
localFailures.add(fail(e, "Cannot use WHERE filtering on aggregate function [{}], use HAVING instead",
Expressions.name(e)));
}
}
});
}
}
}
private static void checkFilterOnGrouping(LogicalPlan p, Set<Failure> localFailures, AttributeMap<Expression> attributeRefs) {
if (p instanceof Filter) {
Filter filter = (Filter) p;
filter.condition().forEachDown(Expression.class, e -> {
if (Functions.isGrouping(attributeRefs.resolve(e, e))) {
localFailures
.add(fail(e, "Cannot filter on grouping function [{}], use its argument instead", Expressions.name(e)));
}
});
}
}
private static void checkForScoreInsideFunctions(LogicalPlan p, Set<Failure> localFailures) {
// Make sure that SCORE is only used in "top level" functions
p.forEachExpression(Function.class, f ->
f.arguments().stream()
.filter(exp -> exp.anyMatch(Score.class::isInstance))
.forEach(exp -> localFailures.add(fail(exp, "[SCORE()] cannot be an argument to a function")))
);
}
private static void checkNestedUsedInGroupByOrHavingOrWhereOrOrderBy(LogicalPlan p, Set<Failure> localFailures,
AttributeMap<Expression> attributeRefs) {
List<FieldAttribute> nested = new ArrayList<>();
Consumer<FieldAttribute> matchNested = fa -> {
if (fa.isNested()) {
nested.add(fa);
}
};
Consumer<Expression> checkForNested = e ->
attributeRefs.resolve(e, e).forEachUp(FieldAttribute.class, matchNested);
Consumer<ScalarFunction> checkForNestedInFunction = f -> f.arguments().forEach(
arg -> arg.forEachUp(FieldAttribute.class, matchNested));
// nested fields shouldn't be used in aggregates or having (yet)
p.forEachDown(Aggregate.class, a -> a.groupings().forEach(agg -> agg.forEachUp(checkForNested)));
if (nested.isEmpty() == false) {
localFailures.add(
fail(nested.get(0), "Grouping isn't (yet) compatible with nested fields " + new AttributeSet(nested).names()));
nested.clear();
}
// check in having
p.forEachDown(Filter.class, f -> f.forEachDown(Aggregate.class, a -> f.condition().forEachUp(checkForNested)));
if (nested.isEmpty() == false) {
localFailures.add(
fail(nested.get(0), "HAVING isn't (yet) compatible with nested fields " + new AttributeSet(nested).names()));
nested.clear();
}
// check in where (scalars not allowed)
p.forEachDown(Filter.class, f -> f.condition().forEachUp(e ->
attributeRefs.resolve(e, e).forEachUp(ScalarFunction.class, sf -> {
if (sf instanceof BinaryComparison == false &&
sf instanceof IsNull == false &&
sf instanceof IsNotNull == false &&
sf instanceof Not == false &&
sf instanceof BinaryLogic == false) {
checkForNestedInFunction.accept(sf);
}
})
));
if (nested.isEmpty() == false) {
localFailures.add(
fail(nested.get(0), "WHERE isn't (yet) compatible with scalar functions on nested fields " +
new AttributeSet(nested).names()));
nested.clear();
}
// check in order by (scalars not allowed)
p.forEachDown(OrderBy.class, ob -> ob.order().forEach(o -> o.forEachUp(e ->
attributeRefs.resolve(e, e).forEachUp(ScalarFunction.class, checkForNestedInFunction)
)));
if (nested.isEmpty() == false) {
localFailures.add(
fail(nested.get(0), "ORDER BY isn't (yet) compatible with scalar functions on nested fields " +
new AttributeSet(nested).names()));
}
}
/**
* Makes sure that geo shapes do not appear in filter, aggregation and sorting contexts
*/
private static void checkForGeoFunctionsOnDocValues(LogicalPlan p, Set<Failure> localFailures) {
p.forEachDown(Filter.class, f -> {
f.condition().forEachUp(FieldAttribute.class, fa -> {
if (fa.field().getDataType() == GEO_SHAPE) {
localFailures.add(fail(fa, "geo shapes cannot be used for filtering"));
}
if (fa.field().getDataType() == SHAPE) {
localFailures.add(fail(fa, "shapes cannot be used for filtering"));
}
});
});
// geo shape fields shouldn't be used in aggregates or having (yet)
p.forEachDown(Aggregate.class, a -> a.groupings().forEach(agg -> agg.forEachUp(FieldAttribute.class, fa -> {
if (fa.field().getDataType() == GEO_SHAPE) {
localFailures.add(fail(fa, "geo shapes cannot be used in grouping"));
}
if (fa.field().getDataType() == SHAPE) {
localFailures.add(fail(fa, "shapes cannot be used in grouping"));
}
})));
// geo shape fields shouldn't be used in order by clauses
p.forEachDown(OrderBy.class, o -> o.order().forEach(agg -> agg.forEachUp(FieldAttribute.class, fa -> {
if (fa.field().getDataType() == GEO_SHAPE) {
localFailures.add(fail(fa, "geo shapes cannot be used for sorting"));
}
if (fa.field().getDataType() == SHAPE) {
localFailures.add(fail(fa, "shapes cannot be used for sorting"));
}
})));
}
private static void checkPivot(LogicalPlan p, Set<Failure> localFailures, AttributeMap<Expression> attributeRefs) {
p.forEachDown(Pivot.class, pv -> {
// check only exact fields are used inside PIVOTing
if (onlyExactFields(combine(pv.groupingSet(), pv.column()), localFailures) == false
|| onlyRawFields(pv.groupingSet(), localFailures, attributeRefs) == false) {
// if that is not the case, no need to do further validation since the declaration is fundamentally wrong
return;
}
// check values
DataType colType = pv.column().dataType();
for (NamedExpression v : pv.values()) {
// check all values are foldable
Expression ex = v instanceof Alias ? ((Alias) v).child() : v;
if (ex instanceof Literal == false) {
localFailures.add(fail(v, "Non-literal [{}] found inside PIVOT values", v.name()));
}
else if (ex.foldable() && ex.fold() == null) {
localFailures.add(fail(v, "Null not allowed as a PIVOT value", v.name()));
}
// and that their type is compatible with that of the column
else if (SqlDataTypes.areCompatible(colType, v.dataType()) == false) {
localFailures.add(fail(v, "Literal [{}] of type [{}] does not match type [{}] of PIVOT column [{}]", v.name(),
v.dataType().typeName(), colType.typeName(), pv.column().sourceText()));
}
}
// check aggregate function, in particular formulas that might hide literals or scalars
pv.aggregates().forEach(a -> {
Holder<Boolean> hasAggs = new Holder<>(Boolean.FALSE);
List<Expression> aggs = a.collectFirstChildren(c -> {
// skip aggregate functions
if (Functions.isAggregate(c)) {
hasAggs.set(Boolean.TRUE);
return true;
}
if (c.children().isEmpty()) {
return true;
}
return false;
});
if (Boolean.FALSE.equals(hasAggs.get())) {
localFailures.add(fail(a, "No aggregate function found in PIVOT at [{}]", a.sourceText()));
}
// check mixture of Agg and column (wrapped in scalar)
else {
for (Expression agg : aggs) {
if (agg instanceof FieldAttribute) {
localFailures.add(fail(a, "Non-aggregate function found in PIVOT at [{}]", a.sourceText()));
}
}
}
});
});
}
private static void checkMatrixStats(LogicalPlan p, Set<Failure> localFailures) {
// MatrixStats aggregate functions cannot operates on scalars
// https://github.com/elastic/elasticsearch/issues/55344
p.forEachExpressionUp(NumericAggregate.class, s -> {
if ((s instanceof Kurtosis || s instanceof Skewness) && s.field() instanceof Function) {
localFailures.add(fail(s.field(), "[{}()] cannot be used on top of operators or scalars", s.functionName()));
}
});
}
private static void checkCastOnInexact(LogicalPlan p, Set<Failure> localFailures) {
p.forEachDown(Filter.class, f -> f.forEachExpressionUp(Cast.class, c -> {
if (c.field() instanceof FieldAttribute) {
EsField.Exact exactInfo = ((FieldAttribute) c.field()).getExactInfo();
if (exactInfo.hasExact() == false
|| ((FieldAttribute) c.field()).exactAttribute().equals(c.field()) == false) {
localFailures.add(fail(c.field(),
"[{}] of data type [{}] cannot be used for [{}()] inside the WHERE clause",
c.field().sourceText(), c.field().dataType().typeName(), c.functionName()));
}
}
}));
}
// check that any binary field used in WHERE, GROUP BY, HAVING or ORDER BY has doc_values, for ES to allow querying it
private static void checkBinaryHasDocValues(LogicalPlan plan, Set<Failure> localFailures) {
List<Tuple<FieldAttribute, String>> fields = new ArrayList<>();
plan.forEachDown(Filter.class, e -> e.condition().forEachDown(FieldAttribute.class,
f -> fields.add(Tuple.tuple(f, "for filtering"))));
plan.forEachDown(Aggregate.class, e -> e.groupings().forEach(g -> g.forEachDown(FieldAttribute.class,
f -> fields.add(Tuple.tuple(f, "in aggregations")))));
plan.forEachDown(OrderBy.class, e -> e.order().forEach(o -> o.child().forEachDown(FieldAttribute.class,
f -> fields.add(Tuple.tuple(f, "for ordering")))));
fields.stream().filter(t -> t.v1().dataType() == BINARY && t.v1().field().isAggregatable() == false).forEach(t -> {
localFailures.add(fail(t.v1(), "Binary field [" + t.v1().name() + "] cannot be used " + t.v2() + " unless it has the "
+ "doc_values setting enabled"));
});
}
}