-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathDataRecyclerTest.cpp
2571 lines (2335 loc) · 126 KB
/
DataRecyclerTest.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 "TestHelpers.h"
#include "Logger/Logger.h"
#include "QueryEngine/CompilationOptions.h"
#include "QueryEngine/Execute.h"
#include "QueryEngine/QueryPlanDagCache.h"
#include "QueryEngine/QueryPlanDagExtractor.h"
#include "QueryEngine/Visitors/SQLOperatorDetector.h"
#include "QueryRunner/QueryRunner.h"
#include <gtest/gtest.h>
#include <boost/algorithm/string/join.hpp>
#include <exception>
#include <future>
#include <random>
#include <stdexcept>
extern bool g_is_test_env;
extern bool g_enable_table_functions;
extern bool g_enable_dev_table_functions;
using QR = QueryRunner::QueryRunner;
using namespace TestHelpers;
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
bool g_cpu_only{false};
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; \
}
namespace {
inline void run_ddl_statement(const std::string& create_table_stmt) {
QR::get()->runDDLStatement(create_table_stmt);
}
TargetValue run_simple_query(const std::string& query_str,
const ExecutorDeviceType device_type,
const bool geo_return_geo_tv = true,
const bool allow_loop_joins = true) {
auto rows = QR::get()->runSQL(query_str, device_type, allow_loop_joins);
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()) << query_str;
return crt_row[0];
}
void drop_tables_for_bbox_intersect() {
const auto cleanup_stmts = {R"(drop table if exists bbox_intersect_t11;)",
R"(drop table if exists bbox_intersect_t12;)",
R"(drop table if exists bbox_intersect_t13;)",
R"(drop table if exists bbox_intersect_t2;)",
R"(drop table if exists bbox_intersect_t3;)",
R"(drop table if exists bbox_intersect_t4;)"};
for (const auto& stmt : cleanup_stmts) {
QR::get()->runDDLStatement(stmt);
}
}
void create_table_for_bbox_intersect() {
const auto init_stmts_ddl = {
R"(create table bbox_intersect_t11 (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table bbox_intersect_t12 (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table bbox_intersect_t13 (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table bbox_intersect_t2 (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table bbox_intersect_t3 (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)",
R"(create table bbox_intersect_t4 (id int,
poly geometry(polygon, 4326),
mpoly geometry(multipolygon, 4326),
pt geometry(point, 4326));
)"};
for (const auto& stmt : init_stmts_ddl) {
QR::get()->runDDLStatement(stmt);
}
}
void insert_dml_for_bbox_intersect() {
std::string value_str =
"(0,'polygon((20 20,30 25,30 30,25 30,20 20))','multipolygon(((20 20,30 25,30 "
"30,25 30,20 2)))','point(22 22)');";
std::string bbox_intersect_val1 =
" values (0,'polygon((20 20,30 25,30 30,25 30,20 "
"20))','multipolygon(((20 20,30 25,30 30,25 30,20 2)))','point(22 22)')";
std::string bbox_intersect_val2 =
" 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)')";
auto insert_stmt = [&value_str](const std::string& tbl_name) {
return "INSERT INTO " + tbl_name + " VALUES " + value_str;
};
std::vector<std::string> tbl_names1{
"bbox_intersect_t11", "bbox_intersect_t12", "bbox_intersect_t13"};
std::vector<std::string> tbl_names2{
"bbox_intersect_t2", "bbox_intersect_t3", "bbox_intersect_t4"};
for (const std::string& tbl_name : tbl_names1) {
QR::get()->runSQL(insert_stmt(tbl_name), ExecutorDeviceType::CPU);
}
for (const std::string& tbl_name : tbl_names2) {
QR::get()->runSQL("insert into " + tbl_name + bbox_intersect_val1,
ExecutorDeviceType::CPU);
QR::get()->runSQL("insert into " + tbl_name + bbox_intersect_val2,
ExecutorDeviceType::CPU);
}
}
int drop_table() {
try {
run_ddl_statement("DROP TABLE IF EXISTS T1;");
run_ddl_statement("DROP TABLE IF EXISTS T2;");
run_ddl_statement("DROP TABLE IF EXISTS T3;");
run_ddl_statement("DROP TABLE IF EXISTS T4;");
run_ddl_statement("DROP TABLE IF EXISTS TF_TEST;");
drop_tables_for_bbox_intersect();
} catch (...) {
LOG(ERROR) << "Failed to drop table";
return -1;
}
return 0;
}
int create_and_populate_table() {
try {
drop_table();
const auto table_ddl = "(x int, y int, z text encoding dict);";
auto create_table_ddl = [&table_ddl](const std::string& tbl_name) {
return "CREATE TABLE " + tbl_name + table_ddl;
};
run_ddl_statement(create_table_ddl("T1"));
run_ddl_statement(create_table_ddl("T2"));
run_ddl_statement(create_table_ddl("T3"));
run_ddl_statement(create_table_ddl("T4"));
const auto data_insertion = [](const std::string& tbl_name) {
auto insert_dml = "INSERT INTO " + tbl_name + " VALUES(";
std::vector<std::string> value_vec = {"1, 1, '1'", "2, 1, '2'", "3, 1, '3'"};
for (auto& v : value_vec) {
QR::get()->runSQL(insert_dml + v + ");", ExecutorDeviceType::CPU);
}
};
data_insertion("T1");
data_insertion("T2");
QR::get()->runSQL("INSERT INTO T2 VALUES(4,2,'4');", ExecutorDeviceType::CPU);
data_insertion("T3");
QR::get()->runSQL("INSERT INTO T3 VALUES(4,2,'4');", ExecutorDeviceType::CPU);
QR::get()->runSQL("INSERT INTO T3 VALUES(5,2,'5');", ExecutorDeviceType::CPU);
data_insertion("T4");
QR::get()->runSQL("INSERT INTO T4 VALUES(4,2,'4');", ExecutorDeviceType::CPU);
QR::get()->runSQL("INSERT INTO T4 VALUES(5,2,'5');", ExecutorDeviceType::CPU);
QR::get()->runSQL("INSERT INTO T4 VALUES(6,2,'6');", ExecutorDeviceType::CPU);
run_ddl_statement("CREATE TABLE TF_TEST(d DOUBLE, d2 DOUBLE);");
auto insert_dml = "INSERT INTO TF_TEST VALUES(";
for (int i = 0; i < 20; i++) {
double dv = i + (0.1 * i);
auto v = std::to_string(dv);
QR::get()->runSQL(insert_dml + v + ", " + v + ");", ExecutorDeviceType::CPU);
}
create_table_for_bbox_intersect();
insert_dml_for_bbox_intersect();
} catch (...) {
LOG(ERROR) << "Failed to (re-)create table";
return -1;
}
return 0;
}
std::shared_ptr<CacheItemMetric> getCachedHashTableMetric(
std::set<QueryPlanHash>& already_visited,
CacheItemType cache_item_type) {
auto cached_ht = QR::get()->getCachedHashtableWithoutCacheKey(
already_visited, cache_item_type, DataRecyclerUtil::CPU_DEVICE_IDENTIFIER);
auto cache_key = std::get<0>(cached_ht);
already_visited.insert(cache_key);
return QR::get()->getCacheItemMetric(
cache_key, cache_item_type, DataRecyclerUtil::CPU_DEVICE_IDENTIFIER);
}
struct BBoxIntersectCachedHTAndMetaInfo {
QueryPlanHash key;
std::shared_ptr<HashTable> cached_ht;
std::optional<HashtableCacheMetaInfo> cached_ht_metainfo;
std::shared_ptr<CacheItemMetric> cached_metric;
std::optional<AutoTunerMetaInfo> cached_tuning_info;
};
BBoxIntersectCachedHTAndMetaInfo
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
std::set<QueryPlanHash>& already_visited) {
auto cached_ht = QR::get()->getCachedHashtableWithoutCacheKey(
already_visited,
CacheItemType::BBOX_INTERSECT_HT,
DataRecyclerUtil::CPU_DEVICE_IDENTIFIER);
auto cache_key = std::get<0>(cached_ht);
already_visited.insert(cache_key);
auto ht_metric = QR::get()->getCacheItemMetric(cache_key,
CacheItemType::BBOX_INTERSECT_HT,
DataRecyclerUtil::CPU_DEVICE_IDENTIFIER);
auto tuning_param_cache =
BoundingBoxIntersectJoinHashTable::getBoundingBoxIntersectTuningParamCache();
auto tuning_param =
tuning_param_cache->getItemFromCache(cache_key,
CacheItemType::BBOX_INTERSECT_AUTO_TUNER_PARAM,
DataRecyclerUtil::CPU_DEVICE_IDENTIFIER);
return {
cache_key, std::get<1>(cached_ht), std::get<2>(cached_ht), ht_metric, tuning_param};
}
} // namespace
TEST(DataRecycler, QueryPlanDagExtractor_Simple_Project_Query) {
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID).get();
auto q1_str = "SELECT x FROM T1 ORDER BY x;";
auto q1_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q1_str);
EXPECT_TRUE(q1_query_info.left_deep_trees_id.empty());
auto q1_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q1_query_info.root_node.get(), executor);
// 1. a sort node becomes a root (dag_rel_id = 0)
// 2. a project node becomes a child of the sort node (dag_rel_id = 1)
// 3. a scan node (the leaf of the query plan) becomes a child of the project node
EXPECT_TRUE(q1_plan_dag.extracted_dag.compare("0|1|2|") == 0);
auto q2_str = "SELECT x FROM T1;";
auto q2_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q2_str);
EXPECT_TRUE(q2_query_info.left_deep_trees_id.empty());
auto q2_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q2_query_info.root_node.get(), executor);
// q2 is the same as q1 except sort node
EXPECT_TRUE(q2_plan_dag.extracted_dag.compare("1|2|") == 0);
auto q3_str = "SELECT x FROM T1 GROUP BY x;";
auto q3_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q3_str);
EXPECT_TRUE(q3_query_info.left_deep_trees_id.empty());
auto q3_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q3_query_info.root_node.get(), executor);
// compound node becomes the root (dag_rel_id = 3), and the scan node
// (that is the same node as both q1 and q2) is the leaf of the query plan
EXPECT_TRUE(q3_plan_dag.extracted_dag.compare("3|2|") == 0);
auto q4_str = "SELECT x FROM T1 GROUP BY x ORDER BY x;";
auto q4_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q4_str);
EXPECT_TRUE(q4_query_info.left_deep_trees_id.empty());
auto q4_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q4_query_info.root_node.get(), executor);
// this sort node has different input compared with that of q1
// so we assign the new dag_rel_id (4) to the sort node
EXPECT_TRUE(q4_plan_dag.extracted_dag.compare("4|3|2|") == 0);
auto q1_dup_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q1_query_info.root_node.get(), executor);
EXPECT_TRUE(q1_dup_plan_dag.extracted_dag.compare("0|1|2|") == 0);
auto q4_dup_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q4_query_info.root_node.get(), executor);
EXPECT_TRUE(q4_dup_plan_dag.extracted_dag.compare("4|3|2|") == 0);
}
TEST(DataRecycler, QueryPlanDagExtractor_Heavy_IN_clause) {
// we do not extract query plan dag where at least one rel node
// containing a heavy IN-expr w.r.t its value list, i.e., |value list| > 20
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID).get();
auto create_query_having_IN_expr = [](const std::string tbl_name,
const std::string agg_col_name,
const int value_list_size) {
// assume value_list_size > 0
std::vector<std::string> value_list;
for (int i = 0; i < value_list_size; i++) {
value_list.push_back(std::to_string(i));
}
return "SELECT COUNT(" + agg_col_name + ") FROM " + tbl_name + " WHERE " +
agg_col_name + " IN (" + boost::algorithm::join(value_list, ",") + ");";
};
// so we can extract q1's dag
auto q1_str = create_query_having_IN_expr("T1", "x", 20);
auto q1_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q1_str);
EXPECT_TRUE(q1_query_info.left_deep_trees_id.empty());
auto q1_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q1_query_info.root_node.get(), executor);
EXPECT_EQ(q1_plan_dag.contain_not_supported_rel_node, false);
// but we skip to extract a DAG for q2 since it contains IN-expr having 21 elems in its
// value list
auto q2_str = create_query_having_IN_expr("T1", "x", 21);
auto q2_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q2_str);
EXPECT_TRUE(q2_query_info.left_deep_trees_id.empty());
auto q2_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q2_query_info.root_node.get(), executor);
EXPECT_EQ(q2_plan_dag.contain_not_supported_rel_node, true);
}
TEST(DataRecycler, QueryPlanDagExtractor_Join_Query) {
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID).get();
auto q1_str = "SELECT T1.x FROM T1, T2 WHERE T1.x = T2.x;";
auto q1_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q1_str);
EXPECT_TRUE(q1_query_info.left_deep_trees_id.size() == 1);
auto q1_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q1_query_info.root_node.get(), executor);
auto q2_str = "SELECT T1.x FROM T1 JOIN T2 ON T1.x = T2.x;";
auto q2_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q2_str);
EXPECT_TRUE(q2_query_info.left_deep_trees_id.size() == 1);
auto q2_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q2_query_info.root_node.get(), executor);
EXPECT_TRUE(q1_plan_dag.extracted_dag.compare(q2_plan_dag.extracted_dag) != 0);
auto q3_str = "SELECT T1.x FROM T1, T2 WHERE T1.x = T2.x and T2.y = T1.y;";
auto q3_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q3_str);
EXPECT_TRUE(q3_query_info.left_deep_trees_id.size() == 1);
auto q3_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q3_query_info.root_node.get(), executor);
auto q4_str = "SELECT T1.x FROM T1 JOIN T2 ON T1.x = T2.x and T1.y = T2.y;";
auto q4_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q4_str);
EXPECT_TRUE(q4_query_info.left_deep_trees_id.size() == 1);
auto q4_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q4_query_info.root_node.get(), executor);
EXPECT_TRUE(q3_plan_dag.extracted_dag.compare(q4_plan_dag.extracted_dag) != 0);
auto q5_str = "SELECT T1.x FROM T1 JOIN T2 ON T1.y = T2.y and T1.x = T2.x;";
auto q5_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q5_str);
EXPECT_TRUE(q5_query_info.left_deep_trees_id.size() == 1);
auto q5_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q5_query_info.root_node.get(), executor);
EXPECT_TRUE(q3_plan_dag.extracted_dag.compare(q5_plan_dag.extracted_dag) != 0);
std::unordered_set<std::string> query_plan_dag_hash;
std::vector<std::string> queries;
queries.emplace_back(
"SELECT COUNT(1) FROM T1 LEFT JOIN T2 ON T1.y <= T2.y WHERE T1.x = T2.x");
queries.emplace_back(
"SELECT COUNT(1) FROM T1 LEFT JOIN T2 ON T1.y = T2.y WHERE T1.x = T2.x");
queries.emplace_back(
"SELECT COUNT(1) FROM T1 INNER JOIN T2 ON T1.y = T2.y WHERE T1.x = T2.x");
queries.emplace_back(
"SELECT COUNT(1) FROM T1 INNER JOIN T2 ON T1.y <= T2.y WHERE T1.x = T2.x");
for (const auto& sql : queries) {
auto query_info = QR::get()->getQueryInfoForDataRecyclerTest(sql);
auto dag =
QueryPlanDagExtractor::extractQueryPlanDag(query_info.root_node.get(), executor);
query_plan_dag_hash.insert(dag.extracted_dag);
}
// check whether we correctly extract query plan DAG for outer join having loop-join
EXPECT_EQ(query_plan_dag_hash.size(), queries.size());
}
TEST(DataRecycler, QueryPlanDagExtractor_TableFunction) {
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID).get();
auto q1_str = "SELECT out0 FROM TABLE(row_copier(cursor(SELECT d FROM TF_TEST), 1));";
auto res1 = QR::get()->runSQL(q1_str, ExecutorDeviceType::CPU, false, false);
auto q1_plan_dag = executor->getLatestQueryPlanDagExtracted();
EXPECT_TRUE(q1_plan_dag.compare(EMPTY_QUERY_PLAN) != 0);
auto q2_str = "SELECT out0 FROM TABLE(row_copier(cursor(SELECT d FROM TF_TEST), 2));";
auto res2 = QR::get()->runSQL(q2_str, ExecutorDeviceType::CPU, false, false);
auto q2_plan_dag = executor->getLatestQueryPlanDagExtracted();
EXPECT_TRUE(q2_plan_dag.compare(EMPTY_QUERY_PLAN) != 0);
EXPECT_TRUE(q2_plan_dag.compare(q1_plan_dag) != 0);
auto q3_str =
"SELECT out0 FROM TABLE(row_adder(1, cursor(SELECT d, d2 FROM TF_TEST)));";
auto res3 = QR::get()->runSQL(q3_str, ExecutorDeviceType::CPU, false, false);
auto q3_plan_dag = executor->getLatestQueryPlanDagExtracted();
EXPECT_TRUE(q3_plan_dag.compare(EMPTY_QUERY_PLAN) != 0);
EXPECT_TRUE(q3_plan_dag.compare(q2_plan_dag) != 0);
auto q4_str =
"SELECT out0, out1 FROM TABLE(row_addsub(1, cursor(SELECT d, d2 FROM TF_TEST)))";
auto res4 = QR::get()->runSQL(q4_str, ExecutorDeviceType::CPU, false, false);
auto q4_plan_dag = executor->getLatestQueryPlanDagExtracted();
EXPECT_TRUE(q4_plan_dag.compare(EMPTY_QUERY_PLAN) != 0);
EXPECT_TRUE(q4_plan_dag.compare(q3_plan_dag) != 0);
auto q5_str = q1_str;
auto res5 = QR::get()->runSQL(q5_str, ExecutorDeviceType::CPU, false, false);
auto q5_plan_dag = executor->getLatestQueryPlanDagExtracted();
EXPECT_TRUE(q5_plan_dag.compare(EMPTY_QUERY_PLAN) != 0);
EXPECT_TRUE(q5_plan_dag.compare(q1_plan_dag) == 0);
}
TEST(DataRecycler, DAG_Cache_Size_Management) {
// test if DAG cache becomes full
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID).get();
// get query info for DAG cache test in advance
auto& DAG_CACHE = executor->getQueryPlanDagCache();
auto original_DAG_cache_max_size = MAX_NODE_CACHE_SIZE;
ScopeGuard reset_dag_state = [&original_DAG_cache_max_size, &DAG_CACHE] {
DAG_CACHE.setNodeMapMaxSize(original_DAG_cache_max_size);
};
auto q1_str = "SELECT x FROM T1 ORDER BY x;";
auto q2_str = "SELECT y FROM T1;";
auto q3_str =
"SELECT T2.y, COUNT(T1.x) FROM T1, T2 WHERE T1.y = T2.y and T1.x = T2.x GROUP BY "
"T2.y;";
auto q1_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q1_str);
auto q2_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q2_str);
auto q3_query_info = QR::get()->getQueryInfoForDataRecyclerTest(q3_str);
DAG_CACHE.clearQueryPlanCache();
// test: when DAG cache becomes full, it should skip the following query and clear the
// cached plan
DAG_CACHE.setNodeMapMaxSize(48);
auto q1_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q1_query_info.root_node.get(), executor);
// 1. a sort node becomes a root (dag_rel_id = 0)
// 2. a project node becomes a child of the sort node (dag_rel_id = 1)
// 3. a scan node (the leaf of the query plan) becomes a child of the project node
EXPECT_TRUE(q1_plan_dag.extracted_dag.compare("0|1|2|") == 0);
// 3 unique REL nodes in the cache --> 3 * 2 * 8 = 48
EXPECT_EQ(DAG_CACHE.getCurrentNodeMapSize(), 48u);
auto q2_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q2_query_info.root_node.get(), executor);
// we set the DAG cache size be 48, so when we try to cache the q2, it becomes full
// so it skips to extract DAG plan of this query and also clear the cache itself
EXPECT_TRUE(q2_plan_dag.extracted_dag.compare("") == 0);
EXPECT_EQ(DAG_CACHE.getCurrentNodeMapSize(), 0u);
DAG_CACHE.clearQueryPlanCache();
// test: when a query size is too large, we skip caching the query
auto q3_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q3_query_info.root_node.get(), executor);
// q3 has more than three nodes, so its size is beyond the limit of the DAG cache (48)
// so we cannot keep it to our DAG cache
EXPECT_EQ(DAG_CACHE.getCurrentNodeMapSize(), 0u);
// test: increase the cache size that is enough to hold both q1 and q2
DAG_CACHE.setNodeMapMaxSize(80);
auto new_q1_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q1_query_info.root_node.get(), executor);
EXPECT_TRUE(new_q1_plan_dag.extracted_dag.compare("0|1|2|") == 0);
auto new_q2_plan_dag =
QueryPlanDagExtractor::extractQueryPlanDag(q2_query_info.root_node.get(), executor);
EXPECT_TRUE(new_q2_plan_dag.extracted_dag.compare("3|2|") == 0);
EXPECT_GE(DAG_CACHE.getCurrentNodeMapSize(), 48u);
}
TEST(DataRecycler, Hashtable_For_BBox_Intersect_Cache_Maintanence) {
ScopeGuard reset_state =
[orig_bbox_hashjoin_state = g_enable_bbox_intersect_hashjoin,
orig_hashjoin_many_to_many_state = g_enable_hashjoin_many_to_many,
orig_trivial_loop_join_threshold = g_trivial_loop_join_threshold,
orig_table_reordering_state = g_from_table_reordering] {
g_enable_bbox_intersect_hashjoin = orig_bbox_hashjoin_state;
g_enable_bbox_intersect_hashjoin = orig_hashjoin_many_to_many_state;
g_trivial_loop_join_threshold = orig_trivial_loop_join_threshold;
g_from_table_reordering = orig_table_reordering_state;
};
g_enable_bbox_intersect_hashjoin = true;
g_enable_hashjoin_many_to_many = true;
g_trivial_loop_join_threshold = 1;
// we need to disable table reordering to control our logic
g_from_table_reordering = false;
std::set<QueryPlanHash> visited_hashtable_key;
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID).get();
auto clearCaches = [&executor, &visited_hashtable_key] {
executor->clearMemory(MemoryLevel::CPU_LEVEL);
executor->getQueryPlanDagCache().clearQueryPlanCache();
visited_hashtable_key.clear();
};
for (auto dt : {ExecutorDeviceType::CPU}) {
// currently we do not support hashtable caching for GPU
// hashtables of t11, t12, t13: 208 bytes
// hashtable of t2: 416 bytes
{
// test1. cache hashtable of t11 and then reuse it correctly?
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q1_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto ht1_ref_count_v1 = q1_ht_metrics->getRefCount();
EXPECT_EQ(static_cast<size_t>(1), ht1_ref_count_v1);
EXPECT_EQ(static_cast<size_t>(208), q1_ht_metrics->getMemSize());
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
auto ht1_ref_count_v2 = q1_ht_metrics->getRefCount();
EXPECT_LT(ht1_ref_count_v1, ht1_ref_count_v2);
auto q2 =
R"(SELECT count(*) from bbox_intersect_t13 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q2, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto ht1_ref_count_v3 = q1_ht_metrics->getRefCount();
EXPECT_LT(ht1_ref_count_v2, ht1_ref_count_v3);
}
{
// test2. cache t11 and t12
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t11 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q2 =
R"(SELECT count(*) from bbox_intersect_t11 as b JOIN bbox_intersect_t12 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q2, dt)));
EXPECT_EQ(static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
}
{
// test3. set hashtable cache size as 420 bytes,
// and try to cache t11's hashtable and then that of t2's
// so we finally have t's only since sizeof(t11) + sizeof(t2) > 420
// and so we need to remove t11's to cache t2's
// (to check we disallow having more hashtables beyond its capacity)
const auto original_total_cache_size = g_hashtable_cache_total_bytes;
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, 420);
ScopeGuard reset_cache_status = [&original_total_cache_size] {
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, original_total_cache_size);
};
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q1_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto ht1_ref_count = q1_ht_metrics->getRefCount();
EXPECT_EQ(static_cast<size_t>(1), ht1_ref_count);
EXPECT_EQ(static_cast<size_t>(208), q1_ht_metrics->getMemSize());
auto q2 =
R"(SELECT count(*) from bbox_intersect_t2 as b JOIN bbox_intersect_t2 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(2), v<int64_t>(run_simple_query(q2, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q2_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto ht2_ref_count = q2_ht_metrics->getRefCount();
EXPECT_EQ(static_cast<size_t>(1), ht2_ref_count);
EXPECT_EQ(static_cast<size_t>(416), q2_ht_metrics->getMemSize());
}
{
// test4. set hashtable cache size as 500 bytes, and
// cache t11 and t12 (so total 416 bytes) and then try to cache t2
// and check whether cache only has t2 (remove t11 and t12 to make a room for t2)
const auto original_total_cache_size = g_hashtable_cache_total_bytes;
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, 500);
ScopeGuard reset_cache_status = [&original_total_cache_size] {
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, original_total_cache_size);
};
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q1_ht_dag_info = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q2 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t12 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q2, dt)));
EXPECT_EQ(static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q2_ht_dag_info = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q3 =
R"(SELECT count(*) from bbox_intersect_t2 as b JOIN bbox_intersect_t2 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(2), v<int64_t>(run_simple_query(q3, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q3_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto ht2_ref_count = q3_ht_metrics->getRefCount();
EXPECT_EQ(static_cast<size_t>(1), ht2_ref_count);
EXPECT_EQ(static_cast<size_t>(416), q3_ht_metrics->getMemSize());
}
{
// test5. set hashtable cache size as 650 bytes, and
// we try to cache t11, t12 and t2's hashtables
// here we make t11 to be more frequently reused one than t12
// and try to cache t2.
// if our cache maintenance works correctly, we should remove t12 since it is
// less frequently reused one
const auto original_total_cache_size = g_hashtable_cache_total_bytes;
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, 650);
ScopeGuard reset_cache_status = [&original_total_cache_size] {
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, original_total_cache_size);
};
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q1_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto ht1_ref_count = q1_ht_metrics->getRefCount();
EXPECT_EQ(static_cast<size_t>(1), ht1_ref_count);
EXPECT_EQ(static_cast<size_t>(208), q1_ht_metrics->getMemSize());
auto q2 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t12 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q2, dt)));
EXPECT_EQ(static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
auto q2_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q3 =
R"(SELECT count(*) from bbox_intersect_t2 as b JOIN bbox_intersect_t2 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(2), v<int64_t>(run_simple_query(q3, dt)));
auto q3_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
EXPECT_LT(static_cast<size_t>(1), q1_ht_metrics->getRefCount());
EXPECT_EQ(static_cast<size_t>(1), q3_ht_metrics->getRefCount());
EXPECT_EQ(static_cast<size_t>(416), q3_ht_metrics->getMemSize());
}
{
// test 6. set per_hashtable_size_limit to be 250
// and try to cache t11, t12 and t2
// due to the per item limit, we can cache t11 and t12 but not t2
const auto original_per_max_hashtable_size = g_max_cacheable_hashtable_size_bytes;
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setMaxCacheItemSize(
CacheItemType::BBOX_INTERSECT_HT, 250);
ScopeGuard reset_cache_status = [&original_per_max_hashtable_size] {
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setMaxCacheItemSize(
CacheItemType::BBOX_INTERSECT_HT, original_per_max_hashtable_size);
};
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(static_cast<size_t>(1),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q1_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q2 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t12 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q2, dt)));
EXPECT_EQ(static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q2_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q3 =
R"(SELECT count(*) from bbox_intersect_t2 as b JOIN bbox_intersect_t2 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(2), v<int64_t>(run_simple_query(q3, dt)));
EXPECT_EQ(static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
auto q3_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto current_cache_size =
BoundingBoxIntersectJoinHashTable::getHashTableCache()
->getCurrentCacheSizeForDevice(CacheItemType::BBOX_INTERSECT_HT,
DataRecyclerUtil::CPU_DEVICE_IDENTIFIER);
EXPECT_EQ(static_cast<size_t>(416), current_cache_size);
}
{
clearCaches();
std::unordered_set<QueryPlanHash> key_set;
// Test 7. check whether we can recycle hash table for bbox intersect correctly for
// st_contain between mpoly and st_point
auto q1 =
R"(SELECT count(1) FROM bbox_intersect_t11 t2, bbox_intersect_t2 t1 WHERE st_contains(t1.mpoly, t2.pt);)";
auto q2 =
R"(SELECT count(1) FROM bbox_intersect_t12 t2, bbox_intersect_t3 t1 WHERE st_contains(t1.mpoly, t2.pt);)";
auto q3 =
R"(SELECT count(1) FROM bbox_intersect_t13 t2, bbox_intersect_t4 t1 WHERE st_contains(t1.mpoly, t2.pt);)";
run_simple_query(q1, dt);
auto q1_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q1_key = q1_ht_metrics->getQueryPlanHash();
key_set.insert(q1_key);
run_simple_query(q2, dt);
auto q2_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q2_key = q2_ht_metrics->getQueryPlanHash();
key_set.insert(q2_key);
run_simple_query(q3, dt);
auto q3_ht_metrics = getCachedHashTableMetric(visited_hashtable_key,
CacheItemType::BBOX_INTERSECT_HT);
auto q3_key = q3_ht_metrics->getQueryPlanHash();
key_set.insert(q3_key);
EXPECT_EQ(static_cast<size_t>(3),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
EXPECT_EQ(static_cast<size_t>(3), key_set.size());
run_simple_query(q2, dt);
run_simple_query(q2, dt);
run_simple_query(q3, dt);
run_simple_query(q3, dt);
run_simple_query(q3, dt);
run_simple_query(q3, dt);
auto q1_ref_cnt = q1_ht_metrics->getRefCount();
auto q2_ref_cnt = q2_ht_metrics->getRefCount();
auto q3_ref_cnt = q3_ht_metrics->getRefCount();
EXPECT_LT(q1_ref_cnt, q2_ref_cnt);
EXPECT_LT(q2_ref_cnt, q3_ref_cnt);
}
}
}
TEST(DataRecycler, Hashtable_For_BBox_Intersect_Reuse_Per_Parameter) {
ScopeGuard reset_state =
[orig_bbox_intersect_state = g_enable_bbox_intersect_hashjoin,
orig_hashjoin_many_to_many_state = g_enable_hashjoin_many_to_many,
orig_trivial_loop_join_threshold = g_trivial_loop_join_threshold] {
g_enable_bbox_intersect_hashjoin = orig_bbox_intersect_state;
g_enable_bbox_intersect_hashjoin = orig_hashjoin_many_to_many_state;
g_trivial_loop_join_threshold = orig_trivial_loop_join_threshold;
};
g_enable_bbox_intersect_hashjoin = true;
g_enable_hashjoin_many_to_many = true;
g_trivial_loop_join_threshold = 1;
std::set<QueryPlanHash> visited_hashtable_key;
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID).get();
auto clearCaches = [&executor, &visited_hashtable_key] {
executor->clearMemory(MemoryLevel::CPU_LEVEL);
executor->getQueryPlanDagCache().clearQueryPlanCache();
visited_hashtable_key.clear();
};
auto compareBucketDims = [](const std::vector<double>& rhs,
const std::vector<double>& lhs) {
if (rhs.size() != lhs.size()) {
return false;
}
for (size_t i = 0; i < rhs.size(); ++i) {
if (rhs[i] != lhs[i]) {
return false;
}
}
return true;
};
auto compareHTParams = [&compareBucketDims](
const std::optional<BoundingBoxIntersectMetaInfo> rhs,
const std::optional<BoundingBoxIntersectMetaInfo> lhs) {
return rhs.has_value() && lhs.has_value() &&
rhs->bbox_intersect_max_table_size_bytes ==
lhs->bbox_intersect_max_table_size_bytes &&
rhs->bbox_intersect_bucket_threshold == lhs->bbox_intersect_bucket_threshold &&
compareBucketDims(rhs->bucket_sizes, lhs->bucket_sizes);
};
for (auto dt : {ExecutorDeviceType::CPU}) {
// currently we do not support hashtable caching for GPU
// hashtables of t11, t12, t13: 208 bytes
// hashtable of t2: 416 bytes
// note that we do not compute bbox-intersect join hashtable params if given sql query
// contains bucket_threshold
// test1. run q1 with different bbox-intersect tuning param hint
// to see whether hashtable recycler utilizes the latest param
{
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(
static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::BBOX_INTERSECT_HT, true));
auto q1_ht_and_metainfo =
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
visited_hashtable_key);
auto q1_ht_metainfo = q1_ht_and_metainfo.cached_ht_metainfo;
EXPECT_TRUE(q1_ht_metainfo.has_value());
EXPECT_TRUE(q1_ht_metainfo->bbox_intersect_meta_info.has_value());
auto q1_tuning_param = q1_ht_and_metainfo.cached_tuning_info;
EXPECT_EQ(static_cast<size_t>(1), q1_ht_and_metainfo.cached_metric->getRefCount());
EXPECT_EQ(static_cast<size_t>(208), q1_ht_and_metainfo.cached_metric->getMemSize());
auto q1_v2 =
R"(SELECT /*+ bbox_intersect_bucket_threshold(0.718) */ count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1_v2, dt)));
EXPECT_EQ(static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
EXPECT_EQ(
static_cast<size_t>(3),
QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::BBOX_INTERSECT_HT, true));
auto q1_v2_ht_and_metainfo =
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
visited_hashtable_key);
auto q1_v2_ht_metainfo = q1_v2_ht_and_metainfo.cached_ht_metainfo;
EXPECT_TRUE(q1_v2_ht_metainfo.has_value());
EXPECT_TRUE(q1_v2_ht_metainfo->bbox_intersect_meta_info.has_value());
auto q1_v2_tuning_param = q1_v2_ht_and_metainfo.cached_tuning_info;
// we do not cache the tuning param if we give a related sql hint
EXPECT_TRUE(!q1_v2_tuning_param.has_value());
// due to the hint the same query has different hashtable params
EXPECT_TRUE(!compareHTParams(q1_ht_metainfo->bbox_intersect_meta_info,
q1_v2_ht_metainfo->bbox_intersect_meta_info));
auto q1_v3 =
R"(SELECT /*+ bbox_intersect_bucket_threshold(0.909), bbox_intersect_max_size(2021) */ count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1_v3, dt)));
EXPECT_EQ(static_cast<size_t>(3),
QR::get()->getNumberOfCachedItem(QueryRunner::CacheItemStatus::ALL,
CacheItemType::BBOX_INTERSECT_HT));
EXPECT_EQ(
static_cast<size_t>(4),
QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::BBOX_INTERSECT_HT, true));
auto q1_v3_ht_and_metainfo =
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
visited_hashtable_key);
auto q1_v3_ht_metainfo = q1_v3_ht_and_metainfo.cached_ht_metainfo;
EXPECT_TRUE(q1_v3_ht_metainfo.has_value());
EXPECT_TRUE(q1_v3_ht_metainfo->bbox_intersect_meta_info.has_value());
auto q1_v3_tuning_param = q1_v3_ht_and_metainfo.cached_tuning_info;
// we do not cache the tuning param if we give a related sql hint
EXPECT_TRUE(!q1_v3_tuning_param.has_value());
// due to the changes in the hint the same query has different hashtable params
EXPECT_TRUE(!compareHTParams(q1_v2_ht_metainfo->bbox_intersect_meta_info,
q1_v3_ht_metainfo->bbox_intersect_meta_info));
}
// test2. run q1 and then run q2 having different bbox_intersect
// ht params to see whether we keep the latest q2's bbox_intersect ht
{
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(
static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::BBOX_INTERSECT_HT, true));
auto q1_ht_and_metainfo =
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
visited_hashtable_key);
auto q1_ht_metainfo = q1_ht_and_metainfo.cached_ht_metainfo;
EXPECT_TRUE(q1_ht_metainfo.has_value());
EXPECT_TRUE(q1_ht_metainfo->bbox_intersect_meta_info.has_value());
auto q1_tuning_param = q1_ht_and_metainfo.cached_tuning_info;
EXPECT_EQ(static_cast<size_t>(1), q1_ht_and_metainfo.cached_metric->getRefCount());
EXPECT_EQ(static_cast<size_t>(208), q1_ht_and_metainfo.cached_metric->getMemSize());
auto q2 =
R"(SELECT count(*) from bbox_intersect_t13 as b JOIN bbox_intersect_t12 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q2, dt)));
EXPECT_EQ(
static_cast<size_t>(4),
QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::BBOX_INTERSECT_HT, true));
auto q2_ht_and_metainfo =
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
visited_hashtable_key);
auto q2_ht_metainfo = q2_ht_and_metainfo.cached_ht_metainfo;
EXPECT_TRUE(q2_ht_metainfo.has_value());
EXPECT_TRUE(q2_ht_metainfo->bbox_intersect_meta_info.has_value());
auto q2_tuning_param = q2_ht_and_metainfo.cached_tuning_info;
EXPECT_TRUE(q2_tuning_param.has_value());
auto q2_v2 =
R"(SELECT /*+ bbox_intersect_max_size(2021) */ count(*) from bbox_intersect_t13 as b JOIN bbox_intersect_t12 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q2_v2, dt)));
EXPECT_EQ(
static_cast<size_t>(6),
QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::BBOX_INTERSECT_HT, true));
auto q2_v2_ht_and_metainfo =
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
visited_hashtable_key);
auto q2_v2_ht_metainfo = q2_v2_ht_and_metainfo.cached_ht_metainfo;
EXPECT_TRUE(q2_v2_ht_metainfo.has_value());
EXPECT_TRUE(q2_v2_ht_metainfo->bbox_intersect_meta_info.has_value());
auto q2_v2_tuning_param = q2_v2_ht_and_metainfo.cached_tuning_info;
// we compute hashtable param when we give max_hashtable size hint
EXPECT_TRUE(q2_v2_tuning_param.has_value());
// we should have different meta info due to the updated ht when executing q2_v2
EXPECT_TRUE(!compareHTParams(q2_ht_metainfo->bbox_intersect_meta_info,
q2_v2_ht_metainfo->bbox_intersect_meta_info));
}
// test3. run q1 and then run q2 but make cache has limited space to
// see whether we invalidate ht cache but keep auto tuner param cache
{
const auto original_total_cache_size = g_hashtable_cache_total_bytes;
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, 250);
ScopeGuard reset_cache_status = [&original_total_cache_size] {
BoundingBoxIntersectJoinHashTable::getHashTableCache()->setTotalCacheSize(
CacheItemType::BBOX_INTERSECT_HT, original_total_cache_size);
};
clearCaches();
auto q1 =
R"(SELECT count(*) from bbox_intersect_t12 as b JOIN bbox_intersect_t11 as a ON ST_Intersects(a.poly, b.pt);)";
EXPECT_EQ(static_cast<int64_t>(1), v<int64_t>(run_simple_query(q1, dt)));
EXPECT_EQ(
static_cast<size_t>(2),
QR::get()->getNumberOfCachedItem(
QueryRunner::CacheItemStatus::ALL, CacheItemType::BBOX_INTERSECT_HT, true));
auto q1_ht_and_metainfo =
getCachedHashTableForBoundingBoxIntersectWithItsTuningParam(
visited_hashtable_key);
auto q1_ht_metainfo = q1_ht_and_metainfo.cached_ht_metainfo;