-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathQueryProcessor.hpp
215 lines (178 loc) · 5.21 KB
/
QueryProcessor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_QUERY_OPTIMIZER_QUERY_PROCESSOR_HPP_
#define QUICKSTEP_QUERY_OPTIMIZER_QUERY_PROCESSOR_HPP_
#include <cstddef>
#include <exception>
#include <memory>
#include <string>
#include <utility>
#include "catalog/Catalog.hpp"
#include "query_optimizer/Optimizer.hpp"
#include "utility/Macros.hpp"
namespace quickstep {
class CatalogDatabase;
class ParseStatement;
class QueryHandle;
/** \addtogroup QueryOptimizer
* @{
*/
/**
* @brief Exception thrown when unable to read the catalog from disk.
**/
class UnableToReadCatalog : public std::exception {
public:
/**
* @brief Constructor.
*
* @param filename The catalog filename provided.
**/
explicit UnableToReadCatalog(const std::string &filename)
: message_("UnableToReadCatalog: Unable to read catalog from file ") {
message_.append(filename);
}
~UnableToReadCatalog() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief Exception thrown when unable to write the catalog to disk.
**/
class UnableToWriteCatalog : public std::exception {
public:
/**
* @brief Constructor.
*
* @param filename The catalog filename provided.
**/
explicit UnableToWriteCatalog(const std::string &filename)
: message_("UnableToWriteCatalog: Unable to write catalog to file ") {
message_.append(filename);
}
~UnableToWriteCatalog() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief Exception thrown when there is a Proto parse error when loading the
* catalog.
**/
class CatalogNotProto : public std::exception {
public:
/**
* @brief Constructor.
*
* @param filename The catalog filename provided.
**/
explicit CatalogNotProto(const std::string &filename)
: message_("CatalogNotProto: The file ") {
message_.append(filename).append(" is not valid Proto");
}
~CatalogNotProto() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief An object which manages the state of quickstep and the execution of
* queries.
**/
class QueryProcessor {
public:
/**
* @brief Constructor.
*
* @param catalog_filename The file to read the serialized catalog from.
**/
explicit QueryProcessor(std::string &&catalog_filename) // NOLINT(whitespace/operators)
: catalog_filename_(std::move(catalog_filename)),
catalog_altered_(false),
query_id_(0) {
loadCatalog();
}
/**
* @brief Destructor.
**/
~QueryProcessor() {}
/**
* @brief Get the next query id.
**/
std::size_t query_id() const {
return query_id_;
}
/**
* @brief Fill a query handle for the given parsed SQL statement. This
* includes that the optimizer creates a QueryPlan inside the handle.
*
* @param statement The parsed SQL statement to generate a query handle for.
* @param query_handle The generated query handle to output.
**/
void generateQueryHandle(const ParseStatement &statement,
QueryHandle *query_handle);
/**
* @brief Find the reference base relations in the query and set them in the
* QueryHandle.
*
* @note This function does not fully optimize the query plan.
*
* @param statement The parsed SQL statement of the query.
* @param query_handle The QueryHandle of the given query.
*/
void findReferencedBaseRelationsInQuery(const ParseStatement &statement, QueryHandle *query_handle);
/**
* @brief Save the catalog back to disk.
**/
void saveCatalog();
/**
* @brief Set \p catalog_altered_ to true to indicate that the catalog
* has been altered.
*/
void markCatalogAltered() {
catalog_altered_ = true;
}
/**
* @brief Get the default database in the Catalog held by this
* QueryProcessor.
**/
CatalogDatabase* getDefaultDatabase() const {
return catalog_->getDatabaseByNameMutable("default");
}
private:
void loadCatalog(); // If it exists, free catalog_ before calling this
optimizer::Optimizer optimizer_;
const std::string catalog_filename_;
std::unique_ptr<Catalog> catalog_;
bool catalog_altered_;
std::size_t query_id_;
DISALLOW_COPY_AND_ASSIGN(QueryProcessor);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_QUERY_OPTIMIZER_QUERY_PROCESSOR_HPP_