Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cpp/src/arrow/csv/writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class UnquotedColumnPopulator : public ColumnPopulator {
return VisitArrayDataInline<StringType>(
*casted_array_->data(),
[&](arrow::util::string_view s) {
ARROW_RETURN_NOT_OK(CheckStringHasNoStructuralChars(s));
RETURN_NOT_OK(CheckStringHasNoStructuralChars(s));
return valid_function(s);
},
null_function);
Expand Down Expand Up @@ -548,6 +548,18 @@ Status WriteCSV(const RecordBatch& batch, const WriteOptions& options,
return writer->Close();
}

Status WriteCSV(const std::shared_ptr<RecordBatchReader>& reader,
const WriteOptions& options, arrow::io::OutputStream* output) {
ASSIGN_OR_RAISE(auto writer, MakeCSVWriter(output, reader->schema(), options));
std::shared_ptr<RecordBatch> batch;
while (true) {
ASSIGN_OR_RAISE(batch, reader->Next());
if (batch == nullptr) break;
RETURN_NOT_OK(writer->WriteRecordBatch(*batch));
}
return writer->Close();
}

ARROW_EXPORT
Result<std::shared_ptr<ipc::RecordBatchWriter>> MakeCSVWriter(
std::shared_ptr<io::OutputStream> sink, const std::shared_ptr<Schema>& schema,
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/csv/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ ARROW_EXPORT Status WriteCSV(const Table& table, const WriteOptions& options,
/// Experimental
ARROW_EXPORT Status WriteCSV(const RecordBatch& batch, const WriteOptions& options,
arrow::io::OutputStream* output);
/// \brief Converts batches read through a RecordBatchReader
/// to CSV and writes the results to output.
/// Experimental
ARROW_EXPORT Status WriteCSV(const std::shared_ptr<RecordBatchReader>& reader,
const WriteOptions& options,
arrow::io::OutputStream* output);

/// \brief Create a new CSV writer. User is responsible for closing the
/// actual OutputStream.
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/csv/writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ TEST_P(TestWriteCSV, TestWrite) {
ASSERT_OK_AND_ASSIGN(csv, ToCsvString(*table, options));
EXPECT_EQ(csv, GetParam().expected_output);

// RecordBatchReader should work identically.
ASSERT_OK_AND_ASSIGN(std::shared_ptr<RecordBatchReader> reader,
RecordBatchReader::Make({record_batch}));
ASSERT_OK_AND_ASSIGN(csv, ToCsvString(reader, options));
EXPECT_EQ(csv, GetParam().expected_output);

// The writer should work identically.
ASSERT_OK_AND_ASSIGN(csv, ToCsvStringUsingWriter(*table, options));
EXPECT_EQ(csv, GetParam().expected_output);
Expand Down