OmniSciDB  a47db9e897
 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 <string>
23 
24 #include "Catalog/SessionInfo.h"
25 #include "Catalog/SysCatalog.h"
27 #include "LeafAggregator.h"
30 
31 namespace Catalog_Namespace {
32 class Catalog;
33 struct UserMetadata;
34 } // namespace Catalog_Namespace
35 
36 class ResultSet;
37 class ExecutionResult;
38 
39 namespace Planner {
40 class RootPlan;
41 }
42 
43 namespace Parser {
44 class CopyTableStmt;
45 }
46 
48 
49 namespace Importer_NS {
50 class Loader;
51 }
52 
53 class Calcite;
54 
55 namespace QueryRunner {
56 
57 struct IRFileWriter {
58  IRFileWriter(const std::string& filename) : filename(filename) {
59  ofs.open(filename, std::ios::trunc);
60  }
61  ~IRFileWriter() { ofs.close(); }
62  std::string filename;
63  std::ofstream ofs;
64 
65  void operator()(const std::string& query_str, const std::string& ir_str) {
66  ofs << query_str << "\n\n" << ir_str << "\n\n";
67  }
68 };
69 
70 class QueryRunner {
71  public:
72  static QueryRunner* init(const char* db_path,
73  const std::string& udf_filename = "",
74  const size_t max_gpu_mem = 0, // use all available mem
75  const int reserved_gpu_mem = 256 << 20) {
76  return QueryRunner::init(db_path,
77  std::string{OMNISCI_ROOT_USER},
78  "HyperInteractive",
79  std::string{OMNISCI_DEFAULT_DB},
80  {},
81  {},
82  udf_filename,
83  true,
84  max_gpu_mem,
85  reserved_gpu_mem);
86  }
87 
88  static QueryRunner* init(const char* db_path,
89  const std::vector<LeafHostInfo>& string_servers,
90  const std::vector<LeafHostInfo>& leaf_servers) {
91  return QueryRunner::init(db_path,
92  std::string{OMNISCI_ROOT_USER},
93  "HyperInteractive",
94  std::string{OMNISCI_DEFAULT_DB},
95  string_servers,
96  leaf_servers);
97  }
98 
99  static QueryRunner* init(const char* db_path,
100  const std::string& user,
101  const std::string& pass,
102  const std::string& db_name,
103  const std::vector<LeafHostInfo>& string_servers,
104  const std::vector<LeafHostInfo>& leaf_servers,
105  const std::string& udf_filename = "",
106  bool uses_gpus = true,
107  const size_t max_gpu_mem = 0, // use all available mem
108  const int reserved_gpu_mem = 256 << 20,
109  const bool create_user = false,
110  const bool create_db = false);
111 
112  static QueryRunner* init(std::unique_ptr<Catalog_Namespace::SessionInfo>& session) {
113  qr_instance_.reset(new QueryRunner(std::move(session)));
114  return qr_instance_.get();
115  }
116 
117  static QueryRunner* get() {
118  if (!qr_instance_) {
119  throw std::runtime_error("QueryRunner must be initialized before calling get().");
120  }
121  return qr_instance_.get();
122  }
123 
124  static void reset();
125 
126  std::shared_ptr<Catalog_Namespace::SessionInfo> getSession() const {
127  return session_info_;
128  }
129  std::shared_ptr<Catalog_Namespace::Catalog> getCatalog() const;
130  std::shared_ptr<Calcite> getCalcite() const;
131 
132  bool gpusPresent() const;
133  virtual void clearGpuMemory() const;
134  virtual void clearCpuMemory() const;
135 
136  virtual void runDDLStatement(const std::string&);
137  virtual std::shared_ptr<ResultSet> runSQL(const std::string& query_str,
138  const ExecutorDeviceType device_type,
139  const bool hoist_literals = true,
140  const bool allow_loop_joins = true);
141  virtual ExecutionResult runSelectQuery(const std::string& query_str,
142  const ExecutorDeviceType device_type,
143  const bool hoist_literals,
144  const bool allow_loop_joins,
145  const bool just_explain = false);
146  virtual std::vector<std::shared_ptr<ResultSet>> runMultipleStatements(
147  const std::string&,
148  const ExecutorDeviceType);
149 
150  virtual void runImport(Parser::CopyTableStmt* import_stmt);
151  virtual std::unique_ptr<Importer_NS::Loader> getLoader(const TableDescriptor* td) const;
152 
153  virtual void setIRFilename(const std::string& filename) {
154  ir_file_writer_ = std::make_unique<IRFileWriter>(filename);
155  }
156 
157  virtual ~QueryRunner() {}
158 
159  QueryRunner(std::unique_ptr<Catalog_Namespace::SessionInfo> session);
160 
162 
163  template <typename... Ts>
164  static std::shared_ptr<query_state::QueryState> create_query_state(Ts&&... args) {
165  return query_states_.create(std::forward<Ts>(args)...);
166  }
167 
168  protected:
169  QueryRunner(const char* db_path,
170  const std::string& user,
171  const std::string& pass,
172  const std::string& db_name,
173  const std::vector<LeafHostInfo>& string_servers,
174  const std::vector<LeafHostInfo>& leaf_servers,
175  const std::string& udf_filename,
176  bool uses_gpus,
177  const size_t max_gpu_mem,
178  const int reserved_gpu_mem,
179  const bool create_user,
180  const bool create_db);
181 
182  Planner::RootPlan* parsePlanLegacy(const std::string& query_str);
185 
186  static std::unique_ptr<QueryRunner> qr_instance_;
187 
188  std::shared_ptr<Catalog_Namespace::SessionInfo> session_info_;
189 
190  private:
191  std::unique_ptr<IRFileWriter> ir_file_writer_;
192 };
193 
194 } // namespace QueryRunner
195 
196 #endif // QUERY_RUNNER_H
static query_state::QueryStates query_states_
Definition: QueryRunner.h:161
virtual 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)
std::string filename(char const *path)
Definition: Logger.cpp:58
std::shared_ptr< Catalog_Namespace::SessionInfo > getSession() const
Definition: QueryRunner.h:126
virtual void clearGpuMemory() const
ExecutorDeviceType
virtual std::vector< std::shared_ptr< ResultSet > > runMultipleStatements(const std::string &, const ExecutorDeviceType)
std::unique_ptr< IRFileWriter > ir_file_writer_
Definition: QueryRunner.h:191
CircleBuffer::value_type create(ARGS &&...args)
Definition: QueryState.h:185
static QueryRunner * init(const char *db_path, const std::vector< LeafHostInfo > &string_servers, const std::vector< LeafHostInfo > &leaf_servers)
Definition: QueryRunner.h:88
Planner::RootPlan * parsePlanCalcite(QueryStateProxy)
Definition: ParserUtils.cpp:48
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:72
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:91
Planner::RootPlan * parsePlan(QueryStateProxy)
Definition: ParserUtils.cpp:69
This file contains the class specification and related data structures for SysCatalog.
void init(LogOptions const &log_opts)
Definition: Logger.cpp:265
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:58
IRFileWriter(const std::string &filename)
Definition: QueryRunner.h:58
QueryRunner(std::unique_ptr< Catalog_Namespace::SessionInfo > session)
virtual void runImport(Parser::CopyTableStmt *import_stmt)
virtual void runDDLStatement(const std::string &)
static std::unique_ptr< QueryRunner > qr_instance_
Definition: QueryRunner.h:186
Planner::RootPlan * parsePlanLegacy(const std::string &query_str)
Definition: ParserUtils.cpp:29
virtual void setIRFilename(const std::string &filename)
Definition: QueryRunner.h:153
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:59
virtual void clearCpuMemory() const
std::shared_ptr< Catalog_Namespace::SessionInfo > session_info_
Definition: QueryRunner.h:188
virtual std::unique_ptr< Importer_NS::Loader > getLoader(const TableDescriptor *td) const
static std::shared_ptr< query_state::QueryState > create_query_state(Ts &&...args)
Definition: QueryRunner.h:164
std::shared_ptr< Calcite > getCalcite() const
void operator()(const std::string &query_str, const std::string &ir_str)
Definition: QueryRunner.h:65
static QueryRunner * init(std::unique_ptr< Catalog_Namespace::SessionInfo > &session)
Definition: QueryRunner.h:112
std::shared_ptr< Catalog_Namespace::Catalog > getCatalog() const
specifies the content in-memory of a row in the table metadata table
static unsigned pass
Definition: testing.h:29