forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringFunctionsTest.cpp
2056 lines (1891 loc) · 80.3 KB
/
StringFunctionsTest.cpp
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
/*
* Copyright 2019 OmniSci, Inc.
*
* 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.
*/
/**
* @file StringFunctionsTest.cpp
* @brief Test suite for string functions
*/
#include "Catalog/Catalog.h"
#include "QueryEngine/ResultSet.h"
#include "QueryRunner/QueryRunner.h"
#include "Shared/scope.h"
#include "TestHelpers.h"
#include <gtest/gtest.h>
#include <boost/format.hpp>
#include <boost/locale/generator.hpp>
#include <sstream>
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
using namespace TestHelpers;
extern bool g_enable_string_functions;
extern unsigned g_trivial_loop_join_threshold;
extern bool g_enable_watchdog;
extern size_t g_watchdog_none_encoded_string_translation_limit;
namespace {
using QR = QueryRunner::QueryRunner;
bool skip_tests(const ExecutorDeviceType device_type) {
#ifdef HAVE_CUDA
return device_type == ExecutorDeviceType::GPU && !(QR::get()->gpusPresent());
#else
return device_type == ExecutorDeviceType::GPU;
#endif
}
#define SKIP_NO_GPU() \
if (skip_tests(dt)) { \
CHECK(dt == ExecutorDeviceType::GPU); \
LOG(WARNING) << "GPU not available, skipping GPU tests"; \
continue; \
}
static const bool reuse_test_data = true;
inline auto sql(const std::string& sql_stmt,
const ExecutorDeviceType device_type = ExecutorDeviceType::CPU,
const bool enable_loop_joins = false) {
const auto trivial_loop_join_threshold_state = g_trivial_loop_join_threshold;
ScopeGuard reset_loop_join_state = [&trivial_loop_join_threshold_state] {
g_trivial_loop_join_threshold = trivial_loop_join_threshold_state;
};
g_trivial_loop_join_threshold = 0;
return QueryRunner::QueryRunner::get()->runSQL(
sql_stmt, device_type, true, enable_loop_joins);
}
inline TargetValue run_simple_agg(const std::string& query_str,
const ExecutorDeviceType device_type,
const bool allow_loop_joins = true) {
auto rows = QR::get()->runSQL(query_str, device_type, allow_loop_joins);
auto crt_row = rows->getNextRow(true, true);
CHECK_EQ(size_t(1), crt_row.size()) << query_str;
return crt_row[0];
}
inline auto multi_sql(const std::string& sql_stmts) {
return QueryRunner::QueryRunner::get()
->runMultipleStatements(sql_stmts, ExecutorDeviceType::CPU)
.back();
}
inline void run_ddl_statement(const std::string& create_table_stmt) {
QR::get()->runDDLStatement(create_table_stmt);
}
class AssertValueEqualsVisitor : public boost::static_visitor<> {
public:
AssertValueEqualsVisitor(const size_t& row, const size_t& column)
: row(row), column(column) {}
template <typename T, typename U>
void operator()(const T& expected, const U& actual) const {
if (std::is_pointer<U>::value) {
EXPECT_EQ(1UL, 1UL);
} else {
FAIL() << boost::format(
"Values are of different types. Expected result set value: %s is of "
"type: %s while actual result set value: %s is of type: %s. At row: "
"%d, column: %d") %
expected % typeid(expected).name() % actual % typeid(actual).name() %
row % column;
}
}
template <typename T>
void operator()(const T& expected, const T& actual) const {
EXPECT_EQ(expected, actual) << boost::format("At row: %d, column: %d") % row % column;
}
private:
size_t row;
size_t column;
};
template <>
void AssertValueEqualsVisitor::operator()<NullableString>(
const NullableString& expected,
const NullableString& actual) const {
boost::apply_visitor(AssertValueEqualsVisitor(row, column), expected, actual);
}
void assert_value_equals(ScalarTargetValue& expected,
ScalarTargetValue& actual,
const size_t& row,
const size_t& column) {
boost::apply_visitor(AssertValueEqualsVisitor(row, column), expected, actual);
}
void compare_result_set(
const std::vector<std::vector<ScalarTargetValue>>& expected_result_set,
const std::shared_ptr<ResultSet>& actual_result_set) {
auto row_count = actual_result_set->rowCount(false);
ASSERT_EQ(expected_result_set.size(), row_count)
<< "Returned result set does not have the expected number of rows";
if (row_count == 0) {
return;
}
auto expected_column_count = expected_result_set[0].size();
auto column_count = actual_result_set->colCount();
ASSERT_EQ(expected_column_count, column_count)
<< "Returned result set does not have the expected number of columns";
;
for (size_t r = 0; r < row_count; ++r) {
auto row = actual_result_set->getNextRow(true, true);
for (size_t c = 0; c < column_count; c++) {
auto column_value = boost::get<ScalarTargetValue>(row[c]);
auto expected_column_value = expected_result_set[r][c];
assert_value_equals(expected_column_value, column_value, r, c);
}
}
}
} // namespace
// begin string function tests
/**
* @brief Class used for setting up and tearing down tables and records that are required
* by the string function test cases
*/
class StringFunctionTest : public testing::Test {
protected:
void SetUp() override {
if (!reuse_test_data || !StringFunctionTest::test_data_loaded) {
ASSERT_NO_THROW(multi_sql(R"(
drop table if exists string_function_test_people;
create table string_function_test_people(id int, first_name text, last_name text encoding none, full_name text, age integer, country_code text, us_phone_number text, zip_plus_4 text, personal_motto text, raw_email text);
insert into string_function_test_people values(1, 'JOHN', 'SMITH', 'John SMITH', 25, 'us', '555-803-2144', '90210-7743', 'All for one and one for all.', 'Shoot me a note at [email protected]');
insert into string_function_test_people values(2, 'John', 'Banks', 'John BANKS', 30, 'Us', '555-803-8244', '94104-8123', 'One plus one does not equal two.', 'Email: [email protected]');
insert into string_function_test_people values(3, 'JOHN', 'Wilson', 'John WILSON', 20, 'cA', '555-614-9814', null, 'What is the sound of one hand clapping?', '[email protected]');
insert into string_function_test_people values(4, 'Sue', 'Smith', 'Sue SMITH', 25, 'CA', '555-614-2282', null, 'Nothing exists entirely alone. Everything is always in relation to everything else.', 'Find me at [email protected], or reach me at [email protected]. I''d love to hear from you!');
drop table if exists string_function_test_countries;
create table string_function_test_countries(id int, code text, arrow_code text, name text, short_name text encoding none, capital text, largest_city text encoding none, lang text encoding none);
insert into string_function_test_countries values(1, 'US', '>>US<<', 'United States', null, 'Washington', 'New York City', 'en');
insert into string_function_test_countries values(2, 'ca', '>>CA<<', 'Canada', 'Canada', 'Ottawa', 'TORONTO', 'EN');
insert into string_function_test_countries values(3, 'Gb', '>>GB<<', 'United Kingdom', 'UK', 'London', 'LONDON', 'en');
insert into string_function_test_countries values(4, 'dE', '>>DE<<', 'Germany', 'Germany', 'Berlin', 'Berlin', 'de');
)"));
StringFunctionTest::test_data_loaded = true;
}
}
void TearDown() override {
if (!reuse_test_data) {
ASSERT_NO_THROW(multi_sql(R"(
drop table string_function_test_people;
drop table string_function_test_countries;
)"););
}
}
static bool test_data_loaded;
};
bool StringFunctionTest::test_data_loaded = false;
TEST_F(StringFunctionTest, Lowercase) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select lower(first_name) from string_function_test_people order by id asc;", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"john"}, {"john"}, {"john"}, {"sue"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, LowercaseLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select lower('fUnNy CaSe');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"funny case"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, Uppercase) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select upper(first_name) from string_function_test_people order by id asc;", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"JOHN"}, {"JOHN"}, {"JOHN"}, {"SUE"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, UppercaseLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select upper('fUnNy CaSe');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"FUNNY CASE"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, InitCap) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select initcap(full_name) from string_function_test_people order by id asc", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"John Smith"}, {"John Banks"}, {"John Wilson"}, {"Sue Smith"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, InitCapLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select initcap('fUnNy CaSe');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"Funny Case"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, Reverse) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select reverse(full_name) from string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"HTIMS nhoJ"}, {"SKNAB nhoJ"}, {"NOSLIW nhoJ"}, {"HTIMS euS"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, ReverseLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select reverse('fUnNy CaSe');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"eSaC yNnUf"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, Repeat) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select repeat(full_name, 2) from string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"John SMITHJohn SMITH"},
{"John BANKSJohn BANKS"},
{"John WILSONJohn WILSON"},
{"Sue SMITHSue SMITH"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, RepeatLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select repeat('fUnNy CaSe', 3);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"fUnNy CaSefUnNy CaSefUnNy CaSe"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, Concat) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select name || ', Earth' from string_function_test_countries order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"United States, Earth"},
{"Canada, Earth"},
{"United Kingdom, Earth"},
{"Germany, Earth"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, ReverseConcat) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select 'Country: ' || code from string_function_test_countries order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"Country: US"}, {"Country: ca"}, {"Country: Gb"}, {"Country: dE"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, ConcatLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select 'fUnNy CaSe' || ' is the case.';", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"fUnNy CaSe is the case."}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, LPad) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select lpad(name, 14) from string_function_test_countries order by id asc;", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{" United States"}, {" Canada"}, {"United Kingdom"}, {" Germany"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, LPadTruncate) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select lpad(name, 5) from string_function_test_countries order by id asc;", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"Unite"}, {"Canad"}, {"Unite"}, {"Germa"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, LPadCustomChars) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select lpad(name, 14, '>|<') from string_function_test_countries order by id "
"asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{">United States"}, {">|<>|<>|Canada"}, {"United Kingdom"}, {">|<>|<>Germany"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, DISABLED_LPadLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select lpad('123', 2);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{" 123"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, RPad) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select rpad(name, 20) from string_function_test_countries order by id asc;", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"United States "},
{"Canada "},
{"United Kingdom "},
{"Germany "}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, RPadLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select rpad('$323.', 8, '98') from string_function_test_countries order by id "
"asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"$323.989"}, {"$323.989"}, {"$323.989"}, {"$323.989"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, TrimBothDefault) {
// Will be a no-op as default trim character is space
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select trim(arrow_code) from string_function_test_countries order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{">>US<<"}, {">>CA<<"}, {">>GB<<"}, {">>DE<<"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, TrimBothCustom) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Implicit 'BOTH
auto result_set1 =
sql("select trim('<>' from arrow_code) from string_function_test_countries order "
"by id asc;",
dt);
// explicit syntax
auto result_set2 =
sql("select trim(both '<>' from arrow_code) from string_function_test_countries "
"order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"US"}, {"CA"}, {"GB"}, {"DE"}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
}
TEST_F(StringFunctionTest, TrimBothLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set1 = sql("select trim(both ' !' from ' Oops!');", dt);
auto result_set2 = sql("select trim(' !' from ' Oops!');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"Oops"}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
}
TEST_F(StringFunctionTest, LeftTrim) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Trim with 'LEADING'
auto result_set1 =
sql("select trim(leading '<>#' from arrow_code) from "
"string_function_test_countries order by id asc;",
dt);
// Explicit LTrim
auto result_set2 = sql(
"select ltrim(arrow_code, '<>#') from string_function_test_countries order by "
"id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"US<<"}, {"CA<<"}, {"GB<<"}, {"DE<<"}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
}
TEST_F(StringFunctionTest, LeftTrimLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Trim with 'LEADING'
auto result_set1 = sql("select trim(leading '$' from '$19.99$');", dt);
// LTrim
auto result_set2 = sql("select ltrim('$19.99$', '$');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"19.99$"}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
}
TEST_F(StringFunctionTest, RightTrim) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Trim with 'TRAILING'
auto result_set1 =
sql("select trim(trailing '<> ' from arrow_code) from "
"string_function_test_countries order by id asc;",
dt);
// RTrim
auto result_set2 = sql(
"select rtrim(arrow_code, '<> ') from string_function_test_countries order by "
"id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{">>US"}, {">>CA"}, {">>GB"}, {">>DE"}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
}
TEST_F(StringFunctionTest, RightTrimLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Trim with 'TRAILING'
auto result_set1 = sql("select trim(trailing '|' from '|half pipe||');");
// RTrim
auto result_set2 = sql("select rtrim('|half pipe||', '|');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"|half pipe"}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
}
TEST_F(StringFunctionTest, Substring) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
{
auto result_set1 = sql(
"select substring(full_name, 1, 4) from string_function_test_people order by "
"id asc;",
dt);
auto result_set2 =
sql("select substring(full_name from 1 for 4) from string_function_test_people "
"order by "
"id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"John"}, {"John"}, {"John"}, {"Sue "}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
{
// Test null inputs
auto result_set1 = sql(
"select substring(zip_plus_4, 1, 5) from string_function_test_people order by "
"id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"90210"}, {"94104"}, {""}, {""}};
}
}
}
TEST_F(StringFunctionTest, SubstringNegativeWrap) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select substring(full_name, -3, 2) from string_function_test_people order by "
"id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"IT"}, {"NK"}, {"SO"}, {"IT"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SubstringLengthOffEnd) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select substring(code, 2, 10) from string_function_test_countries order by id "
"asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"S"}, {"a"}, {"b"}, {"E"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SubstringLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select substring('fUnNy CaSe', 4, 4);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"Ny C"}};
compare_result_set(expected_result_set, result_set);
}
}
// Test that index of 0 is equivalent to index of 1 (first character)
TEST_F(StringFunctionTest, SubstringLengthZeroStartLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set1 = sql("select substring('12345', 1, 3);", dt);
auto result_set2 = sql("select substring('12345', 0, 3);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"123"}};
compare_result_set(expected_result_set, result_set1);
compare_result_set(expected_result_set, result_set2);
}
}
TEST_F(StringFunctionTest, SubstrAlias) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select substr(us_phone_number, 5, 3) from string_function_test_people order "
"by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"803"}, {"803"}, {"614"}, {"614"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SubstrAliasLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select substr('fUnNy CaSe', 4, 4);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"Ny C"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, Overlay) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select overlay(us_phone_number placing '6273' from 9) from "
"string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"555-803-6273"}, {"555-803-6273"}, {"555-614-6273"}, {"555-614-6273"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, OverlayInsert) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select overlay(us_phone_number placing '+1-' from 1 for 0) from "
"string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"+1-555-803-2144"},
{"+1-555-803-8244"},
{"+1-555-614-9814"},
{"+1-555-614-2282"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, OverlayLiteralNoFor) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select overlay('We all love big data.' PLACING 'fast' FROM 13);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"We all love fastdata."}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, OverlayLiteralWithFor) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select overlay('We all love big data.' PLACING 'fast' FROM 13 FOR 3);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"We all love fast data."}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, Replace) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select replace(us_phone_number, '803', '#^!') from "
"string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"555-#^!-2144"}, {"555-#^!-8244"}, {"555-614-9814"}, {"555-614-2282"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, DISABLED_ReplaceEmptyReplacement) {
// Todo: Determine why Calcite is not accepting 2-parameter version
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select replace(us_phone_number, '555-') from "
"string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"803-2144"}, {"803-8244"}, {"614-9814"}, {"614-2282"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, ReplaceLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select replace('We all love big data.', 'big', 'fast');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"We all love fast data."}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, DISABLED_ReplaceLiteralEmptyReplacement) {
// Todo: Determine why Calcite is not accepting 2-parameter version
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select replace('We all love big data.', 'big');", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"We all love data."}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SplitPart) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select split_part(us_phone_number, '-', 2) from string_function_test_people "
"order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"803"}, {"803"}, {"614"}, {"614"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SplitPartNegativeIndex) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select split_part(us_phone_number, '-', -1) from "
"string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"2144"}, {"8244"}, {"9814"}, {"2282"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SplitPartLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select split_part('192.168.0.1', '.', 2);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"168"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SplitPartLiteralNegativeIndex) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select split_part('192.168.0.1', '.', -1);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{"1"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, SplitPartLiteralNullIndex) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql("select split_part('192.168.0.1', '.', 5);", dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{{""}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, RegexpReplace2Args) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select regexp_replace(name, 'United[[:space:]]') from "
"string_function_test_countries order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"States"}, {"Canada"}, {"Kingdom"}, {"Germany"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, RegexpReplace3Args) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select regexp_replace(name, 'United[[:space:]]([[:alnum:]])', 'The United "
"$1') from string_function_test_countries order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"The United States"}, {"Canada"}, {"The United Kingdom"}, {"Germany"}};
compare_result_set(expected_result_set, result_set);
}
}
// 4th argument is position
TEST_F(StringFunctionTest, RegexpReplace4Args) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
{
auto result_set =
sql("select regexp_replace(personal_motto, '([Oo]ne)[[:space:]]', '$1..two ', "
"4) from string_function_test_people order by id asc");
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"All for one..two and one..two for all."},
// Note we don't replace the first One due to start position argument of 4
{"One plus one..two does not equal two."},
{"What is the sound of one..two hand clapping?"},
{"Nothing exists entirely alone. Everything is always in relation to "
"everything else."}};
compare_result_set(expected_result_set, result_set);
}
// Test negative position, should wrap
{
auto result_set =
sql("select regexp_replace(personal_motto, '([Oo]ne)[[:space:]]', '$1..two ', "
"-18) from string_function_test_people order by id asc");
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"All for one and one..two for all."},
// Note we don't replace the first One due to start position argument of 4
{"One plus one does not equal two."},
{"What is the sound of one..two hand clapping?"},
{"Nothing exists entirely alone. Everything is always in relation to "
"everything else."}};
compare_result_set(expected_result_set, result_set);
}
}
}
// 5th argument is occurrence
TEST_F(StringFunctionTest, RegexpReplace5Args) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// 0 for 5th (occurrence) arguments says to replace all matches
{
auto result_set =
sql("select regexp_replace(personal_motto, '([Oo]ne)[[:space:]]', '$1..two ', "
"1, 0) from string_function_test_people order by id asc");
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"All for one..two and one..two for all."},
{"One..two plus one..two does not equal two."},
{"What is the sound of one..two hand clapping?"},
{"Nothing exists entirely alone. Everything is always in relation to "
"everything else."}};
compare_result_set(expected_result_set, result_set);
}
{
// Replace second match
auto result_set =
sql("select regexp_replace(personal_motto, '([Oo]ne)[[:space:]]', '$1..two ', "
"1, 2) from string_function_test_people order by id asc");
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"All for one and one..two for all."},
// Note we don't replace the first One due to start position argument of 4
{"One plus one..two does not equal two."},
{"What is the sound of one hand clapping?"},
{"Nothing exists entirely alone. Everything is always in relation to "
"everything else."}};
compare_result_set(expected_result_set, result_set);
}
{
// Replace second to last match via negative wrapping
auto result_set =
sql("select regexp_replace(personal_motto, '([Oo]ne)[[:space:]]', '$1..two ', "
"1, -2) from string_function_test_people order by id asc");
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"All for one..two and one for all."},
// Note we don't replace the first One due to start position argument of 4
{"One..two plus one does not equal two."},
{"What is the sound of one hand clapping?"},
{"Nothing exists entirely alone. Everything is always in relation to "
"everything else."}};
compare_result_set(expected_result_set, result_set);
}
}
}
// 6th argument is regex parameters
TEST_F(StringFunctionTest, RegexpReplace6Args) {
// Currently only support 'c' (case sensitive-default) and 'i' (case insensitive) for
// RegexpReplace
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// Test 'c' - case sensitive
{
auto result_set =
sql("select regexp_replace(personal_motto, '(one)[[:space:]]', '$1..two ', 1, "
"0, 'c') from string_function_test_people order by id asc");
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"All for one..two and one..two for all."},
// Note "One" in next entry doesn't match due to case sensitive search
{"One plus one..two does not equal two."},
{"What is the sound of one..two hand clapping?"},
{"Nothing exists entirely alone. Everything is always in relation to "
"everything else."}};
compare_result_set(expected_result_set, result_set);
}
// Test 'i' - case insensitive
{
auto result_set =
sql("select regexp_replace(personal_motto, '(one)[[:space:]]', '$1..two ', 1, "
"0, 'i') from string_function_test_people order by id asc");
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"All for one..two and one..two for all."},
// With case insensitive search, "One" will match
{"One..two plus one..two does not equal two."},
{"What is the sound of one..two hand clapping?"},
{"Nothing exists entirely alone. Everything is always in relation to "
"everything else."}};
compare_result_set(expected_result_set, result_set);
}
// Test that invalid regex param causes exception
{
EXPECT_ANY_THROW(
sql("select regexp_replace(personal_motto, '(one)[[:space:]]', '$1..two ', 1, "
"0, 'iz') from string_function_test_people order by id asc;",
dt));
}
}
}
TEST_F(StringFunctionTest, RegexpReplaceLiteral) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select regexp_replace('How much wood would a wood chuck chuck if a wood "
"chuck could chuck wood?', 'wo[[:alnum:]]+d', 'metal', 1, 0, 'i');",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"How much metal metal a metal chuck chuck if a metal chuck could chuck metal?"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, RegexpReplaceLiteralSpecificMatch) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set =
sql("select regexp_replace('How much wood would a wood chuck chuck if a wood "
"chuck could chuck wood?', 'wo[[:alnum:]]+d', 'should', 1, 2, 'i');",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"How much wood should a wood chuck chuck if a wood chuck could chuck wood?"}};
compare_result_set(expected_result_set, result_set);
}
}
TEST_F(StringFunctionTest, RegexpSubstr2Args) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select regexp_substr(raw_email, '[[:alnum:]._-]+@[[:alnum:]]+.[[:alnum:]]+') "
"from string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"[email protected]"},
{"[email protected]"},
{"[email protected]"},
{"[email protected]"}};
compare_result_set(expected_result_set, result_set);
}
}
// 3rd arg is start position
TEST_F(StringFunctionTest, RegexpSubstr3Args) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result_set = sql(
"select regexp_substr(raw_email, '[[:alnum:]._-]+@[[:alnum:]]+.[[:alnum:]]+', "
"20) from string_function_test_people order by id asc;",
dt);
std::vector<std::vector<ScalarTargetValue>> expected_result_set{
{"[email protected]"}, {""}, {""}, {"[email protected]"}};