-
Notifications
You must be signed in to change notification settings - Fork 4.2k
GH-30431: [C++] Add EscapeStyle to CSV Writer for configurable escape control #50846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
waterWang
wants to merge
1
commit into
apache:main
Choose a base branch
from
waterWang:feat/csv-escape-style
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,16 +167,32 @@ class ColumnPopulator { | |
|
|
||
| // Copies the contents of s to out properly escaping any necessary characters. | ||
| // Returns the position next to last copied character. | ||
| char* Escape(std::string_view s, char* out) { | ||
| char* Escape(std::string_view s, char* out, EscapeStyle escape_style) { | ||
| for (const char c : s) { | ||
| if (c == '"' && escape_style == EscapeStyle::Backslash) { | ||
| *out++ = '\\'; | ||
| } | ||
| *out++ = c; | ||
| if (c == '"') { | ||
| if (c == '"' && escape_style == EscapeStyle::Double) { | ||
| *out++ = '"'; | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| // Returns the number of characters needed to escape the given string. | ||
| int64_t EscapedLength(std::string_view s, EscapeStyle escape_style) { | ||
|
Comment on lines
+183
to
+184
|
||
| int64_t quote_count = static_cast<int64_t>(std::count(s.begin(), s.end(), '"')); | ||
| switch (escape_style) { | ||
| case EscapeStyle::Double: | ||
| case EscapeStyle::Backslash: | ||
| return static_cast<int64_t>(s.length()) + quote_count; | ||
| case EscapeStyle::None: | ||
| return static_cast<int64_t>(s.length()); | ||
| } | ||
| return static_cast<int64_t>(s.length()); | ||
| } | ||
|
|
||
| // Return the index of the first structural char in the input. A structural char | ||
| // is a character that needs quoting and/or escaping. | ||
| int64_t StopAtStructuralChar(const uint8_t* data, const int64_t buffer_size, | ||
|
|
@@ -325,8 +341,10 @@ class UnquotedColumnPopulator : public ColumnPopulator { | |
| class QuotedColumnPopulator : public ColumnPopulator { | ||
| public: | ||
| QuotedColumnPopulator(MemoryPool* pool, std::string end_chars, | ||
| std::shared_ptr<Buffer> null_string) | ||
| : ColumnPopulator(pool, std::move(end_chars), std::move(null_string)) {} | ||
| std::shared_ptr<Buffer> null_string, | ||
| EscapeStyle escape_style) | ||
| : ColumnPopulator(pool, std::move(end_chars), std::move(null_string)), | ||
| escape_style_(escape_style) {} | ||
|
|
||
| Status UpdateRowLengths(int64_t* row_lengths) override { | ||
| if (ARROW_PREDICT_TRUE(array_->type_id() == Type::STRING)) { | ||
|
|
@@ -366,8 +384,7 @@ class QuotedColumnPopulator : public ColumnPopulator { | |
| // Each quote in the value string needs to be escaped. | ||
| int64_t escaped_count = CountQuotes(s); | ||
| row_needs_escaping_[row_number] = escaped_count > 0; | ||
| row_lengths[row_number] += | ||
| static_cast<int64_t>(s.length()) + escaped_count + kQuoteCount; | ||
| row_lengths[row_number] += EscapedLength(s, escape_style_) + kQuoteCount; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we reuse |
||
| row_number++; | ||
| }, | ||
| [&]() { | ||
|
|
@@ -401,7 +418,7 @@ class QuotedColumnPopulator : public ColumnPopulator { | |
| memcpy(row, s.data(), s.length()); | ||
| row += s.length(); | ||
| } else { | ||
| row = Escape(s, row); | ||
| row = Escape(s, row, escape_style_); | ||
| } | ||
| *row++ = '"'; | ||
| CopyEndChars(row, end_chars_.data(), end_chars_.length()); | ||
|
|
@@ -436,12 +453,13 @@ class QuotedColumnPopulator : public ColumnPopulator { | |
| // at some point we should change this to use memory_pool | ||
| // backed allocator. | ||
| std::vector<bool> row_needs_escaping_; | ||
| const EscapeStyle escape_style_; | ||
| }; | ||
|
|
||
| Result<std::unique_ptr<ColumnPopulator>> MakePopulator( | ||
| const DataType& type, const std::string& end_chars, const char delimiter, | ||
| const std::shared_ptr<Buffer>& null_string, QuotingStyle quoting_style, | ||
| MemoryPool* pool) { | ||
| EscapeStyle escape_style, MemoryPool* pool) { | ||
| auto make_populator = | ||
| [&](const auto& type) -> Result<std::unique_ptr<ColumnPopulator>> { | ||
| using Type = std::decay_t<decltype(type)>; | ||
|
|
@@ -459,7 +477,8 @@ Result<std::unique_ptr<ColumnPopulator>> MakePopulator( | |
| pool, end_chars, delimiter, null_string, | ||
| /*reject_values_with_quotes=*/false); | ||
| case QuotingStyle::AllValid: | ||
| return std::make_unique<QuotedColumnPopulator>(pool, end_chars, null_string); | ||
| return std::make_unique<QuotedColumnPopulator>(pool, end_chars, null_string, | ||
| escape_style); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -479,13 +498,14 @@ Result<std::unique_ptr<ColumnPopulator>> MakePopulator( | |
| case QuotingStyle::Needed: | ||
| [[fallthrough]]; | ||
| case QuotingStyle::AllValid: | ||
| return std::make_unique<QuotedColumnPopulator>(pool, end_chars, null_string); | ||
| return std::make_unique<QuotedColumnPopulator>(pool, end_chars, null_string, | ||
| escape_style); | ||
| } | ||
| } | ||
|
|
||
| if constexpr (std::is_same<Type, DictionaryType>::value) { | ||
| return MakePopulator(*type.value_type(), end_chars, delimiter, null_string, | ||
| quoting_style, pool); | ||
| quoting_style, escape_style, pool); | ||
| } | ||
|
|
||
| return Status::Invalid("Unsupported Type:", type.ToString()); | ||
|
|
@@ -496,9 +516,9 @@ Result<std::unique_ptr<ColumnPopulator>> MakePopulator( | |
| Result<std::unique_ptr<ColumnPopulator>> MakePopulator( | ||
| const Field& field, const std::string& end_chars, char delimiter, | ||
| const std::shared_ptr<Buffer>& null_string, QuotingStyle quoting_style, | ||
| MemoryPool* pool) { | ||
| EscapeStyle escape_style, MemoryPool* pool) { | ||
| return MakePopulator(*field.type(), end_chars, delimiter, null_string, quoting_style, | ||
| pool); | ||
| escape_style, pool); | ||
| } | ||
|
|
||
| class CSVWriterImpl : public ipc::RecordBatchWriter { | ||
|
|
@@ -525,7 +545,8 @@ class CSVWriterImpl : public ipc::RecordBatchWriter { | |
| ARROW_ASSIGN_OR_RAISE( | ||
| populators[col], | ||
| MakePopulator(*schema->field(col), end_chars, options.delimiter, null_string, | ||
| options.quoting_style, options.io_context.pool())); | ||
| options.quoting_style, options.escape_style, | ||
| options.io_context.pool())); | ||
| } | ||
| auto writer = std::make_shared<CSVWriterImpl>( | ||
| sink, std::move(owned_sink), std::move(schema), std::move(populators), options); | ||
|
|
@@ -601,13 +622,12 @@ class CSVWriterImpl : public ipc::RecordBatchWriter { | |
| int64_t header_length = 0; | ||
| for (int col = 0; col < schema_->num_fields(); col++) { | ||
| const std::string& col_name = schema_->field(col)->name(); | ||
| header_length += col_name.size(); | ||
| header_length += EscapedLength(col_name, options_.escape_style); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we call this when |
||
| switch (quoting_style) { | ||
| case QuotingStyle::None: | ||
| break; | ||
| case QuotingStyle::Needed: | ||
| case QuotingStyle::AllValid: | ||
| header_length += CountQuotes(col_name); | ||
| break; | ||
| } | ||
| } | ||
|
|
@@ -649,7 +669,7 @@ class CSVWriterImpl : public ipc::RecordBatchWriter { | |
| // regardless of whether it contains structural chars. | ||
| // We use consistent semantics for header names, which are strings. | ||
| *next++ = '"'; | ||
| next = Escape(schema_->field(col)->name(), next); | ||
| next = Escape(schema_->field(col)->name(), next, options_.escape_style); | ||
| *next++ = '"'; | ||
| break; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we simplify this?