OmniSciDB  8a228a1076
QueryRunner.h
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 #ifndef QUERY_RUNNER_H
18 #define QUERY_RUNNER_H
19 
20 #include <fstream>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 
25 #include "Catalog/SessionInfo.h"
26 #include "Catalog/SysCatalog.h"
28 #include "LeafAggregator.h"
34 #include "QueryEngine/QueryHint.h"
36 
37 namespace Catalog_Namespace {
38 class Catalog;
39 struct UserMetadata;
40 } // namespace Catalog_Namespace
41 
42 class ResultSet;
43 class ExecutionResult;
44 
45 namespace Parser {
46 class CopyTableStmt;
47 }
48 
50 
51 namespace import_export {
52 class Loader;
53 }
54 
55 class Calcite;
56 
57 namespace QueryRunner {
58 
59 class QueryRunner {
60  public:
61  static QueryRunner* init(const char* db_path,
62  const std::string& udf_filename = "",
63  const size_t max_gpu_mem = 0, // use all available mem
64  const int reserved_gpu_mem = 256 << 20) {
65  return QueryRunner::init(db_path,
66  std::string{OMNISCI_ROOT_USER},
67  "HyperInteractive",
68  std::string{OMNISCI_DEFAULT_DB},
69  {},
70  {},
71  udf_filename,
72  true,
73  max_gpu_mem,
74  reserved_gpu_mem);
75  }
76 
77  static QueryRunner* init(const char* db_path,
78  const std::vector<LeafHostInfo>& string_servers,
79  const std::vector<LeafHostInfo>& leaf_servers) {
80  return QueryRunner::init(db_path,
81  std::string{OMNISCI_ROOT_USER},
82  "HyperInteractive",
83  std::string{OMNISCI_DEFAULT_DB},
84  string_servers,
85  leaf_servers);
86  }
87 
88  static QueryRunner* init(const char* db_path,
89  const std::string& user,
90  const std::string& pass,
91  const std::string& db_name,
92  const std::vector<LeafHostInfo>& string_servers,
93  const std::vector<LeafHostInfo>& leaf_servers,
94  const std::string& udf_filename = "",
95  bool uses_gpus = true,
96  const size_t max_gpu_mem = 0, // use all available mem
97  const int reserved_gpu_mem = 256 << 20,
98  const bool create_user = false,
99  const bool create_db = false);
100 
101  static QueryRunner* init(std::unique_ptr<Catalog_Namespace::SessionInfo>& session) {
102  qr_instance_.reset(new QueryRunner(std::move(session)));
103  return qr_instance_.get();
104  }
105 
106  static QueryRunner* get() {
107  if (!qr_instance_) {
108  throw std::runtime_error("QueryRunner must be initialized before calling get().");
109  }
110  return qr_instance_.get();
111  }
112 
113  static void reset();
114 
115  std::shared_ptr<Catalog_Namespace::SessionInfo> getSession() const {
116  return session_info_;
117  }
118  std::shared_ptr<Catalog_Namespace::Catalog> getCatalog() const;
119  std::shared_ptr<Calcite> getCalcite() const;
120  std::shared_ptr<Executor> getExecutor() const;
121 
122  bool gpusPresent() const;
123  virtual void clearGpuMemory() const;
124  virtual void clearCpuMemory() const;
125 
126  virtual void runDDLStatement(const std::string&);
127  virtual std::shared_ptr<ResultSet> runSQL(const std::string& query_str,
128  const ExecutorDeviceType device_type,
129  const bool hoist_literals = true,
130  const bool allow_loop_joins = true);
131  virtual std::shared_ptr<ExecutionResult> runSelectQuery(
132  const std::string& query_str,
133  const ExecutorDeviceType device_type,
134  const bool hoist_literals,
135  const bool allow_loop_joins,
136  const bool just_explain = false);
137  virtual std::shared_ptr<ResultSet> runSQLWithAllowingInterrupt(
138  const std::string& query_str,
139  std::shared_ptr<Executor> executor,
140  const std::string& session_id,
141  const ExecutorDeviceType device_type,
142  const unsigned interrupt_check_freq = 1000);
143  virtual std::vector<std::shared_ptr<ResultSet>> runMultipleStatements(
144  const std::string&,
145  const ExecutorDeviceType);
146  virtual QueryHint getParsedQueryHintofQuery(const std::string&);
147 
148  virtual void runImport(Parser::CopyTableStmt* import_stmt);
149  virtual std::unique_ptr<import_export::Loader> getLoader(
150  const TableDescriptor* td) const;
151 
152  const std::shared_ptr<std::vector<int32_t>>& getCachedJoinHashTable(size_t idx);
153  const std::shared_ptr<std::vector<int8_t>>& getCachedBaselineHashTable(size_t idx);
154  size_t getEntryCntCachedBaselineHashTable(size_t idx);
155  uint64_t getNumberOfCachedJoinHashTables();
156  uint64_t getNumberOfCachedBaselineJoinHashTables();
157 
158  void resizeDispatchQueue(const size_t num_executors);
159 
160  QueryRunner(std::unique_ptr<Catalog_Namespace::SessionInfo> session);
161 
162  virtual ~QueryRunner() = default;
163 
165 
166  template <typename... Ts>
167  static std::shared_ptr<query_state::QueryState> create_query_state(Ts&&... args) {
168  return query_states_.create(std::forward<Ts>(args)...);
169  }
170 
171  protected:
172  QueryRunner(const char* db_path,
173  const std::string& user,
174  const std::string& pass,
175  const std::string& db_name,
176  const std::vector<LeafHostInfo>& string_servers,
177  const std::vector<LeafHostInfo>& leaf_servers,
178  const std::string& udf_filename,
179  bool uses_gpus,
180  const size_t max_gpu_mem,
181  const int reserved_gpu_mem,
182  const bool create_user,
183  const bool create_db);
184 
185  static std::unique_ptr<QueryRunner> qr_instance_;
186 
187  std::shared_ptr<Catalog_Namespace::SessionInfo> session_info_;
188  std::unique_ptr<QueryDispatchQueue> dispatch_queue_;
189 };
190 
191 class ImportDriver : public QueryRunner {
192  public:
193  ImportDriver(std::shared_ptr<Catalog_Namespace::Catalog> cat,
196 
197  void importGeoTable(const std::string& file_path,
198  const std::string& table_name,
199  const bool compression,
200  const bool create_table,
201  const bool explode_collections);
202 };
203 
204 } // namespace QueryRunner
205 
206 #endif // QUERY_RUNNER_H
static query_state::QueryStates query_states_
Definition: QueryRunner.h:164
std::unique_ptr< QueryDispatchQueue > dispatch_queue_
Definition: QueryRunner.h:188
ExecutorDeviceType
CircleBuffer::value_type create(ARGS &&... args)
Definition: QueryState.h:185
static std::shared_ptr< query_state::QueryState > create_query_state(Ts &&... args)
Definition: QueryRunner.h:167
static QueryRunner * init(const char *db_path, const std::vector< LeafHostInfo > &string_servers, const std::vector< LeafHostInfo > &leaf_servers)
Definition: QueryRunner.h:77
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
ResultSet(const std::vector< TargetInfo > &targets, const ExecutorDeviceType device_type, const QueryMemoryDescriptor &query_mem_desc, const std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner, const Executor *executor)
Definition: ResultSet.cpp:104
This file contains the class specification and related data structures for SysCatalog.
std::string cat(Ts &&... args)
void init(LogOptions const &log_opts)
Definition: Logger.cpp:280
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:58
static std::unique_ptr< QueryRunner > qr_instance_
Definition: QueryRunner.h:185
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:59
Definition: Catalog.h:63
std::shared_ptr< Catalog_Namespace::SessionInfo > session_info_
Definition: QueryRunner.h:187
std::shared_ptr< Catalog_Namespace::SessionInfo > getSession() const
Definition: QueryRunner.h:115
static QueryRunner * init(std::unique_ptr< Catalog_Namespace::SessionInfo > &session)
Definition: QueryRunner.h:101
specifies the content in-memory of a row in the table metadata table