forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryExporterCSV.cpp
271 lines (252 loc) · 8.73 KB
/
QueryExporterCSV.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2022 HEAVY.AI, Inc.
*
* Licensed 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.
*/
#include "ImportExport/QueryExporterCSV.h"
#include <boost/variant/get.hpp>
#include "QueryEngine/ResultSet.h"
#include "Shared/misc.h"
namespace import_export {
QueryExporterCSV::QueryExporterCSV() : QueryExporter(FileType::kCSV) {}
QueryExporterCSV::~QueryExporterCSV() {}
void QueryExporterCSV::beginExport(const std::string& file_path,
const std::string& layer_name,
const CopyParams& copy_params,
const std::vector<TargetMetaInfo>& column_infos,
const FileCompression file_compression,
const ArrayNullHandling array_null_handling) {
validateFileExtensions(file_path, "CSV", {".csv", ".tsv"});
// compression?
auto actual_file_path{file_path};
if (file_compression != FileCompression::kNone) {
// @TODO(se) implement post-export compression
throw std::runtime_error("Compression not yet supported for this file type");
}
// open file
outfile_.open(actual_file_path);
if (!outfile_) {
throw std::runtime_error("Failed to create file '" + actual_file_path + "'");
}
// write header?
if (copy_params.has_header != import_export::ImportHeaderRow::kNoHeader) {
bool not_first{false};
int column_index = 0;
for (auto const& column_info : column_infos) {
// get name or default
auto column_name = safeColumnName(column_info.get_resname(), column_index + 1);
// output to header line
if (not_first) {
outfile_ << copy_params.delimiter;
} else {
not_first = true;
}
outfile_ << column_name;
column_index++;
}
outfile_ << copy_params.line_delim;
}
// keep these
copy_params_ = copy_params;
}
namespace {
std::string nullable_str_to_string(const NullableString& str) {
auto nptr = boost::get<void*>(&str);
if (nptr) {
CHECK(!*nptr);
return "NULL";
}
auto sptr = boost::get<std::string>(&str);
CHECK(sptr);
return *sptr;
}
std::string target_value_to_string(const TargetValue& tv,
const SQLTypeInfo& ti,
const std::string& delim) {
if (ti.is_array()) {
const auto array_tv = boost::get<ArrayTargetValue>(&tv);
CHECK(array_tv);
if (array_tv->is_initialized()) {
const auto& vec = array_tv->get();
std::vector<std::string> elem_strs;
elem_strs.reserve(vec.size());
const auto& elem_ti = ti.get_elem_type();
for (const auto& elem_tv : vec) {
elem_strs.push_back(target_value_to_string(elem_tv, elem_ti, delim));
}
return "{" + boost::algorithm::join(elem_strs, delim) + "}";
}
return "NULL";
}
const auto scalar_tv = boost::get<ScalarTargetValue>(&tv);
if (ti.is_time()) {
return shared::convert_temporal_to_iso_format(ti, *boost::get<int64_t>(scalar_tv));
}
if (ti.is_decimal()) {
Datum datum;
datum.bigintval = *boost::get<int64_t>(scalar_tv);
if (datum.bigintval == NULL_BIGINT) {
return "NULL";
}
return DatumToString(datum, ti);
}
if (ti.is_boolean()) {
const auto bool_val = *boost::get<int64_t>(scalar_tv);
return bool_val == NULL_BOOLEAN ? "NULL" : (bool_val ? "true" : "false");
}
auto iptr = boost::get<int64_t>(scalar_tv);
if (iptr) {
return *iptr == inline_int_null_val(ti) ? "NULL" : std::to_string(*iptr);
}
auto fptr = boost::get<float>(scalar_tv);
if (fptr) {
return *fptr == inline_fp_null_val(ti) ? "NULL" : std::to_string(*fptr);
}
auto dptr = boost::get<double>(scalar_tv);
if (dptr) {
return *dptr == inline_fp_null_val(ti.is_decimal() ? SQLTypeInfo(kDOUBLE, false) : ti)
? "NULL"
: std::to_string(*dptr);
}
auto sptr = boost::get<NullableString>(scalar_tv);
CHECK(sptr);
return nullable_str_to_string(*sptr);
}
} // namespace
void QueryExporterCSV::exportResults(const std::vector<AggregatedResult>& query_results) {
for (auto& agg_result : query_results) {
auto results = agg_result.rs;
auto const& targets = agg_result.targets_meta;
while (true) {
auto const crt_row = results->getNextRow(true, true);
if (crt_row.empty()) {
break;
}
bool not_first = false;
for (size_t i = 0; i < results->colCount(); ++i) {
bool is_null{false};
auto const tv = crt_row[i];
auto const scalar_tv = boost::get<ScalarTargetValue>(&tv);
if (not_first) {
outfile_ << copy_params_.delimiter;
} else {
not_first = true;
}
if (copy_params_.quoted) {
outfile_ << copy_params_.quote;
}
auto const& ti = targets[i].get_type_info();
if (!scalar_tv) {
outfile_ << target_value_to_string(crt_row[i], ti, " | ");
if (copy_params_.quoted) {
outfile_ << copy_params_.quote;
}
continue;
}
if (boost::get<int64_t>(scalar_tv)) {
auto int_val = *(boost::get<int64_t>(scalar_tv));
switch (ti.get_type()) {
case kBOOLEAN:
is_null = (int_val == NULL_BOOLEAN);
break;
case kTINYINT:
is_null = (int_val == NULL_TINYINT);
break;
case kSMALLINT:
is_null = (int_val == NULL_SMALLINT);
break;
case kINT:
is_null = (int_val == NULL_INT);
break;
case kBIGINT:
is_null = (int_val == NULL_BIGINT);
break;
case kTIME:
case kTIMESTAMP:
case kDATE:
is_null = (int_val == NULL_BIGINT);
break;
default:
is_null = false;
}
if (is_null) {
outfile_ << copy_params_.null_str;
} else if (ti.is_time()) {
outfile_ << shared::convert_temporal_to_iso_format(ti, int_val);
} else if (ti.is_boolean()) {
outfile_ << (int_val ? "true" : "false");
} else {
outfile_ << int_val;
}
} else if (boost::get<double>(scalar_tv)) {
auto real_val = *(boost::get<double>(scalar_tv));
if (ti.get_type() == kFLOAT) {
is_null = (real_val == NULL_FLOAT);
} else {
is_null = (real_val == NULL_DOUBLE);
}
if (is_null) {
outfile_ << copy_params_.null_str;
} else if (ti.get_type() == kNUMERIC) {
outfile_ << std::setprecision(ti.get_precision()) << real_val;
} else {
outfile_ << std::setprecision(std::numeric_limits<double>::digits10 + 1)
<< real_val;
}
} else if (boost::get<float>(scalar_tv)) {
CHECK_EQ(kFLOAT, ti.get_type());
auto real_val = *(boost::get<float>(scalar_tv));
if (real_val == NULL_FLOAT) {
outfile_ << copy_params_.null_str;
} else {
outfile_ << std::setprecision(std::numeric_limits<float>::digits10 + 1)
<< real_val;
}
} else {
auto s = boost::get<NullableString>(scalar_tv);
is_null = !s || boost::get<void*>(s);
if (is_null) {
outfile_ << copy_params_.null_str;
} else {
auto s_notnull = boost::get<std::string>(s);
CHECK(s_notnull);
if (!copy_params_.quoted) {
outfile_ << *s_notnull;
} else {
size_t q = s_notnull->find(copy_params_.quote);
if (q == std::string::npos) {
outfile_ << *s_notnull;
} else {
std::string str(*s_notnull);
while (q != std::string::npos) {
str.insert(q, 1, copy_params_.escape);
q = str.find(copy_params_.quote, q + 2);
}
outfile_ << str;
}
}
}
}
if (copy_params_.quoted) {
outfile_ << copy_params_.quote;
}
}
outfile_ << copy_params_.line_delim;
}
}
}
void QueryExporterCSV::endExport() {
// just close the file
outfile_.close();
}
} // namespace import_export