-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathLoadTableTest.cpp
1522 lines (1381 loc) · 59.9 KB
/
LoadTableTest.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 2022 HEAVY.AI, 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.
*/
#include <arrow/api.h>
#include <arrow/io/memory.h>
#include <arrow/ipc/api.h>
#include <arrow/ipc/writer.h>
#include <gtest/gtest.h>
#include <boost/filesystem.hpp>
#ifdef HAVE_AWS_S3
#include "AwsHelpers.h"
#include "DataMgr/HeavyDbAwsSdk.h"
#include "Shared/ThriftTypesConvert.h"
#endif // HAVE_AWS_S3
#include "Geospatial/ColumnNames.h"
#include "Shared/ArrowUtil.h"
#include "Tests/DBHandlerTestHelpers.h"
#include "Tests/TestHelpers.h"
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
class LoadTableTest : public DBHandlerTestFixture {
protected:
void SetUp() override {
DBHandlerTestFixture::SetUp();
sql("DROP TABLE IF EXISTS load_test");
sql("DROP TABLE IF EXISTS geo_load_test");
sql("CREATE TABLE geo_load_test(i1 INTEGER, ls LINESTRING DEFAULT 'LINESTRING(0 0, 1 "
"1)', s TEXT, mp MULTIPOLYGON, "
"nns TEXT not null)");
sql("CREATE TABLE load_test(i1 INTEGER, s TEXT DEFAULT 'default str' ENCODING "
"DICT(8), nns TEXT not null)");
initData();
}
void TearDown() override {
sql("DROP TABLE IF EXISTS load_test");
sql("DROP TABLE IF EXISTS geo_load_test");
DBHandlerTestFixture::TearDown();
}
TStringValue getNullSV() const {
TStringValue v;
v.is_null = true;
v.str_val = "";
return v;
}
TStringValue getSV(const std::string& value) const {
TStringValue v;
v.is_null = false;
v.str_val = value;
return v;
}
const std::string LINESTRING = "LINESTRING (0 0,1 1,1 2)";
const std::string DEFAULT_LINESTRING = "LINESTRING (0 0,1 1)";
const std::string MULTIPOLYGON =
"MULTIPOLYGON (((0 0,4 0,4 4,0 4,0 0),"
"(1 1,1 2,2 2,2 1,1 1)),((-1 -1,-2 -1,-2 -2,-1 -2,-1 -1)))";
const std::string POLYGON = "POLYGON ((0 0,4 0,4 4,0 4,0 0),(1 1,1 2,2 2,2 1,1 1))";
const std::string POLYGON_PROMOTED =
"MULTIPOLYGON (((0 0,4 0,4 4,0 4,0 0),(1 1,1 2,2 2,2 1,1 1)))";
TColumn i1_column, s_column, nns_column, ls_column, mp_column, p_column;
TDatum i1_datum, s_datum, nns_datum, ls_datum, mp_datum, p_datum;
std::shared_ptr<arrow::Field> i1_field, s_field, nns_field, ls_field, mp_field, p_field;
private:
void initData() {
p_column.nulls = std::vector<bool>(1, false);
i1_column.nulls = s_column.nulls = nns_column.nulls = ls_column.nulls =
mp_column.nulls = p_column.nulls;
i1_column.data.int_col = {1};
s_column.data.str_col = {"s"};
nns_column.data.str_col = {"nns"};
ls_column.data.str_col = {LINESTRING};
mp_column.data.str_col = {MULTIPOLYGON};
p_column.data.str_col = {POLYGON};
i1_datum.is_null = s_datum.is_null = nns_datum.is_null = ls_datum.is_null =
mp_datum.is_null = p_datum.is_null = false;
i1_datum.val.int_val = 1;
s_datum.val.str_val = "s";
nns_datum.val.str_val = "nns";
ls_datum.val.str_val = LINESTRING;
mp_datum.val.str_val = MULTIPOLYGON;
p_datum.val.str_val = POLYGON;
i1_field = arrow::field("i1", arrow::int32());
s_field = arrow::field("s", arrow::utf8());
nns_field = arrow::field("nns", arrow::utf8());
ls_field = arrow::field("ls", arrow::utf8());
mp_field = arrow::field("mp", arrow::utf8());
p_field = arrow::field("mp", arrow::utf8());
}
};
TEST_F(LoadTableTest, AllColumnsNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TStringRow row;
row.cols = {getSV("1"), getSV("s"), getSV("nns")};
handler->load_table(session, "load_test", {row}, {});
sqlAndCompareResult("SELECT * FROM load_test", {{i(1), "s", "nns"}});
}
TEST_F(LoadTableTest, AllColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TStringRow row;
row.cols = {
getSV("1"), getSV(LINESTRING), getSV("s"), getSV(MULTIPOLYGON), getSV("nns")};
handler->load_table(session, "geo_load_test", {row}, {});
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", MULTIPOLYGON, "nns"}});
}
TEST_F(LoadTableTest, AllColumnsPromotePolyToMPoly) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TStringRow row;
row.cols = {getSV("1"), getSV(LINESTRING), getSV("s"), getSV(POLYGON), getSV("nns")};
handler->load_table(session, "geo_load_test", {row}, {});
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", POLYGON_PROMOTED, "nns"}});
}
TEST_F(LoadTableTest, AllColumnsReordered) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "nns", "ls", "i1", "s"};
TStringRow row;
row.cols = {
getSV(MULTIPOLYGON), getSV("nns"), getSV(LINESTRING), getSV("1"), getSV("s")};
handler->load_table(session, "geo_load_test", {row}, column_names);
sqlAndCompareResult("SELECT mp, nns, ls, i1, s FROM geo_load_test",
{{MULTIPOLYGON, "nns", LINESTRING, i(1), "s"}});
}
TEST_F(LoadTableTest, SomeColumnsReordered) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "nns", "ls"};
TStringRow row;
row.cols = {getSV(MULTIPOLYGON), getSV("nns"), getSV(LINESTRING)};
handler->load_table(session, "geo_load_test", {row}, column_names);
sqlAndCompareResult("SELECT mp, nns, ls, i1, s FROM geo_load_test",
{{MULTIPOLYGON, "nns", LINESTRING, nullptr, nullptr}});
}
TEST_F(LoadTableTest, OmitNotNullableColumn) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls"};
TStringRow row;
row.cols = {getSV(MULTIPOLYGON), getSV(LINESTRING)};
executeLambdaAndAssertException(
[&]() { handler->load_table(session, "geo_load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Column 'nns' cannot be omitted due to NOT NULL constraint)");
}
TEST_F(LoadTableTest, OmitGeoColumn) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "s", "nns", "ls"};
TStringRow row;
row.cols = {getSV("1"), getSV("s"), getSV("nns"), getSV(LINESTRING)};
handler->load_table(session, "geo_load_test", {row}, column_names);
sqlAndCompareResult("SELECT i1, s, nns, mp, ls FROM geo_load_test",
{{i(1), "s", "nns", (void*)0, LINESTRING}});
}
TEST_F(LoadTableTest, DuplicateColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls", "mp", "nns"};
TStringRow row;
row.cols = {getSV(MULTIPOLYGON), getSV(LINESTRING), getSV(MULTIPOLYGON), getSV("nns")};
executeLambdaAndAssertException(
[&]() { handler->load_table(session, "geo_load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Column mp is mentioned multiple times)");
}
TEST_F(LoadTableTest, UnexistingColumn) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls", "mp2", "nns"};
TStringRow row;
row.cols = {getSV(MULTIPOLYGON), getSV(LINESTRING), getSV(MULTIPOLYGON), getSV("nns")};
executeLambdaAndAssertException(
[&]() { handler->load_table(session, "geo_load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Column mp2 does not exist)");
}
TEST_F(LoadTableTest, ColumnNumberMismatch) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls", "i1", "nns"};
TStringRow row;
row.cols = {getSV(MULTIPOLYGON), getSV(LINESTRING), getSV("nns")};
executeLambdaAndAssertException(
[&]() { handler->load_table(session, "geo_load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Number of columns specified does not match the number of columns given (3 vs 4))");
}
TEST_F(LoadTableTest, NoColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
executeLambdaAndAssertException(
[&]() { handler->load_table(session, "geo_load_test", {}, {}); },
"TException - service has thrown: TDBException(error_msg="
"No rows to insert)");
}
TEST_F(LoadTableTest, DefaultString) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "nns"};
TStringRow row;
row.cols = {getSV("1"), getSV("nns")};
handler->load_table(session, "load_test", {row}, column_names);
sqlAndCompareResult("SELECT i1, s, nns FROM load_test", {{i(1), "default str", "nns"}});
}
TEST_F(LoadTableTest, DefaultGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "s", "nns", "mp"};
TStringRow row;
row.cols = {getSV("1"), getSV("s"), getSV("nns"), getSV(MULTIPOLYGON)};
handler->load_table(session, "geo_load_test", {row}, column_names);
sqlAndCompareResult("SELECT i1, s, nns, mp, ls FROM geo_load_test",
{{i(1), "s", "nns", MULTIPOLYGON, DEFAULT_LINESTRING}});
}
TEST_F(LoadTableTest, BinaryAllColumnsNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRow row;
row.cols = {i1_datum, s_datum, nns_datum};
handler->load_table_binary(session, "load_test", {row}, {});
sqlAndCompareResult("SELECT * FROM load_test", {{i(1), "s", "nns"}});
}
TEST_F(LoadTableTest, DictOutOfBounds) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<TRow> rows;
for (int i = 0; i < 300; i++) {
TRow row;
TDatum str_datum;
str_datum.is_null = false;
i1_datum.val.int_val = 1;
str_datum.val.str_val = std::to_string(i);
row.cols = {i1_datum, str_datum, nns_datum};
rows.emplace_back(row);
}
executeLambdaAndAssertPartialException(
[&]() { handler->load_table_binary(session, "load_test", rows, {}); },
"has exceeded it's limit of 8 bits (255 unique values) while attempting to "
"add the new string '255'. To load more data, please re-create the table "
"with this column as type TEXT ENCODING DICT(16) or TEXT ENCODING DICT(32) "
"and reload your data.");
sqlAndCompareResult("SELECT count(*) FROM load_test", {{i(0)}});
}
// TODO(max): load_table_binary doesn't support tables with geo columns yet
TEST_F(LoadTableTest, DISABLED_BinaryAllColumnsPromotePolyToMPoly) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRow row;
row.cols = {i1_datum, ls_datum, s_datum, p_datum, nns_datum};
handler->load_table_binary(session, "geo_load_test", {row}, {});
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", POLYGON_PROMOTED, "nns"}});
}
TEST_F(LoadTableTest, DISABLED_BinaryAllColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRow row;
row.cols = {i1_datum, ls_datum, s_datum, mp_datum, nns_datum};
handler->load_table_binary(session, "geo_load_test", {row}, {});
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", MULTIPOLYGON, "nns"}});
}
TEST_F(LoadTableTest, BinaryAllColumnsReorderedNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"nns", "i1", "s"};
TRow row;
row.cols = {nns_datum, i1_datum, s_datum};
handler->load_table_binary(session, "load_test", {row}, column_names);
sqlAndCompareResult("SELECT i1, s, nns FROM load_test", {{i(1), "s", "nns"}});
}
TEST_F(LoadTableTest, BinarySomeColumnsReorderedNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"nns", "s"};
TRow row;
row.cols = {nns_datum, s_datum};
handler->load_table_binary(session, "load_test", {row}, column_names);
sqlAndCompareResult("SELECT i1, s, nns FROM load_test", {{nullptr, "s", "nns"}});
}
TEST_F(LoadTableTest, BinaryOmitNotNullableColumnNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "s"};
TRow row;
row.cols = {i1_datum, s_datum};
executeLambdaAndAssertException(
[&]() { handler->load_table_binary(session, "load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Column 'nns' cannot be omitted due to NOT NULL constraint)");
}
TEST_F(LoadTableTest, BinaryDuplicateColumnsNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"nns", "i1", "i1"};
TRow row;
row.cols = {nns_datum, i1_datum, i1_datum};
executeLambdaAndAssertException(
[&]() { handler->load_table_binary(session, "load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Column i1 is mentioned multiple times)");
}
TEST_F(LoadTableTest, BinaryUnexistingColumnNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"nns", "i1", "i2"};
TRow row;
row.cols = {nns_datum, i1_datum, i1_datum};
executeLambdaAndAssertException(
[&]() { handler->load_table_binary(session, "load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Column i2 does not exist)");
}
TEST_F(LoadTableTest, BinaryColumnNumberMismatchNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"nns", "i1", "s"};
TRow row;
row.cols = {nns_datum, i1_datum};
executeLambdaAndAssertException(
[&]() { handler->load_table_binary(session, "load_test", {row}, column_names); },
"TException - service has thrown: TDBException(error_msg="
"Number of columns specified does not match the number of columns given "
"(2 vs 3))");
}
TEST_F(LoadTableTest, BinaryNoColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
executeLambdaAndAssertException(
[&]() { handler->load_table_binary(session, "load_test", {}, {}); },
"TException - service has thrown: TDBException(error_msg="
"No rows to insert)");
}
TEST_F(LoadTableTest, BinaryDefaultString) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "nns"};
TRow row;
row.cols = {i1_datum, nns_datum};
handler->load_table_binary(session, "load_test", {row}, column_names);
sqlAndCompareResult("SELECT * FROM load_test", {{i(1), "default str", "nns"}});
}
TEST_F(LoadTableTest, ColumnarAllColumnsNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
handler->load_table_binary_columnar(
session, "load_test", {i1_column, s_column, nns_column}, {});
sqlAndCompareResult("SELECT * FROM load_test", {{i(1), "s", "nns"}});
}
TEST_F(LoadTableTest, ColumnarAllColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
handler->load_table_binary_columnar(
session,
"geo_load_test",
{i1_column, ls_column, s_column, mp_column, nns_column},
{});
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", MULTIPOLYGON, "nns"}});
}
TEST_F(LoadTableTest, ColumnarAllColumnsPromotePolyToMPoly) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
handler->load_table_binary_columnar(
session,
"geo_load_test",
{i1_column, ls_column, s_column, p_column, nns_column},
{});
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", POLYGON_PROMOTED, "nns"}});
}
TEST_F(LoadTableTest, ColumnarAllColumnsReordered) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "nns", "ls", "i1", "s"};
handler->load_table_binary_columnar(
session,
"geo_load_test",
{mp_column, nns_column, ls_column, i1_column, s_column},
column_names);
sqlAndCompareResult("SELECT mp, nns, ls, i1, s FROM geo_load_test",
{{MULTIPOLYGON, "nns", LINESTRING, i(1), "s"}});
}
TEST_F(LoadTableTest, ColumnarSomeColumnsReordered) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "nns", "ls"};
handler->load_table_binary_columnar(
session, "geo_load_test", {mp_column, nns_column, ls_column}, column_names);
sqlAndCompareResult("SELECT mp, nns, ls, i1, s FROM geo_load_test",
{{MULTIPOLYGON, "nns", LINESTRING, nullptr, nullptr}});
}
TEST_F(LoadTableTest, ColumnarOmitNotNullableColumn) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls"};
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_columnar(
session, "geo_load_test", {mp_column, ls_column}, column_names);
},
"Column 'nns' cannot be omitted due to NOT NULL constraint");
}
TEST_F(LoadTableTest, ColumnarOmitGeoColumn) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "s", "nns", "ls"};
handler->load_table_binary_columnar(session,
"geo_load_test",
{i1_column, s_column, nns_column, ls_column},
column_names);
sqlAndCompareResult("SELECT i1, s, nns, mp, ls FROM geo_load_test",
{{i(1), "s", "nns", (void*)0, LINESTRING}});
}
TEST_F(LoadTableTest, ColumnarDuplicateColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls", "mp"};
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_columnar(
session, "geo_load_test", {mp_column, ls_column, mp_column}, column_names);
},
"Column mp is mentioned multiple times");
}
TEST_F(LoadTableTest, ColumnarUnexistingColumn) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls", "mp2"};
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_columnar(
session, "geo_load_test", {mp_column, ls_column, mp_column}, column_names);
},
"Column mp2 does not exist");
}
TEST_F(LoadTableTest, ColumnarColumnNumberMismatch) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"mp", "ls", "i1"};
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_columnar(
session, "geo_load_test", {mp_column, ls_column}, column_names);
},
"Number of columns specified does not match the number of columns given (2 vs 3)");
}
TEST_F(LoadTableTest, ColumnarNoColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
executeLambdaAndAssertException(
[&]() { handler->load_table_binary_columnar(session, "geo_load_test", {}, {}); },
"No columns to insert");
}
TEST_F(LoadTableTest, ColumnarDefaultStr) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "nns"};
handler->load_table_binary_columnar(
session, "load_test", {i1_column, nns_column}, column_names);
sqlAndCompareResult("SELECT * FROM load_test", {{i(1), "default str", "nns"}});
}
TEST_F(LoadTableTest, ColumnarDefaultGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::vector<std::string> column_names{"i1", "s", "mp", "nns"};
handler->load_table_binary_columnar(session,
"geo_load_test",
{i1_column, s_column, mp_column, nns_column},
column_names);
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), DEFAULT_LINESTRING, "s", MULTIPOLYGON, "nns"}});
}
// A small helper to build Arrow stream for load_table_binary_arrow
class ArrowStreamBuilder {
public:
ArrowStreamBuilder(const std::shared_ptr<arrow::Schema>& schema) : schema_(schema) {}
std::string finish() {
CHECK(columns_.size() == schema_->fields().size());
size_t length = columns_.empty() ? 0 : columns_[0]->length();
auto records = arrow::RecordBatch::Make(schema_, length, columns_);
auto out_stream = *arrow::io::BufferOutputStream::Create();
auto stream_writer = *arrow::ipc::MakeStreamWriter(out_stream.get(), schema_);
ARROW_THROW_NOT_OK(stream_writer->WriteRecordBatch(*records));
ARROW_THROW_NOT_OK(stream_writer->Close());
auto buffer = *out_stream->Finish();
columns_.clear();
return buffer->ToString();
}
void appendInt32(const std::vector<int32_t>& values,
const std::vector<bool>& is_null = {}) {
append<arrow::Int32Builder, int32_t>(values, is_null);
}
void appendString(const std::vector<std::string>& values,
const std::vector<bool>& is_null = {}) {
append<arrow::StringBuilder, std::string>(values, is_null);
}
private:
template <typename Builder, typename T>
void append(const std::vector<T>& values, const std::vector<bool>& is_null) {
Builder builder;
CHECK(is_null.empty() || values.size() == is_null.size());
for (size_t i = 0; i < values.size(); ++i) {
if (!is_null.empty() && is_null[i]) {
ARROW_THROW_NOT_OK(builder.AppendNull());
} else {
ARROW_THROW_NOT_OK(builder.Append(values[i]));
}
}
columns_.push_back(nullptr);
ARROW_THROW_NOT_OK(builder.Finish(&columns_.back()));
}
std::shared_ptr<arrow::Schema> schema_;
std::vector<std::shared_ptr<arrow::Array>> columns_;
};
TEST_F(LoadTableTest, ArrowAllColumnsNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({i1_field, s_field, nns_field});
ArrowStreamBuilder builder(schema);
builder.appendInt32({1});
builder.appendString({"s"});
builder.appendString({"nns"});
handler->load_table_binary_arrow(session, "load_test", builder.finish(), false);
sqlAndCompareResult("SELECT * FROM load_test", {{i(1), "s", "nns"}});
}
TEST_F(LoadTableTest, ArrowAllColumnsPromotePolyToMPoly) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({i1_field, ls_field, s_field, p_field, nns_field});
ArrowStreamBuilder builder(schema);
builder.appendInt32({1});
builder.appendString({LINESTRING});
builder.appendString({"s"});
builder.appendString({POLYGON});
builder.appendString({"nns"});
handler->load_table_binary_arrow(session, "geo_load_test", builder.finish(), false);
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", POLYGON_PROMOTED, "nns"}});
}
TEST_F(LoadTableTest, ArrowAllColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({i1_field, ls_field, s_field, mp_field, nns_field});
ArrowStreamBuilder builder(schema);
builder.appendInt32({1});
builder.appendString({LINESTRING});
builder.appendString({"s"});
builder.appendString({MULTIPOLYGON});
builder.appendString({"nns"});
handler->load_table_binary_arrow(session, "geo_load_test", builder.finish(), false);
sqlAndCompareResult("SELECT * FROM geo_load_test",
{{i(1), LINESTRING, "s", MULTIPOLYGON, "nns"}});
}
TEST_F(LoadTableTest, ArrowAllColumnsReorderedNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({nns_field, i1_field, s_field});
ArrowStreamBuilder builder(schema);
builder.appendString({"nns"});
builder.appendInt32({1});
builder.appendString({"s"});
handler->load_table_binary_arrow(session, "load_test", builder.finish(), true);
sqlAndCompareResult("SELECT i1, s, nns FROM load_test", {{i(1), "s", "nns"}});
}
TEST_F(LoadTableTest, ArrowSomeColumnsReorderedNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({nns_field, s_field});
ArrowStreamBuilder builder(schema);
builder.appendString({"nns"});
builder.appendString({"s"});
handler->load_table_binary_arrow(session, "load_test", builder.finish(), true);
sqlAndCompareResult("SELECT i1, s, nns FROM load_test", {{nullptr, "s", "nns"}});
}
TEST_F(LoadTableTest, ArrowOmitNotNullableColumnNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({i1_field, s_field});
ArrowStreamBuilder builder(schema);
builder.appendInt32({1});
builder.appendString({"s"});
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_arrow(session, "load_test", builder.finish(), true);
},
"Column 'nns' cannot be omitted due to NOT NULL constraint");
}
TEST_F(LoadTableTest, ArrowDuplicateColumnsNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({nns_field, i1_field, i1_field});
ArrowStreamBuilder builder(schema);
builder.appendString({"nns"});
builder.appendInt32({1});
builder.appendInt32({1});
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_arrow(session, "load_test", builder.finish(), true);
},
"Column i1 is mentioned multiple times");
}
TEST_F(LoadTableTest, ArrowUnexistingColumnNoGeo) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
std::shared_ptr<arrow::Field> i2_field = arrow::field("i2", arrow::int32());
auto bad_schema = arrow::schema({nns_field, i1_field, i2_field});
ArrowStreamBuilder builder(bad_schema);
builder.appendString({"nns"});
builder.appendInt32({1});
builder.appendInt32({2});
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_arrow(session, "load_test", builder.finish(), true);
},
"Column i2 does not exist");
}
TEST_F(LoadTableTest, ArrowNoColumns) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({});
ArrowStreamBuilder builder(schema);
executeLambdaAndAssertException(
[&]() {
handler->load_table_binary_arrow(session, "load_test", builder.finish(), false);
},
"No columns to insert");
}
TEST_F(LoadTableTest, ArrowDefaultStr) {
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
auto schema = arrow::schema({i1_field, nns_field});
ArrowStreamBuilder builder(schema);
builder.appendInt32({1});
builder.appendString({"nns"});
handler->load_table_binary_arrow(session, "load_test", builder.finish(), true);
sqlAndCompareResult("SELECT * FROM load_test", {{i(1), "default str", "nns"}});
}
namespace {
struct MemorySignature {
Buffer_Namespace::MemStatus memStatus_;
Data_Namespace::MemoryLevel memLevel_;
};
void assert_memory_signature_for_chunk_key_prefix(
const std::shared_ptr<DataMgr> data_mgr,
const ChunkKey& table_chunk_key,
const std::vector<MemorySignature>& expected_memory_signature) {
std::vector<MemorySignature> actual_memory_signature;
for (int memory_level = Data_Namespace::MemoryLevel::DISK_LEVEL;
memory_level <= Data_Namespace::MemoryLevel::GPU_LEVEL;
++memory_level) {
auto memory_level_info_vec =
data_mgr->getMemoryInfo(static_cast<Data_Namespace::MemoryLevel>(memory_level));
for (auto memory_info : memory_level_info_vec) {
for (auto memory_data : memory_info.nodeMemoryData) {
if (has_table_prefix(memory_data.chunk_key) &&
in_same_table(table_chunk_key, memory_data.chunk_key)) {
actual_memory_signature.push_back(
{memory_data.memStatus,
static_cast<Data_Namespace::MemoryLevel>(memory_level)});
}
}
}
}
ASSERT_EQ(actual_memory_signature.size(), expected_memory_signature.size());
for (size_t i = 0; i < actual_memory_signature.size(); i++) {
ASSERT_EQ(actual_memory_signature[i].memStatus_,
expected_memory_signature[i].memStatus_);
ASSERT_EQ(actual_memory_signature[i].memLevel_,
expected_memory_signature[i].memLevel_);
}
}
} // namespace
TEST_F(LoadTableTest, TemporaryTableChunkBufferCleanup) {
// create a temporary table with multiple fragments to make that we're not leaking any
// buffers on fragment creation
sql("drop table if exists temp_table_cleanup_chuk_buffers;");
sql("create temporary table temp_table_cleanup_chuk_buffers (i1 integer) with "
"(FRAGMENT_SIZE=1);");
sql("insert into temp_table_cleanup_chuk_buffers values(1);");
sql("insert into temp_table_cleanup_chuk_buffers values(2);");
auto [db_handler, _] = getDbHandlerAndSessionId();
auto data_mgr = db_handler->data_mgr_;
auto tb_id = getCatalog().getTableId("temp_table_cleanup_chuk_buffers");
CHECK(tb_id.has_value());
ChunkKey table_chunk_key;
table_chunk_key.push_back(getCatalog().getDatabaseId());
table_chunk_key.push_back(tb_id.value());
CHECK(has_table_prefix(table_chunk_key));
assert_memory_signature_for_chunk_key_prefix(
data_mgr,
table_chunk_key,
{{Buffer_Namespace::MemStatus::USED, MemoryLevel::CPU_LEVEL},
{Buffer_Namespace::MemStatus::USED, MemoryLevel::CPU_LEVEL},
{Buffer_Namespace::MemStatus::USED, MemoryLevel::CPU_LEVEL},
{Buffer_Namespace::MemStatus::USED, MemoryLevel::CPU_LEVEL}});
sql("drop table temp_table_cleanup_chuk_buffers;");
assert_memory_signature_for_chunk_key_prefix(
data_mgr,
table_chunk_key,
{{Buffer_Namespace::MemStatus::FREE, MemoryLevel::CPU_LEVEL}});
}
class ImportGeoTableTest : public DBHandlerTestFixture {
protected:
void SetUp() override {
DBHandlerTestFixture::SetUp();
sql("DROP TABLE IF EXISTS import_geo_table_test");
}
const std::string getGeoFileName() const {
auto geo_file_name =
boost::filesystem::absolute(
boost::filesystem::path(
"../../Tests/ImportGeoTableTest/datafiles/geospatial_poly.geojson"))
.string();
return geo_file_name;
}
const TCopyParams getCopyParams() const {
TCopyParams copy_params;
copy_params.source_type = TSourceType::GEO_FILE;
return copy_params;
}
const TCreateParams getCreateParams() const {
TCreateParams create_params;
create_params.is_replicated = false;
return create_params;
}
TColumnType getScalarColumnType(const std::string& name,
const TDatumType::type type) const {
TColumnType ct;
ct.col_name = name;
ct.col_type.type = type;
ct.col_type.encoding = TEncodingType::type::NONE;
ct.col_type.nullable = true;
ct.col_type.is_array = false;
ct.col_type.precision = 0;
ct.col_type.scale = 0;
ct.col_type.comp_param = 0;
ct.col_type.size = 0;
ct.is_reserved_keyword = false;
ct.src_name = name;
ct.is_system = false;
ct.is_physical = false;
ct.col_id = 0;
return ct;
};
TColumnType getPolyColumnType(const std::string& name) const {
TColumnType ct;
ct.col_name = name;
ct.col_type.type = TDatumType::type::POLYGON;
ct.col_type.encoding = TEncodingType::type::GEOINT;
ct.col_type.nullable = true;
ct.col_type.is_array = false;
ct.col_type.precision = 23; // aka subtype = SQLTypes::kGEOMETRY (not the same as
// TDatumType::type::GEOMETRY)
ct.col_type.scale = 4326; // aka output_srid = WGS84
ct.col_type.comp_param = 32;
ct.col_type.size = 0;
ct.is_reserved_keyword = false;
ct.src_name = name;
ct.is_system = false;
ct.is_physical = false;
ct.col_id = 0;
return ct;
};
void TearDown() override {
sql("DROP TABLE IF EXISTS import_geo_table_test");
DBHandlerTestFixture::TearDown();
}
};
TEST_F(ImportGeoTableTest, ImportGeoTableAuto) {
// geo import with empty row descriptor
// will automatically create table
// equivalent to COPY FROM WITH (source_type='geo_file')
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRowDescriptor row_descriptor;
EXPECT_NO_THROW(handler->import_geo_table(session,
"import_geo_table_test",
getGeoFileName(),
getCopyParams(),
row_descriptor,
getCreateParams()));
sqlAndCompareResult("SELECT count(*) FROM import_geo_table_test", {{i(10)}});
sqlAndCompareResult("SELECT trip FROM import_geo_table_test WHERE rowid=0", {{0.0f}});
}
TEST_F(ImportGeoTableTest, ImportGeoTableExplicit) {
// geo import with explicit row descriptor (e.g. Immerse import)
// must create table first
// correct types
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRowDescriptor row_descriptor{getScalarColumnType("trip", TDatumType::type::FLOAT),
getPolyColumnType(Geospatial::kGeoColumnName)};
EXPECT_NO_THROW(handler->create_table(
session, "import_geo_table_test", row_descriptor, getCreateParams()));
EXPECT_NO_THROW(handler->import_geo_table(session,
"import_geo_table_test",
getGeoFileName(),
getCopyParams(),
row_descriptor,
getCreateParams()));
sqlAndCompareResult("SELECT count(*) FROM import_geo_table_test", {{i(10)}});
sqlAndCompareResult("SELECT trip FROM import_geo_table_test WHERE rowid=0", {{0.0f}});
}
TEST_F(ImportGeoTableTest, ImportGeoTableOverride) {
// geo import with explicit row descriptor (e.g. Immerse import)
// must create table first
// type of column 'trip' overridden from FLOAT to INT (valid)
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRowDescriptor row_descriptor{getScalarColumnType("trip", TDatumType::type::INT),
getPolyColumnType(Geospatial::kGeoColumnName)};
EXPECT_NO_THROW(handler->create_table(
session, "import_geo_table_test", row_descriptor, getCreateParams()));
EXPECT_NO_THROW(handler->import_geo_table(session,
"import_geo_table_test",
getGeoFileName(),
getCopyParams(),
row_descriptor,
getCreateParams()));
sqlAndCompareResult("SELECT count(*) FROM import_geo_table_test", {{i(10)}});
sqlAndCompareResult("SELECT trip FROM import_geo_table_test WHERE rowid=0", {{i(0)}});
}
TEST_F(ImportGeoTableTest, ImportGeoTableTypeMismatch1) {
// geo import with explicit row descriptor (e.g. Immerse import)
// must create table first
// types of columns swapped (possible in Immerse for now)
// import will not fail, but should reject all rows
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRowDescriptor row_descriptor{
getPolyColumnType("trip"),
getScalarColumnType(Geospatial::kGeoColumnName, TDatumType::type::FLOAT)};
EXPECT_NO_THROW(handler->create_table(
session, "import_geo_table_test", row_descriptor, getCreateParams()));
EXPECT_THROW(handler->import_geo_table(session,
"import_geo_table_test",
getGeoFileName(),
getCopyParams(),
row_descriptor,
getCreateParams()),
TDBException);
sqlAndCompareResult("SELECT count(*) FROM import_geo_table_test", {{i(0)}});
}
TEST_F(ImportGeoTableTest, ImportGeoTableFailTypeMismatch2) {
// geo import with explicit row descriptor (e.g. Immerse import)
// must create table first
// column types valid but columns swapped (possible in Immerse for now)
// import will not fail, but should reject all rows
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRowDescriptor row_descriptor{
getScalarColumnType(Geospatial::kGeoColumnName, TDatumType::type::FLOAT),
getPolyColumnType("trip")};
EXPECT_NO_THROW(handler->create_table(
session, "import_geo_table_test", row_descriptor, getCreateParams()));
EXPECT_THROW(handler->import_geo_table(session,
"import_geo_table_test",
getGeoFileName(),
getCopyParams(),
row_descriptor,
getCreateParams()),
TDBException);
sqlAndCompareResult("SELECT count(*) FROM import_geo_table_test", {{i(0)}});
}
TEST_F(ImportGeoTableTest, ImportGeoTableFailNoGeoColumns) {
// geo import with explicit row descriptor (e.g. Immerse import)
// must create table first
// no geo columns in row descriptor
// import should fail
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRowDescriptor row_descriptor{getScalarColumnType("trip", TDatumType::type::FLOAT)};
EXPECT_NO_THROW(handler->create_table(
session, "import_geo_table_test", row_descriptor, getCreateParams()));
EXPECT_THROW(handler->import_geo_table(session,
"import_geo_table_test",
getGeoFileName(),
getCopyParams(),
row_descriptor,
getCreateParams()),
TDBException);
sqlAndCompareResult("SELECT count(*) FROM import_geo_table_test", {{i(0)}});
}
TEST_F(ImportGeoTableTest, ImportGeoTableFailTooManyGeoColumns) {
// geo import with explicit row descriptor (e.g. Immerse import)
// must create table first
// more than one geo column in row descriptor
// import should fail
auto* handler = getDbHandlerAndSessionId().first;
auto& session = getDbHandlerAndSessionId().second;
TRowDescriptor row_descriptor{getScalarColumnType("trip", TDatumType::type::FLOAT),
getPolyColumnType("geo1"),
getPolyColumnType("geo2")};
EXPECT_NO_THROW(handler->create_table(