You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Posnet (via GitHub)" <gi...@apache.org> on 2024/04/22 15:43:23 UTC

[PR] Add support for flexible column lengths [arrow-rs]

Posnet opened a new pull request, #5679:
URL: https://github.com/apache/arrow-rs/pull/5679

   Similar to what is supported in the csv crate, as well as the pandas, arrow-cpp and polars crates. A subset of CSV files treat missing columns at the end of rows as null (if the schema allows it). This commit adds support to optionally enable treating such missing columns as null. The default behavior is still to treat an incorrect number of columns as an error.
   
   # Which issue does this PR close?
   
   Closes 5678
    
   
   # What changes are included in this PR?
   
   Added boolean config fields to Format and Decoder to allow for specifying whether flexible columns are desired. And provide the ability to set when via builder methods.
   The change also passes down the new flexible fields to the rust csv config.
   The change also updates the decoder such that if flexible columns are enabled it will pad the offset buffers with empty offsets in order to allow for null values to be interpreted at parsing.
   The change also adds unit tests for verify the new behavior.
   
   # Are there any user-facing changes?
   
   Yes, it extends the API surface of the Format struct and ReaderBuilder struct. It also adds a set_flexible_lengths method to the RecordDecoder struct since adding the field to the `new` constructor would be a public api break. I believe the changes are only semantically minor.


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1590735760


##########
arrow-csv/src/reader/mod.rs:
##########
@@ -265,6 +266,14 @@ impl Format {
         self
     }
 
+    /// Whether to allow flexible lengths for records.

Review Comment:
   `truncated_rows` does sound clearer. Though, `with_allow_truncated_rows()` does seem a bit unwieldy as a name; do you think `with_truncated_rows()` might be sufficient?



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1575933006


##########
arrow-csv/test/data/flexible_columns.csv:
##########


Review Comment:
   Oh I'm a little confused by this, as this file seems just a regular valid CSV file?
   
   All the rows have the expected number of columns & delimiters :thinking: 
   
   Am I misunderstanding what the change actually represents?



##########
arrow-csv/src/reader/mod.rs:
##########
@@ -265,6 +266,14 @@ impl Format {
         self
     }
 
+    /// Whether to allow flexible lengths for records.

Review Comment:
   What do you think about `incomplete_rows`?



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Posnet (via GitHub)" <gi...@apache.org>.
Posnet commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1583992897


##########
arrow-csv/test/data/flexible_columns.csv:
##########


Review Comment:
   Sorry about the confusion, it turns out my IDE had a feature that automatically formatted CSV files, which included adding missing commas 😅 
   I've added more example rows to the file, it should be clearer now.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Posnet (via GitHub)" <gi...@apache.org>.
Posnet commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1591621047


##########
arrow-csv/test/data/truncated_rows.csv:
##########
@@ -0,0 +1,7 @@
+Name,Age,Occupation,DOB
+A1,34,Engineer,1985-07-16
+B2,29,Doctor
+,,

Review Comment:
   Empty rows are ignored, which appears to be part of the default behavior of CSV core.
   I've updated the tests for truncated rows to include ensuring this is the case (i.e. in case the default changes in future, someone will notice when the tests break).



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Posnet (via GitHub)" <gi...@apache.org>.
Posnet commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1583993623


##########
arrow-csv/src/reader/mod.rs:
##########
@@ -265,6 +266,14 @@ impl Format {
         self
     }
 
+    /// Whether to allow flexible lengths for records.

Review Comment:
   I've pushed a new version with `allow_truncated_rows`. It's like incomplete rows, but maybe a bit clearer that it is only the end of the row that can be incomplete/missing.
   



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1590730608


##########
arrow-csv/src/reader/records.rs:
##########
@@ -172,6 +188,11 @@ impl RecordDecoder {
         self.num_rows = 0;
     }
 
+    /// Sets the decoder to allow rows with less than the expected number columns
+    pub fn set_allow_truncated_rows(&mut self, allow: bool) {
+        self.allow_truncated_rows = allow;
+    }

Review Comment:
   Maybe we can put this directly in the `new()`? I don't think `RecordDecoder` is a public API so should be good to change `new()` :thinking: 



##########
arrow-csv/src/reader/mod.rs:
##########
@@ -265,6 +266,14 @@ impl Format {
         self
     }
 
+    /// Whether to allow flexible lengths for records.

Review Comment:
   `truncated_rows` does sound clearer. Thought, `with_allow_truncated_rows()` does seem a bit unwieldy as a name; do you think `with_truncated_rows()` might be sufficient?



##########
arrow-csv/src/reader/records.rs:
##########
@@ -127,10 +134,19 @@ impl RecordDecoder {
                     }
                     ReadRecordResult::Record => {
                         if self.current_field != self.num_columns {
-                            return Err(ArrowError::CsvError(format!(
-                                "incorrect number of fields for line {}, expected {} got {}",
-                                self.line_number, self.num_columns, self.current_field
-                            )));
+                            if self.allow_truncated_rows && self.current_field < self.num_columns {
+                                // If the number of fields is less than expected, pad with nulls
+                                let fill_count = self.num_columns - self.current_field;
+                                let fill_value = self.offsets[self.offsets_len - 1];
+                                self.offsets[self.offsets_len..self.offsets_len + fill_count]
+                                    .fill(fill_value);
+                                self.offsets_len += fill_count;
+                            } else {
+                                return Err(ArrowError::CsvError(format!(
+                                    "incorrect number of fields for line {}, expected {} got {}",
+                                    self.line_number, self.num_columns, self.current_field
+                                )));
+                            }

Review Comment:
   Not too familiar with the `RecordDecoder` code but this looks good to me :+1: 



##########
arrow-csv/test/data/truncated_rows.csv:
##########
@@ -0,0 +1,7 @@
+Name,Age,Occupation,DOB
+A1,34,Engineer,1985-07-16
+B2,29,Doctor
+,,

Review Comment:
   I'm curious if a completely empty line would introduce just a row of nulls?



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#issuecomment-2098043030

   Thanks for this; I think this should be good to go once the clippy lint is fixed (can ignore docs and integration CI failures are unrelated)


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1575382060


##########
arrow-csv/src/reader/mod.rs:
##########
@@ -265,6 +266,14 @@ impl Format {
         self
     }
 
+    /// Whether to allow flexible lengths for records.

Review Comment:
   ```suggestion
       /// Whether to allow flexible number of columns for records.
       /// If enabled, records with not enough delimited values compared to expected
       /// number of columns will be padded with nulls for the missing columns.
   ```
   
   Maybe something like this to make it clearer?



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Posnet (via GitHub)" <gi...@apache.org>.
Posnet commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1575428702


##########
arrow-csv/src/reader/mod.rs:
##########
@@ -265,6 +266,14 @@ impl Format {
         self
     }
 
+    /// Whether to allow flexible lengths for records.

Review Comment:
   Yes, I'm not sure how verbose to be in the naming. I've always called them ragged rows, but a colleague said that was confusing as well.
   
   Maybe being even more explicit as `allow_trailing_empty_values_without_separarator` would be better?



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Posnet (via GitHub)" <gi...@apache.org>.
Posnet commented on PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#issuecomment-2083990472

   I've pushed a new version with clearer language, specifically with the option `allow_truncated_rows` since truncated can only mean the end of something is missing, and we are actually truncating a row (i.e. less columns) not truncating the column itself.
   
   I've also added more tests to ensure that the default case returns the expected error,
   
   and a test for non-nullable schemas to ensure they still return an error even if truncated rows are allowed.


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#issuecomment-2100194310

   Thanks for this @Posnet :+1: 


-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add support for flexible column lengths [arrow-rs]

Posted by "Posnet (via GitHub)" <gi...@apache.org>.
Posnet commented on code in PR #5679:
URL: https://github.com/apache/arrow-rs/pull/5679#discussion_r1591619911


##########
arrow-csv/src/reader/records.rs:
##########
@@ -172,6 +188,11 @@ impl RecordDecoder {
         self.num_rows = 0;
     }
 
+    /// Sets the decoder to allow rows with less than the expected number columns
+    pub fn set_allow_truncated_rows(&mut self, allow: bool) {
+        self.allow_truncated_rows = allow;
+    }

Review Comment:
   Makes sense it isn't on Docs.rs, but it was marked pub so I wasn't sure.
   I've made this change as a second commit in case it needs to be removed.



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org