HuaHuaY commented on code in PR #47524: URL: https://github.com/apache/arrow/pull/47524#discussion_r2330682136
########## cpp/src/arrow/csv/writer.cc: ########## @@ -594,18 +604,33 @@ class CSVWriterImpl : public ipc::RecordBatchWriter { // Only called once, as part of initialization RETURN_NOT_OK(data_buffer_->Resize(CalculateHeaderSize(), /*shrink_to_fit=*/false)); char* next = reinterpret_cast<char*>(data_buffer_->mutable_data()); + int64_t no_quoting_count = 0; for (int col = 0; col < schema_->num_fields(); ++col) { - *next++ = '"'; - next = Escape(schema_->field(col)->name(), next); - *next++ = '"'; + const std::string& col_name = schema_->field(col)->name(); + if (options_.quoting_header != QuotingStyle::None || + StopAtStructuralChar(reinterpret_cast<const uint8_t*>(col_name.c_str()), + col_name.length(), options_.delimiter) != + static_cast<int64_t>(col_name.length())) { + *next++ = '"'; + next = Escape(schema_->field(col)->name(), next); + *next++ = '"'; + } else { + memcpy(next, col_name.data(), col_name.size()); + next += col_name.size(); + no_quoting_count += 1; + } if (col != schema_->num_fields() - 1) { *next++ = options_.delimiter; } } memcpy(next, options_.eol.data(), options_.eol.size()); next += options_.eol.size(); - DCHECK_EQ(reinterpret_cast<uint8_t*>(next), + DCHECK_EQ(reinterpret_cast<uint8_t*>(next) + no_quoting_count * kQuoteCount, data_buffer_->data() + data_buffer_->size()); + if (no_quoting_count != 0) { + RETURN_NOT_OK(data_buffer_->Resize( + data_buffer_->size() - no_quoting_count * kQuoteCount, /*shrink_to_fit=*/true)); Review Comment: My mistakes. Fixed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org