This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new ec771cc372 Expose option to set line terminator for CSV writer (#9617)
ec771cc372 is described below
commit ec771cc372a119222a68759335715f824872d217
Author: Sava Vranešević <[email protected]>
AuthorDate: Fri Apr 3 22:57:38 2026 +0200
Expose option to set line terminator for CSV writer (#9617)
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
- Closes #9571.
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
Enable configuring line terminator for CSV writer.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
See above.
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Yes, added tests.
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
Yes, expose option to set line terminator for CSV writer.
---------
Co-authored-by: svranesevic <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
---
arrow-csv/src/writer.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 72 insertions(+), 4 deletions(-)
diff --git a/arrow-csv/src/writer.rs b/arrow-csv/src/writer.rs
index c38d1cdec3..72dcb640e1 100644
--- a/arrow-csv/src/writer.rs
+++ b/arrow-csv/src/writer.rs
@@ -358,6 +358,8 @@ pub struct WriterBuilder {
quote: u8,
/// Optional escape character. Defaults to `b'\\'`
escape: u8,
+ /// Optional line terminator. Defaults to `LF` (`\n`)
+ terminator: Terminator,
/// Enable double quote escapes. Defaults to `true`
double_quote: bool,
/// Optional date format for date arrays
@@ -380,6 +382,15 @@ pub struct WriterBuilder {
quote_style: QuoteStyle,
}
+/// The line terminator to use when writing CSV files.
+#[derive(Clone, Debug)]
+pub enum Terminator {
+ /// Use CRLF (`\r\n`) as the line terminator
+ CRLF,
+ /// Use the specified byte character as the line terminator
+ Any(u8),
+}
+
impl Default for WriterBuilder {
fn default() -> Self {
WriterBuilder {
@@ -387,6 +398,7 @@ impl Default for WriterBuilder {
has_header: true,
quote: b'"',
escape: b'\\',
+ terminator: Terminator::Any(b'\n'),
double_quote: true,
date_format: None,
datetime_format: None,
@@ -609,15 +621,33 @@ impl WriterBuilder {
self.quote_style
}
+ /// Set the CSV file's line terminator
+ pub fn with_line_terminator(mut self, terminator: Terminator) -> Self {
+ self.terminator = terminator;
+ self
+ }
+
+ /// Get the CSV file's line terminator, defaults to `LF` (`\n`)
+ pub fn line_terminator(&self) -> &Terminator {
+ &self.terminator
+ }
+
/// Create a new `Writer`
pub fn build<W: Write>(self, writer: W) -> Writer<W> {
let mut builder = csv::WriterBuilder::new();
+
+ let terminator = match self.terminator {
+ Terminator::CRLF => csv::Terminator::CRLF,
+ Terminator::Any(byte) => csv::Terminator::Any(byte),
+ };
+
let writer = builder
.delimiter(self.delimiter)
.quote(self.quote)
.quote_style(self.quote_style)
.double_quote(self.double_quote)
.escape(self.escape)
+ .terminator(terminator)
.from_writer(writer);
Writer {
writer,
@@ -1027,10 +1057,48 @@ sed do eiusmod
tempor,-556132.25,1,,2019-04-18T02:45:55.555,23:46:03,foo
let mut buffer: Vec<u8> = vec![];
file.read_to_end(&mut buffer).unwrap();
- assert_eq!(
- "c1,c2\n00:02,46:17\n00:02,\n",
- String::from_utf8(buffer).unwrap()
- );
+ let output = String::from_utf8(buffer).unwrap();
+ assert_eq!(output, "c1,c2\n00:02,46:17\n00:02,\n");
+ }
+
+ #[test]
+ fn test_write_csv_with_lf_terminator() {
+ let output = write_batch_with_terminator(Terminator::Any(b'\n'));
+ assert_eq!(output, "c1,c2\nhello,1\nworld,2\n");
+ }
+
+ #[test]
+ fn test_write_csv_with_crlf_terminator() {
+ let output = write_batch_with_terminator(Terminator::CRLF);
+ assert_eq!(output, "c1,c2\r\nhello,1\r\nworld,2\r\n");
+ }
+
+ #[test]
+ fn test_write_csv_with_any_terminator() {
+ let output = write_batch_with_terminator(Terminator::Any(b'|'));
+ assert_eq!(output, "c1,c2|hello,1|world,2|");
+ }
+
+ fn write_batch_with_terminator(terminator: Terminator) -> String {
+ let schema = Schema::new(vec![
+ Field::new("c1", DataType::Utf8, false),
+ Field::new("c2", DataType::UInt32, false),
+ ]);
+
+ let c1 = StringArray::from(vec!["hello", "world"]);
+ let c2 = PrimitiveArray::<UInt32Type>::from(vec![1, 2]);
+
+ let batch =
+ RecordBatch::try_new(Arc::new(schema), vec![Arc::new(c1),
Arc::new(c2)]).unwrap();
+
+ let mut buf = Vec::new();
+ let mut writer = WriterBuilder::new()
+ .with_line_terminator(terminator)
+ .build(&mut buf);
+ writer.write(&batch).unwrap();
+ drop(writer);
+
+ String::from_utf8(buf).unwrap()
}
#[test]