-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SqlValidatorTest.java
11493 lines (10253 loc) · 469 KB
/
SqlValidatorTest.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 to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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 org.apache.calcite.test;
import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.avatica.util.Quoting;
import org.apache.calcite.avatica.util.TimeUnit;
import org.apache.calcite.config.Lex;
import org.apache.calcite.rel.type.DelegatingTypeSystem;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.TimeFrameSet;
import org.apache.calcite.runtime.CalciteContextException;
import org.apache.calcite.sql.SqlCollation;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlOperatorTable;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.fun.SqlLibrary;
import org.apache.calcite.sql.fun.SqlLibraryOperatorTableFactory;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParseException;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.type.ArraySqlType;
import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.sql.util.SqlShuttle;
import org.apache.calcite.sql.validate.SqlAbstractConformance;
import org.apache.calcite.sql.validate.SqlConformance;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import org.apache.calcite.sql.validate.SqlDelegatingConformance;
import org.apache.calcite.sql.validate.SqlMonotonicity;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
import org.apache.calcite.sql.validate.SqlValidatorImpl;
import org.apache.calcite.sql.validate.SqlValidatorUtil;
import org.apache.calcite.test.catalog.CountingFactory;
import org.apache.calcite.testlib.annotations.LocaleEnUs;
import org.apache.calcite.tools.ValidationException;
import org.apache.calcite.util.Bug;
import org.apache.calcite.util.ImmutableBitSet;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Ordering;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static org.apache.calcite.test.Matchers.isCharset;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasToString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static java.util.Arrays.asList;
/**
* Concrete child class of {@link SqlValidatorTestCase}, containing lots of unit
* tests.
*
* <p>If you want to run these same tests in a different environment, create a
* derived class whose {@link #fixture()} returns a different implementation of
* {@link SqlValidatorFixture}.
*/
@LocaleEnUs
public class SqlValidatorTest extends SqlValidatorTestCase {
//~ Static fields/initializers ---------------------------------------------
// CHECKSTYLE: IGNORE 1
/** @deprecated Deprecated so that usages of this constant will show up in
* yellow in Intellij and maybe someone will fix them. */
protected static final boolean TODO = false;
private static final String ANY = "(?s).*";
protected static final Logger LOGGER =
LoggerFactory.getLogger(SqlValidatorTest.class);
private static final String ERR_IN_VALUES_INCOMPATIBLE =
"Values in expression list must have compatible types";
private static final String ERR_IN_OPERANDS_INCOMPATIBLE =
"Values passed to IN operator must have compatible types";
private static final String ERR_AGG_IN_GROUP_BY =
"Aggregate expression is illegal in GROUP BY clause";
private static final String ERR_AGG_IN_ORDER_BY =
"Aggregate expression is illegal in ORDER BY clause of non-aggregating SELECT";
private static final String ERR_NESTED_AGG =
"Aggregate expressions cannot be nested";
private static final String EMP_RECORD_TYPE =
"RecordType(INTEGER NOT NULL EMPNO,"
+ " VARCHAR(20) NOT NULL ENAME,"
+ " VARCHAR(10) NOT NULL JOB,"
+ " INTEGER MGR,"
+ " TIMESTAMP(0) NOT NULL HIREDATE,"
+ " INTEGER NOT NULL SAL,"
+ " INTEGER NOT NULL COMM,"
+ " INTEGER NOT NULL DEPTNO,"
+ " BOOLEAN NOT NULL SLACKER) NOT NULL";
private static final String STR_AGG_REQUIRES_MONO = "Streaming aggregation "
+ "requires at least one monotonic expression in GROUP BY clause";
private static final String STR_ORDER_REQUIRES_MONO =
"Streaming ORDER BY must start with monotonic expression";
private static final String STR_SET_OP_INCONSISTENT =
"Set operator cannot combine streaming and non-streaming inputs";
private static final String ROW_RANGE_NOT_ALLOWED_WITH_RANK =
"ROW/RANGE not allowed with RANK, DENSE_RANK or ROW_NUMBER functions";
private static final String RANK_REQUIRES_ORDER_BY = "RANK or DENSE_RANK "
+ "functions require ORDER BY clause in window specification";
private static String cannotConvertToStream(String name) {
return "Cannot convert table '" + name + "' to stream";
}
private static String cannotConvertToRelation(String table) {
return "Cannot convert stream '" + table + "' to relation";
}
private static String cannotStreamResultsForNonStreamingInputs(String inputs) {
return "Cannot stream results of a query with no streaming inputs: '"
+ inputs
+ "'. At least one input should be convertible to a stream";
}
static SqlOperatorTable operatorTableFor(SqlLibrary library) {
return SqlLibraryOperatorTableFactory.INSTANCE.getOperatorTable(
SqlLibrary.STANDARD, library);
}
@Test void testMultipleSameAsPass() {
sql("select 1 as again,2 as \"again\", 3 as AGAiN from (values (true))")
.ok();
}
@Test void testMultipleDifferentAs() {
sql("select 1 as c1,2 as c2 from (values(true))").ok();
}
@Test void testTypeOfAs() {
sql("select 1 as c1 from (values (true))")
.columnType("INTEGER NOT NULL");
sql("select 'hej' as c1 from (values (true))")
.columnType("CHAR(3) NOT NULL");
sql("select x'deadbeef' as c1 from (values (true))")
.columnType("BINARY(4) NOT NULL");
sql("select cast(null as boolean) as c1 from (values (true))")
.columnType("BOOLEAN");
}
@Test void testTypesLiterals() {
expr("'abc'")
.columnType("CHAR(3) NOT NULL");
expr("n'abc'")
.columnType("CHAR(3) NOT NULL");
expr("_UTF16'abc'")
.columnType("CHAR(3) NOT NULL");
expr("'ab '\n"
+ "' cd'")
.columnType("CHAR(6) NOT NULL");
expr("'ab'\n"
+ "'cd'\n"
+ "'ef'\n"
+ "'gh'\n"
+ "'ij'\n"
+ "'kl'")
.columnType("CHAR(12) NOT NULL");
expr("n'ab '\n"
+ "' cd'")
.columnType("CHAR(6) NOT NULL");
expr("_UTF16'ab '\n"
+ "' cd'")
.columnType("CHAR(6) NOT NULL");
expr("^x'abc'^")
.fails("Binary literal string must contain an even number of hexits");
expr("x'abcd'")
.columnType("BINARY(2) NOT NULL");
expr("x'abcd'\n"
+ "'ff001122aabb'")
.columnType("BINARY(8) NOT NULL");
expr("x'aaaa'\n"
+ "'bbbb'\n"
+ "'0000'\n"
+ "'1111'")
.columnType("BINARY(8) NOT NULL");
expr("1234567890")
.columnType("INTEGER NOT NULL");
expr("123456.7890")
.columnType("DECIMAL(10, 4) NOT NULL");
expr("123456.7890e3")
.columnType("DOUBLE NOT NULL");
expr("true")
.columnType("BOOLEAN NOT NULL");
expr("false")
.columnType("BOOLEAN NOT NULL");
expr("unknown")
.columnType("BOOLEAN");
}
/** Tests that date-time literals with invalid strings are considered invalid.
* Originally the parser did that checking, but now the parser creates a
* {@link org.apache.calcite.sql.SqlUnknownLiteral} and the checking is
* deferred to the validator. */
@Test void testLiteral() {
expr("^DATE '12/21/99'^")
.fails("(?s).*Illegal DATE literal.*");
expr("^TIME '1230:33'^")
.fails("(?s).*Illegal TIME literal.*");
expr("^TIME '12:00:00 PM'^")
.fails("(?s).*Illegal TIME literal.*");
expr("^TIMESTAMP '12-21-99, 12:30:00'^")
.fails("(?s).*Illegal TIMESTAMP literal.*");
expr("^TIMESTAMP WITH LOCAL TIME ZONE '12-21-99, 12:30:00'^")
.fails("(?s).*Illegal TIMESTAMP WITH LOCAL TIME ZONE literal.*");
}
/** PostgreSQL and Redshift allow TIMESTAMP literals that contain only a
* date part. */
@Test void testShortTimestampLiteral() {
sql("select timestamp '1969-07-20'")
.ok();
// PostgreSQL allows the following. We should too.
sql("select ^timestamp '1969-07-20 1:2'^")
.fails("Illegal TIMESTAMP literal '1969-07-20 1:2': not in format "
+ "'yyyy-MM-dd HH:mm:ss'"); // PostgreSQL gives 1969-07-20 01:02:00
sql("select ^timestamp '1969-07-20:23:'^")
.fails("Illegal TIMESTAMP literal '1969-07-20:23:': not in format "
+ "'yyyy-MM-dd HH:mm:ss'"); // PostgreSQL gives 1969-07-20 23:00:00
}
@Test void testBooleans() {
sql("select TRUE OR unknowN from (values(true))").ok();
sql("select false AND unknown from (values(true))").ok();
sql("select not UNKNOWn from (values(true))").ok();
sql("select not true from (values(true))").ok();
sql("select not false from (values(true))").ok();
}
@Test void testAndOrIllegalTypesFails() {
// TODO need col+line number
wholeExpr("'abc' AND FaLsE")
.fails("(?s).*'<CHAR.3.> AND <BOOLEAN>'.*");
wholeExpr("TRUE OR 1")
.fails(ANY);
wholeExpr("unknown OR 1.0")
.fails(ANY);
wholeExpr("true OR 1.0e4")
.fails(ANY);
if (TODO) {
wholeExpr("TRUE OR (TIME '12:00' AT LOCAL)")
.fails(ANY);
}
}
@Test void testNotIllegalTypeFails() {
sql("select ^NOT 3.141^ from (values(true))")
.fails("(?s).*Cannot apply 'NOT' to arguments of type "
+ "'NOT<DECIMAL.4, 3.>'.*");
sql("select ^NOT 'abc'^ from (values(true))")
.fails(ANY);
sql("select ^NOT 1^ from (values(true))")
.fails(ANY);
}
@Test void testIs() {
sql("select TRUE IS FALSE FROM (values(true))").ok();
sql("select false IS NULL FROM (values(true))").ok();
sql("select UNKNOWN IS NULL FROM (values(true))").ok();
sql("select FALSE IS UNKNOWN FROM (values(true))").ok();
sql("select TRUE IS NOT FALSE FROM (values(true))").ok();
sql("select TRUE IS NOT NULL FROM (values(true))").ok();
sql("select false IS NOT NULL FROM (values(true))").ok();
sql("select UNKNOWN IS NOT NULL FROM (values(true))").ok();
sql("select FALSE IS NOT UNKNOWN FROM (values(true))").ok();
sql("select 1 IS NULL FROM (values(true))").ok();
sql("select 1.2 IS NULL FROM (values(true))").ok();
expr("^'abc' IS NOT UNKNOWN^")
.fails("(?s).*Cannot apply.*");
}
@Test void testIsFails() {
sql("select ^1 IS TRUE^ FROM (values(true))")
.fails("(?s).*'<INTEGER> IS TRUE'.*");
sql("select ^1.1 IS NOT FALSE^ FROM (values(true))")
.fails(ANY);
sql("select ^1.1e1 IS NOT FALSE^ FROM (values(true))")
.fails("(?s).*Cannot apply 'IS NOT FALSE' to arguments of type "
+ "'<DOUBLE> IS NOT FALSE'.*");
sql("select ^'abc' IS NOT TRUE^ FROM (values(true))")
.fails(ANY);
}
@Test void testScalars() {
sql("select 1 + 1 from (values(true))").ok();
sql("select 1 + 2.3 from (values(true))").ok();
sql("select 1.2+3 from (values(true))").ok();
sql("select 1.2+3.4 from (values(true))").ok();
sql("select 1 - 1 from (values(true))").ok();
sql("select 1 - 2.3 from (values(true))").ok();
sql("select 1.2-3 from (values(true))").ok();
sql("select 1.2-3.4 from (values(true))").ok();
sql("select 1 * 2 from (values(true))").ok();
sql("select 1.2* 3 from (values(true))").ok();
sql("select 1 * 2.3 from (values(true))").ok();
sql("select 1.2* 3.4 from (values(true))").ok();
sql("select 1 / 2 from (values(true))").ok();
sql("select 1 / 2.3 from (values(true))").ok();
sql("select 1.2/ 3 from (values(true))").ok();
sql("select 1.2/3.4 from (values(true))").ok();
}
@Test void testScalarsFails() {
sql("select ^1+TRUE^ from (values(true))")
.fails("(?s).*Cannot apply '\\+' to arguments of type "
+ "'<INTEGER> \\+ <BOOLEAN>'\\. Supported form\\(s\\):.*");
}
@Test void testNumbers() {
sql("select 1+-2.*-3.e-1/-4>+5 AND true from (values(true))").ok();
}
@Test void testPrefix() {
expr("+interval '1' second")
.columnType("INTERVAL SECOND NOT NULL");
expr("-interval '1' month")
.columnType("INTERVAL MONTH NOT NULL");
sql("SELECT ^-'abc'^ from (values(true))")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply '-' to arguments of type '-<CHAR.3.>'.*");
sql("SELECT -'abc' from (values(true))").ok();
sql("SELECT ^+'abc'^ from (values(true))")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply '\\+' to arguments of type '\\+<CHAR.3.>'.*");
sql("SELECT +'abc' from (values(true))").ok();
}
@Test void testNiladicForBigQuery() {
sql("select current_time, current_time(), current_date, "
+ "current_date(), current_timestamp, current_timestamp()")
.withConformance(SqlConformanceEnum.BIG_QUERY).ok();
}
@Test void testEqualNotEqual() {
expr("''=''").ok();
expr("'abc'=n''").ok();
expr("''=_latin1''").ok();
expr("n''=''").ok();
expr("n'abc'=n''").ok();
expr("n''=_latin1''").ok();
expr("_latin1''=''").ok();
expr("_latin1''=n''").ok();
expr("_latin1''=_latin1''").ok();
expr("''<>''").ok();
expr("'abc'<>n''").ok();
expr("''<>_latin1''").ok();
expr("n''<>''").ok();
expr("n'abc'<>n''").ok();
expr("n''<>_latin1''").ok();
expr("_latin1''<>''").ok();
expr("_latin1'abc'<>n''").ok();
expr("_latin1''<>_latin1''").ok();
expr("true=false").ok();
expr("unknown<>true").ok();
expr("1=1").ok();
expr("1=.1").ok();
expr("1=1e-1").ok();
expr("0.1=1").ok();
expr("0.1=0.1").ok();
expr("0.1=1e1").ok();
expr("1.1e1=1").ok();
expr("1.1e1=1.1").ok();
expr("1.1e-1=1e1").ok();
expr("''<>''").ok();
expr("1<>1").ok();
expr("1<>.1").ok();
expr("1<>1e-1").ok();
expr("0.1<>1").ok();
expr("0.1<>0.1").ok();
expr("0.1<>1e1").ok();
expr("1.1e1<>1").ok();
expr("1.1e1<>1.1").ok();
expr("1.1e-1<>1e1").ok();
}
@Test void testEqualNotEqualFails() {
// compare CHAR, INTEGER ok; implicitly convert CHAR
expr("''<>1").ok();
expr("'1'>=1").ok();
// compare INTEGER, NCHAR ok
expr("1<>n'abc'").ok();
// compare CHAR, DECIMAL ok
expr("''=.1").ok();
expr("true<>1e-1").ok();
expr("^true<>1e-1^")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply '<>' to arguments of type '<BOOLEAN> <> <DOUBLE>'.*");
// compare BOOLEAN, CHAR ok
expr("false=''").ok();
expr("^x'a4'=0.01^")
.fails("(?s).*Cannot apply '=' to arguments of type "
+ "'<BINARY.1.> = <DECIMAL.3, 2.>'.*");
expr("^x'a4'=1^")
.fails("(?s).*Cannot apply '=' to arguments of type '<BINARY.1.> = <INTEGER>'.*");
expr("^x'13'<>0.01^")
.fails("(?s).*Cannot apply '<>' to arguments of type "
+ "'<BINARY.1.> <> <DECIMAL.3, 2.>'.*");
expr("^x'abcd'<>1^")
.fails("(?s).*Cannot apply '<>' to arguments of type "
+ "'<BINARY.2.> <> <INTEGER>'.*");
}
@Test void testBinaryString() {
sql("select x'face'=X'' from (values(true))").ok();
sql("select x'ff'=X'' from (values(true))").ok();
}
@Test void testBinaryStringFails() {
expr("select x'ffee'='abc' from (values(true))")
.columnType("BOOLEAN");
sql("select ^x'ffee'='abc'^ from (values(true))")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply '=' to arguments of type "
+ "'<BINARY.2.> = <CHAR.3.>'.*");
sql("select ^x'ff'=88^ from (values(true))")
.fails("(?s).*Cannot apply '=' to arguments of type "
+ "'<BINARY.1.> = <INTEGER>'.*");
sql("select ^x''<>1.1e-1^ from (values(true))")
.fails("(?s).*Cannot apply '<>' to arguments of type "
+ "'<BINARY.0.> <> <DOUBLE>'.*");
sql("select ^x''<>1.1^ from (values(true))")
.fails("(?s).*Cannot apply '<>' to arguments of type "
+ "'<BINARY.0.> <> <DECIMAL.2, 1.>'.*");
}
@Test void testStringLiteral() {
sql("select n''=_iso-8859-1'abc' from (values(true))").ok();
sql("select N'f'<>'''' from (values(true))").ok();
}
@Test void testStringLiteralBroken() {
sql("select 'foo'\n"
+ "'bar' from (values(true))").ok();
sql("select 'foo'\r'bar' from (values(true))").ok();
sql("select 'foo'\n\r'bar' from (values(true))").ok();
sql("select 'foo'\r\n'bar' from (values(true))").ok();
sql("select 'foo'\n'bar' from (values(true))").ok();
sql("select 'foo' /* comment */ ^'bar'^ from (values(true))")
.fails("String literal continued on same line");
sql("select 'foo' -- comment\r from (values(true))").ok();
sql("select 'foo' ^'bar'^ from (values(true))")
.fails("String literal continued on same line");
}
@Test void testArithmeticOperators() {
expr("power(2,3)").ok();
expr("aBs(-2.3e-2)").ok();
expr("MOD(5 ,\t\f\r\n2)").ok();
expr("ln(5.43 )").ok();
expr("log10(- -.2 )").ok();
expr("mod(5.1, 3)").ok();
expr("mod(2,5.1)").ok();
expr("5.1 % 3")
.withConformance(SqlConformanceEnum.LENIENT)
.columnType("DECIMAL(2, 1) NOT NULL");
expr("2 % 5.1")
.withConformance(SqlConformanceEnum.LENIENT)
.columnType("DECIMAL(2, 1) NOT NULL");
expr("exp(3.67)").ok();
}
@Test void testArithmeticOperatorsFails() {
expr("^power(2,'abc')^")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply 'POWER' to arguments of type "
+ "'POWER.<INTEGER>, <CHAR.3.>.*");
expr("power(2,'abc')")
.columnType("DOUBLE NOT NULL");
expr("^power(true,1)^")
.fails("(?s).*Cannot apply 'POWER' to arguments of type "
+ "'POWER.<BOOLEAN>, <INTEGER>.*");
expr("^mod(x'1100',1)^")
.fails("(?s).*Cannot apply 'MOD' to arguments of type "
+ "'MOD.<BINARY.2.>, <INTEGER>.*");
expr("^mod(1, x'1100')^")
.fails("(?s).*Cannot apply 'MOD' to arguments of type "
+ "'MOD.<INTEGER>, <BINARY.2.>.*");
expr("^abs(x'')^")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply 'ABS' to arguments of type 'ABS.<BINARY.0.>.*");
expr("^ln(x'face12')^")
.fails("(?s).*Cannot apply 'LN' to arguments of type 'LN.<BINARY.3.>.*");
expr("^log10(x'fa')^")
.fails("(?s).*Cannot apply 'LOG10' to arguments of type 'LOG10.<BINARY.1.>.*");
expr("^exp('abc')^")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply 'EXP' to arguments of type 'EXP.<CHAR.3.>.*");
expr("exp('abc')")
.columnType("DOUBLE NOT NULL");
}
@Test void testCaseExpression() {
expr("case 1 when 1 then 'one' end").ok();
expr("case 1 when 1 then 'one' else null end").ok();
expr("case 1 when 1 then 'one' else 'more' end").ok();
expr("case 1 when 1 then 'one' when 2 then null else 'more' end").ok();
expr("case when TRUE then 'true' else 'false' end").ok();
sql("values case when TRUE then 'true' else 'false' end").ok();
expr("CASE 1 WHEN 1 THEN cast(null as integer) WHEN 2 THEN null END").ok();
expr("CASE 1 WHEN 1 THEN cast(null as integer) WHEN 2 THEN cast(null as "
+ "integer) END").ok();
expr("CASE 1 WHEN 1 THEN null WHEN 2 THEN cast(null as integer) END").ok();
expr("CASE 1 WHEN 1 THEN cast(null as integer) WHEN 2 THEN cast(cast(null"
+ " as tinyint) as integer) END").ok();
}
@Test void testCaseExpressionTypes() {
expr("case 1 when 1 then 'one' else 'not one' end")
.columnType("CHAR(7) NOT NULL");
expr("case when 2<1 then 'impossible' end")
.columnType("CHAR(10)");
expr("case 'one' when 'two' then 2.00 when 'one' then 1.3 else 3.2 end")
.columnType("DECIMAL(3, 2) NOT NULL");
expr("case 'one' when 'two' then 2 when 'one' then 1.00 else 3 end")
.columnType("DECIMAL(12, 2) NOT NULL");
expr("case 1 when 1 then 'one' when 2 then null else 'more' end")
.columnType("CHAR(4)");
expr("case when TRUE then 'true' else 'false' end")
.columnType("CHAR(5) NOT NULL");
expr("CASE 1 WHEN 1 THEN cast(null as integer) END")
.columnType("INTEGER");
expr("CASE 1\n"
+ "WHEN 1 THEN NULL\n"
+ "WHEN 2 THEN cast(cast(null as tinyint) as integer) END")
.columnType("INTEGER");
expr("CASE 1\n"
+ "WHEN 1 THEN cast(null as integer)\n"
+ "WHEN 2 THEN cast(null as integer) END")
.columnType("INTEGER");
expr("CASE 1\n"
+ "WHEN 1 THEN cast(null as integer)\n"
+ "WHEN 2 THEN cast(cast(null as tinyint) as integer)\n"
+ "END")
.columnType("INTEGER");
expr("CASE 1\n"
+ "WHEN 1 THEN INTERVAL '12 3:4:5.6' DAY TO SECOND(6)\n"
+ "WHEN 2 THEN INTERVAL '12 3:4:5.6' DAY TO SECOND(9)\n"
+ "END")
.columnType("INTERVAL DAY TO SECOND(9)");
sql("select\n"
+ "CASE WHEN job is not null THEN mgr\n"
+ "ELSE 5 end as mgr\n"
+ "from EMP")
.columnType("INTEGER");
}
@Test void testCaseExpressionFails() {
// varchar not comparable with bit string
expr("case 'string' when x'01' then 'zero one' else 'something' end")
.columnType("CHAR(9) NOT NULL");
wholeExpr("case 'string' when x'01' then 'zero one' else 'something' end")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply '=' to arguments of type '<CHAR.6.> = <BINARY.1.>'.*");
// all thens and else return null
wholeExpr("case 1 when 1 then null else null end")
.withTypeCoercion(false)
.fails("(?s).*ELSE clause or at least one THEN clause must be non-NULL.*");
expr("case 1 when 1 then null else null end")
.columnType("NULL");
// all thens and else return null
wholeExpr("case 1 when 1 then null end")
.withTypeCoercion(false)
.fails("(?s).*ELSE clause or at least one THEN clause must be non-NULL.*");
expr("case 1 when 1 then null end")
.columnType("NULL");
wholeExpr("case when true and true then 1 "
+ "when false then 2 "
+ "when false then true " + "else "
+ "case when true then 3 end end")
.fails("Illegal mixing of types in CASE or COALESCE statement");
}
@Test void testNullIf() {
expr("nullif(1,2)").ok();
expr("nullif(1,2)")
.columnType("INTEGER");
expr("nullif('a','b')")
.columnType("CHAR(1)");
expr("nullif(345.21, 2)")
.columnType("DECIMAL(5, 2)");
expr("nullif(345.21, 2e0)")
.columnType("DECIMAL(5, 2)");
wholeExpr("nullif(1,2,3)")
.fails("Invalid number of arguments to function 'NULLIF'. Was "
+ "expecting 2 arguments");
}
@Test void testCoalesce() {
expr("coalesce('a','b')").ok();
expr("coalesce('a','b','c')")
.columnType("CHAR(1) NOT NULL");
sql("select COALESCE(mgr, 12) as m from EMP")
.columnType("INTEGER NOT NULL");
}
@Test void testCoalesceFails() {
wholeExpr("coalesce('a',1)")
.withTypeCoercion(false)
.fails("Illegal mixing of types in CASE or COALESCE statement");
expr("coalesce('a',1)")
.columnType("VARCHAR NOT NULL");
wholeExpr("coalesce('a','b',1)")
.withTypeCoercion(false)
.fails("Illegal mixing of types in CASE or COALESCE statement");
expr("coalesce('a','b',1)")
.columnType("VARCHAR NOT NULL");
}
@Test void testStringCompare() {
expr("'a' = 'b'").ok();
expr("'a' <> 'b'").ok();
expr("'a' > 'b'").ok();
expr("'a' < 'b'").ok();
expr("'a' >= 'b'").ok();
expr("'a' <= 'b'").ok();
expr("cast('' as varchar(1))>cast('' as char(1))").ok();
expr("cast('' as varchar(1))<cast('' as char(1))").ok();
expr("cast('' as varchar(1))>=cast('' as char(1))").ok();
expr("cast('' as varchar(1))<=cast('' as char(1))").ok();
expr("cast('' as varchar(1))=cast('' as char(1))").ok();
expr("cast('' as varchar(1))<>cast('' as char(1))").ok();
}
@Test void testStringCompareType() {
expr("'a' = 'b'")
.columnType("BOOLEAN NOT NULL");
expr("'a' <> 'b'")
.columnType("BOOLEAN NOT NULL");
expr("'a' > 'b'")
.columnType("BOOLEAN NOT NULL");
expr("'a' < 'b'")
.columnType("BOOLEAN NOT NULL");
expr("'a' >= 'b'")
.columnType("BOOLEAN NOT NULL");
expr("'a' <= 'b'")
.columnType("BOOLEAN NOT NULL");
expr("CAST(NULL AS VARCHAR(33)) > 'foo'")
.columnType("BOOLEAN");
}
@Test void testConcat() {
expr("'a'||'b'").ok();
expr("x'12'||x'34'").ok();
expr("'a'||'b'")
.columnType("CHAR(2) NOT NULL");
expr("cast('a' as char(1))||cast('b' as char(2))")
.columnType("CHAR(3) NOT NULL");
expr("cast(null as char(1))||cast('b' as char(2))")
.columnType("CHAR(3)");
expr("'a'||'b'||'c'")
.columnType("CHAR(3) NOT NULL");
expr("'a'||'b'||'cde'||'f'")
.columnType("CHAR(6) NOT NULL");
expr("'a'||'b'||cast('cde' as VARCHAR(3))|| 'f'")
.columnType("VARCHAR(6) NOT NULL");
expr("_UTF16'a'||_UTF16'b'||_UTF16'c'").ok();
}
@Test void testConcatWithCharset() {
sql("_UTF16'a'||_UTF16'b'||_UTF16'c'")
.assertCharset(isCharset("UTF-16LE"));
}
@Test void testConcatFails() {
wholeExpr("'a'||x'ff'")
.fails("(?s).*Cannot apply '\\|\\|' to arguments of type "
+ "'<CHAR.1.> \\|\\| <BINARY.1.>'.*Supported form.s.: "
+ "'<STRING> \\|\\| <STRING>.*'");
}
/** Tests the CONCAT function, which unlike the concat operator ('||') is not
* standard but enabled in the ORACLE, MySQL, BigQuery, POSTGRESQL and MSSQL libraries. */
@Test void testConcatFunction() {
// CONCAT is not in the library operator table
final SqlValidatorFixture s = fixture()
.withOperatorTable(operatorTableFor(SqlLibrary.POSTGRESQL));
s.withExpr("concat('a', 'b')").ok();
s.withExpr("concat(x'12', x'34')").ok();
s.withExpr("concat(_UTF16'a', _UTF16'b', _UTF16'c')").ok();
s.withExpr("concat('aabbcc', 'ab', '+-')")
.columnType("VARCHAR(10) NOT NULL");
s.withExpr("concat('aabbcc', CAST(NULL AS VARCHAR(20)), '+-')")
.columnType("VARCHAR(28) NOT NULL");
s.withExpr("concat('aabbcc', 2)")
.withWhole(true)
.withTypeCoercion(false)
.fails("(?s)Cannot apply 'CONCAT' to arguments of type "
+ "'CONCAT\\(<CHAR\\(6\\)>, <INTEGER>\\)'\\. .*");
s.withExpr("concat('aabbcc', 2)").ok();
s.withExpr("concat('abc', 'ab', 123)")
.withWhole(true)
.withTypeCoercion(false)
.fails("(?s)Cannot apply 'CONCAT' to arguments of type "
+ "'CONCAT\\(<CHAR\\(3\\)>, <CHAR\\(2\\)>, <INTEGER>\\)'\\. .*");
s.withExpr("concat('abc', 'ab', 123)").ok();
s.withExpr("concat(true, false)")
.withWhole(true)
.withTypeCoercion(false)
.fails("(?s)Cannot apply 'CONCAT' to arguments of type "
+ "'CONCAT\\(<BOOLEAN>, <BOOLEAN>\\)'\\. .*");
s.withExpr("concat(true, false)").ok();
s.withExpr("concat(DATE '2020-04-17', TIMESTAMP '2020-04-17 14:17:51')")
.withWhole(true)
.withTypeCoercion(false)
.fails("(?s)Cannot apply 'CONCAT' to arguments of type "
+ "'CONCAT\\(<DATE>, <TIMESTAMP\\(0\\)>\\)'\\. .*");
s.withExpr("concat(DATE '2020-04-17', TIMESTAMP '2020-04-17 14:17:51')").ok();
}
@Test void testBetween() {
expr("1 between 2 and 3").ok();
expr("'a' between 'b' and 'c'").ok();
// can implicitly convert CHAR to INTEGER
expr("'' between 2 and 3").ok();
wholeExpr("date '2012-02-03' between 2 and 3")
.fails("(?s).*Cannot apply 'BETWEEN ASYMMETRIC' to arguments of type.*");
}
@Test void testCharsetMismatch() {
wholeExpr("''=_UTF16''")
.fails("Cannot apply .* to the two different charsets ISO-8859-1 and "
+ "UTF-16LE");
wholeExpr("''<>_UTF16''")
.fails("(?s).*Cannot apply .* to the two different charsets.*");
wholeExpr("''>_UTF16''")
.fails("(?s).*Cannot apply .* to the two different charsets.*");
wholeExpr("''<_UTF16''")
.fails("(?s).*Cannot apply .* to the two different charsets.*");
wholeExpr("''<=_UTF16''")
.fails("(?s).*Cannot apply .* to the two different charsets.*");
wholeExpr("''>=_UTF16''")
.fails("(?s).*Cannot apply .* to the two different charsets.*");
wholeExpr("''||_UTF16''")
.fails(ANY);
wholeExpr("'a'||'b'||_UTF16'c'")
.fails(ANY);
}
// FIXME jvs 2-Feb-2005:
@Disabled("all collation-related tests are disabled due to dtbug 280")
void testSimpleCollate() {
expr("'s' collate latin1$en$1").ok();
expr("'s' collate latin1$en$1")
.columnType("CHAR(1)");
sql("'s'")
.assertCollation(is("ISO-8859-1$en_US$primary"),
is(SqlCollation.Coercibility.COERCIBLE));
sql("'s' collate latin1$sv$3")
.assertCollation(is("ISO-8859-1$sv$3"),
is(SqlCollation.Coercibility.EXPLICIT));
}
@Disabled("all collation-related tests are disabled due to dtbug 280")
void testCharsetAndCollateMismatch() {
// todo
expr("_UTF16's' collate latin1$en$1")
.fails("?");
}
@Disabled("all collation-related tests are disabled due to dtbug 280")
void testDyadicCollateCompare() {
expr("'s' collate latin1$en$1 < 't'").ok();
expr("'t' > 's' collate latin1$en$1").ok();
expr("'s' collate latin1$en$1 <> 't' collate latin1$en$1").ok();
}
@Disabled("all collation-related tests are disabled due to dtbug 280")
void testDyadicCompareCollateFails() {
// two different explicit collations. difference in strength
expr("'s' collate latin1$en$1 <= 't' collate latin1$en$2")
.fails("(?s).*Two explicit different collations.*are illegal.*");
// two different explicit collations. difference in language
expr("'s' collate latin1$sv$1 >= 't' collate latin1$en$1")
.fails("(?s).*Two explicit different collations.*are illegal.*");
}
@Disabled("all collation-related tests are disabled due to dtbug 280")
void testDyadicCollateOperator() {
sql("'a' || 'b'")
.assertCollation(is("ISO-8859-1$en_US$primary"),
is(SqlCollation.Coercibility.COERCIBLE));
sql("'a' collate latin1$sv$3 || 'b'")
.assertCollation(is("ISO-8859-1$sv$3"),
is(SqlCollation.Coercibility.EXPLICIT));
sql("'a' collate latin1$sv$3 || 'b' collate latin1$sv$3")
.assertCollation(is("ISO-8859-1$sv$3"),
is(SqlCollation.Coercibility.EXPLICIT));
}
@Test void testCharLength() {
expr("char_length('string')").ok();
expr("char_length(_UTF16'string')").ok();
expr("character_length('string')").ok();
expr("char_length('string')")
.columnType("INTEGER NOT NULL");
expr("character_length('string')")
.columnType("INTEGER NOT NULL");
}
@Test void testUpperLower() {
expr("upper(_UTF16'sadf')").ok();
expr("lower(n'sadf')").ok();
expr("lower('sadf')")
.columnType("CHAR(4) NOT NULL");
wholeExpr("upper(123)")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply 'UPPER' to arguments of type 'UPPER.<INTEGER>.'.*");
expr("upper(123)")
.columnType("VARCHAR NOT NULL");
}
@Test void testPosition() {
expr("position('mouse' in 'house')").ok();
expr("position(x'11' in x'100110')").ok();
expr("position(x'11' in x'100110' FROM 10)").ok();
expr("position(x'abcd' in x'')").ok();
expr("position('mouse' in 'house')")
.columnType("INTEGER NOT NULL");
wholeExpr("position(x'1234' in '110')")
.fails("Parameters must be of the same type");
wholeExpr("position(x'1234' in '110' from 3)")
.fails("Parameters must be of the same type");
}
@Test void testTrim() {
expr("trim('mustache' FROM 'beard')").ok();
expr("trim(both 'mustache' FROM 'beard')").ok();
expr("trim(leading 'mustache' FROM 'beard')").ok();
expr("trim(trailing 'mustache' FROM 'beard')").ok();
expr("trim('mustache' FROM 'beard')")
.columnType("VARCHAR(5) NOT NULL");
expr("trim('beard ')")
.columnType("VARCHAR(7) NOT NULL");
expr("trim('mustache' FROM cast(null as varchar(4)))")
.columnType("VARCHAR(4)");
if (TODO) {
final SqlCollation.Coercibility expectedCoercibility = null;
sql("trim('mustache' FROM 'beard')")
.assertCollation(is("CHAR(5)"), is(expectedCoercibility));
}
}
@Test void testTrimFails() {
wholeExpr("trim(123 FROM 'beard')")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply 'TRIM' to arguments of type.*");
expr("trim(123 FROM 'beard')")
.columnType("VARCHAR(5) NOT NULL");
wholeExpr("trim('a' FROM 123)")
.withTypeCoercion(false)
.fails("(?s).*Cannot apply 'TRIM' to arguments of type.*");
expr("trim('a' FROM 123)")
.columnType("VARCHAR NOT NULL");
wholeExpr("trim('a' FROM _UTF16'b')")
.fails("(?s).*not comparable to each other.*");
}
@Test void testConvertAndTranslate() {
sql("select convert('abc' using utf16) from emp").ok();
sql("select convert(cast(deptno as varchar) using utf8) from emp");
sql("select convert(null using utf16) from emp").ok();
sql("select ^convert(deptno using latin1)^ from emp")
.fails("Invalid type 'INTEGER NOT NULL' in 'TRANSLATE' function\\. "
+ "Only 'CHARACTER' type is supported");
sql("select convert(ename using utf9) from emp").fails("UTF9");
sql("select translate('abc' using utf8) from emp").ok();
sql("select translate(cast(deptno as varchar) using utf8) from emp");
sql("select translate(null using utf16) from emp").ok();
sql("select ^translate(deptno using latin1)^ from emp")
.fails("Invalid type 'INTEGER NOT NULL' in 'TRANSLATE' function\\. "
+ "Only 'CHARACTER' type is supported");
sql("select translate(ename using utf9) from emp").fails("UTF9");
}
@Test void testTranslate3() {
// TRANSLATE3 is not in the standard operator table
wholeExpr("translate('aabbcc', 'ab', '+-')")
.fails("No match found for function signature "
+ "TRANSLATE3\\(<CHARACTER>, <CHARACTER>, <CHARACTER>\\)");
final SqlOperatorTable opTable = operatorTableFor(SqlLibrary.ORACLE);
expr("translate('aabbcc', 'ab', '+-')")
.withOperatorTable(opTable)
.columnType("VARCHAR(6) NOT NULL");
wholeExpr("translate('abc', 'ab')")
.withOperatorTable(opTable)
.fails("Invalid number of arguments to function 'TRANSLATE3'. "
+ "Was expecting 3 arguments");
wholeExpr("translate('abc', 'ab', 123)")
.withOperatorTable(opTable)
.withTypeCoercion(false)
.fails("(?s)Cannot apply 'TRANSLATE3' to arguments of type "
+ "'TRANSLATE3\\(<CHAR\\(3\\)>, <CHAR\\(2\\)>, <INTEGER>\\)'\\. .*");
expr("translate('abc', 'ab', 123)")
.withOperatorTable(opTable)
.columnType("VARCHAR(3) NOT NULL");
wholeExpr("translate('abc', 'ab', '+-', 'four')")
.withOperatorTable(opTable)
.fails("Invalid number of arguments to function 'TRANSLATE3'. "
+ "Was expecting 3 arguments");
}
@Test void testOverlay() {
expr("overlay('ABCdef' placing 'abc' from 1)").ok();
expr("overlay('ABCdef' placing 'abc' from 1 for 3)").ok();
wholeExpr("overlay('ABCdef' placing 'abc' from '1' for 3)")
.withTypeCoercion(false)
.fails("(?s).*OVERLAY\\(<STRING> PLACING <STRING> FROM <INTEGER>\\).*");
expr("overlay('ABCdef' placing 'abc' from '1' for 3)")
.columnType("VARCHAR(9) NOT NULL");
expr("overlay('ABCdef' placing 'abc' from 1 for 3)")
.columnType("VARCHAR(9) NOT NULL");
expr("overlay('ABCdef' placing 'abc' from 6 for 3)")