-
Notifications
You must be signed in to change notification settings - Fork 55
/
SQLPushdownTest.scala
4204 lines (3994 loc) · 155 KB
/
SQLPushdownTest.scala
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
package com.singlestore.spark
import com.singlestore.spark.SQLGen.{Relation, SinglestoreVersion}
import com.singlestore.spark.SQLHelper._
import org.apache.log4j.{Level, LogManager}
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.types._
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import scala.util.matching.Regex
class SQLPushdownTest extends IntegrationSuiteBase with BeforeAndAfterEach with BeforeAndAfterAll {
override def beforeAll(): Unit = {
super.beforeAll()
super.beforeEach() // we want to run beforeEach to set up a spark session
// need to specify explicit schemas - otherwise Spark will infer them
// incorrectly from the JSON file
val usersSchema = StructType(
StructField("id", LongType)
:: StructField("first_name", StringType)
:: StructField("last_name", StringType)
:: StructField("email", StringType)
:: StructField("owns_house", BooleanType)
:: StructField("favorite_color", StringType, nullable = true)
:: StructField("age", IntegerType)
:: StructField("birthday", DateType)
:: Nil)
writeTable("testdb.users",
spark.read.schema(usersSchema).json("src/test/resources/data/users.json"))
val moviesSchema = StructType(
StructField("id", LongType)
:: StructField("title", StringType)
:: StructField("genre", StringType)
:: StructField("critic_review", StringType, nullable = true)
:: StructField("critic_rating", FloatType, nullable = true)
:: Nil)
writeTable("testdb.movies",
spark.read.schema(moviesSchema).json("src/test/resources/data/movies.json"))
val reviewsSchema = StructType(
StructField("user_id", LongType)
:: StructField("movie_id", LongType)
:: StructField("rating", FloatType)
:: StructField("review", StringType)
:: StructField("created", TimestampType)
:: Nil)
writeTable("testdb.reviews",
spark.read.schema(reviewsSchema).json("src/test/resources/data/reviews.json"))
writeTable("testdb.users_sample",
spark.read
.format(DefaultSource.SINGLESTORE_SOURCE_NAME_SHORT)
.load("testdb.users")
.sample(0.5)
.limit(10))
val movieRatingSchema = StructType(
StructField("id", LongType)
:: StructField("movie_rating", StringType)
:: StructField("same_rate_movies", StringType)
:: Nil)
writeTable(
"testdb.movies_rating",
spark.read.schema(movieRatingSchema).json("src/test/resources/data/movies_rating.json"))
}
override def beforeEach(): Unit = {
super.beforeEach()
spark.sql("create database testdb")
spark.sql("create database testdb_nopushdown")
spark.sql("create database testdb_jdbc")
def makeTables(sourceTable: String) = {
spark.sql(
s"create table testdb.$sourceTable using singlestore options ('dbtable'='testdb.$sourceTable')")
spark.sql(
s"create table testdb_nopushdown.$sourceTable using memsql options ('dbtable'='testdb.$sourceTable','disablePushdown'='true')")
spark.sql(s"create table testdb_jdbc.$sourceTable using jdbc options (${jdbcOptionsSQL(
s"testdb.$sourceTable")})")
}
makeTables("users")
makeTables("users_sample")
makeTables("movies")
makeTables("movies_rating")
makeTables("reviews")
spark.udf.register("stringIdentity", (s: String) => s)
}
def extractQueriesFromPlan(root: LogicalPlan): Seq[String] = {
root
.map({
case Relation(relation) => relation.sql
case _ => ""
})
.sorted
}
def testCodegenDeterminism(q: String, filterDF: DataFrame => DataFrame): Unit = {
val logManager = LogManager.getLogger("com.singlestore.spark.SQLGen$Statement")
var setLogToTrace = false
if (logManager.isTraceEnabled) {
logManager.setLevel(Level.DEBUG)
setLogToTrace = true
}
assert(
extractQueriesFromPlan(filterDF(spark.sql(q)).queryExecution.optimizedPlan) ==
extractQueriesFromPlan(filterDF(spark.sql(q)).queryExecution.optimizedPlan),
"All generated SingleStore queries should be the same"
)
if (setLogToTrace) {
logManager.setLevel(Level.TRACE)
}
}
def testQuery(q: String,
alreadyOrdered: Boolean = false,
expectPartialPushdown: Boolean = false,
expectSingleRead: Boolean = false,
expectEmpty: Boolean = false,
expectSameResult: Boolean = true,
expectCodegenDeterminism: Boolean = true,
pushdown: Boolean = true,
enableParallelRead: String = "automaticLite",
dataFrameEqualityPrecision: Double = 0.1,
filterDF: DataFrame => DataFrame = x => x): Unit = {
spark.sqlContext.setConf("spark.datasource.singlestore.enableParallelRead", enableParallelRead)
spark.sql("use testdb_jdbc")
val jdbcDF = filterDF(spark.sql(q))
// verify that the jdbc DF works first
jdbcDF.collect()
if (pushdown) { spark.sql("use testdb") } else { spark.sql("use testdb_nopushdown") }
if (expectCodegenDeterminism) {
testCodegenDeterminism(q, filterDF)
}
val singlestoreDF = filterDF(spark.sql(q))
if (!continuousIntegration) { singlestoreDF.show(4) }
if (expectEmpty) {
assert(singlestoreDF.count == 0, "result is expected to be empty")
} else {
assert(singlestoreDF.count > 0, "result is expected to not be empty")
}
if (expectSingleRead) {
assert(singlestoreDF.rdd.getNumPartitions == 1,
"query is expected to read from a single partition")
} else {
assert(singlestoreDF.rdd.getNumPartitions > 1,
"query is expected to read from multiple partitions")
}
assert(
(singlestoreDF.queryExecution.optimizedPlan match {
case SQLGen.Relation(_) => false
case _ => true
}) == expectPartialPushdown,
s"the optimized plan does not match expectPartialPushdown=$expectPartialPushdown"
)
if (expectSameResult) {
try {
def changeTypes(df: DataFrame): DataFrame = {
var newDf = df
df.schema
.foreach(x =>
x.dataType match {
// Replace all Floats with Doubles, because JDBC connector converts FLOAT to DoubleType when SingleStore connector converts it to FloatType
// Replace all Decimals with Doubles, because assertApproximateDataFrameEquality compare Decimals for strong equality
case _: DecimalType | FloatType =>
newDf = newDf.withColumn(x.name, newDf(x.name).cast(DoubleType))
// Replace all Shorts with Integers, because JDBC connector converts SMALLINT to IntegerType when SingleStore connector converts it to ShortType
case _: ShortType =>
newDf = newDf.withColumn(x.name, newDf(x.name).cast(IntegerType))
// Replace all CalendarIntervals with Strings, because assertApproximateDataFrameEquality can't sort CalendarIntervals
case _: CalendarIntervalType =>
newDf = newDf.withColumn(x.name, newDf(x.name).cast(StringType))
case _ =>
})
newDf
}
assertApproximateDataFrameEquality(changeTypes(singlestoreDF),
changeTypes(jdbcDF),
dataFrameEqualityPrecision,
orderedComparison = alreadyOrdered)
} catch {
case e: Throwable =>
if (continuousIntegration) { println(singlestoreDF.explain(true)) }
throw e
}
}
}
def testOrderedQuery(q: String, pushdown: Boolean = true): Unit = {
// order by in SingleStore requires single read
testQuery(q, alreadyOrdered = true, expectSingleRead = true, pushdown = pushdown)
afterEach()
beforeEach()
testQuery(q,
alreadyOrdered = true,
expectPartialPushdown = true,
expectSingleRead = true,
pushdown = pushdown,
enableParallelRead = "automatic")
afterEach()
beforeEach()
testQuery(q,
alreadyOrdered = true,
expectSingleRead = true,
pushdown = pushdown,
enableParallelRead = "disabled")
}
def testSingleReadQuery(q: String,
alreadyOrdered: Boolean = false,
expectPartialPushdown: Boolean = false,
pushdown: Boolean = true): Unit = {
testQuery(q,
alreadyOrdered = alreadyOrdered,
expectPartialPushdown = expectPartialPushdown,
expectSingleRead = true,
pushdown = pushdown)
}
def testSingleReadForReadFromLeaves(q: String): Unit = {
if (canDoParallelReadFromAggregators) {
testQuery(q)
} else {
testSingleReadQuery(q)
}
}
def testSingleReadForOldS2(q: String, minVersion: SinglestoreVersion): Unit = {
if (version.atLeast(minVersion) && canDoParallelReadFromAggregators) {
testQuery(q)
} else {
testSingleReadQuery(q)
}
}
describe("Attributes") {
describe("successful pushdown") {
it("Attribute") { testQuery("select id from users") }
it("Alias") { testQuery("select id as user_id from users") }
it("Alias with new line") { testQuery("select id as `user_id\n` from users") }
it("Alias with hyphen") { testQuery("select id as `user-id` from users") }
// DatasetComparer fails to sort a DataFrame with weird names, because of it following queries are ran as alreadyOrdered
it("Alias with dot") {
testOrderedQuery("select id as `user.id` from users order by id")
}
it("Alias with backtick") {
testOrderedQuery("select id as `user``id` from users order by id")
}
}
describe("unsuccessful pushdown") {
it("alias with udf") {
testQuery("select stringIdentity(id) as user_id from users", expectPartialPushdown = true)
}
}
}
describe("Literals") {
describe("successful pushdown") {
it("string") {
testSingleReadForReadFromLeaves("select 'string' from users")
}
it("null") {
testSingleReadForReadFromLeaves("select null from users")
}
describe("boolean") {
it("true") { testQuery("select true from users") }
it("false") { testQuery("select false from users") }
}
it("byte") { testQuery("select 100Y from users") }
it("short") { testQuery("select 100S from users") }
it("integer") { testQuery("select 100 from users") }
it("long") {
testSingleReadForReadFromLeaves("select 100L from users")
}
it("float") { testQuery("select 1.1 as x from users") }
it("double") { testQuery("select 1.1D as x from users") }
it("decimal") { testQuery("select 1.1BD as x from users") }
it("datetime") { testQuery("select date '1997-11-11' as x from users") }
}
describe("unsuccessful pushdown") {
it("interval") {
testQuery("select interval 1 year 1 month as x from users", expectPartialPushdown = true)
}
it("binary literal") {
testQuery("select X'123456' from users", expectPartialPushdown = true)
}
}
}
describe("Cast") {
describe("to boolean") {
it("boolean") {
testQuery("select cast(owns_house as boolean) from users")
}
it("int") {
testQuery("select cast(age as boolean) from users")
}
it("long") {
testQuery("select cast(id as boolean) from users")
}
it("float") {
testQuery("select cast(critic_rating as boolean) from movies")
}
it("date") {
testQuery("select cast(birthday as boolean) from users")
}
it("timestamp") {
testQuery("select cast(created as boolean) from reviews")
}
}
describe("to byte") {
it("boolean") {
testQuery("select cast(owns_house as byte) from users")
}
it("int") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min TINYINT value
// spark returns module (452->-60)
testQuery("select cast(age as byte) from users where age > -129 and age < 128")
}
it("long") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min TINYINT value
// spark returns module (452->-60)
testQuery("select cast(id as byte) from users where id > -129 and id < 128")
}
it("float") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min TINYINT value
// spark returns module (452->-60)
// singlestore and spark use different rounding
// singlestore round to the closest value
// spark round to the smaller one
testQuery(
"select cast(critic_rating as byte) from movies where critic_rating > -129 and critic_rating < 128 and critic_rating - floor(critic_rating) < 0.5")
}
it("date") {
testQuery("select cast(birthday as byte) from users")
}
it("timestamp") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min TINYINT value
// spark returns module (452->-60)
testQuery(
"select cast(created as byte) from reviews where cast(created as long) > -129 and cast(created as long) < 128")
}
}
describe("to short") {
it("boolean") {
testQuery("select cast(owns_house as short) from users")
}
it("int") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min SMALLINT value
// spark returns module (40004->-25532)
testQuery("select cast(age as short) from users where age > -32769 and age < 32768")
}
it("long") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min SMALLINT value
// spark returns module (40004->-25532)
testQuery("select cast(id as short) as d from users where id > -32769 and id < 32768")
}
it("float") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min SMALLINT value
// spark returns module (40004->-25532)
// singlestore and spark use different rounding
// singlestore round to the closest value
// spark round to the smaller one
testQuery(
"select cast(critic_rating as short) from movies where critic_rating > -32769 and critic_rating < 32768 and critic_rating - floor(critic_rating) < 0.5")
}
it("date") {
testQuery("select cast(birthday as short) from users")
}
it("timestamp") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min SMALLINT value
// spark returns module (40004->-25532)
testQuery(
"select cast(created as short) from reviews where cast(created as long) > -32769 and cast(created as long) < 32768")
}
}
describe("to int") {
it("boolean") {
testQuery("select cast(owns_house as int) from users")
}
it("int") {
testQuery("select cast(age as int) from users")
}
it("long") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min INT value
// spark returns module (10000000000->1410065408)
testQuery(
"select cast(id as int) as d from users where id > -2147483649 and id < 2147483648")
}
it("float") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min INT value
// spark returns module (10000000000->1410065408)
// singlestore and spark use different rounding
// singlestore round to the closest value
// spark round to the smaller one
testQuery(
"select cast(critic_rating as int) from movies where critic_rating > -2147483649 and critic_rating < 2147483648 and critic_rating - floor(critic_rating) < 0.5")
}
it("date") {
testQuery("select cast(birthday as int) from users")
}
it("timestamp") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min INT value
// spark returns module (10000000000->1410065408)
testQuery(
"select cast(created as int) from reviews where cast(created as long) > -2147483649 and cast(created as long) < 2147483648")
}
}
describe("to long") {
it("boolean") {
testQuery("select cast(owns_house as long) from users")
}
it("int") {
testQuery("select cast(age as long) from users")
}
it("long") {
testQuery("select cast(id as long) from users")
}
it("float") {
// singlestore and spark use different rounding
// singlestore round to the closest value
// spark round to the smaller one
testQuery(
"select cast(critic_rating as long) from movies where critic_rating - floor(critic_rating) < 0.5")
}
it("date") {
testQuery("select cast(birthday as long) from users")
}
it("timestamp") {
testQuery("select cast(created as long) from reviews")
}
}
describe("to float") {
it("boolean") {
testQuery("select cast(owns_house as float) from users")
}
it("int") {
testQuery("select cast(age as float) from users")
}
it("long") {
testQuery("select cast(id as float) from users")
}
it("float") {
testQuery("select cast(critic_rating as float) from movies")
}
it("date") {
testQuery("select cast(birthday as float) from users")
}
it("timestamp") {
testQuery("select cast(created as float) from reviews")
}
}
describe("to double") {
it("boolean") {
testQuery("select cast(owns_house as double) from users")
}
it("int") {
testQuery("select cast(age as double) from users")
}
it("long") {
testQuery("select cast(id as double) from users")
}
it("float") {
testQuery("select cast(critic_rating as double) from movies")
}
it("date") {
testQuery("select cast(birthday as double) from users")
}
it("timestamp") {
testQuery("select cast(created as double) from reviews")
}
}
describe("to decimal") {
it("boolean") {
testQuery("select cast(owns_house as decimal(20, 5)) from users")
}
it("int") {
testQuery("select cast(age as decimal(20, 5)) from users")
}
it("long") {
// singlestore and spark behaviour differs on the overflow
// singlestore returns max/min DECIMAL(x, y) value
// spark returns null
testQuery("select cast(id as decimal(20, 5)) from users where id < 999999999999999.99999")
}
it("float") {
testQuery("select cast(critic_rating as decimal(20, 5)) from movies")
}
it("date") {
testQuery("select cast(birthday as decimal(20, 5)) from users")
}
it("timestamp") {
testQuery("select cast(created as decimal(20, 5)) from reviews")
}
}
describe("to string") {
it("boolean") {
testQuery("select cast(owns_house as string) from users")
}
it("int") {
testQuery("select cast(age as string) from users")
}
it("long") {
testQuery("select cast(id as string) from users")
}
it("float") {
// singlestore converts integers to string with 0 digits after the point
// spark adds 1 digit after the point
testQuery("select cast(cast(critic_rating as string) as float) from movies")
}
it("date") {
testQuery("select cast(birthday as string) from users")
}
it("timestamp") {
// singlestore converts timestamps to string with 6 digits after the point
// spark adds 0 digit after the point
testQuery("select cast(cast(created as string) as timestamp) from reviews")
}
it("string") {
testQuery("select cast(first_name as string) from users")
}
}
describe("to binary") {
// spark doesn't support casting other types to binary
it("string") {
testQuery("select cast(first_name as binary) from users")
}
}
describe("to date") {
it("date") {
testQuery("select cast(birthday as date) from users")
}
it("timestamp") {
testQuery("select cast(created as date) from reviews")
}
it("string") {
testQuery("select cast(first_name as date) from users")
}
}
describe("to timestamp") {
it("boolean") {
testQuery("select cast(owns_house as timestamp) from users")
}
it("int") {
testQuery("select cast(age as timestamp) from users")
}
it("long") {
// TIMESTAMP in SingleStore doesn't support values greater then 2147483647999
testQuery("select cast(id as timestamp) from users where id < 2147483647999")
}
it("float") {
// singlestore and spark use different rounding
// singlestore round to the closest value
// spark round to the smaller one
testQuery(
"select to_unix_timestamp(cast(critic_rating as timestamp)) as x from movies where critic_rating - floor(critic_rating) < 0.5")
}
it("date") {
testQuery("select cast(birthday as timestamp) from users")
}
it("timestamp") {
testQuery("select cast(created as timestamp) from reviews")
}
it("string") {
testQuery("select cast(first_name as timestamp) from users")
}
}
}
describe("Variable Expressions") {
describe("Coalesce") {
it("one non-null value") { testQuery("select coalesce(id) from users") }
it("one null value") {
testSingleReadForReadFromLeaves("select coalesce(null) from users")
}
it("a lot of values") { testQuery("select coalesce(null, id, null, id+1) from users") }
it("a lot of nulls") {
testSingleReadForReadFromLeaves("select coalesce(null, null, null) from users")
}
it("partial pushdown with udf") {
testQuery(
"select coalesce(stringIdentity(first_name), 'qwerty', 'bob', 'alice') from users",
expectPartialPushdown = true)
}
}
describe("Least") {
it("a lot of ints") { testQuery("select least(id+5, id, 5, id+1) from users") }
it("a lot of strings") {
testQuery("select least('qwerty', 'bob', first_name, 'alice') from users")
}
// SingleStore returns NULL if at least one argument is NULL, when spark skips nulls
// it("ints with null") { testQuery("select least(null, id, null, id+1) from users") }
it("a lot of nulls") {
testSingleReadForReadFromLeaves("select least(null, null, null) from users")
}
it("partial pushdown with udf") {
testQuery("select least('qwerty', 'bob', stringIdentity(first_name), 'alice') from users",
expectPartialPushdown = true)
}
}
describe("Greatest") {
it("a lot of ints") { testQuery("select greatest(id+5, id, 5, id+1) from users") }
it("a lot of strings") {
testQuery("select greatest('qwerty', 'bob', first_name, 'alice') from users")
}
// SingleStore returns NULL if at least one argument is NULL, when spark skips nulls
// it("ints with null") { testQuery("select greatest(null, id, null, id+1) from users") }
it("a lot of nulls") {
testSingleReadForReadFromLeaves("select greatest(null, null, null) from users")
}
it("partial pushdown with udf") {
testQuery(
"select greatest('qwerty', 'bob', stringIdentity(first_name), 'alice') from users",
expectPartialPushdown = true)
}
}
describe("Concat") {
it("a lot of ints") { testQuery("select concat(id+5, id, 5, id+1) from users") }
it("a lot of strings") {
testQuery("select concat('qwerty', 'bob', first_name, 'alice') from users")
}
it("ints with null") { testQuery("select concat(null, id, null, id+1) from users") }
it("a lot of nulls") {
testSingleReadForReadFromLeaves("select concat(null, null, null) from users")
}
it("int and string") { testQuery("select concat(id, first_name) from users") }
it("same expressions") {
testQuery("select concat(id, id, id) from users")
}
it("nested same expressions") {
testQuery("select * from (select concat(id, id, id) from users)")
}
it("partial pushdown with udf") {
testQuery("select concat('qwerty', 'bob', stringIdentity(first_name), 'alice') from users",
expectPartialPushdown = true)
}
}
describe("Elt") {
it("a lot of ints") { testQuery("select elt(id+5, id, 5, id+1) from users") }
it("a lot of strings") {
testQuery("select elt('qwerty', 'bob', first_name, 'alice') from users")
}
it("ints with null") { testQuery("select elt(null, id, null, id+1) from users") }
it("a lot of nulls") { testQuery("select elt(null, null, null) from users") }
it("int and string") { testQuery("select elt(id, first_name) from users") }
it("partial pushdown with udf") {
testQuery("select elt('qwerty', 'bob', stringIdentity(first_name), 'alice') from users",
expectPartialPushdown = true)
}
}
}
describe("Null Expressions") {
describe("IfNull") {
it("returns the second argument") {
testQuery("select IfNull(null, id) as ifn from users")
}
it("returns the first argument") {
testQuery("select IfNull(id, null) as ifn from users")
}
it("partial pushdown with udf") {
testQuery("select IfNull(stringIdentity(id), id) as ifn from users",
expectPartialPushdown = true)
}
}
describe("NullIf") {
it("equal arguments") {
testQuery("select id, NullIf(1, 1) as nif from users")
}
it("non-equal arguments") {
testQuery("select NullIf(id, null) as nif from users")
}
it("partial pushdown with udf") {
testQuery("select IfNull(null, stringIdentity(id)) from users",
expectPartialPushdown = true)
}
}
describe("Nvl") {
it("returns the second argument") {
testQuery("select Nvl(null, id) as id1 from users")
}
it("returns the first argument") {
testQuery("select Nvl(id, id+1) as id1 from users")
}
it("partial pushdown with udf") {
testQuery("select Nvl(stringIdentity(id), null) from users", expectPartialPushdown = true)
}
}
describe("Nvl2") {
it("returns the second argument") {
testSingleReadForReadFromLeaves("select Nvl2(null, id, 0) as id1 from users")
}
it("returns the first argument") {
testQuery("select Nvl2(id, 10, id+1) as id1 from users")
}
it("partial pushdown with udf") { // error
testQuery("select Nvl2(stringIdentity(id), null, id) as id1 from users",
expectPartialPushdown = true)
}
}
describe("IsNull") {
it("not null") {
testQuery("select IsNull(favorite_color) from users")
}
it("null") {
testQuery("select IsNull(null) from users")
}
it("partial pushdown with udf") {
testQuery("select IsNull(stringIdentity(id)) from users", expectPartialPushdown = true)
}
}
describe("IsNotNull") {
it("not null") {
testQuery("select IsNotNull(favorite_color) from users")
}
it("null") {
testQuery("select IsNotNull(null) from users")
}
it("partial pushdown with udf") {
testQuery("select IsNotNull(stringIdentity(id)) from users", expectPartialPushdown = true)
}
}
}
describe("Predicates") {
describe("Not") {
it("not null") {
testQuery("select not(cast(owns_house as boolean)) from users")
}
it("null") {
testQuery("select not(null) from users")
}
it("partial pushdown with udf") {
testQuery("select not(cast(stringIdentity(id) as boolean)) from users",
expectPartialPushdown = true)
}
}
describe("If") {
it("boolean") {
testQuery("select if(cast(owns_house as boolean), first_name, last_name) from users")
}
it("always true") {
testQuery("select if(true, first_name, last_name) from users")
}
it("partial pushdown with udf") {
testQuery(
"select if(cast(stringIdentity(id) as boolean), first_name, last_name) from users",
expectPartialPushdown = true)
}
}
describe("case when") {
it("simple") {
testQuery("select case when id < 10 then 1 else 3 end from users_sample")
}
it("match multiple conditions") {
testQuery(
"select case id when id < 3 then id when id < 6 and id >= 3 then age when id < 9 and id >=6 then last_name else first_name end from users_sample")
}
it("match else condition") {
testQuery(
"select id, case when 1 < 0 then id when 2 < 0 or 3 < 0 then last_name else first_name end from users_sample")
}
it("without else condition, select NULL") {
testQuery("select id, case when 1 < 0 then id end from users_sample")
}
it("complicated case: comparing string with a numeric type") {
testQuery(
"select id, case last_name when id < '5' then 'No_name' when 'Paute' then 'Random_surname' else last_name end from users_sample",
expectSameResult = false
// When string is casted to numeric type, singlestore takes the prefix of it
// which is numeric (spark returns null if the whole string is not numeric)
)
}
it("partial pushdown: invalid condition") {
testQuery("select case when IsNull(stringIdentity(id)) then 1 else id end from users",
expectPartialPushdown = true)
}
it("partial pushdown: invalid condition, more complicated query") {
testQuery(
"select case when id < 2 then last_name when IsNull(stringIdentity(id)) then 1 else id end from users",
expectPartialPushdown = true)
}
it("partial pushdown: invalid `else` condition") {
testQuery("select case when id < 5 then 1 else stringIdentity(id) end from users",
expectPartialPushdown = true)
}
}
}
describe("arithmetic") {
describe("Add") {
it("numbers") { testQuery("select user_id + movie_id as x from reviews") }
it("floats") { testQuery("select rating + 1.0 as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select stringIdentity(user_id) + movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id + stringIdentity(movie_id) as x from reviews",
expectPartialPushdown = true)
}
}
describe("Subtract") {
it("numbers") { testQuery("select user_id - movie_id as x from reviews") }
it("floats") { testQuery("select rating - 1.0 as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select stringIdentity(user_id) - movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id - stringIdentity(movie_id) as x from reviews",
expectPartialPushdown = true)
}
}
describe("Multiply") {
it("numbers") { testQuery("select user_id * movie_id as x from reviews") }
it("floats") { testQuery("select rating * 1.3 as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select stringIdentity(user_id) * movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id * stringIdentity(movie_id) as x from reviews",
expectPartialPushdown = true)
}
}
describe("Divide") {
it("numbers") { testQuery("select user_id / movie_id as x from reviews") }
it("floats") { testQuery("select rating / 1.3 as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select stringIdentity(user_id) / movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id / stringIdentity(movie_id) as x from reviews",
expectPartialPushdown = true)
}
}
describe("Remainder") {
it("numbers") { testQuery("select user_id % movie_id as x from reviews") }
it("floats") { testQuery("select rating % 4 as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select stringIdentity(user_id) % movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id % stringIdentity(movie_id) as x from reviews",
expectPartialPushdown = true)
}
}
describe("Pmod") {
it("numbers") { testQuery("select pmod(user_id, movie_id) as x from reviews") }
it("floats") { testQuery("select pmod(rating, 4) as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select pmod(stringIdentity(user_id), movie_id) as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select pmod(user_id, stringIdentity(movie_id)) as x from reviews",
expectPartialPushdown = true)
}
}
describe("UnaryMinus") {
it("numbers") { testQuery("select -id from users") }
it("floats") { testQuery("select -critic_rating from movies") }
it("partial pushdown with udf") {
testQuery("select -stringIdentity(id) from users", expectPartialPushdown = true)
}
}
describe("UnaryPositive") {
it("numbers") { testQuery("select +id from users") }
it("floats") { testQuery("select +critic_rating from movies") }
it("partial pushdown with udf") {
testQuery("select +stringIdentity(id) from users", expectPartialPushdown = true)
}
}
describe("Abs") {
it("positive numbers") { testQuery("select abs(id) from users") }
it("negative numbers") { testQuery("select abs(-id) from users") }
it("positive floats") { testQuery("select abs(critic_rating) from movies") }
it("negative floats") { testQuery("select abs(-critic_rating) from movies") }
it("partial pushdown with udf") {
testQuery("select abs(stringIdentity(id)) from users", expectPartialPushdown = true)
}
}
}
describe("bitwiseExpressions") {
describe("And") {
it("succeeds") { testQuery("select user_id & movie_id as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select cast(stringIdentity(user_id) as integer) & movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id & cast(stringIdentity(movie_id) as integer) as x from reviews",
expectPartialPushdown = true)
}
}
describe("Or") {
it("numbers") { testQuery("select user_id | movie_id as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select cast(stringIdentity(user_id) as integer) | movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id | cast(stringIdentity(movie_id) as integer) as x from reviews",
expectPartialPushdown = true)
}
}
describe("Xor") {
it("numbers") { testQuery("select user_id ^ movie_id as x from reviews") }
it("partial pushdown because of udf in the left argument") {
testQuery("select cast(stringIdentity(user_id) as integer) ^ movie_id as x from reviews",
expectPartialPushdown = true)
}
it("partial pushdown because of udf in the right argument") {
testQuery("select user_id ^ cast(stringIdentity(movie_id) as integer) as x from reviews",
expectPartialPushdown = true)
}
}
describe("bitwiseGet") {
it("numbers", ExcludeFromSpark31) {
testQuery("select bit_get(id, 2) from users_sample")
}
it("negative left argument", ExcludeFromSpark31) {
try {
testQuery("select bit_get(id, -2) from users_sample")
} catch {
case e: Throwable =>
if (e.toString.contains("Invalid bit position: -2 is less than zero")) {
None
} else {
throw e
}
}
}
it("negative right argument", ExcludeFromSpark31) {
testQuery("select bit_get(-200, 2), id from users_sample")