-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathndjson_sink.cpp
More file actions
227 lines (203 loc) · 7.27 KB
/
Copy pathndjson_sink.cpp
File metadata and controls
227 lines (203 loc) · 7.27 KB
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
#include "silo/query_engine/exec_node/ndjson_sink.h"
#include <ios>
#include <string_view>
#include <arrow/acero/options.h>
#include <arrow/array.h>
#include <arrow/array/array_binary.h>
#include <fmt/format.h>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include "evobench/evobench.hpp"
#include "silo/common/panic.h"
#include "silo/common/size_constants.h"
namespace silo::query_engine::exec_node {
namespace {
void writeChunked(std::ostream& output, std::string_view content) {
const size_t chunk_size = 8192;
for (size_t pos = 0; pos < content.size(); pos += chunk_size) {
size_t remaining_size = content.size() - pos;
size_t write_size = std::min(chunk_size, remaining_size);
output.write(content.data() + pos, static_cast<std::streamsize>(write_size));
output.flush(); // Flush after each small chunk
}
}
template <size_t BatchSize>
struct BatchedStringStream {
std::array<std::stringstream, BatchSize> streams;
void operator<<(std::string_view bytes) {
for (size_t i = 0; i < BatchSize; ++i) {
streams[i] << bytes;
}
}
void operator>>(std::ostream& output) {
for (size_t i = 0; i < BatchSize; ++i) {
std::string content = std::move(streams[i]).str();
writeChunked(output, content);
}
}
};
template <size_t BatchSize>
class ArrayToJsonTypeVisitor : public arrow::ArrayVisitor {
BatchedStringStream<BatchSize>& output_stream;
size_t& row_base;
public:
ArrayToJsonTypeVisitor(BatchedStringStream<BatchSize>& output_stream, size_t& row_base)
: output_stream(output_stream),
row_base(row_base) {}
arrow::Status Visit(const arrow::Int32Array& array) override {
for (size_t i = 0; i < BatchSize; ++i) {
if (array.IsNull(row_base + i)) {
output_stream.streams.at(i) << "null";
} else {
output_stream.streams.at(i) << array.GetView(row_base + i);
}
}
return arrow::Status::OK();
}
arrow::Status Visit(const arrow::Int64Array& array) override {
for (size_t i = 0; i < BatchSize; ++i) {
if (array.IsNull(row_base + i)) {
output_stream.streams.at(i) << "null";
} else {
output_stream.streams.at(i) << array.GetView(row_base + i);
}
}
return arrow::Status::OK();
}
arrow::Status Visit(const arrow::DoubleArray& array) override {
for (size_t i = 0; i < BatchSize; ++i) {
if (array.IsNull(row_base + i)) {
output_stream.streams.at(i) << "null";
} else {
nlohmann::json json = array.GetView(row_base + i);
output_stream.streams.at(i) << json;
}
}
return arrow::Status::OK();
}
arrow::Status Visit(const arrow::FloatArray& array) override {
for (size_t i = 0; i < BatchSize; ++i) {
if (array.IsNull(row_base + i)) {
output_stream.streams.at(i) << "null";
} else {
nlohmann::json json = array.GetView(row_base + i);
output_stream.streams.at(i) << json;
}
}
return arrow::Status::OK();
}
arrow::Status Visit(const arrow::StringArray& array) override {
for (size_t i = 0; i < BatchSize; ++i) {
if (array.IsNull(row_base + i)) {
output_stream.streams.at(i) << "null";
} else {
std::string_view value = array.GetView(row_base + i);
nlohmann::json json_string = value;
output_stream.streams.at(i) << json_string.dump();
}
}
return arrow::Status::OK();
}
arrow::Status Visit(const arrow::BooleanArray& array) override {
for (size_t i = 0; i < BatchSize; ++i) {
if (array.IsNull(row_base + i)) {
output_stream.streams.at(i) << "null";
} else {
output_stream.streams.at(i) << (array.GetView(row_base + i) ? "true" : "false");
}
}
return arrow::Status::OK();
}
};
template <size_t BatchSize>
void sendJsonLinesInBatches(
size_t& row_idx_base,
size_t row_count,
const std::vector<std::string>& prepared_column_strings_for_json_attributes,
const std::vector<std::shared_ptr<arrow::Array>>& column_arrays,
std::ostream& output_stream
) {
BatchedStringStream<BatchSize> ndjson_line_streams;
ArrayToJsonTypeVisitor<BatchSize> my_visitor(ndjson_line_streams, row_idx_base);
size_t column_count = column_arrays.size();
for (; row_idx_base + BatchSize <= row_count; row_idx_base += BatchSize) {
ndjson_line_streams << "{";
for (size_t column_idx = 0; column_idx < column_count; column_idx++) {
const auto& column_array = column_arrays.at(column_idx);
ndjson_line_streams << prepared_column_strings_for_json_attributes.at(column_idx);
(void)column_array->Accept(&my_visitor);
}
ndjson_line_streams << "}\n";
{
EVOBENCH_SCOPE_EVERY(100, "QueryPlan", "sendDataToOutputStream");
ndjson_line_streams >> output_stream;
}
}
if constexpr (BatchSize > 1) {
// Send remaining lines
sendJsonLinesInBatches<1>(
row_idx_base,
row_count,
prepared_column_strings_for_json_attributes,
column_arrays,
output_stream
);
}
}
} // namespace
arrow::Status writeBatchAsNdjson(
const arrow::compute::ExecBatch& batch,
const std::shared_ptr<arrow::Schema>& schema,
std::ostream* output_stream
) {
EVOBENCH_SCOPE("QueryPlan", "writeBatchAsNdjson");
size_t row_count = batch.length;
std::vector<std::shared_ptr<arrow::Array>> column_arrays;
for (const auto& datum : batch.values) {
SILO_ASSERT(datum.is_array());
column_arrays.emplace_back(datum.make_array());
}
std::vector<std::string> prepared_column_strings_for_json_attributes;
bool first_column = true;
for (const auto& column_name : schema->fields()) {
nlohmann::json column_name_json = column_name->name();
std::string json_formatted_column_name;
if (!first_column) {
json_formatted_column_name += ",";
}
first_column = false;
json_formatted_column_name += column_name_json.dump();
json_formatted_column_name += ":";
prepared_column_strings_for_json_attributes.emplace_back(json_formatted_column_name);
}
size_t row_idx = 0;
constexpr size_t BATCH_SIZE = 8;
sendJsonLinesInBatches<BATCH_SIZE>(
row_idx, row_count, prepared_column_strings_for_json_attributes, column_arrays, *output_stream
);
if (!*output_stream) {
return arrow::Status::IOError("Could not write to network stream");
}
return arrow::Status::OK();
}
arrow::Result<arrow::acero::BackpressureMonitor*> createGenerator(
arrow::acero::ExecPlan* plan,
arrow::acero::ExecNode* input,
arrow::AsyncGenerator<std::optional<arrow::ExecBatch>>* generator
) {
arrow::acero::BackpressureMonitor* backpressure_monitor;
arrow::acero::SinkNodeOptions options{
generator,
arrow::acero::BackpressureOptions{
/*.resume_if_below =*/common::S_16_KB,
/*.pause_if_above =*/common::S_64_MB
},
&backpressure_monitor
};
ARROW_ASSIGN_OR_RAISE(
auto node, arrow::acero::MakeExecNode(std::string{"sink"}, plan, {input}, options)
);
node->SetLabel("final sink of the plan");
return backpressure_monitor;
}
} // namespace silo::query_engine::exec_node