lidavidm commented on a change in pull request #11863:
URL: https://github.com/apache/arrow/pull/11863#discussion_r766826456



##########
File path: cpp/src/arrow/csv/writer.cc
##########
@@ -462,25 +483,32 @@ class CSVWriterImpl : public ipc::RecordBatchWriter {
     return Status::OK();
   }
 
-  int64_t CalculateHeaderSize() const {
+  Result<int64_t> CalculateHeaderSize() const {
     int64_t header_length = 0;
     for (int col = 0; col < schema_->num_fields(); col++) {
       const std::string& col_name = schema_->field(col)->name();
+      int64_t escaped_count = CountQuotes(col_name);
+      if (!options_.escaping && escaped_count > 0) {
+        return Status::Invalid("Need to escape in CSV header, but escaping is 
disabled.");
+      }
       header_length += col_name.size();
-      header_length += CountQuotes(col_name);
+      header_length += escaped_count;
     }
     return header_length + (kQuoteDelimiterCount * schema_->num_fields());
   }
 
   Status WriteHeader() {
     // Only called once, as part of initialization
-    RETURN_NOT_OK(data_buffer_->Resize(CalculateHeaderSize(), 
/*shrink_to_fit=*/false));
+    int64_t header_size;
+    ASSIGN_OR_RAISE(header_size, CalculateHeaderSize());

Review comment:
       N.B. the ASSIGN_OR_RAISE macro lets you declare the variable inline 
(`ASSIGN_OR_RAISE(int64_t header_size, CalculateHeaderSize());`)

##########
File path: cpp/src/arrow/csv/writer_test.cc
##########
@@ -49,6 +49,8 @@ struct WriterTestParams {
   WriteOptions options;
   util::optional<std::string> expected_output;
   Status expected_status;
+  bool options_invalid;
+  bool escaping_invalid;

Review comment:
       It seems these aren't used anymore?




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to