-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparquet_import.cpp
More file actions
396 lines (328 loc) · 14.3 KB
/
Copy pathparquet_import.cpp
File metadata and controls
396 lines (328 loc) · 14.3 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2026 Davide Faconti
// SPDX-License-Identifier: MPL-2.0
// parquet_import — load a Parquet file into DataEngine, report memory stats.
//
// Usage: ./parquet_import <file.parquet> [chunk_rows]
#include <arrow/api.h>
#include <arrow/io/file.h>
#include <arrow/io/memory.h>
#include <arrow/ipc/writer.h>
#include <parquet/arrow/reader.h>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include "pj_base/number_parse.hpp"
#include "pj_base/span.hpp"
#include "pj_base/types.hpp"
#include "pj_datastore/arrow_import.hpp"
#include "pj_datastore/chunk.hpp"
#include "pj_datastore/encoding.hpp"
#include "pj_datastore/engine.hpp"
#include "pj_datastore/topic_storage.hpp"
namespace {
using PJ::DataEngine;
using PJ::EncodingType;
using PJ::PrimitiveType;
using PJ::TopicChunk;
// ── Local column mapping for report formatting ──────────────────────────────
struct ColumnMapping {
int arrow_index; // column index in Arrow table
std::size_t pj_col_index; // column index in PJ schema
PrimitiveType pj_type;
std::string name;
};
std::string_view encoding_name(EncodingType enc) {
switch (enc) {
case EncodingType::kRaw:
return "Raw";
case EncodingType::kDictionary:
return "Dictionary";
case EncodingType::kPackedBool:
return "PackedBool";
case EncodingType::kConstant:
return "Constant";
case EncodingType::kFrameOfReference:
return "FrameOfRef";
}
return "Unknown";
}
std::string arrow_type_name(const std::shared_ptr<arrow::DataType>& type) {
return type->name();
}
// ── Per-column memory measurement ───────────────────────────────────────────
struct ColumnMemory {
std::size_t actual_bytes = 0;
std::size_t theoretical_bytes = 0;
EncodingType dominant_encoding = EncodingType::kRaw;
};
std::size_t encoded_column_bytes(const TopicChunk& chunk, std::size_t col) {
return std::visit(
[](const auto& v) -> std::size_t {
using T = std::decay_t<decltype(v)>;
if constexpr (std::is_same_v<T, PJ::RawBuffer>) {
return v.size();
} else if constexpr (std::is_same_v<T, PJ::encoding::ConstantEncoded>) {
return v.value_size;
} else if constexpr (std::is_same_v<T, PJ::encoding::FrameOfReferenceEncoded>) {
return v.offsets.size();
} else if constexpr (std::is_same_v<T, PJ::encoding::DictionaryEncoded>) {
std::size_t dict_bytes = 0;
for (const auto& s : v.dictionary) {
dict_bytes += s.size();
}
return v.indices.size() + dict_bytes;
} else if constexpr (std::is_same_v<T, PJ::encoding::PackedBools>) {
return v.bits.size();
}
},
chunk.columns[col].data);
}
std::vector<ColumnMemory> measure_memory(
const std::deque<TopicChunk>& chunks, std::size_t num_columns, const std::vector<ColumnMapping>& mappings,
const std::shared_ptr<arrow::Table>& table) {
std::vector<ColumnMemory> result(num_columns);
// Count encoding occurrences per column to determine dominant encoding
constexpr int kNumEncodings = 5;
std::vector<std::vector<uint32_t>> enc_counts(num_columns, std::vector<uint32_t>(kNumEncodings, 0));
for (const auto& chunk : chunks) {
for (std::size_t col = 0; col < num_columns; ++col) {
result[col].actual_bytes += encoded_column_bytes(chunk, col);
if (chunk.columns[col].validity_bitmap) {
result[col].actual_bytes += chunk.columns[col].validity_bitmap->sizeBytes();
}
enc_counts[col][static_cast<uint8_t>(chunk.columnEncoding(col))]++;
}
}
// Determine dominant encoding per column
for (std::size_t col = 0; col < num_columns; ++col) {
uint32_t max_count = 0;
for (int enc = 0; enc < kNumEncodings; ++enc) {
if (enc_counts[col][static_cast<std::size_t>(enc)] > max_count) {
max_count = enc_counts[col][static_cast<std::size_t>(enc)];
result[col].dominant_encoding = static_cast<EncodingType>(enc);
}
}
}
// Theoretical bytes: arrow type byte width * total_rows, or actual string
// length for string columns
auto total_rows = static_cast<std::size_t>(table->num_rows());
for (std::size_t col = 0; col < num_columns; ++col) {
const auto& mapping = mappings[col];
auto arrow_col = table->column(mapping.arrow_index);
auto arrow_type = arrow_col->type();
if (arrow_type->id() == arrow::Type::STRING || arrow_type->id() == arrow::Type::LARGE_STRING) {
// Sum actual string data length across all chunks
std::size_t string_bytes = 0;
for (int i = 0; i < arrow_col->num_chunks(); ++i) {
if (arrow_type->id() == arrow::Type::STRING) {
auto arr = std::static_pointer_cast<arrow::StringArray>(arrow_col->chunk(i));
string_bytes += static_cast<std::size_t>(arr->total_values_length());
} else {
auto arr = std::static_pointer_cast<arrow::LargeStringArray>(arrow_col->chunk(i));
string_bytes += static_cast<std::size_t>(arr->total_values_length());
}
}
result[col].theoretical_bytes = string_bytes;
} else if (arrow_type->id() == arrow::Type::BOOL) {
// 1 byte per bool uncompressed
result[col].theoretical_bytes = total_rows;
} else {
auto byte_width = static_cast<std::size_t>(arrow_type->byte_width());
result[col].theoretical_bytes = byte_width * total_rows;
}
}
return result;
}
// ── Report formatting ───────────────────────────────────────────────────────
void print_report(
const std::vector<ColumnMapping>& mappings, const std::vector<ColumnMemory>& memory,
const std::shared_ptr<arrow::Table>& table, double ingest_seconds) {
// Column widths
constexpr int kNameW = 24;
constexpr int kTypeW = 12;
constexpr int kEncW = 14;
constexpr int kBytesW = 12;
constexpr int kRatioW = 8;
int total_w = kNameW + kTypeW + kEncW + kBytesW * 2 + kRatioW;
std::cout << "\n";
std::cout << "Rows: " << table->num_rows() << "\n";
std::cout << "Columns: " << mappings.size() << "\n";
std::cout << "Ingest: " << std::fixed << std::setprecision(3) << ingest_seconds << " s\n\n";
// Header
std::cout << std::left << std::setw(kNameW) << "Column" << std::setw(kTypeW) << "Arrow Type" << std::setw(kEncW)
<< "PJ Encoding" << std::right << std::setw(kBytesW) << "Actual" << std::setw(kBytesW) << "Theoretical"
<< std::setw(kRatioW) << "Ratio"
<< "\n";
std::cout << std::string(static_cast<std::size_t>(total_w), '-') << "\n";
std::size_t total_actual = 0;
std::size_t total_theoretical = 0;
for (std::size_t i = 0; i < mappings.size(); ++i) {
const auto& m = mappings[i];
const auto& mem = memory[i];
auto arrow_type = table->column(m.arrow_index)->type();
double ratio = mem.theoretical_bytes > 0
? static_cast<double>(mem.actual_bytes) / static_cast<double>(mem.theoretical_bytes)
: 0.0;
std::cout << std::left << std::setw(kNameW) << m.name << std::setw(kTypeW) << arrow_type_name(arrow_type)
<< std::setw(kEncW) << encoding_name(mem.dominant_encoding) << std::right << std::setw(kBytesW)
<< mem.actual_bytes << std::setw(kBytesW) << mem.theoretical_bytes << std::setw(kRatioW - 1) << std::fixed
<< std::setprecision(2) << ratio << "x\n";
total_actual += mem.actual_bytes;
total_theoretical += mem.theoretical_bytes;
}
std::cout << std::string(static_cast<std::size_t>(total_w), '-') << "\n";
double total_ratio =
total_theoretical > 0 ? static_cast<double>(total_actual) / static_cast<double>(total_theoretical) : 0.0;
std::cout << std::left << std::setw(kNameW) << "TOTAL" << std::setw(kTypeW) << "" << std::setw(kEncW) << ""
<< std::right << std::setw(kBytesW) << total_actual << std::setw(kBytesW) << total_theoretical
<< std::setw(kRatioW - 1) << std::fixed << std::setprecision(2) << total_ratio << "x\n\n";
}
} // namespace
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <file.parquet> [chunk_rows]\n";
return 1;
}
const std::string path = argv[1];
uint32_t chunk_rows = 8192;
if (argc >= 3) {
const auto parsed = PJ::parseNumber<uint32_t>(argv[2]);
if (!parsed.has_value() || *parsed == 0) {
std::cerr << "Invalid chunk_rows value: " << argv[2] << " (expected a positive integer)\n";
return 1;
}
chunk_rows = *parsed;
}
// ── 1. Open Parquet file ──────────────────────────────────────────────────
auto maybe_infile = arrow::io::ReadableFile::Open(path);
if (!maybe_infile.ok()) {
std::cerr << "Failed to open file: " << maybe_infile.status().ToString() << "\n";
return 1;
}
auto maybe_reader = parquet::arrow::OpenFile(*maybe_infile, arrow::default_memory_pool());
if (!maybe_reader.ok()) {
std::cerr << "Failed to open Parquet reader: " << maybe_reader.status().ToString() << "\n";
return 1;
}
auto arrow_reader = std::move(*maybe_reader);
std::shared_ptr<arrow::Table> table;
auto st = arrow_reader->ReadTable(&table);
if (!st.ok()) {
std::cerr << "Failed to read table: " << st.ToString() << "\n";
return 1;
}
std::cout << "Loaded " << path << ": " << table->num_rows() << " rows, " << table->num_columns() << " columns\n";
// ── 2. Serialize Arrow Table to IPC stream bytes ─────────────────────────
auto sink_result = arrow::io::BufferOutputStream::Create();
if (!sink_result.ok()) {
std::cerr << "Failed to create buffer output stream: " << sink_result.status().ToString() << "\n";
return 1;
}
auto sink = *sink_result;
auto ipc_writer_result = arrow::ipc::MakeStreamWriter(sink, table->schema());
if (!ipc_writer_result.ok()) {
std::cerr << "Failed to create IPC writer: " << ipc_writer_result.status().ToString() << "\n";
return 1;
}
auto ipc_writer = *ipc_writer_result;
st = ipc_writer->WriteTable(*table);
if (!st.ok()) {
std::cerr << "IPC WriteTable failed: " << st.ToString() << "\n";
return 1;
}
st = ipc_writer->Close();
if (!st.ok()) {
std::cerr << "IPC writer Close failed: " << st.ToString() << "\n";
return 1;
}
auto ipc_buf_result = sink->Finish();
if (!ipc_buf_result.ok()) {
std::cerr << "Failed to finish IPC buffer: " << ipc_buf_result.status().ToString() << "\n";
return 1;
}
auto ipc_buffer = *ipc_buf_result;
PJ::Span<const uint8_t> ipc_bytes(ipc_buffer->data(), static_cast<std::size_t>(ipc_buffer->size()));
std::cout << "Serialized to IPC: " << ipc_buffer->size() << " bytes\n";
// ── 3. Map IPC schema → TypeTreeNode via arrow_import ──────────────────
auto schema_result = PJ::arrow_import::schemaFromIpc(ipc_bytes);
if (!schema_result.has_value()) {
std::cerr << "Schema conversion failed: " << schema_result.error() << "\n";
return 1;
}
auto& [type_tree, arrow_mappings] = *schema_result;
// Build local ColumnMapping for the report
std::vector<ColumnMapping> mappings;
mappings.reserve(arrow_mappings.size());
for (const auto& am : arrow_mappings) {
ColumnMapping m;
m.arrow_index = am.arrow_column_index;
m.pj_col_index = am.pj_column_index;
m.pj_type = am.pj_type;
m.name = am.field_name;
mappings.push_back(std::move(m));
}
// ── 4. Create DataEngine, dataset, schema, topic ──────────────────────────
DataEngine engine;
auto td_or = engine.createTimeDomain("default");
if (!td_or.has_value()) {
std::cerr << "Failed to create time domain: " << td_or.error() << "\n";
return 1;
}
PJ::DatasetDescriptor ds_desc;
ds_desc.source_name = path;
ds_desc.time_domain_id = *td_or;
auto ds_or = engine.createDataset(std::move(ds_desc));
if (!ds_or.has_value()) {
std::cerr << "Failed to create dataset: " << ds_or.error() << "\n";
return 1;
}
auto dataset_id = *ds_or;
auto writer = engine.createWriter();
auto schema_or = writer.registerSchema("parquet_schema", type_tree);
if (!schema_or.has_value()) {
std::cerr << "Failed to register schema: " << schema_or.error() << "\n";
return 1;
}
PJ::TopicDescriptor topic_desc;
topic_desc.name = "parquet_data";
topic_desc.schema_id = *schema_or;
topic_desc.dataset_id = dataset_id;
topic_desc.max_chunk_rows = chunk_rows;
auto topic_or = writer.registerTopic(dataset_id, std::move(topic_desc));
if (!topic_or.has_value()) {
std::cerr << "Failed to register topic: " << topic_or.error() << "\n";
return 1;
}
auto topic_id = *topic_or;
// ── 5. Bulk ingest via IPC import API ────────────────────────────────────
auto t_start = std::chrono::steady_clock::now();
auto import_st = PJ::arrow_import::importIpcStream(writer, topic_id, ipc_bytes, arrow_mappings);
if (!import_st.has_value()) {
std::cerr << "import_ipc_stream failed: " << import_st.error() << "\n";
return 1;
}
// Flush and commit
auto flushed = writer.flushAll();
engine.commitChunks(std::move(flushed));
auto t_end = std::chrono::steady_clock::now();
double ingest_seconds = std::chrono::duration<double>(t_end - t_start).count();
// ── 6. Memory report ─────────────────────────────────────────────────────
const auto* storage = engine.getTopicStorage(topic_id);
if (storage == nullptr) {
std::cerr << "Topic storage not found after commit.\n";
return 1;
}
auto memory = measure_memory(storage->sealedChunks(), mappings.size(), mappings, table);
print_report(mappings, memory, table, ingest_seconds);
// Cross-check with TopicMetadata
auto meta = storage->metadata();
std::cout << "TopicMetadata.total_byte_size: " << meta.total_byte_size << "\n";
std::cout << "TopicMetadata.total_row_count: " << meta.total_row_count << "\n";
return 0;
}