OmniSciDB  04ee39c94c
JoinHashTableTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 #include <boost/filesystem.hpp>
19 #include <boost/filesystem/operations.hpp>
20 #include <boost/program_options.hpp>
21 #include <csignal>
22 #include <exception>
23 #include <memory>
24 #include <vector>
25 #include "Catalog/Catalog.h"
26 #include "Catalog/DBObject.h"
27 #include "DataMgr/DataMgr.h"
28 #include "QueryEngine/Execute.h"
31 #include "QueryEngine/ResultSet.h"
34 #include "Shared/Logger.h"
35 #include "Shared/MapDParameters.h"
36 #include "TestHelpers.h"
37 
38 namespace po = boost::program_options;
39 
40 #ifndef BASE_PATH
41 #define BASE_PATH "./tmp"
42 #endif
43 
44 constexpr size_t c_calcite_port = 36279;
45 
46 using namespace Catalog_Namespace;
47 using namespace TestHelpers;
48 
50 
51 namespace {
53 }
54 
55 inline auto sql(const std::string& sql_stmts) {
56  return QR::get()->runMultipleStatements(sql_stmts, g_device_type);
57 }
58 
60  const ExecutorDeviceType device_type) {
61  if (device_type == ExecutorDeviceType::GPU) {
62  const auto cuda_mgr = catalog->getDataMgr().getCudaMgr();
63  CHECK(cuda_mgr);
64  return cuda_mgr->getDeviceCount();
65  } else {
66  return 1;
67  }
68 }
69 
72 
73  sql(R"(
74  drop table if exists my_points;
75  drop table if exists my_grid;
76  )");
77 
78  sql(R"(
79  create table my_points (locations geometry(point, 4326) encoding none);
80  create table my_grid (cells geometry(multipolygon, 4326) encoding none);
81 
82  insert into my_points values ('point(5 5)');
83  insert into my_points values ('point(5 25)');
84  insert into my_points values ('point(10 5)');
85 
86  insert into my_grid values ('multipolygon(((0 0,10 0,10 10,0 10,0 0)))');
87  insert into my_grid values ('multipolygon(((10 0,20 0,20 10,10 10,10 0)))');
88  insert into my_grid values ('multipolygon(((0 10,10 10,10 20,0 20,0 10)))');
89  insert into my_grid values ('multipolygon(((10 10,20 10,20 20,10 20,10 10)))');
90  )");
91 
92  auto catalog = QR::get()->getCatalog();
93  auto executor = Executor::getExecutor(catalog->getCurrentDB().dbId);
94 
95  auto tmeta1 = catalog->getMetadataForTable("my_points");
96  auto tmeta2 = catalog->getMetadataForTable("my_grid");
97 
98  SQLTypeInfo ti1{kPOINT, 4326, 4326, false, kENCODING_NONE, 64, kGEOMETRY};
99  SQLTypeInfo ti2{kARRAY, 0, 0, true, kENCODING_NONE, 0, kDOUBLE};
100  ti2.set_size(32);
101 
102  auto a1 = std::make_shared<Analyzer::ColumnVar>(ti1, tmeta1->tableId, 1, 0);
103  auto a2 = std::make_shared<Analyzer::ColumnVar>(ti2, tmeta2->tableId, 5, 1);
104 
105  auto op = std::make_shared<Analyzer::BinOper>(kBOOLEAN, kOVERLAPS, kONE, a1, a2);
106 
107  size_t number_of_join_tables{2};
108  std::vector<InputTableInfo> viti(number_of_join_tables);
109  viti[0].table_id = tmeta1->tableId;
110  viti[0].info = tmeta1->fragmenter->getFragmentsForQuery();
111  viti[1].table_id = tmeta2->tableId;
112  viti[1].info = tmeta2->fragmenter->getFragmentsForQuery();
113 
114  auto memory_level = Data_Namespace::CPU_LEVEL;
115 
116  auto device_count = deviceCount(catalog.get(), g_device_type);
117 
118  ColumnCacheMap ccm;
119 
120  auto hash_table = OverlapsJoinHashTable::getInstance(
121  op, viti, memory_level, device_count, ccm, executor.get());
122 
123  auto ptr1 =
124  reinterpret_cast<const int32_t*>(hash_table->getJoinHashBuffer(g_device_type, 0));
125  auto ptr2 = reinterpret_cast<const int32_t*>(
126  hash_table->getJoinHashBuffer(g_device_type, 0) + hash_table->offsetBufferOff());
127  CHECK_EQ(*ptr1, -1);
128  CHECK_EQ(*ptr2, -1);
129 }
130 
131 int main(int argc, char** argv) {
132  testing::InitGoogleTest(&argc, argv);
133 
134  logger::LogOptions log_options(argv[0]);
135  log_options.severity_ = logger::Severity::DEBUG1;
136  logger::init(log_options);
137 
139 
140  int err{0};
141  try {
142  err = RUN_ALL_TESTS();
143  } catch (const std::exception& e) {
144  LOG(ERROR) << e.what();
145  }
146  QR::reset();
147  return err;
148 }
TEST(Memcmp, OverlapsJoinHashTable)
#define CHECK_EQ(x, y)
Definition: Logger.h:195
static std::shared_ptr< Executor > getExecutor(const int db_id, const std::string &debug_dir="", const std::string &debug_file="", const MapDParameters mapd_parameters=MapDParameters(), ::QueryRenderer::QueryRenderManager *render_manager=nullptr)
Definition: Execute.cpp:122
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
ExecutorDeviceType
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:177
#define LOG(tag)
Definition: Logger.h:182
auto sql(const std::string &sql_stmts)
constexpr size_t c_calcite_port
External interface for parsing AST and bitcode files.
virtual std::vector< std::shared_ptr< ResultSet > > runMultipleStatements(const std::string &, const ExecutorDeviceType)
static std::shared_ptr< OverlapsJoinHashTable > getInstance(const std::shared_ptr< Analyzer::BinOper > condition, const std::vector< InputTableInfo > &query_infos, const Data_Namespace::MemoryLevel memory_level, const int device_count, ColumnCacheMap &column_map, Executor *executor)
static QueryRunner * init(const char *db_path, const std::string &udf_filename="", const size_t max_gpu_mem=0, const int reserved_gpu_mem=256<< 20)
Definition: QueryRunner.h:70
This file contains the class specification and related data structures for Catalog.
void init(LogOptions const &log_opts)
Definition: Logger.cpp:260
std::shared_ptr< Catalog_Namespace::Catalog > getCatalog() const
static QueryRunner * get()
Definition: QueryRunner.h:115
Definition: sqldefs.h:69
int main(int argc, char **argv)
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:116
Severity severity_
Definition: Logger.h:113
#define CHECK(condition)
Definition: Logger.h:187
#define BASE_PATH
std::unordered_map< int, std::unordered_map< int, std::shared_ptr< const ColumnarResults > > > ColumnCacheMap
Basic constructors and methods of the row set interface.
int deviceCount(const Catalog_Namespace::Catalog *catalog, const ExecutorDeviceType device_type)