-
Notifications
You must be signed in to change notification settings - Fork 86
/
query_result.cpp
146 lines (122 loc) · 4 KB
/
query_result.cpp
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
#include "main/query_result.h"
#include "binder/expression/expression.h"
#include "common/arrow/arrow_converter.h"
#include "common/exception/runtime.h"
#include "common/types/value/node.h"
#include "common/types/value/rel.h"
#include "processor/result/factorized_table.h"
#include "processor/result/flat_tuple.h"
using namespace kuzu::common;
using namespace kuzu::processor;
namespace kuzu {
namespace main {
QueryResult::QueryResult() = default;
QueryResult::QueryResult(const PreparedSummary& preparedSummary) {
querySummary = std::make_unique<QuerySummary>();
querySummary->setPreparedSummary(preparedSummary);
nextQueryResult = nullptr;
queryResultIterator = QueryResultIterator{this};
}
QueryResult::~QueryResult() = default;
bool QueryResult::isSuccess() const {
return success;
}
std::string QueryResult::getErrorMessage() const {
return errMsg;
}
size_t QueryResult::getNumColumns() const {
return columnDataTypes.size();
}
std::vector<std::string> QueryResult::getColumnNames() const {
return columnNames;
}
std::vector<LogicalType> QueryResult::getColumnDataTypes() const {
return LogicalType::copy(columnDataTypes);
}
uint64_t QueryResult::getNumTuples() const {
return factorizedTable->getTotalNumFlatTuples();
}
QuerySummary* QueryResult::getQuerySummary() const {
return querySummary.get();
}
void QueryResult::resetIterator() {
iterator->resetState();
}
void QueryResult::initResultTableAndIterator(
std::shared_ptr<processor::FactorizedTable> factorizedTable_,
const binder::expression_vector& columns) {
factorizedTable = std::move(factorizedTable_);
tuple = std::make_shared<FlatTuple>();
std::vector<Value*> valuesToCollect;
for (auto i = 0u; i < columns.size(); ++i) {
auto column = columns[i].get();
const auto& columnType = column->getDataType();
auto columnName = column->hasAlias() ? column->getAlias() : column->toString();
columnDataTypes.push_back(columnType.copy());
columnNames.push_back(columnName);
std::unique_ptr<Value> value =
std::make_unique<Value>(Value::createDefaultValue(columnType.copy()));
valuesToCollect.push_back(value.get());
tuple->addValue(std::move(value));
}
iterator = std::make_unique<FlatTupleIterator>(*factorizedTable, std::move(valuesToCollect));
}
bool QueryResult::hasNext() const {
validateQuerySucceed();
return iterator->hasNextFlatTuple();
}
bool QueryResult::hasNextQueryResult() const {
return queryResultIterator.hasNextQueryResult();
}
QueryResult* QueryResult::getNextQueryResult() {
if (hasNextQueryResult()) {
++queryResultIterator;
return queryResultIterator.getCurrentResult();
}
return nullptr;
}
std::shared_ptr<FlatTuple> QueryResult::getNext() {
if (!hasNext()) {
throw RuntimeException(
"No more tuples in QueryResult, Please check hasNext() before calling getNext().");
}
validateQuerySucceed();
iterator->getNextFlatTuple();
return tuple;
}
std::string QueryResult::toString() {
std::string result;
if (isSuccess()) {
// print header
for (auto i = 0u; i < columnNames.size(); ++i) {
if (i != 0) {
result += "|";
}
result += columnNames[i];
}
result += "\n";
resetIterator();
while (hasNext()) {
getNext();
result += tuple->toString();
}
} else {
result = errMsg;
}
return result;
}
void QueryResult::validateQuerySucceed() const {
if (!success) {
throw Exception(errMsg);
}
}
std::unique_ptr<ArrowSchema> QueryResult::getArrowSchema() const {
return ArrowConverter::toArrowSchema(getColumnDataTypes(), getColumnNames());
}
std::unique_ptr<ArrowArray> QueryResult::getNextArrowChunk(int64_t chunkSize) {
auto data = std::make_unique<ArrowArray>();
ArrowConverter::toArrowArray(*this, data.get(), chunkSize);
return data;
}
} // namespace main
} // namespace kuzu