forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverlapsJoinTest.cpp
2254 lines (2042 loc) · 91 KB
/
OverlapsJoinTest.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 2020, 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.
*/
#include "QueryRunner/QueryRunner.h"
#include <fmt/core.h>
#include <gtest/gtest.h>
#include <functional>
#include <iostream>
#include <string>
#include "QueryEngine/ArrowResultSet.h"
#include "QueryEngine/Execute.h"
#include "Shared/scope.h"
#include "TestHelpers.h"
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
using QR = QueryRunner::QueryRunner;
using namespace TestHelpers;
bool skip_tests_on_gpu(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_on_gpu(dt)) { \
CHECK(dt == ExecutorDeviceType::GPU); \
LOG(WARNING) << "GPU not available, skipping GPU tests"; \
continue; \
}
struct ExecutionContext {
ExecutorDeviceType device_type;
bool hash_join_enabled;
std::string toString() const {
const auto device_str = device_type == ExecutorDeviceType::CPU ? "CPU" : "GPU";
return fmt::format(
"Execution Context:\n"
" Device Type: {}\n"
" Hash Join Enabled: {}\n",
device_str,
hash_join_enabled);
}
};
template <typename TEST_BODY>
void executeAllScenarios(TEST_BODY fn) {
for (const auto overlaps_state : {true, false}) {
const auto enable_overlaps_hashjoin_state = g_enable_overlaps_hashjoin;
const auto enable_hashjoin_many_to_many_state = g_enable_hashjoin_many_to_many;
g_enable_overlaps_hashjoin = overlaps_state;
g_enable_hashjoin_many_to_many = overlaps_state;
g_trivial_loop_join_threshold = overlaps_state ? 1 : 1000;
ScopeGuard reset_overlaps_state = [&enable_overlaps_hashjoin_state,
&enable_hashjoin_many_to_many_state] {
g_enable_overlaps_hashjoin = enable_overlaps_hashjoin_state;
g_enable_overlaps_hashjoin = enable_hashjoin_many_to_many_state;
g_trivial_loop_join_threshold = 1000;
};
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
// .device_type = dt, .hash_join_enabled = overlaps_state,
ExecutionContext execution_context{
dt,
overlaps_state,
};
QR::get()->clearGpuMemory();
QR::get()->clearCpuMemory();
fn(execution_context);
}
}
}
// clang-format off
const auto cleanup_stmts = {
R"(drop table if exists does_intersect_a;)",
R"(drop table if exists does_intersect_b;)",
R"(drop table if exists does_not_intersect_a;)",
R"(drop table if exists does_not_intersect_b;)",
R"(drop table if exists empty_table;)"
};
// clang-format off
const auto init_stmts_ddl = {
R"(create table does_intersect_a (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table does_intersect_b (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326),
x DOUBLE,
y DOUBLE);
)",
R"(create table does_not_intersect_a (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table does_not_intersect_b (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table empty_table (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)"
};
const auto init_stmts_dml = {
R"(insert into does_intersect_a
values (0,
'polygon((25 25,30 25,30 30,25 30,25 25))',
'multipolygon(((25 25,30 25,30 30,25 30,25 25)))',
'point(22 22)');
)",
R"(insert into does_intersect_a
values (1,
'polygon((2 2,10 2,10 10,2 10,2 2))',
'multipolygon(((2 2,10 2,10 10,2 10,2 2)))',
'point(8 8)');
)",
R"(insert into does_intersect_a
values (2,
'polygon((2 2,10 2,10 10,2 10,2 2))',
'multipolygon(((2 2,10 2,10 10,2 10,2 2)))',
'point(8 8)');
)",
R"(insert into does_intersect_b
values (0,
'polygon((0 0,30 0,30 0,30 30,0 0))',
'multipolygon(((0 0,30 0,30 0,30 30,0 0)))',
'point(8 8)',
8, 8);
)",
R"(insert into does_intersect_b
values (1,
'polygon((25 25,30 25,30 30,25 30,25 25))',
'multipolygon(((25 25,30 25,30 30,25 30,25 25)))',
'point(28 28)',
28, 28);
)",
R"(insert into does_not_intersect_a
values (1,
'polygon((0 0,0 1,1 0,1 1,0 0))',
'multipolygon(((0 0,0 1,1 0,1 1,0 0)))',
'point(0 0)');
)",
R"(insert into does_not_intersect_a
values (1,
'polygon((0 0,0 1,1 0,1 1,0 0))',
'multipolygon(((0 0,0 1,1 0,1 1,0 0)))',
'point(0 0)');
)",
R"(insert into does_not_intersect_a
values (1,
'polygon((0 0,0 1,1 0,1 1,0 0))',
'multipolygon(((0 0,0 1,1 0,1 1,0 0)))',
'point(0 0)');
)",
R"(insert into does_not_intersect_b
values (1,
'polygon((2 2,2 4,4 2,4 4,2 2))',
'multipolygon(((2 2,2 4,4 2,4 4,2 2)))',
'point(2 2)');
)"
};
// clang-format on
class OverlapsTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
for (const auto& stmt : cleanup_stmts) {
QR::get()->runDDLStatement(stmt);
}
for (const auto& stmt : init_stmts_ddl) {
QR::get()->runDDLStatement(stmt);
}
for (const auto& stmt : init_stmts_dml) {
QR::get()->runSQL(stmt, ExecutorDeviceType::CPU);
}
}
static void TearDownTestSuite() {
for (const auto& stmt : cleanup_stmts) {
QR::get()->runDDLStatement(stmt);
}
}
};
TargetValue execSQL(const std::string& stmt,
const ExecutorDeviceType dt,
const bool geo_return_geo_tv = true) {
auto rows = QR::get()->runSQL(stmt, dt, true, false);
if (geo_return_geo_tv) {
rows->setGeoReturnType(ResultSet::GeoReturnType::GeoTargetValue);
}
auto crt_row = rows->getNextRow(true, true);
CHECK_EQ(size_t(1), crt_row.size()) << stmt;
return crt_row[0];
}
TargetValue execSQLWithAllowLoopJoin(const std::string& stmt,
const ExecutorDeviceType dt,
const bool geo_return_geo_tv = true) {
auto rows = QR::get()->runSQL(stmt, dt, true, true);
if (geo_return_geo_tv) {
rows->setGeoReturnType(ResultSet::GeoReturnType::GeoTargetValue);
}
auto crt_row = rows->getNextRow(true, true);
CHECK_EQ(size_t(1), crt_row.size()) << stmt;
return crt_row[0];
}
TEST_F(OverlapsTest, SimplePointInPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
const auto sql =
"SELECT "
"count(*) from "
"does_intersect_a WHERE ST_Intersects(poly, pt);";
ASSERT_EQ(static_cast<int64_t>(2), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, InnerJoinPointInPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql =
"SELECT "
"count(*) from "
"does_intersect_b as b JOIN does_intersect_a as a ON ST_Intersects(a.poly, "
"b.pt);";
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
sql =
"SELECT count(*) from does_intersect_b as b JOIN "
"does_intersect_a as a ON ST_Intersects(a.poly, b.pt);";
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
sql =
"SELECT "
"count(*) from "
"does_intersect_b as b JOIN does_intersect_a as a ON ST_Intersects(a.poly, "
"ST_SetSRID(ST_Point(b.x, b.y), 4326));";
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
sql =
"SELECT count(*) from does_intersect_b as b JOIN "
"does_intersect_a as a ON ST_Intersects(a.poly, "
"ST_SetSRID(ST_Point(b.x, b.y), 4326));";
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
// TODO(jclay): This should succeed without failure.
// For now, we test against the (incorrect) failure.
TEST_F(OverlapsTest, InnerJoinPolyInPointIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
const auto sql =
"SELECT "
"count(*) from "
"does_intersect_b as b JOIN does_intersect_a as a ON ST_Intersects(a.pt, "
"b.poly);";
if (g_enable_hashjoin_many_to_many) {
EXPECT_ANY_THROW(execSQL(sql, ctx.device_type));
} else {
// Note(jclay): We return 0, postgis returns 4
// Note(adb): Now we return 3. Progress?
// Note(ds): After switching to cIntersects we return 0 again. Progress?
int64_t expected_value = (g_enable_geo_ops_on_uncompressed_coords) ? 0 : 3;
ASSERT_EQ(expected_value, v<int64_t>(execSQL(sql, ctx.device_type)));
}
});
}
TEST_F(OverlapsTest, InnerJoinPolyPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
JOIN does_intersect_b as b
ON ST_Intersects(a.poly, b.poly);)";
ASSERT_EQ(static_cast<int64_t>(4), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, InnerJoinMPolyPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
JOIN does_intersect_b as b
ON ST_Intersects(a.mpoly, b.poly);)";
ASSERT_EQ(static_cast<int64_t>(4), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, InnerJoinMPolyMPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
JOIN does_intersect_b as b
ON ST_Intersects(a.mpoly, b.mpoly);)";
ASSERT_EQ(static_cast<int64_t>(4), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, LeftJoinMPolyPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
LEFT JOIN does_intersect_b as b
ON ST_Intersects(a.mpoly, b.poly);)";
ASSERT_EQ(static_cast<int64_t>(4), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, LeftJoinMPolyMPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
LEFT JOIN does_intersect_b as b
ON ST_Intersects(a.mpoly, b.poly);)";
ASSERT_EQ(static_cast<int64_t>(4), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, InnerJoinPolyPolyIntersectsTranspose) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
const auto sql = R"(SELECT count(*) from does_intersect_a as a
JOIN does_intersect_b as b
ON ST_Intersects(b.poly, a.poly);)";
ASSERT_EQ(static_cast<int64_t>(4), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, LeftJoinPolyPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_b as b
LEFT JOIN does_intersect_a as a
ON ST_Intersects(a.poly, b.poly);)";
ASSERT_EQ(static_cast<int64_t>(4), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, LeftJoinPointInPolyIntersects) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
LEFT JOIN does_intersect_b as b
ON ST_Intersects(b.poly, a.pt);)";
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
// TODO(jclay): This should succeed without failure.
// Look into rewriting this in overlaps rewrite.
// For now, we test against the (incorrect) failure.
// It should return 3.
TEST_F(OverlapsTest, LeftJoinPointInPolyIntersectsWrongLHS) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
LEFT JOIN does_intersect_b as b
ON ST_Intersects(a.poly, b.pt);)";
if (g_enable_hashjoin_many_to_many) {
EXPECT_ANY_THROW(execSQL(sql, ctx.device_type));
} else {
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
}
});
}
TEST_F(OverlapsTest, InnerJoinPolyPolyContains) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_b as b
JOIN does_intersect_a as a
ON ST_Contains(a.poly, b.poly);)";
ASSERT_EQ(static_cast<int64_t>(0), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
// TODO(jclay): The following runtime functions are not implemented:
// - ST_Contains_MultiPolygon_MultiPolygon
// - ST_Contains_MultiPolygon_Polygon
// As a result, the following should succeed rather than throw error.
TEST_F(OverlapsTest, InnerJoinMPolyPolyContains) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
JOIN does_intersect_b as b
ON ST_Contains(a.mpoly, b.poly);)";
EXPECT_ANY_THROW(execSQL(sql, ctx.device_type));
});
}
TEST_F(OverlapsTest, InnerJoinMPolyMPolyContains) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_a as a
JOIN does_intersect_b as b
ON ST_Contains(a.mpoly, b.poly);)";
// should return 4
EXPECT_ANY_THROW(execSQL(sql, ctx.device_type));
});
}
// NOTE(jclay): We don't support multipoly / poly ST_Contains
TEST_F(OverlapsTest, LeftJoinMPolyPolyContains) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_b as b
LEFT JOIN does_intersect_a as a
ON ST_Contains(a.mpoly, b.poly);)";
// should return 4
EXPECT_ANY_THROW(execSQL(sql, ctx.device_type));
});
}
TEST_F(OverlapsTest, LeftJoinMPolyMPolyContains) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql = R"(SELECT count(*) from does_intersect_b as b
LEFT JOIN does_intersect_a as a
ON ST_Contains(a.mpoly, b.mpoly);)";
// should return 4
EXPECT_ANY_THROW(execSQL(sql, ctx.device_type));
});
}
TEST_F(OverlapsTest, JoinPolyPointContains) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql =
"SELECT "
"count(*) from "
"does_intersect_b as b JOIN does_intersect_a as a ON ST_Contains(a.poly, b.pt);";
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
sql =
"SELECT "
"count(*) from "
"does_intersect_b as b JOIN does_intersect_a as a ON "
"ST_Contains(a.poly, ST_SetSRID(ST_Point(b.x, b.y), 4326));";
ASSERT_EQ(static_cast<int64_t>(3), v<int64_t>(execSQL(sql, ctx.device_type)));
// sql =
// "SELECT "
// "count(*) from "
// "does_intersect_b as b JOIN does_intersect_a as a ON ST_Contains(a.pt,
// b.poly);";
// ASSERT_EQ(static_cast<int64_t>(0), v<int64_t>(execSQL(sql, dt)));
});
}
TEST_F(OverlapsTest, JoinPolyCentroidContains) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
auto sql =
"SELECT "
"count(*) from "
"does_intersect_b as b JOIN does_intersect_a as a ON "
"ST_Contains(a.poly, ST_SetSRID(ST_Centroid(b.poly), 4326));";
ASSERT_EQ(static_cast<int64_t>(1), v<int64_t>(execSQL(sql, ctx.device_type)));
sql =
"SELECT "
"count(*) from "
"does_intersect_b as b JOIN does_intersect_a as a ON "
"ST_Contains(a.poly, ST_SetSRID(ST_Centroid(b.mpoly), 4326));";
ASSERT_EQ(static_cast<int64_t>(1), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, PolyPolyDoesNotIntersect) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
ASSERT_EQ(static_cast<int64_t>(0),
v<int64_t>(execSQL("SELECT count(*) FROM does_not_intersect_b as b "
"JOIN does_not_intersect_a as a "
"ON ST_Intersects(a.poly, b.poly);",
ctx.device_type)));
});
}
TEST_F(OverlapsTest, EmptyPolyPolyJoin) {
executeAllScenarios([](const ExecutionContext ctx) -> void {
const auto sql =
"SELECT count(*) FROM does_not_intersect_a as a "
"JOIN empty_table as b "
"ON ST_Intersects(a.poly, b.poly);";
ASSERT_EQ(static_cast<int64_t>(0), v<int64_t>(execSQL(sql, ctx.device_type)));
});
}
TEST_F(OverlapsTest, SkipHashtableCaching) {
const auto enable_overlaps_hashjoin_state = g_enable_overlaps_hashjoin;
const auto enable_hashjoin_many_to_many_state = g_enable_hashjoin_many_to_many;
g_enable_overlaps_hashjoin = true;
g_enable_hashjoin_many_to_many = true;
g_trivial_loop_join_threshold = 1;
ScopeGuard reset_overlaps_state = [&enable_overlaps_hashjoin_state,
&enable_hashjoin_many_to_many_state] {
g_enable_overlaps_hashjoin = enable_overlaps_hashjoin_state;
g_enable_overlaps_hashjoin = enable_hashjoin_many_to_many_state;
g_trivial_loop_join_threshold = 1000;
};
QR::get()->clearCpuMemory();
// check whether overlaps hashtable caching works properly
const auto q1 =
"SELECT count(*) FROM does_not_intersect_b as b JOIN does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q1, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)2);
const auto q2 =
"SELECT /*+ overlaps_bucket_threshold(0.2), overlaps_no_cache */ count(*) FROM "
"does_not_intersect_b as b JOIN does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q2, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)2);
QR::get()->clearCpuMemory();
execSQL(q2, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)0);
const auto q3 =
"SELECT /*+ overlaps_no_cache */ count(*) FROM does_not_intersect_b as b JOIN "
"does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q3, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)0);
const auto q4 =
"SELECT /*+ overlaps_max_size(1000), overlaps_no_cache */ count(*) FROM "
"does_not_intersect_b as b JOIN does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q4, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)0);
const auto q5 =
"SELECT /*+ overlaps_bucket_threshold(0.2), overlaps_max_size(1000), "
"overlaps_no_cache */ count(*) FROM does_not_intersect_b as b JOIN "
"does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q5, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)0);
}
TEST_F(OverlapsTest, CacheBehaviorUnderQueryHint) {
// consider the following symbols:
// T_E: bucket_threshold_hint_enabled
// T_D: bucket_threshold_hint_disabled (use default value)
// T_C: use calculated bucket_threshold value
// by performing auto tuner with an initial value of T_D
// M_E: hashtable_max_size_hint_enabled
// M_D: hashtable_max_size_hint_disabled (use default value)
// here, we only add param setting to auto_tuner iff the initial setting is <T_D, *>
// and replace the cached hashtable when we have already cached one
// (and have different params)
// let say a hashtable T is built from the setting C as C ----> T
// then we reuse the hashtable T iff we have a cached hashtable which is mapped to C
// all combinations of <chosen bucket_threshold, max_hashtable_size> are:
// <T_E, M_E> --> impossible, we use <T_E, M_D> instead since we skip M_E and set M_D
// <T_E, M_D> --> possible, but do not add the pair to auto_tuner_cache
// and map <T_E, M_D> ----> T to hashtable cache
// <T_D, M_E> --> possible, and auto tuner calculates <T_C, M_D>
// add map <T_D, M_D> ----> <T_C, M_E> to auto_tuner_cache
// add map <T_C, M_E> ----> T to hashtable cache
// <T_D, M_D> --> possible, and auto tuner calculates <T_C, M_D>
// add map <T_D, M_D> ----> <T_C, M_D> to auto_tuner_cache
// add map <T_C, M_D> ----> T to hashtable cache
// <T_C, M_E> --> possible, and comes from the initial setting of <T_D, M_E>
// <T_C, M_D> --> possible, and comes from the initial setting of <T_D, M_D>
QR::get()->clearCpuMemory();
const auto enable_overlaps_hashjoin_state = g_enable_overlaps_hashjoin;
const auto enable_hashjoin_many_to_many_state = g_enable_hashjoin_many_to_many;
g_enable_overlaps_hashjoin = true;
g_enable_hashjoin_many_to_many = true;
g_trivial_loop_join_threshold = 1;
ScopeGuard reset_overlaps_state = [&enable_overlaps_hashjoin_state,
&enable_hashjoin_many_to_many_state] {
g_enable_overlaps_hashjoin = enable_overlaps_hashjoin_state;
g_enable_overlaps_hashjoin = enable_hashjoin_many_to_many_state;
g_trivial_loop_join_threshold = 1000;
};
// <T_D, M_D> case, add both <T_C, M_D> to auto tuner and its hashtable to cache
const auto q1 =
"SELECT count(*) FROM does_not_intersect_b as b JOIN does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q1, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)2);
// <T_E, M_D> case, only add hashtable to cache with <T_E: 0.1, M_D>
const auto q2 =
"SELECT /*+ overlaps_bucket_threshold(0.1) */ count(*) FROM does_not_intersect_b "
"as b JOIN does_not_intersect_a as a ON ST_Intersects(a.poly, b.poly);";
execSQL(q2, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)3);
// <T_E, M_D> case... only add hashtable to cache with <T_E: 0.2, M_D>
const auto q3 =
"SELECT /*+ overlaps_bucket_threshold(0.2) */ count(*) FROM does_not_intersect_b "
"as b JOIN does_not_intersect_a as a ON ST_Intersects(a.poly, b.poly);";
execSQL(q3, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)4);
// only reuse cached hashtable for <T_E: 0.1, M_D>
const auto q4 =
"SELECT /*+ overlaps_bucket_threshold(0.1) */ count(*) FROM does_not_intersect_b "
"as b JOIN does_not_intersect_a as a ON ST_Intersects(a.poly, b.poly);";
execSQL(q4, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)4);
// skip max_size hint, so <T_E, M_D> case and only reuse <T_E: 0.1, M_D> hashtable
const auto q5 =
"SELECT /*+ overlaps_bucket_threshold(0.1), overlaps_max_size(1000) */ count(*) "
"FROM does_not_intersect_b as b JOIN does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q5, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)4);
// <T_D, M_E> case, so it now becomes <T_C, M_E>
// add <T_D, M_E> --> <T_C, M_E: 1000> mapping to auto_tuner
// add <T_C, M_E: 1000> hashtable to cache
const auto q6 =
"SELECT /*+ overlaps_max_size(1000) */ count(*) FROM does_not_intersect_b as b "
"JOIN does_not_intersect_a as a ON ST_Intersects(a.poly, b.poly);";
execSQL(q6, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)6);
// <T_E, M_D> case, only reuse cached hashtable of <T_E: 0.2, M_D>
const auto q7 =
"SELECT /*+ overlaps_max_size(1000), overlaps_bucket_threshold(0.2) */ count(*) "
"FROM does_not_intersect_b as b JOIN does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q7, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)6);
// <T_E, M_D> case... only add hashtable to cache with <T_E: 0.3, M_D>
const auto q8 =
"SELECT /*+ overlaps_max_size(1000), overlaps_bucket_threshold(0.3) */ count(*) "
"FROM does_not_intersect_b as b JOIN does_not_intersect_a as a ON "
"ST_Intersects(a.poly, b.poly);";
execSQL(q8, ExecutorDeviceType::CPU);
ASSERT_EQ(QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::OVERLAPS_HT, true),
(size_t)7);
}
class OverlapsJoinHashTableMock : public OverlapsJoinHashTable {
public:
struct ExpectedValues {
size_t entry_count;
size_t emitted_keys_count;
};
static std::shared_ptr<OverlapsJoinHashTableMock> getInstance(
const std::shared_ptr<Analyzer::BinOper> condition,
const std::vector<InputTableInfo>& query_infos,
const Data_Namespace::MemoryLevel memory_level,
ColumnCacheMap& column_cache,
Executor* executor,
const int device_count,
const RegisteredQueryHint& query_hint,
const std::vector<OverlapsJoinHashTableMock::ExpectedValues>& expected_values) {
auto hash_join = std::make_shared<OverlapsJoinHashTableMock>(condition,
query_infos,
memory_level,
column_cache,
executor,
device_count,
expected_values);
hash_join->registerQueryHint(query_hint);
hash_join->reifyWithLayout(HashType::OneToMany);
return hash_join;
}
OverlapsJoinHashTableMock(const std::shared_ptr<Analyzer::BinOper> condition,
const std::vector<InputTableInfo>& query_infos,
const Data_Namespace::MemoryLevel memory_level,
ColumnCacheMap& column_cache,
Executor* executor,
const int device_count,
const std::vector<ExpectedValues>& expected_values)
: OverlapsJoinHashTable(
condition,
JoinType::INVALID, // b/c this is mock
query_infos,
memory_level,
column_cache,
executor,
HashJoin::normalizeColumnPairs(condition.get(),
*executor->getCatalog(),
executor->getTemporaryTables())
.first,
device_count,
{},
{})
, expected_values_per_step_(expected_values) {}
protected:
void reifyImpl(std::vector<ColumnsForDevice>& columns_per_device,
const Fragmenter_Namespace::TableInfo& query_info,
const HashType layout,
const size_t shard_count,
const size_t entry_count,
const size_t emitted_keys_count,
const bool skip_hashtable_caching,
const size_t chosen_max_hashtable_size,
const double chosen_bucket_threshold) final {
EXPECT_LE(step_, expected_values_per_step_.size());
auto& expected_values = expected_values_per_step_.back();
EXPECT_EQ(entry_count, expected_values.entry_count);
EXPECT_EQ(emitted_keys_count, expected_values.emitted_keys_count);
return;
}
// returns entry_count, emitted_keys_count
std::pair<size_t, size_t> approximateTupleCount(
const std::vector<double>& bucket_sizes_for_dimension,
std::vector<ColumnsForDevice>& columns_per_device,
const size_t chosen_max_hashtable_size,
const double chosen_bucket_threshold) final {
auto [entry_count, emitted_keys_count] =
OverlapsJoinHashTable::approximateTupleCount(bucket_sizes_for_dimension,
columns_per_device,
chosen_max_hashtable_size,
chosen_bucket_threshold);
return std::make_pair(entry_count, emitted_keys_count);
}
// returns entry_count, emitted_keys_count
std::pair<size_t, size_t> computeHashTableCounts(
const size_t shard_count,
const std::vector<double>& bucket_sizes_for_dimension,
std::vector<ColumnsForDevice>& columns_per_device,
const size_t chosen_max_hashtable_size,
const double chosen_bucket_threshold) final {
auto [entry_count, emitted_keys_count] =
OverlapsJoinHashTable::computeHashTableCounts(shard_count,
bucket_sizes_for_dimension,
columns_per_device,
chosen_max_hashtable_size,
chosen_bucket_threshold);
EXPECT_LT(step_, expected_values_per_step_.size());
auto& expected_values = expected_values_per_step_[step_];
EXPECT_EQ(entry_count, expected_values.entry_count);
EXPECT_EQ(emitted_keys_count, expected_values.emitted_keys_count);
step_++;
return std::make_pair(entry_count, emitted_keys_count);
}
std::vector<ExpectedValues> expected_values_per_step_;
size_t step_{0};
};
class BucketSizeTest : public ::testing::Test {
protected:
void SetUp() override {
QR::get()->runDDLStatement("DROP TABLE IF EXISTS bucket_size_poly;");
QR::get()->runDDLStatement("CREATE TABLE bucket_size_poly (poly MULTIPOLYGON);");
QR::get()->runSQL(
R"(INSERT INTO bucket_size_poly VALUES ('MULTIPOLYGON(((0 0, 0 2, 2 0, 2 2)))');)",
ExecutorDeviceType::CPU);
QR::get()->runSQL(
R"(INSERT INTO bucket_size_poly VALUES ('MULTIPOLYGON(((0 0, 0 2, 2 0, 2 2)))');)",
ExecutorDeviceType::CPU);
QR::get()->runSQL(
R"(INSERT INTO bucket_size_poly VALUES ('MULTIPOLYGON(((2 2, 2 4, 4 2, 4 4)))');)",
ExecutorDeviceType::CPU);
QR::get()->runSQL(
R"(INSERT INTO bucket_size_poly VALUES ('MULTIPOLYGON(((0 0, 0 50, 50 0, 50 50)))');)",
ExecutorDeviceType::CPU);
QR::get()->runDDLStatement("DROP TABLE IF EXISTS bucket_size_pt;");
QR::get()->runDDLStatement("CREATE TABLE bucket_size_pt (pt POINT);");
}
void TearDown() override {
QR::get()->runDDLStatement("DROP TABLE IF EXISTS bucket_size_poly;");
QR::get()->runDDLStatement("DROP TABLE IF EXISTS bucket_size_pt;");
}
public:
static std::pair<std::shared_ptr<Analyzer::BinOper>, std::vector<InputTableInfo>>
getOverlapsBuildInfo() {
auto catalog = QR::get()->getCatalog();
CHECK(catalog);
std::vector<InputTableInfo> query_infos;
const auto pts_td = catalog->getMetadataForTable("bucket_size_pt");
CHECK(pts_td);
const auto pts_cd = catalog->getMetadataForColumn(pts_td->tableId, "pt");
CHECK(pts_cd);
auto pt_col_var = std::make_shared<Analyzer::ColumnVar>(
pts_cd->columnType, pts_cd->tableId, pts_cd->columnId, 0);
query_infos.emplace_back(InputTableInfo{pts_td->tableId, build_table_info({pts_td})});
const auto poly_td = catalog->getMetadataForTable("bucket_size_poly");
CHECK(poly_td);
const auto poly_cd = catalog->getMetadataForColumn(poly_td->tableId, "poly");
CHECK(poly_cd);
const auto bounds_cd =
catalog->getMetadataForColumn(poly_td->tableId, poly_cd->columnId + 4);
CHECK(bounds_cd && bounds_cd->columnType.is_array());
auto poly_col_var = std::make_shared<Analyzer::ColumnVar>(
bounds_cd->columnType, poly_cd->tableId, bounds_cd->columnId, 1);
query_infos.emplace_back(
InputTableInfo{poly_td->tableId, build_table_info({poly_td})});
auto condition = std::make_shared<Analyzer::BinOper>(
kBOOLEAN, kOVERLAPS, kANY, pt_col_var, poly_col_var);
return std::make_pair(condition, query_infos);
}
};
TEST_F(BucketSizeTest, OverlapsTunerEarlyOut) {
// 2 steps, early out due to increasing keys per bin
auto catalog = QR::get()->getCatalog();
CHECK(catalog);
auto executor = QR::get()->getExecutor();
executor->setCatalog(catalog.get());
auto [condition, query_infos] = BucketSizeTest::getOverlapsBuildInfo();
ColumnCacheMap column_cache;
std::vector<OverlapsJoinHashTableMock::ExpectedValues> expected_values;
expected_values.emplace_back(
OverlapsJoinHashTableMock::ExpectedValues{8, 7}); // step 1
expected_values.emplace_back(
OverlapsJoinHashTableMock::ExpectedValues{1340, 688}); // step 2
expected_values.emplace_back(OverlapsJoinHashTableMock::ExpectedValues{
1340, 688}); // increasing keys per bin, stop at step 2
auto hash_table =
OverlapsJoinHashTableMock::getInstance(condition,
query_infos,
Data_Namespace::MemoryLevel::CPU_LEVEL,
column_cache,
executor.get(),
/*device_count=*/1,
RegisteredQueryHint::defaults(),
expected_values);
CHECK(hash_table);
}
TEST_F(BucketSizeTest, OverlapsTooBig) {
auto catalog = QR::get()->getCatalog();
CHECK(catalog);
auto executor = QR::get()->getExecutor();
executor->setCatalog(catalog.get());
auto [condition, query_infos] = BucketSizeTest::getOverlapsBuildInfo();
ColumnCacheMap column_cache;
std::vector<OverlapsJoinHashTableMock::ExpectedValues> expected_values;
// runs 8 back tuner steps after initial size too big failure
expected_values.emplace_back(
OverlapsJoinHashTableMock::ExpectedValues{8, 7}); // step 1
expected_values.emplace_back(
OverlapsJoinHashTableMock::ExpectedValues{2, 4}); // step 2 (reversal)
expected_values.emplace_back(OverlapsJoinHashTableMock::ExpectedValues{
2, 4}); // step 3 (hash table not getting smaller, bails)
RegisteredQueryHint hint;
hint.overlaps_max_size = 2;
hint.registerHint(QueryHint::kOverlapsMaxSize);
EXPECT_ANY_THROW(
OverlapsJoinHashTableMock::getInstance(condition,
query_infos,
Data_Namespace::MemoryLevel::CPU_LEVEL,
column_cache,
executor.get(),
/*device_count=*/1,
hint,
expected_values));
}
void populateTablesForVarlenLinearizationTest() {
{
QR::get()->runDDLStatement("DROP TABLE IF EXISTS mfgeo;"); // non-null geo col val
QR::get()->runDDLStatement("DROP TABLE IF EXISTS sfgeo;"); // non-null geo col val
QR::get()->runDDLStatement(
"DROP TABLE IF EXISTS mfgeo_n;"); // contains null-valued geo col val
QR::get()->runDDLStatement(
"DROP TABLE IF EXISTS sfgeo_n;"); // contains null-valued geo col val
QR::get()->runDDLStatement("DROP TABLE IF EXISTS mfgeo3;"); // non-null geo col val
QR::get()->runDDLStatement(
"DROP TABLE IF EXISTS mfgeo3_n;"); // contains null-valued geo col val
auto table_ddl =
" (id INTEGER,\n"
" gpt GEOMETRY(POINT),\n"
" gpt4e GEOMETRY(POINT, 4326) ENCODING COMPRESSED(32),\n"
" gpt4n GEOMETRY(POINT, 4326) ENCODING NONE,\n"
" gl GEOMETRY(LINESTRING),\n"
" gl4e GEOMETRY(LINESTRING, 4326) ENCODING COMPRESSED(32),\n"
" gl4n GEOMETRY(LINESTRING, 4326) ENCODING NONE,\n"
" gp GEOMETRY(POLYGON),\n"
" gp4e GEOMETRY(POLYGON, 4326) ENCODING COMPRESSED(32),\n"
" gp4n GEOMETRY(POLYGON, 4326) ENCODING NONE,\n"
" gmp GEOMETRY(MULTIPOLYGON),\n"
" gmp4e GEOMETRY(MULTIPOLYGON, 4326) ENCODING COMPRESSED(32),\n"
" gmp4n GEOMETRY(MULTIPOLYGON, 4326) ENCODING NONE)";
auto create_table_ddl_gen = [&table_ddl](const std::string& tbl_name,
const bool multi_frag,
const int fragment_size = 2) {
std::ostringstream oss;
oss << "CREATE TABLE " << tbl_name << table_ddl;
if (multi_frag) {
oss << " WITH (FRAGMENT_SIZE = " << fragment_size << ")";
}
oss << ";";
return oss.str();
};
QR::get()->runDDLStatement(create_table_ddl_gen("mfgeo", true, 2));
QR::get()->runDDLStatement(create_table_ddl_gen("sfgeo", false));
QR::get()->runDDLStatement(create_table_ddl_gen("mfgeo_n", true, 2));
QR::get()->runDDLStatement(create_table_ddl_gen("sfgeo_n", false));
QR::get()->runDDLStatement(create_table_ddl_gen("mfgeo3", true, 3));
QR::get()->runDDLStatement(create_table_ddl_gen("mfgeo3_n", true, 3));
std::vector<std::vector<std::string>> input_val_non_nullable;
input_val_non_nullable.push_back({"0",
"\'POINT(0 0)\'",
"\'LINESTRING(0 0,1 0)\'",
"\'POLYGON((0 0,1 0,1 1,0 1,0 0))\'",
"\'MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0)))\'"});
input_val_non_nullable.push_back({"1",
"\'POINT(1 1)\'",
"\'LINESTRING(2 0,4 4)\'",
"\'POLYGON((0 0,2 0,0 2,0 0))\'",
"\'MULTIPOLYGON(((0 0,2 0,0 2,0 0)))\'"});
input_val_non_nullable.push_back(
{"2",
"\'POINT(2 2)\'",
"\'LINESTRING(1 0,2 2,3 3)\'",
"\'POLYGON((0 0,2 0,3 0,3 3,0 3,1 0,0 0))\'",
"\'MULTIPOLYGON(((0 0,2 0,3 0,3 3,0 3,1 0,0 0)))\'"});
input_val_non_nullable.push_back({"3",
"\'POINT(3 3)\'",
"\'LINESTRING(3 0,6 6,7 7)\'",
"\'POLYGON((0 0,4 0,0 4,0 0))\'",
"\'MULTIPOLYGON(((0 0,4 0,0 4,0 0)))\'"});
input_val_non_nullable.push_back(
{"4",