-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
ExpressionAnalyzer.java
1038 lines (903 loc) · 42.3 KB
/
ExpressionAnalyzer.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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.sql.analyzer;
import com.facebook.presto.Session;
import com.facebook.presto.metadata.FunctionInfo;
import com.facebook.presto.metadata.FunctionRegistry;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.metadata.OperatorNotFoundException;
import com.facebook.presto.metadata.OperatorType;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.TypeSignature;
import com.facebook.presto.sql.parser.SqlParser;
import com.facebook.presto.sql.planner.DependencyExtractor;
import com.facebook.presto.sql.planner.Symbol;
import com.facebook.presto.sql.tree.ArithmeticBinaryExpression;
import com.facebook.presto.sql.tree.ArithmeticUnaryExpression;
import com.facebook.presto.sql.tree.ArrayConstructor;
import com.facebook.presto.sql.tree.AstVisitor;
import com.facebook.presto.sql.tree.BetweenPredicate;
import com.facebook.presto.sql.tree.BooleanLiteral;
import com.facebook.presto.sql.tree.Cast;
import com.facebook.presto.sql.tree.CoalesceExpression;
import com.facebook.presto.sql.tree.ComparisonExpression;
import com.facebook.presto.sql.tree.CurrentTime;
import com.facebook.presto.sql.tree.DefaultTraversalVisitor;
import com.facebook.presto.sql.tree.DoubleLiteral;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.sql.tree.Extract;
import com.facebook.presto.sql.tree.FunctionCall;
import com.facebook.presto.sql.tree.GenericLiteral;
import com.facebook.presto.sql.tree.IfExpression;
import com.facebook.presto.sql.tree.InListExpression;
import com.facebook.presto.sql.tree.InPredicate;
import com.facebook.presto.sql.tree.InputReference;
import com.facebook.presto.sql.tree.IntervalLiteral;
import com.facebook.presto.sql.tree.IsNotNullPredicate;
import com.facebook.presto.sql.tree.IsNullPredicate;
import com.facebook.presto.sql.tree.LikePredicate;
import com.facebook.presto.sql.tree.LogicalBinaryExpression;
import com.facebook.presto.sql.tree.LongLiteral;
import com.facebook.presto.sql.tree.Node;
import com.facebook.presto.sql.tree.NotExpression;
import com.facebook.presto.sql.tree.NullIfExpression;
import com.facebook.presto.sql.tree.NullLiteral;
import com.facebook.presto.sql.tree.QualifiedName;
import com.facebook.presto.sql.tree.QualifiedNameReference;
import com.facebook.presto.sql.tree.Row;
import com.facebook.presto.sql.tree.SearchedCaseExpression;
import com.facebook.presto.sql.tree.SimpleCaseExpression;
import com.facebook.presto.sql.tree.SortItem;
import com.facebook.presto.sql.tree.StringLiteral;
import com.facebook.presto.sql.tree.SubqueryExpression;
import com.facebook.presto.sql.tree.SubscriptExpression;
import com.facebook.presto.sql.tree.TimeLiteral;
import com.facebook.presto.sql.tree.TimestampLiteral;
import com.facebook.presto.sql.tree.WhenClause;
import com.facebook.presto.sql.tree.WindowFrame;
import com.facebook.presto.type.RowType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import static com.facebook.presto.metadata.FunctionRegistry.canCoerce;
import static com.facebook.presto.metadata.FunctionRegistry.getCommonSuperType;
import static com.facebook.presto.metadata.OperatorType.SUBSCRIPT;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
import static com.facebook.presto.spi.type.DateType.DATE;
import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
import static com.facebook.presto.spi.type.IntervalDayTimeType.INTERVAL_DAY_TIME;
import static com.facebook.presto.spi.type.IntervalYearMonthType.INTERVAL_YEAR_MONTH;
import static com.facebook.presto.spi.type.TimeType.TIME;
import static com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE;
import static com.facebook.presto.spi.type.TimestampType.TIMESTAMP;
import static com.facebook.presto.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.AMBIGUOUS_ATTRIBUTE;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.MISSING_ATTRIBUTE;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.MULTIPLE_FIELDS_FROM_SCALAR_SUBQUERY;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.NOT_SUPPORTED;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.TYPE_MISMATCH;
import static com.facebook.presto.sql.tree.Extract.Field.TIMEZONE_HOUR;
import static com.facebook.presto.sql.tree.Extract.Field.TIMEZONE_MINUTE;
import static com.facebook.presto.type.ArrayParametricType.ARRAY;
import static com.facebook.presto.type.RowType.RowField;
import static com.facebook.presto.type.UnknownType.UNKNOWN;
import static com.facebook.presto.util.DateTimeUtils.timeHasTimeZone;
import static com.facebook.presto.util.DateTimeUtils.timestampHasTimeZone;
import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
import static com.facebook.presto.util.Types.checkType;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Sets.newIdentityHashSet;
public class ExpressionAnalyzer
{
private final Analysis analysis;
private final Metadata metadata;
private final SqlParser sqlParser;
private final boolean experimentalSyntaxEnabled;
private final Session session;
private final Map<QualifiedName, Integer> resolvedNames = new HashMap<>();
private final IdentityHashMap<FunctionCall, FunctionInfo> resolvedFunctions = new IdentityHashMap<>();
private final IdentityHashMap<Expression, Type> expressionTypes = new IdentityHashMap<>();
private final IdentityHashMap<Expression, Type> expressionCoercions = new IdentityHashMap<>();
private final IdentityHashMap<Expression, Boolean> rowFieldReferences = new IdentityHashMap<>();
private final Set<InPredicate> subqueryInPredicates = newIdentityHashSet();
public ExpressionAnalyzer(Analysis analysis, Session session, Metadata metadata, SqlParser sqlParser, boolean experimentalSyntaxEnabled)
{
this.analysis = checkNotNull(analysis, "analysis is null");
this.session = checkNotNull(session, "session is null");
this.metadata = checkNotNull(metadata, "metadata is null");
this.sqlParser = checkNotNull(sqlParser, "sqlParser is null");
this.experimentalSyntaxEnabled = experimentalSyntaxEnabled;
}
public Map<QualifiedName, Integer> getResolvedNames()
{
return resolvedNames;
}
public IdentityHashMap<FunctionCall, FunctionInfo> getResolvedFunctions()
{
return resolvedFunctions;
}
public IdentityHashMap<Expression, Type> getExpressionTypes()
{
return expressionTypes;
}
public IdentityHashMap<Expression, Type> getExpressionCoercions()
{
return expressionCoercions;
}
public IdentityHashMap<Expression, Boolean> getRowFieldReferences()
{
return rowFieldReferences;
}
public Set<InPredicate> getSubqueryInPredicates()
{
return subqueryInPredicates;
}
/**
* @param tupleDescriptor the tuple descriptor to use to resolve QualifiedNames
* @param context the namespace context of the surrounding query
*/
public Type analyze(Expression expression, TupleDescriptor tupleDescriptor, AnalysisContext context)
{
ScalarSubqueryDetector scalarSubqueryDetector = new ScalarSubqueryDetector();
expression.accept(scalarSubqueryDetector, null);
Visitor visitor = new Visitor(tupleDescriptor);
return expression.accept(visitor, context);
}
private static class ScalarSubqueryDetector
extends DefaultTraversalVisitor<Void, Void>
{
@Override
protected Void visitInPredicate(InPredicate node, Void context)
{
Expression valueList = node.getValueList();
if (valueList instanceof SubqueryExpression) {
process(node.getValue(), context);
super.visitSubqueryExpression((SubqueryExpression) valueList, context);
}
else {
super.visitInPredicate(node, context);
}
return null;
}
@Override
protected Void visitSubqueryExpression(SubqueryExpression node, Void context)
{
throw new SemanticException(SemanticErrorCode.NOT_SUPPORTED, node, "Scalar subqueries not yet supported");
}
}
private class Visitor
extends AstVisitor<Type, AnalysisContext>
{
private final TupleDescriptor tupleDescriptor;
private Visitor(TupleDescriptor tupleDescriptor)
{
this.tupleDescriptor = checkNotNull(tupleDescriptor, "tupleDescriptor is null");
}
@Override
public Type process(Node node, @Nullable AnalysisContext context)
{
// don't double processs a node
Type type = expressionTypes.get(node);
if (type != null) {
return type;
}
return super.process(node, context);
}
@Override
protected Type visitRow(Row node, AnalysisContext context)
{
List<Type> types = node.getItems().stream()
.map((child) -> process(child, context))
.collect(toImmutableList());
Type type = new RowType(types, Optional.empty());
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitCurrentTime(CurrentTime node, AnalysisContext context)
{
if (node.getPrecision() != null) {
throw new SemanticException(NOT_SUPPORTED, node, "non-default precision not yet supported");
}
Type type;
switch (node.getType()) {
case DATE:
type = DATE;
break;
case TIME:
type = TIME_WITH_TIME_ZONE;
break;
case LOCALTIME:
type = TIME;
break;
case TIMESTAMP:
type = TIMESTAMP_WITH_TIME_ZONE;
break;
case LOCALTIMESTAMP:
type = TIMESTAMP;
break;
default:
throw new SemanticException(NOT_SUPPORTED, node, "%s not yet supported", node.getType().getName());
}
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitQualifiedNameReference(QualifiedNameReference node, AnalysisContext context)
{
List<Field> matches = tupleDescriptor.resolveFields(node.getName());
if (matches.isEmpty()) {
// TODO This is kind of hacky, instead we should change the way QualifiedNameReferences are parsed
return tryVisitRowFieldAccessor(node, context);
}
else if (matches.size() > 1) {
throw new SemanticException(AMBIGUOUS_ATTRIBUTE, node, "Column '%s' is ambiguous", node.getName());
}
Field field = Iterables.getOnlyElement(matches);
int fieldIndex = tupleDescriptor.indexOf(field);
resolvedNames.put(node.getName(), fieldIndex);
expressionTypes.put(node, field.getType());
return field.getType();
}
private Type tryVisitRowFieldAccessor(QualifiedNameReference node, AnalysisContext context)
{
if (node.getName().getParts().size() < 2) {
throw createMissingAttributeException(node);
}
QualifiedName base = new QualifiedName(node.getName().getParts().subList(0, node.getName().getParts().size() - 1));
List<Field> matches = tupleDescriptor.resolveFields(base);
if (matches.isEmpty()) {
throw createMissingAttributeException(node);
}
else if (matches.size() > 1) {
throw new SemanticException(AMBIGUOUS_ATTRIBUTE, node, "Column '%s' is ambiguous", node.getName());
}
Field field = Iterables.getOnlyElement(matches);
if (field.getType() instanceof RowType) {
RowType rowType = checkType(field.getType(), RowType.class, "field.getType()");
Type rowFieldType = null;
for (RowField rowField : rowType.getFields()) {
if (rowField.getName().equals(Optional.of(node.getName().getSuffix()))) {
rowFieldType = rowField.getType();
break;
}
}
if (rowFieldType == null) {
throw createMissingAttributeException(node);
}
int fieldIndex = tupleDescriptor.indexOf(field);
resolvedNames.put(node.getName(), fieldIndex);
expressionTypes.put(node, rowFieldType);
rowFieldReferences.put(node, true);
return rowFieldType;
}
throw createMissingAttributeException(node);
}
private SemanticException createMissingAttributeException(QualifiedNameReference node)
{
return new SemanticException(MISSING_ATTRIBUTE, node, "Column '%s' cannot be resolved", node.getName());
}
@Override
protected Type visitNotExpression(NotExpression node, AnalysisContext context)
{
coerceType(context, node.getValue(), BOOLEAN, "Value of logical NOT expression");
expressionTypes.put(node, BOOLEAN);
return BOOLEAN;
}
@Override
protected Type visitLogicalBinaryExpression(LogicalBinaryExpression node, AnalysisContext context)
{
coerceType(context, node.getLeft(), BOOLEAN, "Left side of logical expression");
coerceType(context, node.getRight(), BOOLEAN, "Right side of logical expression");
expressionTypes.put(node, BOOLEAN);
return BOOLEAN;
}
@Override
protected Type visitComparisonExpression(ComparisonExpression node, AnalysisContext context)
{
OperatorType operatorType;
if (node.getType() == ComparisonExpression.Type.IS_DISTINCT_FROM) {
operatorType = OperatorType.EQUAL;
}
else {
operatorType = OperatorType.valueOf(node.getType().name());
}
return getOperator(context, node, operatorType, node.getLeft(), node.getRight());
}
@Override
protected Type visitIsNullPredicate(IsNullPredicate node, AnalysisContext context)
{
process(node.getValue(), context);
expressionTypes.put(node, BOOLEAN);
return BOOLEAN;
}
@Override
protected Type visitIsNotNullPredicate(IsNotNullPredicate node, AnalysisContext context)
{
process(node.getValue(), context);
expressionTypes.put(node, BOOLEAN);
return BOOLEAN;
}
@Override
protected Type visitNullIfExpression(NullIfExpression node, AnalysisContext context)
{
Type firstType = process(node.getFirst(), context);
Type secondType = process(node.getSecond(), context);
if (!FunctionRegistry.getCommonSuperType(firstType, secondType).isPresent()) {
throw new SemanticException(TYPE_MISMATCH, node, "Types are not comparable with NULLIF: %s vs %s", firstType, secondType);
}
expressionTypes.put(node, firstType);
return firstType;
}
@Override
protected Type visitIfExpression(IfExpression node, AnalysisContext context)
{
coerceType(context, node.getCondition(), BOOLEAN, "IF condition");
Type type;
if (node.getFalseValue().isPresent()) {
type = coerceToSingleType(context, node, "Result types for IF must be the same: %s vs %s", node.getTrueValue(), node.getFalseValue().get());
}
else {
type = process(node.getTrueValue(), context);
}
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitSearchedCaseExpression(SearchedCaseExpression node, AnalysisContext context)
{
for (WhenClause whenClause : node.getWhenClauses()) {
coerceType(context, whenClause.getOperand(), BOOLEAN, "CASE WHEN clause");
}
Type type = coerceToSingleType(context,
"All CASE results must be the same type: %s",
getCaseResultExpressions(node.getWhenClauses(), node.getDefaultValue()));
expressionTypes.put(node, type);
for (WhenClause whenClause : node.getWhenClauses()) {
Type whenClauseType = process(whenClause.getResult(), context);
checkNotNull(whenClauseType, "Expression types does not contain an entry for %s", whenClause);
expressionTypes.put(whenClause, whenClauseType);
}
return type;
}
@Override
protected Type visitSimpleCaseExpression(SimpleCaseExpression node, AnalysisContext context)
{
for (WhenClause whenClause : node.getWhenClauses()) {
coerceToSingleType(context, node, "CASE operand type does not match WHEN clause operand type: %s vs %s", node.getOperand(), whenClause.getOperand());
}
Type type = coerceToSingleType(context,
"All CASE results must be the same type: %s",
getCaseResultExpressions(node.getWhenClauses(), node.getDefaultValue()));
expressionTypes.put(node, type);
for (WhenClause whenClause : node.getWhenClauses()) {
Type whenClauseType = process(whenClause.getResult(), context);
checkNotNull(whenClauseType, "Expression types does not contain an entry for %s", whenClause);
expressionTypes.put(whenClause, whenClauseType);
}
return type;
}
private List<Expression> getCaseResultExpressions(List<WhenClause> whenClauses, Optional<Expression> defaultValue)
{
List<Expression> resultExpressions = new ArrayList<>();
for (WhenClause whenClause : whenClauses) {
resultExpressions.add(whenClause.getResult());
}
defaultValue.ifPresent(resultExpressions::add);
return resultExpressions;
}
@Override
protected Type visitCoalesceExpression(CoalesceExpression node, AnalysisContext context)
{
Type type = coerceToSingleType(context, "All COALESCE operands must be the same type: %s", node.getOperands());
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitArithmeticUnary(ArithmeticUnaryExpression node, AnalysisContext context)
{
switch (node.getSign()) {
case PLUS:
Type type = process(node.getValue(), context);
if (!type.equals(BIGINT) && !type.equals(DOUBLE)) {
// TODO: figure out a type-agnostic way of dealing with this. Maybe add a special unary operator
// that types can chose to implement, or piggyback on the existence of the negation operator
throw new SemanticException(TYPE_MISMATCH, node, "Unary '+' operator cannot by applied to %s type", type);
}
expressionTypes.put(node, type);
break;
case MINUS:
return getOperator(context, node, OperatorType.NEGATION, node.getValue());
}
throw new UnsupportedOperationException("Unsupported unary operator: " + node.getSign());
}
@Override
protected Type visitArithmeticBinary(ArithmeticBinaryExpression node, AnalysisContext context)
{
return getOperator(context, node, OperatorType.valueOf(node.getType().name()), node.getLeft(), node.getRight());
}
@Override
protected Type visitLikePredicate(LikePredicate node, AnalysisContext context)
{
coerceType(context, node.getValue(), VARCHAR, "Left side of LIKE expression");
coerceType(context, node.getPattern(), VARCHAR, "Pattern for LIKE expression");
if (node.getEscape() != null) {
coerceType(context, node.getEscape(), VARCHAR, "Escape for LIKE expression");
}
expressionTypes.put(node, BOOLEAN);
return BOOLEAN;
}
@Override
protected Type visitSubscriptExpression(SubscriptExpression node, AnalysisContext context)
{
return getOperator(context, node, SUBSCRIPT, node.getBase(), node.getIndex());
}
@Override
protected Type visitArrayConstructor(ArrayConstructor node, AnalysisContext context)
{
Type type = coerceToSingleType(context, "All ARRAY elements must be the same type: %s", node.getValues());
Type arrayType = metadata.getTypeManager().getParameterizedType(ARRAY.getName(), ImmutableList.of(type.getTypeSignature()), ImmutableList.of());
expressionTypes.put(node, arrayType);
return arrayType;
}
@Override
protected Type visitStringLiteral(StringLiteral node, AnalysisContext context)
{
expressionTypes.put(node, VARCHAR);
return VARCHAR;
}
@Override
protected Type visitLongLiteral(LongLiteral node, AnalysisContext context)
{
expressionTypes.put(node, BIGINT);
return BIGINT;
}
@Override
protected Type visitDoubleLiteral(DoubleLiteral node, AnalysisContext context)
{
expressionTypes.put(node, DOUBLE);
return DOUBLE;
}
@Override
protected Type visitBooleanLiteral(BooleanLiteral node, AnalysisContext context)
{
expressionTypes.put(node, BOOLEAN);
return BOOLEAN;
}
@Override
protected Type visitGenericLiteral(GenericLiteral node, AnalysisContext context)
{
Type type = metadata.getType(parseTypeSignature(node.getType()));
if (type == null) {
throw new SemanticException(TYPE_MISMATCH, node, "Unknown type: " + node.getType());
}
try {
metadata.getFunctionRegistry().getCoercion(VARCHAR, type);
}
catch (IllegalArgumentException e) {
throw new SemanticException(TYPE_MISMATCH, node, "No literal form for type %s", type);
}
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitTimeLiteral(TimeLiteral node, AnalysisContext context)
{
Type type;
if (timeHasTimeZone(node.getValue())) {
type = TIME_WITH_TIME_ZONE;
}
else {
type = TIME;
}
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitTimestampLiteral(TimestampLiteral node, AnalysisContext context)
{
Type type;
if (timestampHasTimeZone(node.getValue())) {
type = TIMESTAMP_WITH_TIME_ZONE;
}
else {
type = TIMESTAMP;
}
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitIntervalLiteral(IntervalLiteral node, AnalysisContext context)
{
Type type;
if (node.isYearToMonth()) {
type = INTERVAL_YEAR_MONTH;
}
else {
type = INTERVAL_DAY_TIME;
}
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitNullLiteral(NullLiteral node, AnalysisContext context)
{
expressionTypes.put(node, UNKNOWN);
return UNKNOWN;
}
@Override
protected Type visitFunctionCall(FunctionCall node, AnalysisContext context)
{
if (node.getWindow().isPresent()) {
for (Expression expression : node.getWindow().get().getPartitionBy()) {
process(expression, context);
Type type = expressionTypes.get(expression);
if (!type.isComparable()) {
throw new SemanticException(TYPE_MISMATCH, node, "%s is not comparable, and therefore cannot be used in window function PARTITION BY", type);
}
}
for (SortItem sortItem : node.getWindow().get().getOrderBy()) {
process(sortItem.getSortKey(), context);
Type type = expressionTypes.get(sortItem.getSortKey());
if (!type.isOrderable()) {
throw new SemanticException(TYPE_MISMATCH, node, "%s is not orderable, and therefore cannot be used in window function ORDER BY", type);
}
}
if (node.getWindow().get().getFrame().isPresent()) {
WindowFrame frame = node.getWindow().get().getFrame().get();
if (frame.getStart().getValue().isPresent()) {
Type type = process(frame.getStart().getValue().get(), context);
if (!type.equals(BIGINT)) {
throw new SemanticException(TYPE_MISMATCH, node, "Window frame start value type must be BIGINT (actual %s)", type);
}
}
if (frame.getEnd().isPresent() && frame.getEnd().get().getValue().isPresent()) {
Type type = process(frame.getEnd().get().getValue().get(), context);
if (!type.equals(BIGINT)) {
throw new SemanticException(TYPE_MISMATCH, node, "Window frame end value type must be BIGINT (actual %s)", type);
}
}
}
}
ImmutableList.Builder<TypeSignature> argumentTypes = ImmutableList.builder();
for (Expression expression : node.getArguments()) {
argumentTypes.add(process(expression, context).getTypeSignature());
}
FunctionInfo function = metadata.resolveFunction(node.getName(), argumentTypes.build(), context.isApproximate());
for (int i = 0; i < node.getArguments().size(); i++) {
Expression expression = node.getArguments().get(i);
Type type = metadata.getType(function.getArgumentTypes().get(i));
checkNotNull(type, "Type %s not found", function.getArgumentTypes().get(i));
if (node.isDistinct() && !type.isComparable()) {
throw new SemanticException(TYPE_MISMATCH, node, "DISTINCT can only be applied to comparable types (actual: %s)", type);
}
coerceType(context, expression, type, String.format("Function %s argument %d", function.getSignature(), i));
}
resolvedFunctions.put(node, function);
Type type = metadata.getType(function.getReturnType());
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitExtract(Extract node, AnalysisContext context)
{
Type type = process(node.getExpression(), context);
if (!isDateTimeType(type)) {
throw new SemanticException(TYPE_MISMATCH, node.getExpression(), "Type of argument to extract must be DATE, TIME, TIMESTAMP, or INTERVAL (actual %s)", type);
}
Extract.Field field = node.getField();
if ((field == TIMEZONE_HOUR || field == TIMEZONE_MINUTE) && !(type == TIME_WITH_TIME_ZONE || type == TIMESTAMP_WITH_TIME_ZONE)) {
throw new SemanticException(TYPE_MISMATCH, node.getExpression(), "Type of argument to extract time zone field must have a time zone (actual %s)", type);
}
expressionTypes.put(node, BIGINT);
return BIGINT;
}
private boolean isDateTimeType(Type type)
{
return type == DATE ||
type == TIME ||
type == TIME_WITH_TIME_ZONE ||
type == TIMESTAMP ||
type == TIMESTAMP_WITH_TIME_ZONE ||
type == INTERVAL_DAY_TIME ||
type == INTERVAL_YEAR_MONTH;
}
@Override
protected Type visitBetweenPredicate(BetweenPredicate node, AnalysisContext context)
{
return getOperator(context, node, OperatorType.BETWEEN, node.getValue(), node.getMin(), node.getMax());
}
@Override
public Type visitCast(Cast node, AnalysisContext context)
{
Type type = metadata.getType(parseTypeSignature(node.getType()));
if (type == null) {
throw new SemanticException(TYPE_MISMATCH, node, "Unknown type: " + node.getType());
}
if (type == UNKNOWN) {
throw new SemanticException(TYPE_MISMATCH, node, "UNKNOWN is not a valid type");
}
Type value = process(node.getExpression(), context);
if (value != UNKNOWN) {
try {
metadata.getFunctionRegistry().getCoercion(value, type);
}
catch (OperatorNotFoundException e) {
throw new SemanticException(TYPE_MISMATCH, node, "Cannot cast %s to %s", value, type);
}
}
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitInPredicate(InPredicate node, AnalysisContext context)
{
Expression value = node.getValue();
process(value, context);
Expression valueList = node.getValueList();
process(valueList, context);
if (valueList instanceof InListExpression) {
InListExpression inListExpression = (InListExpression) valueList;
coerceToSingleType(context,
"IN value and list items must be the same type: %s",
ImmutableList.<Expression>builder().add(value).addAll(inListExpression.getValues()).build());
}
else if (valueList instanceof SubqueryExpression) {
coerceToSingleType(context, node, "value and result of subquery must be of the same type for IN expression: %s vs %s", value, valueList);
subqueryInPredicates.add(node);
}
expressionTypes.put(node, BOOLEAN);
return BOOLEAN;
}
@Override
protected Type visitInListExpression(InListExpression node, AnalysisContext context)
{
Type type = coerceToSingleType(context, "All IN list values must be the same type: %s", node.getValues());
expressionTypes.put(node, type);
return type; // TODO: this really should a be relation type
}
@Override
protected Type visitSubqueryExpression(SubqueryExpression node, AnalysisContext context)
{
StatementAnalyzer analyzer = new StatementAnalyzer(analysis, metadata, sqlParser, session, experimentalSyntaxEnabled, Optional.empty());
TupleDescriptor descriptor = analyzer.process(node.getQuery(), context);
// Scalar subqueries should only produce one column
if (descriptor.getVisibleFieldCount() != 1) {
throw new SemanticException(MULTIPLE_FIELDS_FROM_SCALAR_SUBQUERY,
node,
"Subquery expression must produce only one field. Found %s",
descriptor.getVisibleFieldCount());
}
Type type = Iterables.getOnlyElement(descriptor.getVisibleFields()).getType();
expressionTypes.put(node, type);
return type;
}
@Override
public Type visitInputReference(InputReference node, AnalysisContext context)
{
Type type = tupleDescriptor.getFieldByIndex(node.getChannel()).getType();
expressionTypes.put(node, type);
return type;
}
@Override
protected Type visitExpression(Expression node, AnalysisContext context)
{
throw new UnsupportedOperationException("not yet implemented: " + node.getClass().getName());
}
private Type getOperator(AnalysisContext context, Expression node, OperatorType operatorType, Expression... arguments)
{
ImmutableList.Builder<Type> argumentTypes = ImmutableList.builder();
for (Expression expression : arguments) {
argumentTypes.add(process(expression, context));
}
FunctionInfo operatorInfo;
try {
operatorInfo = metadata.resolveOperator(operatorType, argumentTypes.build());
}
catch (OperatorNotFoundException e) {
throw new SemanticException(TYPE_MISMATCH, node, e.getMessage());
}
for (int i = 0; i < arguments.length; i++) {
Expression expression = arguments[i];
Type type = metadata.getType(operatorInfo.getArgumentTypes().get(i));
coerceType(context, expression, type, String.format("Operator %s argument %d", operatorInfo, i));
}
Type type = metadata.getType(operatorInfo.getReturnType());
expressionTypes.put(node, type);
return type;
}
private void coerceType(AnalysisContext context, Expression expression, Type expectedType, String message)
{
Type actualType = process(expression, context);
if (!actualType.equals(expectedType)) {
if (!canCoerce(actualType, expectedType)) {
throw new SemanticException(TYPE_MISMATCH, expression, message + " must evaluate to a %s (actual: %s)", expectedType, actualType);
}
expressionCoercions.put(expression, expectedType);
}
}
private Type coerceToSingleType(AnalysisContext context, Node node, String message, Expression first, Expression second)
{
Type firstType = null;
if (first != null) {
firstType = process(first, context);
}
Type secondType = null;
if (second != null) {
secondType = process(second, context);
}
if (firstType == null) {
return secondType;
}
if (secondType == null) {
return firstType;
}
if (firstType.equals(secondType)) {
return firstType;
}
// coerce types if possible
if (canCoerce(firstType, secondType)) {
expressionCoercions.put(first, secondType);
return secondType;
}
if (canCoerce(secondType, firstType)) {
expressionCoercions.put(second, firstType);
return firstType;
}
throw new SemanticException(TYPE_MISMATCH, node, message, firstType, secondType);
}
private Type coerceToSingleType(AnalysisContext context, String message, List<Expression> expressions)
{
// determine super type
Type superType = UNKNOWN;
for (Expression expression : expressions) {
Optional<Type> newSuperType = getCommonSuperType(superType, process(expression, context));
if (!newSuperType.isPresent()) {
throw new SemanticException(TYPE_MISMATCH, expression, message, superType);
}
superType = newSuperType.get();
}
// verify all expressions can be coerced to the superType
for (Expression expression : expressions) {
Type type = process(expression, context);
if (!type.equals(superType)) {
if (!canCoerce(type, superType)) {
throw new SemanticException(TYPE_MISMATCH, expression, message, superType);
}
expressionCoercions.put(expression, superType);
}
}
return superType;
}
}
public static IdentityHashMap<Expression, Type> getExpressionTypes(
Session session,
Metadata metadata,
SqlParser sqlParser,
Map<Symbol, Type> types,
Expression expression)
{
return getExpressionTypes(session, metadata, sqlParser, types, ImmutableList.of(expression));
}
public static IdentityHashMap<Expression, Type> getExpressionTypes(
Session session,
Metadata metadata,
SqlParser sqlParser,
Map<Symbol, Type> types,
Iterable<? extends Expression> expressions)
{
return analyzeExpressionsWithSymbols(session, metadata, sqlParser, types, expressions).getExpressionTypes();
}
public static IdentityHashMap<Expression, Type> getExpressionTypesFromInput(
Session session,
Metadata metadata,
SqlParser sqlParser,
Map<Integer, Type> types,
Expression expression)
{
return getExpressionTypesFromInput(session, metadata, sqlParser, types, ImmutableList.of(expression));
}
public static IdentityHashMap<Expression, Type> getExpressionTypesFromInput(
Session session,
Metadata metadata,
SqlParser sqlParser,
Map<Integer, Type> types,
Iterable<? extends Expression> expressions)
{
return analyzeExpressionsWithInputs(session, metadata, sqlParser, types, expressions).getExpressionTypes();
}
public static ExpressionAnalysis analyzeExpressionsWithSymbols(
Session session,
Metadata metadata,
SqlParser sqlParser,
final Map<Symbol, Type> types,
Iterable<? extends Expression> expressions)
{
List<Field> fields = DependencyExtractor.extractUnique(expressions).stream()
.map(symbol -> {
Type type = types.get(symbol);
checkArgument(type != null, "No type for symbol %s", symbol);
return Field.newUnqualified(symbol.getName(), type);
})
.collect(toImmutableList());
return analyzeExpressions(session, metadata, sqlParser, new TupleDescriptor(fields), expressions);
}
public static ExpressionAnalysis analyzeExpressionsWithInputs(
Session session,
Metadata metadata,
SqlParser sqlParser,
Map<Integer, Type> types,
Iterable<? extends Expression> expressions)
{
Field[] fields = new Field[types.size()];
for (Entry<Integer, Type> entry : types.entrySet()) {
fields[entry.getKey()] = Field.newUnqualified(Optional.empty(), entry.getValue());
}
TupleDescriptor tupleDescriptor = new TupleDescriptor(fields);
return analyzeExpressions(session, metadata, sqlParser, tupleDescriptor, expressions);
}
private static ExpressionAnalysis analyzeExpressions(
Session session,
Metadata metadata,
SqlParser sqlParser,
TupleDescriptor tupleDescriptor,
Iterable<? extends Expression> expressions)
{
ExpressionAnalyzer analyzer = new ExpressionAnalyzer(new Analysis(), session, metadata, sqlParser, false);
for (Expression expression : expressions) {
analyzer.analyze(expression, tupleDescriptor, new AnalysisContext());
}