OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 double running_query_check_freq = 0.9,
143  const unsigned pending_query_check_freq = 1000);
144  virtual std::vector<std::shared_ptr<ResultSet>> runMultipleStatements(
145  const std::string&,
146  const ExecutorDeviceType);
147  virtual QueryHint getParsedQueryHintofQuery(const std::string&);
148 
149  virtual void runImport(Parser::CopyTableStmt* import_stmt);
150  virtual std::unique_ptr<import_export::Loader> getLoader(
151  const TableDescriptor* td) const;
152 
153  const std::shared_ptr<std::vector<int32_t>>& getCachedJoinHashTable(size_t idx);
154  const int8_t* getCachedBaselineHashTable(size_t idx);
155  size_t getEntryCntCachedBaselineHashTable(size_t idx);
158 
159  void resizeDispatchQueue(const size_t num_executors);
160 
161  QueryRunner(std::unique_ptr<Catalog_Namespace::SessionInfo> session);
162 
163  virtual ~QueryRunner() = default;
164 
166 
167  template <typename... Ts>
168  static std::shared_ptr<query_state::QueryState> create_query_state(Ts&&... args) {
169  return query_states_.create(std::forward<Ts>(args)...);
170  }
171 
172  protected:
173  QueryRunner(const char* db_path,
174  const std::string& user,
175  const std::string& pass,
176  const std::string& db_name,
177  const std::vector<LeafHostInfo>& string_servers,
178  const std::vector<LeafHostInfo>& leaf_servers,
179  const std::string& udf_filename,
180  bool uses_gpus,
181  const size_t max_gpu_mem,
182  const int reserved_gpu_mem,
183  const bool create_user,
184  const bool create_db);
185 
186  static std::unique_ptr<QueryRunner> qr_instance_;
187 
188  std::shared_ptr<Catalog_Namespace::SessionInfo> session_info_;
189  std::unique_ptr<QueryDispatchQueue> dispatch_queue_;
190 };
191 
192 class ImportDriver : public QueryRunner {
193  public:
194  ImportDriver(std::shared_ptr<Catalog_Namespace::Catalog> cat,
197 
198  void importGeoTable(const std::string& file_path,
199  const std::string& table_name,
200  const bool compression,
201  const bool create_table,
202  const bool explode_collections);
203 };
204 
205 } // namespace QueryRunner
206 
207 #endif // QUERY_RUNNER_H
static query_state::QueryStates query_states_
Definition: QueryRunner.h:165
uint64_t getNumberOfCachedBaselineJoinHashTables()
std::shared_ptr< Catalog_Namespace::SessionInfo > getSession() const
Definition: QueryRunner.h:115
std::string cat(Ts &&...args)
std::unique_ptr< QueryDispatchQueue > dispatch_queue_
Definition: QueryRunner.h:189
virtual void clearGpuMemory() const
ExecutorDeviceType
const std::shared_ptr< std::vector< int32_t > > & getCachedJoinHashTable(size_t idx)
virtual std::vector< std::shared_ptr< ResultSet > > runMultipleStatements(const std::string &, const ExecutorDeviceType)
CircleBuffer::value_type create(ARGS &&...args)
Definition: QueryState.h:185
const int8_t * getCachedBaselineHashTable(size_t idx)
virtual QueryHint getParsedQueryHintofQuery(const std::string &)
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
virtual std::shared_ptr< ResultSet > runSQL(const std::string &query_str, const ExecutorDeviceType device_type, const bool hoist_literals=true, const bool allow_loop_joins=true)
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:56
ImportDriver(std::shared_ptr< Catalog_Namespace::Catalog > cat, const Catalog_Namespace::UserMetadata &user, const ExecutorDeviceType dt=ExecutorDeviceType::GPU)
This file contains the class specification and related data structures for SysCatalog.
virtual std::shared_ptr< ResultSet > runSQLWithAllowingInterrupt(const std::string &query_str, std::shared_ptr< Executor > executor, const std::string &session_id, const ExecutorDeviceType device_type, const double running_query_check_freq=0.9, const unsigned pending_query_check_freq=1000)
void init(LogOptions const &log_opts)
Definition: Logger.cpp:280
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:56
virtual std::unique_ptr< import_export::Loader > getLoader(const TableDescriptor *td) const
QueryRunner(std::unique_ptr< Catalog_Namespace::SessionInfo > session)
virtual std::shared_ptr< ExecutionResult > runSelectQuery(const std::string &query_str, const ExecutorDeviceType device_type, const bool hoist_literals, const bool allow_loop_joins, const bool just_explain=false)
virtual void runImport(Parser::CopyTableStmt *import_stmt)
size_t getEntryCntCachedBaselineHashTable(size_t idx)
virtual void runDDLStatement(const std::string &)
static std::unique_ptr< QueryRunner > qr_instance_
Definition: QueryRunner.h:186
void importGeoTable(const std::string &file_path, const std::string &table_name, const bool compression, const bool create_table, const bool explode_collections)
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:57
std::shared_ptr< Executor > getExecutor() const
virtual void clearCpuMemory() const
uint64_t getNumberOfCachedJoinHashTables()
std::shared_ptr< Catalog_Namespace::SessionInfo > session_info_
Definition: QueryRunner.h:188
static std::shared_ptr< query_state::QueryState > create_query_state(Ts &&...args)
Definition: QueryRunner.h:168
std::shared_ptr< Calcite > getCalcite() const
void resizeDispatchQueue(const size_t num_executors)
static QueryRunner * init(std::unique_ptr< Catalog_Namespace::SessionInfo > &session)
Definition: QueryRunner.h:101
std::shared_ptr< Catalog_Namespace::Catalog > getCatalog() const
specifies the content in-memory of a row in the table metadata table
virtual ~QueryRunner()=default