Skip to content

Commit e3a670e

Browse files
authored
Add ignore leading and trailing white space to csv parser (#8960)
# 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 #8961 # Rationale for this change Spark's csv writer can do this and it would be nice to have feature parity in projects like datafusion. 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. # What changes are included in this PR? Adds two new options for `ignore_leading_whitespace` and `ignore_trailing_whitespace` to csv writer. 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. # Are these changes tested? Yes 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)? # 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.
1 parent 91234b5 commit e3a670e

File tree

2 files changed

+409
-2
lines changed

2 files changed

+409
-2
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use arrow_array::*;
19+
use arrow_csv::WriterBuilder;
20+
use arrow_schema::*;
21+
use std::sync::Arc;
22+
23+
fn main() {
24+
// Create a sample schema with string columns
25+
let schema = Schema::new(vec![
26+
Field::new("name", DataType::Utf8, false),
27+
Field::new("city", DataType::Utf8, false),
28+
Field::new("country", DataType::Utf8, false),
29+
]);
30+
31+
// Create sample data with leading and trailing whitespace
32+
let name = StringArray::from(vec![
33+
" John Doe ",
34+
" Jane Smith",
35+
"Bob Johnson ",
36+
"Alice Williams",
37+
]);
38+
let city = StringArray::from(vec![
39+
" New York ",
40+
"Los Angeles ",
41+
" Chicago",
42+
"Houston",
43+
]);
44+
let country = StringArray::from(vec![" USA ", " USA ", " USA ", " USA "]);
45+
46+
let batch = RecordBatch::try_new(
47+
Arc::new(schema),
48+
vec![Arc::new(name), Arc::new(city), Arc::new(country)],
49+
)
50+
.unwrap();
51+
52+
println!("Original CSV (with whitespace):");
53+
let mut buf = Vec::new();
54+
let mut writer = WriterBuilder::new().build(&mut buf);
55+
writer.write(&batch).unwrap();
56+
drop(writer);
57+
println!("{}", String::from_utf8(buf).unwrap());
58+
59+
println!("\nCSV with ignore_leading_whitespace:");
60+
let mut buf = Vec::new();
61+
let mut writer = WriterBuilder::new()
62+
.with_ignore_leading_whitespace(true)
63+
.build(&mut buf);
64+
writer.write(&batch).unwrap();
65+
drop(writer);
66+
println!("{}", String::from_utf8(buf).unwrap());
67+
68+
println!("\nCSV with ignore_trailing_whitespace:");
69+
let mut buf = Vec::new();
70+
let mut writer = WriterBuilder::new()
71+
.with_ignore_trailing_whitespace(true)
72+
.build(&mut buf);
73+
writer.write(&batch).unwrap();
74+
drop(writer);
75+
println!("{}", String::from_utf8(buf).unwrap());
76+
77+
println!("\nCSV with both ignore_leading_whitespace and ignore_trailing_whitespace:");
78+
let mut buf = Vec::new();
79+
let mut writer = WriterBuilder::new()
80+
.with_ignore_leading_whitespace(true)
81+
.with_ignore_trailing_whitespace(true)
82+
.build(&mut buf);
83+
writer.write(&batch).unwrap();
84+
drop(writer);
85+
println!("{}", String::from_utf8(buf).unwrap());
86+
}

0 commit comments

Comments
 (0)