DDtKey commented on code in PR #3365:
URL: https://github.com/apache/arrow-rs/pull/3365#discussion_r1081030906


##########
arrow-csv/src/reader/records.rs:
##########
@@ -0,0 +1,266 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow_schema::ArrowError;
+use csv_core::{ReadRecordResult, Reader};
+use std::io::BufRead;
+
+/// The estimated length of a field in bytes
+const AVERAGE_FIELD_SIZE: usize = 8;
+
+/// The minimum amount of data in a single read
+const MIN_CAPACITY: usize = 1024;
+
+pub struct RecordReader<R> {
+    reader: R,
+    delimiter: Reader,
+
+    num_columns: usize,
+
+    num_rows: usize,
+    offsets: Vec<usize>,
+    data: Vec<u8>,
+}
+
+impl<R: BufRead> RecordReader<R> {
+    pub fn new(reader: R, delimiter: Reader, num_columns: usize) -> Self {
+        Self {
+            reader,
+            delimiter,
+            num_columns,
+            num_rows: 0,
+            offsets: vec![],
+            data: vec![],
+        }
+    }
+
+    fn fill_buf(&mut self, to_read: usize) -> Result<(), ArrowError> {
+        // Reserve sufficient capacity in offsets
+        self.offsets.resize(to_read * self.num_columns + 1, 0);
+        self.num_rows = 0;
+
+        if to_read == 0 {
+            return Ok(());
+        }
+
+        // The current offset into `self.data`
+        let mut output_offset = 0;
+        // The current offset into `input`
+        let mut input_offset = 0;
+        // The current offset into `self.offsets`
+        let mut field_offset = 1;
+        // The number of fields read for the current row
+        let mut field_count = 0;
+
+        'outer: loop {
+            let input = self.reader.fill_buf()?;
+
+            'input: loop {
+                // Reserve necessary space in output data based on best 
estimate
+                let remaining_rows = to_read - self.num_rows;
+                let capacity = remaining_rows * self.num_columns * 
AVERAGE_FIELD_SIZE;
+                let estimated_data = capacity.max(MIN_CAPACITY);
+                self.data.resize(output_offset + estimated_data, 0);
+
+                loop {
+                    let (result, bytes_read, bytes_written, end_positions) =
+                        self.delimiter.read_record(
+                            &input[input_offset..],
+                            &mut self.data[output_offset..],
+                            &mut self.offsets[field_offset..],
+                        );
+
+                    field_count += end_positions;
+                    field_offset += end_positions;
+                    input_offset += bytes_read;
+                    output_offset += bytes_written;
+
+                    match result {
+                        ReadRecordResult::End => break 'outer, // Reached end 
of file
+                        ReadRecordResult::InputEmpty => break 'input, // Input 
exhausted, need to read more
+                        ReadRecordResult::OutputFull => break, // Need to 
allocate more capacity
+                        ReadRecordResult::OutputEndsFull => {
+                            return Err(ArrowError::CsvError(format!("incorrect 
number of fields, expected {} got more than {}", self.num_columns, 
field_count)))

Review Comment:
   @tustvold yes, I did & it prints. But the line in files are correct, it 
always refer to first one in my case. Probably it's related to intermediate 
schemas during execution 🤔  As I said, I'll try to create a MRE, otherwise it's 
hard to explain



-- 
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

Reply via email to