OmniSciDB  2e3a973ef4
DBEngine.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 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 "DBEngine.h"
18 #include <boost/filesystem.hpp>
19 #include <boost/variant.hpp>
20 #include <iostream>
21 #include "Catalog/Catalog.h"
22 #include "Logger/Logger.h"
24 #include "QueryEngine/ResultSet.h"
26 #include "Shared/sqltypes.h"
27 
28 namespace EmbeddedDatabase {
29 
30 enum class ColumnType : uint32_t { Unknown, Integer, Double, Float, String, Array };
31 
35 class CursorImpl : public Cursor {
36  public:
37  CursorImpl(std::shared_ptr<ResultSet> result_set,
38  std::shared_ptr<Data_Namespace::DataMgr> data_mgr)
39  : result_set_(result_set), data_mgr_(data_mgr) {}
40 
41  size_t getColCount() { return result_set_->colCount(); }
42 
43  size_t getRowCount() { return result_set_->rowCount(); }
44 
46  auto row = result_set_->getNextRow(true, false);
47  if (row.empty()) {
48  return Row();
49  }
50  return Row(row);
51  }
52 
53  ColumnType getColType(uint32_t col_num) {
54  if (col_num < getColCount()) {
55  SQLTypeInfo type_info = result_set_->getColType(col_num);
56  switch (type_info.get_type()) {
57  case kNUMERIC:
58  case kDECIMAL:
59  case kINT:
60  case kSMALLINT:
61  case kBIGINT:
62  case kTINYINT:
63  return ColumnType::Integer;
64 
65  case kDOUBLE:
66  return ColumnType::Double;
67 
68  case kFLOAT:
69  return ColumnType::Float;
70 
71  case kCHAR:
72  case kVARCHAR:
73  case kTEXT:
74  return ColumnType::String;
75 
76  default:
77  return ColumnType::Unknown;
78  }
79  }
80  return ColumnType::Unknown;
81  }
82 
83  private:
84  std::shared_ptr<ResultSet> result_set_;
85  std::weak_ptr<Data_Namespace::DataMgr> data_mgr_;
86 };
87 
91 class DBEngineImpl : public DBEngine {
92  public:
93  // TODO: Remove all that hardcoded settings
94  const int CALCITEPORT = 3279;
95  const std::string OMNISCI_DEFAULT_DB = "omnisci";
96  const std::string OMNISCI_ROOT_USER = "admin";
97  const std::string OMNISCI_DATA_PATH = "//mapd_data";
98 
99  void reset() {
100  // TODO: Destroy all cursors in the cursors_
101  if (query_runner_ != nullptr) {
102  query_runner_->reset();
103  }
104  }
105 
106  void executeDDL(const std::string& query) {
107  if (query_runner_ != nullptr) {
108  query_runner_->runDDLStatement(query);
109  }
110  }
111 
112  Cursor* executeDML(const std::string& query) {
113  if (query_runner_ != nullptr) {
114  auto rs = query_runner_->runSQL(query, ExecutorDeviceType::CPU);
115  cursors_.emplace_back(new CursorImpl(rs, data_mgr_));
116  return cursors_.back();
117  }
118  return nullptr;
119  }
120 
121  DBEngineImpl(const std::string& base_path)
122  : base_path_(base_path), query_runner_(nullptr) {
123  if (!boost::filesystem::exists(base_path_)) {
124  std::cerr << "Catalog basepath " + base_path_ + " does not exist.\n";
125  // TODO: Create database if it does not exist
126  } else {
127  SystemParameters system_parameters;
128  std::string data_path = base_path_ + OMNISCI_DATA_PATH;
129  data_mgr_ = std::make_shared<Data_Namespace::DataMgr>(
130  data_path, system_parameters, false, 0);
131  auto calcite = std::make_shared<Calcite>(-1, CALCITEPORT, base_path_, 1024, 5000);
132  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
133  sys_cat.init(base_path_, data_mgr_, {}, calcite, false, false, {});
134  if (!sys_cat.getSqliteConnector()) {
135  std::cerr << "SqliteConnector is null " << std::endl;
136  } else {
137  sys_cat.getMetadataForDB(OMNISCI_DEFAULT_DB, database_); // TODO: Check
138  auto catalog = Catalog_Namespace::Catalog::get(base_path_,
139  database_,
140  data_mgr_,
141  std::vector<LeafHostInfo>(),
142  calcite,
143  false);
144  sys_cat.getMetadataForUser(OMNISCI_ROOT_USER, user_);
145  auto session = std::make_unique<Catalog_Namespace::SessionInfo>(
146  catalog, user_, ExecutorDeviceType::CPU, "");
147  query_runner_ = QueryRunner::QueryRunner::init(session);
148  }
149  }
150  }
151 
152  private:
153  std::string base_path_;
154  std::shared_ptr<Data_Namespace::DataMgr> data_mgr_;
158  std::vector<CursorImpl*> cursors_;
159 };
160 
161 /********************************************* DBEngine external methods*/
162 
168 DBEngine* DBEngine::create(std::string path) {
169  return new DBEngineImpl(path);
170 }
171 
174  return (DBEngineImpl*)ptr;
175 }
176 inline const DBEngineImpl* getImpl(const DBEngine* ptr) {
177  return (const DBEngineImpl*)ptr;
178 }
179 
181  // TODO: Make sure that dbengine does not released twice
182  DBEngineImpl* engine = getImpl(this);
183  engine->reset();
184 }
185 
186 void DBEngine::executeDDL(std::string query) {
187  DBEngineImpl* engine = getImpl(this);
188  engine->executeDDL(query);
189 }
190 
191 Cursor* DBEngine::executeDML(std::string query) {
192  DBEngineImpl* engine = getImpl(this);
193  return engine->executeDML(query);
194 }
195 
196 /********************************************* Row methods */
197 
199 
200 Row::Row(std::vector<TargetValue>& row) : row_(std::move(row)) {}
201 
202 int64_t Row::getInt(size_t col_num) {
203  if (col_num < row_.size()) {
204  const auto scalar_value = boost::get<ScalarTargetValue>(&row_[col_num]);
205  const auto value = boost::get<int64_t>(scalar_value);
206  return *value;
207  }
208  return 0;
209 }
210 
211 double Row::getDouble(size_t col_num) {
212  if (col_num < row_.size()) {
213  const auto scalar_value = boost::get<ScalarTargetValue>(&row_[col_num]);
214  const auto value = boost::get<double>(scalar_value);
215  return *value;
216  }
217  return 0.;
218 }
219 
220 std::string Row::getStr(size_t col_num) {
221  if (col_num < row_.size()) {
222  const auto scalar_value = boost::get<ScalarTargetValue>(&row_[col_num]);
223  auto value = boost::get<NullableString>(scalar_value);
224  bool is_null = !value || boost::get<void*>(value);
225  if (is_null) {
226  return "Empty";
227  } else {
228  auto value_notnull = boost::get<std::string>(value);
229  return *value_notnull;
230  }
231  }
232  return "Out of range";
233 }
234 
235 /********************************************* Cursor external methods*/
236 
238 inline CursorImpl* getImpl(Cursor* ptr) {
239  return (CursorImpl*)ptr;
240 }
241 inline const CursorImpl* getImpl(const Cursor* ptr) {
242  return (const CursorImpl*)ptr;
243 }
244 
246  CursorImpl* cursor = getImpl(this);
247  return cursor->getColCount();
248 }
249 
251  CursorImpl* cursor = getImpl(this);
252  return cursor->getRowCount();
253 }
254 
256  CursorImpl* cursor = getImpl(this);
257  return cursor->getNextRow();
258 }
259 
260 int Cursor::getColType(uint32_t col_num) {
261  CursorImpl* cursor = getImpl(this);
262  return (int)cursor->getColType(col_num);
263 }
264 } // namespace EmbeddedDatabase
std::string getStr(size_t col_num)
Definition: DBEngine.cpp:220
std::vector< CursorImpl * > cursors_
Definition: DBEngine.cpp:158
double getDouble(size_t col_num)
Definition: DBEngine.cpp:211
Cursor * executeDML(const std::string &query)
Definition: DBEngine.cpp:112
static std::shared_ptr< Catalog > get(const std::string &dbName)
Definition: Catalog.cpp:3753
void executeDDL(std::string query)
Definition: DBEngine.cpp:186
void executeDDL(const std::string &query)
Definition: DBEngine.cpp:106
Constants for Builtin SQL Types supported by OmniSci.
ColumnType getColType(uint32_t col_num)
Definition: DBEngine.cpp:53
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:61
int64_t getInt(size_t col_num)
Definition: DBEngine.cpp:202
CursorImpl(std::shared_ptr< ResultSet > result_set, std::shared_ptr< Data_Namespace::DataMgr > data_mgr)
Definition: DBEngine.cpp:37
This file contains the class specification and related data structures for Catalog.
static SysCatalog & instance()
Definition: SysCatalog.h:286
std::shared_ptr< ResultSet > result_set_
Definition: DBEngine.cpp:84
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:56
static DBEngine * create(std::string path)
Definition: DBEngine.cpp:168
std::weak_ptr< Data_Namespace::DataMgr > data_mgr_
Definition: DBEngine.cpp:85
Catalog_Namespace::UserMetadata user_
Definition: DBEngine.cpp:156
DBEngineImpl(const std::string &base_path)
Definition: DBEngine.cpp:121
Definition: sqltypes.h:54
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:57
bool is_null(const T &v, const SQLTypeInfo &t)
std::vector< TargetValue > row_
Definition: DBEngine.h:37
int getColType(uint32_t col_num)
Definition: DBEngine.cpp:260
#define CALCITEPORT
Definition: initdb.cpp:32
Definition: sqltypes.h:43
QueryRunner::QueryRunner * query_runner_
Definition: DBEngine.cpp:157
Catalog_Namespace::DBMetadata database_
Definition: DBEngine.cpp:155
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
DBEngineImpl * getImpl(DBEngine *ptr)
Definition: DBEngine.cpp:173
Basic constructors and methods of the row set interface.
Definition: sqltypes.h:47
std::shared_ptr< Data_Namespace::DataMgr > data_mgr_
Definition: DBEngine.cpp:154
Cursor * executeDML(std::string query)
Definition: DBEngine.cpp:191