Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
ParquetFormatter.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2012-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// An output formatter for Parquet files
19/****************************************************************************/
20#include <config.h>
21
22#ifdef _MSC_VER
23#pragma warning(push)
24/* Disable warning about unused parameters */
25#pragma warning(disable: 4100)
26/* Disable warning about hidden function arrow::io::Writable::Write */
27#pragma warning(disable: 4266)
28/* Disable warning about padded memory layout */
29#pragma warning(disable: 4324)
30/* Disable warning about this in initializers */
31#pragma warning(disable: 4355)
32/* Disable warning about changed memory layout due to virtual base class */
33#pragma warning(disable: 4435)
34/* Disable warning about declaration hiding class member */
35#pragma warning(disable: 4458)
36/* Disable warning about implicit conversion of int to bool */
37#pragma warning(disable: 4800)
38#endif
39#include <arrow/api.h>
40#include <arrow/io/api.h>
41#include <parquet/arrow/writer.h>
42#ifdef _MSC_VER
43#pragma warning(pop)
44#endif
45
48#include "ParquetFormatter.h"
49
50
51// ===========================================================================
52// helper class definitions
53// ===========================================================================
54#ifdef _MSC_VER
55#pragma warning(push)
56/* Disable warning about hidden function arrow::io::Writable::Write */
57#pragma warning(disable: 4266)
58#endif
59class ArrowOStreamWrapper : public arrow::io::OutputStream {
60public:
61 ArrowOStreamWrapper(std::ostream& out)
62 : myOStream(out), myAmOpen(true) {}
63
64 arrow::Status Close() override {
65 myAmOpen = false;
66 return arrow::Status::OK();
67 }
68
69 arrow::Status Flush() override {
70 myOStream.flush();
71 return arrow::Status::OK();
72 }
73
74 arrow::Result<int64_t> Tell() const override {
75 return myOStream.tellp();
76 }
77
78 bool closed() const override {
79 return !myAmOpen;
80 }
81
82 arrow::Status Write(const void* data, int64_t nbytes) override {
83 if (!myAmOpen) {
84 return arrow::Status::IOError("Write on closed stream");
85 }
86 myOStream.write(reinterpret_cast<const char*>(data), nbytes);
87 if (!myOStream) {
88 return arrow::Status::IOError("Failed to write to ostream");
89 }
90 return arrow::Status::OK();
91 }
92
93private:
94 std::ostream& myOStream;
96};
97#ifdef _MSC_VER
98#pragma warning(pop)
99#endif
100
101
102// ===========================================================================
103// ParquetFormatter::Impl definition
104// ===========================================================================
106 Impl(const std::string& columnNames, const int batchSize)
107 : myHeaderFormat(columnNames), myBatchSize(batchSize) {}
108
110 const std::string myHeaderFormat;
111
113 std::vector<std::string> myFullHeader;
114
116 parquet::Compression::type myCompression = parquet::Compression::UNCOMPRESSED;
117
119 const int myBatchSize;
120
122 std::shared_ptr<arrow::Schema> mySchema = arrow::schema({});
123
125 std::unique_ptr<parquet::arrow::FileWriter> myParquetWriter;
126
128 std::vector<std::shared_ptr<arrow::ArrayBuilder> > myBuilders;
129
131 std::vector<std::pair<const std::string, int> > myXMLStack;
132
134 std::vector<std::shared_ptr<arrow::Scalar> > myValues;
135
137 int myMaxDepth = 1000;
138
140 bool myWroteHeader = false;
141
143 bool myCheckColumns = false;
144
146 bool myNeedsWrite = false;
147
149 bool myHaveRootAttrs = false;
150
153
156
157 void checkAttr(const SumoXMLAttr attr) {
158 if (myCheckColumns && myMaxDepth == (int)myXMLStack.size()) {
159 mySeenAttrs.set(attr);
160 if (!myExpectedAttrs.test(attr)) {
161 throw ProcessError(TLF("Unexpected attribute '%', this file format does not support Parquet output yet.", toString(attr)));
162 }
163 }
164 }
165
166 template <class ATTR_TYPE, class BUILDER>
167 void checkBuilder(const ATTR_TYPE& attr, const std::shared_ptr<arrow::DataType>& (*dataType)()) {
168 myNeedsWrite = true;
169 if (!myWroteHeader) {
170 std::string fieldName = toString(attr);
171 std::string prefix;
172 for (const auto& entry : myXMLStack) {
173 prefix += entry.first + "_";
174 }
175 const std::string fullHeaderName = prefix + fieldName;
176 if (myHeaderFormat == "none") {
177 fieldName = "";
178 } else if (myHeaderFormat != "plain") {
179 fieldName = myXMLStack.back().first + "_" + fieldName;
180 }
181 const auto colIt = std::find(myFullHeader.begin(), myFullHeader.end(), fullHeaderName);
182 if (colIt == myFullHeader.end()) {
183 mySchema = *mySchema->AddField(mySchema->num_fields(), arrow::field(fieldName, dataType()));
184 auto builder = std::make_shared<BUILDER>();
185 if (!myBuilders.empty()) {
186 if (myBuilders.back()->length() > 0) {
187 PARQUET_THROW_NOT_OK(builder->AppendNulls(myBuilders.back()->length()));
188 }
189 while (myValues.size() < myBuilders.size()) {
190 myValues.push_back(nullptr);
191 }
192 }
193 myBuilders.push_back(builder);
194 myFullHeader.emplace_back(fullHeaderName);
195 } else {
196 myValues.resize(std::distance(myFullHeader.begin(), colIt));
197 }
198 }
199 }
200};
201
202
203// ===========================================================================
204// member method definitions
205// ===========================================================================
206ParquetFormatter::ParquetFormatter(const std::string& columnNames, const std::string& compression, const int batchSize)
207 : OutputFormatter(OutputFormatterType::PARQUET), myImpl(std::make_unique<Impl>(columnNames, batchSize)) {
208 if (compression == "snappy") {
209 myImpl->myCompression = parquet::Compression::SNAPPY;
210 } else if (compression == "gzip") {
211 myImpl->myCompression = parquet::Compression::GZIP;
212 } else if (compression == "brotli") {
213 myImpl->myCompression = parquet::Compression::BROTLI;
214 } else if (compression == "zstd") {
215 myImpl->myCompression = parquet::Compression::ZSTD;
216 } else if (compression == "lz4") {
217 myImpl->myCompression = parquet::Compression::LZ4;
218 } else if (compression == "bz2") {
219 myImpl->myCompression = parquet::Compression::BZ2;
220 } else if (compression != "" && compression != "uncompressed") {
221 WRITE_ERRORF("Unknown compression: %", compression);
222 }
223 if (!arrow::util::Codec::IsAvailable(myImpl->myCompression)) {
224 WRITE_WARNINGF("Compression '%' not available, falling back to uncompressed.", compression);
225 myImpl->myCompression = parquet::Compression::UNCOMPRESSED;
226 }
227}
228
229
231
232
233bool
234ParquetFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
235 const std::map<SumoXMLAttr, std::string>& attrs, bool /* includeConfig */) {
236 if (attrs.size() > 2) {
237 myImpl->myHaveRootAttrs = true;
238 openTag(into, rootElement);
239 for (const auto& a : attrs) {
240 if (a.first != SUMO_ATTR_XMLNS && a.first != SUMO_ATTR_SCHEMA_LOCATION) {
241 writeAttr(into, a.first, a.second, false, false);
242 }
243 }
244 return true;
245 }
246 return false;
247}
248
249
250void
251ParquetFormatter::openTag(std::ostream& /* into */, const std::string& xmlElement) {
252 myImpl->myXMLStack.push_back({xmlElement, (int)myImpl->myValues.size()});
253}
254
255
256void
257ParquetFormatter::openTag(std::ostream& /* into */, const SumoXMLTag& xmlElement) {
258 myImpl->myXMLStack.push_back({toString(xmlElement), (int)myImpl->myValues.size()});
259}
260
261
262bool
263ParquetFormatter::closeTag(std::ostream& into, const std::string& /* comment */) {
264 if (myImpl->myMaxDepth == 0) {
265 // the auto detection case: the first closed tag determines the depth
266 myImpl->myMaxDepth = (int)myImpl->myXMLStack.size();
267 }
268 const bool eof = myImpl->myXMLStack.empty() || (myImpl->myHaveRootAttrs && myImpl->myXMLStack.size() == 1);
269 if ((myImpl->myMaxDepth == (int)myImpl->myXMLStack.size() || eof) && !myImpl->myWroteHeader) {
270 // we are at the correct depth or the document has ended (XML stack is empty)
271 // so we should initialize the writer with the schema (if not done yet)
272 if (!myImpl->myCheckColumns) {
273 WRITE_WARNING("Column based formats are still experimental. Autodetection only works for homogeneous output.");
274 }
275 bool full = myImpl->myHeaderFormat == "full";
276 if (myImpl->myHeaderFormat == "auto") {
277 std::set<std::string> uniq;
278 for (const auto& field : myImpl->mySchema->fields()) {
279 const auto result = uniq.insert(field->name());
280 if (!result.second) {
281 full = true;
282 break;
283 }
284 }
285 }
286 if (full) {
287 arrow::FieldVector new_fields;
288 for (const auto& field : myImpl->mySchema->fields()) {
289 new_fields.push_back(field->WithName(myImpl->myFullHeader[new_fields.size()]));
290 }
291 myImpl->mySchema = arrow::schema(std::move(new_fields), myImpl->mySchema->metadata());
292 }
293 auto arrow_stream = std::make_shared<ArrowOStreamWrapper>(into);
294 std::shared_ptr<parquet::WriterProperties> props = parquet::WriterProperties::Builder().compression(myImpl->myCompression)->build();
295 myImpl->myParquetWriter = *parquet::arrow::FileWriter::Open(*myImpl->mySchema, arrow::default_memory_pool(), arrow_stream, props);
296 myImpl->myWroteHeader = true;
297 }
298 bool writeBatch = false;
299 if (myImpl->myNeedsWrite) {
300 if (myImpl->myCheckColumns && (int)myImpl->myXMLStack.size() == myImpl->myMaxDepth && myImpl->myExpectedAttrs != myImpl->mySeenAttrs) {
301 for (int i = 0; i < (int)myImpl->myExpectedAttrs.size(); ++i) {
302 if (myImpl->myExpectedAttrs.test(i) && !myImpl->mySeenAttrs.test(i)) {
303 WRITE_ERRORF("Incomplete attribute set, '%' is missing. This file format does not support Parquet output yet.",
305 }
306 }
307 }
308 int index = 0;
309 for (auto& builder : myImpl->myBuilders) {
310 const auto val = index < (int)myImpl->myValues.size() ? myImpl->myValues[index] : nullptr;
311 arrow::Status s = val == nullptr ? builder->AppendNull() : builder->AppendScalar(*val);
312 if (!s.ok()) {
313 throw ProcessError(TLF("Error writing attribute '%' (index: %, value: '%'): %",
314 myImpl->myFullHeader[index], index, val == nullptr ? "nullptr" : val->ToString(), s.ToString()));
315 }
316 index++;
317 }
318 writeBatch = myImpl->myWroteHeader && myImpl->myBuilders.back()->length() >= myImpl->myBatchSize;
319 myImpl->mySeenAttrs.reset();
320 myImpl->myNeedsWrite = false;
321 }
322 if (writeBatch || (eof && !myImpl->myBuilders.empty())) {
323 std::vector<std::shared_ptr<arrow::Array> > data;
324 for (auto& builder : myImpl->myBuilders) {
325 std::shared_ptr<arrow::Array> column;
326 PARQUET_THROW_NOT_OK(builder->Finish(&column));
327 data.push_back(column);
328 // builder.reset();
329 }
330 auto batch = arrow::RecordBatch::Make(myImpl->mySchema, data.back()->length(), data);
331 PARQUET_THROW_NOT_OK(myImpl->myParquetWriter->WriteRecordBatch(*batch));
332 }
333 if (!myImpl->myXMLStack.empty()) {
334 if ((int)myImpl->myValues.size() > myImpl->myXMLStack.back().second) {
335 myImpl->myValues.resize(myImpl->myXMLStack.back().second);
336 }
337 myImpl->myXMLStack.pop_back();
338 return true;
339 }
340 return false;
341}
342
343
344void
345ParquetFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val, const bool isNull, const bool /* escape */) {
346 myImpl->checkAttr(attr);
347 if (attr == SUMO_ATTR_X || attr == SUMO_ATTR_Y || into.precision() > 2) {
348 myImpl->checkBuilder<SumoXMLAttr, arrow::DoubleBuilder>(attr, arrow::float64);
349 myImpl->myValues.push_back(isNull ? nullptr : std::make_shared<arrow::DoubleScalar>(val));
350 } else {
351 myImpl->checkBuilder<SumoXMLAttr, arrow::FloatBuilder>(attr, arrow::float32);
352 myImpl->myValues.push_back(isNull ? nullptr : std::make_shared<arrow::FloatScalar>((float)val));
353 }
354}
355
356
357void
358ParquetFormatter::writeAttr(std::ostream& /* into */, const SumoXMLAttr attr, const int& val, const bool isNull, const bool /* escape */) {
359 myImpl->checkAttr(attr);
360 myImpl->checkBuilder<SumoXMLAttr, arrow::Int32Builder>(attr, arrow::int32);
361 myImpl->myValues.push_back(isNull ? nullptr : std::make_shared<arrow::Int32Scalar>(val));
362}
363
364
365void
366ParquetFormatter::writeAttr(std::ostream& into, const std::string& attr, const double& val, const bool isNull, const bool /* escape */) {
367 assert(!myImpl->myCheckColumns);
368 if (into.precision() > 2) {
369 myImpl->checkBuilder<std::string, arrow::DoubleBuilder>(attr, arrow::float64);
370 myImpl->myValues.push_back(isNull ? nullptr : std::make_shared<arrow::DoubleScalar>(val));
371 } else {
372 myImpl->checkBuilder<std::string, arrow::FloatBuilder>(attr, arrow::float32);
373 myImpl->myValues.push_back(isNull ? nullptr : std::make_shared<arrow::FloatScalar>((float)val));
374 }
375}
376
377
378void
379ParquetFormatter::writeAttr(std::ostream& /* into */, const std::string& attr, const int& val, const bool isNull, const bool /* escape */) {
380 assert(!myImpl->myCheckColumns);
381 myImpl->checkBuilder<std::string, arrow::Int32Builder>(attr, arrow::int32);
382 myImpl->myValues.push_back(isNull ? nullptr : std::make_shared<arrow::Int32Scalar>(val));
383}
384
385
386void
387ParquetFormatter::writeStringAttr(const SumoXMLAttr attr, const std::string& val) {
388 myImpl->checkAttr(attr);
389 myImpl->checkBuilder<SumoXMLAttr, arrow::StringBuilder>(attr, arrow::utf8);
390 myImpl->myValues.push_back(std::make_shared<arrow::StringScalar>(val));
391}
392
393
394void
395ParquetFormatter::writeStringAttr(const std::string& attr, const std::string& val) {
396 assert(!myImpl->myCheckColumns);
397 myImpl->checkBuilder<std::string, arrow::StringBuilder>(attr, arrow::utf8);
398 myImpl->myValues.push_back(std::make_shared<arrow::StringScalar>(val));
399}
400
401
402void
404 myImpl->checkAttr(attr);
405 myImpl->checkBuilder<SumoXMLAttr, arrow::StringBuilder>(attr, arrow::utf8);
406 myImpl->myValues.push_back(nullptr);
407}
408
409
410void
411ParquetFormatter::writeNullAttr(const std::string& attr) {
412 assert(!myImpl->myCheckColumns);
413 myImpl->checkBuilder<std::string, arrow::StringBuilder>(attr, arrow::utf8);
414 myImpl->myValues.push_back(nullptr);
415}
416
417
418void
419ParquetFormatter::writeTime(std::ostream& /* into */, const SumoXMLAttr attr, const SUMOTime val) {
420 if (!gHumanReadableTime) {
421 // always float64 for machine-readable time, regardless of stream precision
422 myImpl->checkBuilder<SumoXMLAttr, arrow::DoubleBuilder>(attr, arrow::float64);
423 myImpl->myValues.push_back(std::make_shared<arrow::DoubleScalar>(STEPS2TIME(val)));
424 return;
425 }
426 writeStringAttr(attr, time2string(val));
427}
428
429
430bool
432 return myImpl->myWroteHeader;
433}
434
435
436void
438 myImpl->myExpectedAttrs = expected;
439 myImpl->myMaxDepth = depth;
440 myImpl->myCheckColumns = expected.any();
441}
442
443
444/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define WRITE_ERRORF(...)
Definition MsgHandler.h:308
#define WRITE_WARNING(msg)
Definition MsgHandler.h:298
#define TLF(string,...)
Definition MsgHandler.h:318
OutputFormatterType
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:107
#define STEPS2TIME(x)
Definition SUMOTime.h:58
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::bitset< 96 > SumoXMLAttrMask
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ SUMO_ATTR_Y
@ SUMO_ATTR_X
@ SUMO_ATTR_XMLNS
@ SUMO_ATTR_SCHEMA_LOCATION
bool gHumanReadableTime
Definition StdDefs.cpp:31
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
bool closed() const override
arrow::Status Close() override
arrow::Status Flush() override
arrow::Status Write(const void *data, int64_t nbytes) override
ArrowOStreamWrapper(std::ostream &out)
arrow::Result< int64_t > Tell() const override
Abstract base class for output formatters.
ParquetFormatter(const std::string &columnNames, const std::string &compression="", const int batchSize=1000000)
Constructor.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool) override
Writes an "XML header".
void writeNullAttr(const SumoXMLAttr attr)
void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth) override
Which elements are expected and which maximum depth the XML tree has.
void openTag(std::ostream &into, const std::string &xmlElement) override
Keeps track of an open XML tag by adding a new element to the stack.
~ParquetFormatter() override
Destructor (out-of-line: Impl is incomplete here)
bool wroteHeader() const override
Whether a complete row has been encountered and triggered writing.
void writeAttr(std::ostream &into, const SumoXMLAttr attr, const T &val, const bool isNull, const bool)
Writes a named attribute.
std::unique_ptr< Impl > myImpl
void writeStringAttr(const SumoXMLAttr attr, const std::string &val)
non-template helpers; defined in the .cpp where arrow/parquet are available
bool closeTag(std::ostream &into, const std::string &comment="") override
Closes the most recently opened tag.
void writeTime(std::ostream &into, const SumoXMLAttr attr, const SUMOTime val) override
Writes a time value.
Definition json.hpp:5639
void checkBuilder(const ATTR_TYPE &attr, const std::shared_ptr< arrow::DataType > &(*dataType)())
parquet::Compression::type myCompression
the compression to use
std::vector< std::shared_ptr< arrow::Scalar > > myValues
the current attribute / column values
std::vector< std::string > myFullHeader
the column names if we write the full name
std::vector< std::shared_ptr< arrow::ArrayBuilder > > myBuilders
the content array builders for the table
std::unique_ptr< parquet::arrow::FileWriter > myParquetWriter
the output stream writer
std::shared_ptr< arrow::Schema > mySchema
the table schema
SumoXMLAttrMask mySeenAttrs
the attributes already seen (including null values)
void checkAttr(const SumoXMLAttr attr)
std::vector< std::pair< const std::string, int > > myXMLStack
The name and number of attributes in the currently open XML elements.
bool myNeedsWrite
whether there is still unwritten data
Impl(const std::string &columnNames, const int batchSize)
const int myBatchSize
the number of rows to write per batch
int myMaxDepth
the maximum depth of the XML hierarchy
SumoXMLAttrMask myExpectedAttrs
the attributes which are expected for a complete row (including null values)
bool myCheckColumns
whether the columns should be checked for completeness
bool myWroteHeader
whether the schema has been constructed completely
bool myHaveRootAttrs
whether any root attribute have been encountered
const std::string myHeaderFormat
the format to use for the column names