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