1#include "duckdb/main/materialized_query_result.hpp"
2
3using namespace duckdb;
4using namespace std;
5
6MaterializedQueryResult::MaterializedQueryResult(StatementType statement_type)
7 : QueryResult(QueryResultType::MATERIALIZED_RESULT, statement_type) {
8}
9
10MaterializedQueryResult::MaterializedQueryResult(StatementType statement_type, vector<SQLType> sql_types,
11 vector<TypeId> types, vector<string> names)
12 : QueryResult(QueryResultType::MATERIALIZED_RESULT, statement_type, sql_types, types, names) {
13}
14
15MaterializedQueryResult::MaterializedQueryResult(string error)
16 : QueryResult(QueryResultType::MATERIALIZED_RESULT, error) {
17}
18
19Value MaterializedQueryResult::GetValue(idx_t column, idx_t index) {
20 auto &data = collection.GetChunk(index).data[column];
21 auto offset_in_chunk = index % STANDARD_VECTOR_SIZE;
22 return data.GetValue(offset_in_chunk);
23}
24
25string MaterializedQueryResult::ToString() {
26 string result;
27 if (success) {
28 result = HeaderToString();
29 result += "[ Rows: " + to_string(collection.count) + "]\n";
30 for (idx_t j = 0; j < collection.count; j++) {
31 for (idx_t i = 0; i < collection.column_count(); i++) {
32 auto val = collection.GetValue(i, j);
33 result += val.is_null ? "NULL" : val.ToString(sql_types[i]);
34 result += "\t";
35 }
36 result += "\n";
37 }
38 result += "\n";
39 } else {
40 result = "Query Error: " + error + "\n";
41 }
42 return result;
43}
44
45unique_ptr<DataChunk> MaterializedQueryResult::Fetch() {
46 if (!success) {
47 return nullptr;
48 }
49 if (collection.chunks.size() == 0) {
50 return make_unique<DataChunk>();
51 }
52 auto chunk = move(collection.chunks[0]);
53 collection.chunks.erase(collection.chunks.begin() + 0);
54 return chunk;
55}
56