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
#pragma once
#include <string>
#include "common/api.h"
#include "common/arrow/arrow.h"
#include "common/database_lifecycle_manager.h"
#include "common/types/types.h"
#include "query_summary.h"
namespace lbug {
namespace processor {
class FlatTuple;
}
namespace main {
enum class QueryResultType {
FTABLE = 0,
ARROW = 1,
};
/**
* @brief QueryResult stores the result of a query execution.
*/
class QueryResult {
public:
/**
* @brief Used to create a QueryResult object for the failing query.
*/
LBUG_API QueryResult();
explicit QueryResult(QueryResultType type);
QueryResult(QueryResultType type, std::vector<std::string> columnNames,
std::vector<common::LogicalType> columnTypes);
/**
* @brief Deconstructs the QueryResult object.
*/
LBUG_API virtual ~QueryResult() = 0;
/**
* @return if the query is executed successfully or not.
*/
LBUG_API bool isSuccess() const;
/**
* @return error message of the query execution if the query fails.
*/
LBUG_API std::string getErrorMessage() const;
/**
* @return number of columns in query result.
*/
LBUG_API size_t getNumColumns() const;
/**
* @return name of each column in the query result.
*/
LBUG_API std::vector<std::string> getColumnNames() const;
/**
* @return dataType of each column in the query result.
*/
LBUG_API std::vector<common::LogicalType> getColumnDataTypes() const;
/**
* @return query summary which stores the execution time, compiling time, plan and query
* options.
*/
LBUG_API QuerySummary* getQuerySummary() const;
QuerySummary* getQuerySummaryUnsafe();
/**
* @return whether there are more query results to read.
*/
LBUG_API bool hasNextQueryResult() const;
/**
* @return get the next query result to read (for multiple query statements).
*/
LBUG_API QueryResult* getNextQueryResult();
/**
* @return num of tuples in query result.
*/
LBUG_API virtual uint64_t getNumTuples() const = 0;
/**
* @return whether there are more tuples to read.
*/
LBUG_API virtual bool hasNext() const = 0;
/**
* @return next flat tuple in the query result. Note that to reduce resource allocation, all
* calls to getNext() reuse the same FlatTuple object. Since its contents will be overwritten,
* please complete processing a FlatTuple or make a copy of its data before calling getNext()
* again.
*/
LBUG_API virtual std::shared_ptr<processor::FlatTuple> getNext() = 0;
/**
* @brief Resets the result tuple iterator.
*/
LBUG_API virtual void resetIterator() = 0;
/**
* @return string of first query result.
*/
LBUG_API virtual std::string toString() const = 0;
/**
* @brief Returns the arrow schema of the query result.
* @return datatypes of the columns as an arrow schema
*
* It is the caller's responsibility to call the release function to release the underlying data
* If converting to another arrow type, this is usually handled automatically.
*/
LBUG_API std::unique_ptr<ArrowSchema> getArrowSchema() const;
/**
* @return whether there are more arrow chunk to read.
*/
LBUG_API virtual bool hasNextArrowChunk() = 0;
/**
* @brief Returns the next chunk of the query result as an arrow array.
* @param chunkSize number of tuples to return in the chunk.
* @return An arrow array representation of the next chunkSize tuples of the query result.
*
* The ArrowArray internally stores an arrow struct with fields for each of the columns.
* This can be converted to a RecordBatch with arrow's ImportRecordBatch function
*
* It is the caller's responsibility to call the release function to release the underlying data
* If converting to another arrow type, this is usually handled automatically.
*/
LBUG_API virtual std::unique_ptr<ArrowArray> getNextArrowChunk(int64_t chunkSize) = 0;
QueryResultType getType() const { return type; }
void setColumnNames(std::vector<std::string> columnNames);
void setColumnTypes(std::vector<common::LogicalType> columnTypes);
void addNextResult(std::unique_ptr<QueryResult> next_);
std::unique_ptr<QueryResult> moveNextResult();
void setQuerySummary(std::unique_ptr<QuerySummary> summary);
void setDBLifeCycleManager(
std::shared_ptr<common::DatabaseLifeCycleManager> dbLifeCycleManager);
static std::unique_ptr<QueryResult> getQueryResultWithError(const std::string& errorMessage);
template<class TARGET>
TARGET& cast() {
return common::dynamic_cast_checked<TARGET&>(*this);
}
template<class TARGET>
const TARGET& constCast() const {
return common::dynamic_cast_checked<const TARGET&>(*this);
}
protected:
void validateQuerySucceed() const;
void checkDatabaseClosedOrThrow() const;
protected:
class QueryResultIterator {
public:
QueryResultIterator() = default;
explicit QueryResultIterator(QueryResult* startResult) : current(startResult) {}
void operator++() {
if (current) {
current = current->nextQueryResult.get();
}
}
bool isEnd() const { return current == nullptr; }
bool hasNextQueryResult() const { return current->nextQueryResult != nullptr; }
QueryResult* getCurrentResult() const { return current; }
private:
QueryResult* current;
};
QueryResultType type;
bool success = true;
std::string errMsg;
std::vector<std::string> columnNames;
std::vector<common::LogicalType> columnTypes;
std::shared_ptr<processor::FlatTuple> tuple;
std::unique_ptr<QuerySummary> querySummary;
std::unique_ptr<QueryResult> nextQueryResult;
QueryResultIterator queryResultIterator;
std::shared_ptr<common::DatabaseLifeCycleManager> dbLifeCycleManager;
};
} // namespace main
} // namespace lbug