tustvold commented on code in PR #6820:
URL: https://github.com/apache/arrow-rs/pull/6820#discussion_r1864883393


##########
arrow-avro/src/reader/cursor.rs:
##########
@@ -0,0 +1,140 @@
+// 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;
+
+/// A wrapper around a byte slice, providing low-level decoding for Avro
+///
+/// <https://avro.apache.org/docs/1.11.1/specification/#encodings>
+#[derive(Debug)]
+pub(crate) struct AvroCursor<'a> {
+    buf: &'a [u8],
+    start_len: usize,
+}
+
+impl<'a> AvroCursor<'a> {
+    pub(crate) fn new(buf: &'a [u8]) -> Self {
+        Self {
+            buf,
+            start_len: buf.len(),
+        }
+    }
+
+    /// Returns the current cursor position
+    #[inline]
+    pub(crate) fn position(&self) -> usize {
+        self.start_len - self.buf.len()

Review Comment:
   This is where start_len is used



##########
arrow-avro/src/reader/cursor.rs:
##########
@@ -0,0 +1,140 @@
+// 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;
+
+/// A wrapper around a byte slice, providing low-level decoding for Avro
+///
+/// <https://avro.apache.org/docs/1.11.1/specification/#encodings>
+#[derive(Debug)]
+pub(crate) struct AvroCursor<'a> {
+    buf: &'a [u8],
+    start_len: usize,
+}
+
+impl<'a> AvroCursor<'a> {
+    pub(crate) fn new(buf: &'a [u8]) -> Self {
+        Self {
+            buf,
+            start_len: buf.len(),
+        }
+    }
+
+    /// Returns the current cursor position
+    #[inline]
+    pub(crate) fn position(&self) -> usize {
+        self.start_len - self.buf.len()
+    }
+
+    /// Read a single `u8`
+    #[inline]
+    pub(crate) fn get_u8(&mut self) -> Result<u8, ArrowError> {
+        match self.buf.first().copied() {
+            Some(x) => {
+                self.buf = &self.buf[1..];

Review Comment:
   It gets used to compute the cursor "position"



##########
arrow-avro/src/reader/record.rs:
##########
@@ -0,0 +1,292 @@
+// 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 crate::codec::{AvroDataType, Codec, Nullability};
+use crate::reader::block::{Block, BlockDecoder};
+use crate::reader::cursor::AvroCursor;
+use crate::reader::header::Header;
+use crate::schema::*;
+use arrow_array::types::*;
+use arrow_array::*;
+use arrow_buffer::*;
+use arrow_schema::{
+    ArrowError, DataType, Field as ArrowField, FieldRef, Fields, Schema as 
ArrowSchema, SchemaRef,
+};
+use std::collections::HashMap;
+use std::io::Read;
+use std::sync::Arc;
+
+/// Decodes avro encoded data into [`RecordBatch`]
+pub struct RecordDecoder {
+    schema: SchemaRef,
+    fields: Vec<Decoder>,
+}
+
+impl RecordDecoder {
+    pub fn try_new(data_type: &AvroDataType) -> Result<Self, ArrowError> {
+        match Decoder::try_new(data_type)? {
+            Decoder::Record(fields, encodings) => Ok(Self {
+                schema: Arc::new(ArrowSchema::new(fields)),
+                fields: encodings,
+            }),
+            encoding => Err(ArrowError::ParseError(format!(
+                "Expected record got {encoding:?}"
+            ))),
+        }
+    }
+
+    pub fn schema(&self) -> &SchemaRef {
+        &self.schema
+    }
+
+    /// Decode `count` records from `buf`
+    pub fn decode(&mut self, buf: &[u8], count: usize) -> Result<usize, 
ArrowError> {
+        let mut cursor = AvroCursor::new(buf);
+        for _ in 0..count {
+            for field in &mut self.fields {
+                field.decode(&mut cursor)?;
+            }
+        }
+        Ok(cursor.position())
+    }
+
+    /// Flush the decoded records into a [`RecordBatch`]
+    pub fn flush(&mut self) -> Result<RecordBatch, ArrowError> {
+        let arrays = self
+            .fields
+            .iter_mut()
+            .map(|x| x.flush(None))
+            .collect::<Result<Vec<_>, _>>()?;
+
+        RecordBatch::try_new(self.schema.clone(), arrays)
+    }
+}
+
+#[derive(Debug)]
+enum Decoder {
+    Null(usize),
+    Boolean(BooleanBufferBuilder),
+    Int32(Vec<i32>),
+    Int64(Vec<i64>),
+    Float32(Vec<f32>),
+    Float64(Vec<f64>),
+    Date32(Vec<i32>),
+    TimeMillis(Vec<i32>),
+    TimeMicros(Vec<i64>),
+    TimestampMillis(bool, Vec<i64>),
+    TimestampMicros(bool, Vec<i64>),
+    Binary(OffsetBufferBuilder<i32>, Vec<u8>),
+    String(OffsetBufferBuilder<i32>, Vec<u8>),
+    List(FieldRef, OffsetBufferBuilder<i32>, Box<Decoder>),
+    Record(Fields, Vec<Decoder>),
+    Nullable(Nullability, NullBufferBuilder, Box<Decoder>),
+}
+
+impl Decoder {
+    fn try_new(data_type: &AvroDataType) -> Result<Self, ArrowError> {
+        let nyi = |s: &str| Err(ArrowError::NotYetImplemented(s.to_string()));
+
+        let decoder = match data_type.codec() {
+            Codec::Null => Self::Null(0),
+            Codec::Boolean => 
Self::Boolean(BooleanBufferBuilder::new(DEFAULT_CAPACITY)),
+            Codec::Int32 => Self::Int32(Vec::with_capacity(DEFAULT_CAPACITY)),
+            Codec::Int64 => Self::Int64(Vec::with_capacity(DEFAULT_CAPACITY)),
+            Codec::Float32 => 
Self::Float32(Vec::with_capacity(DEFAULT_CAPACITY)),
+            Codec::Float64 => 
Self::Float64(Vec::with_capacity(DEFAULT_CAPACITY)),
+            Codec::Binary => Self::Binary(
+                OffsetBufferBuilder::new(DEFAULT_CAPACITY),
+                Vec::with_capacity(DEFAULT_CAPACITY),
+            ),
+            Codec::Utf8 => Self::String(
+                OffsetBufferBuilder::new(DEFAULT_CAPACITY),
+                Vec::with_capacity(DEFAULT_CAPACITY),
+            ),
+            Codec::Date32 => 
Self::Date32(Vec::with_capacity(DEFAULT_CAPACITY)),
+            Codec::TimeMillis => 
Self::TimeMillis(Vec::with_capacity(DEFAULT_CAPACITY)),
+            Codec::TimeMicros => 
Self::TimeMicros(Vec::with_capacity(DEFAULT_CAPACITY)),
+            Codec::TimestampMillis(is_utc) => {
+                Self::TimestampMillis(*is_utc, 
Vec::with_capacity(DEFAULT_CAPACITY))
+            }
+            Codec::TimestampMicros(is_utc) => {
+                Self::TimestampMicros(*is_utc, 
Vec::with_capacity(DEFAULT_CAPACITY))
+            }
+            Codec::Fixed(_) => return nyi("decoding fixed"),
+            Codec::Interval => return nyi("decoding interval"),
+            Codec::List(item) => {

Review Comment:
   Yeah, I wanted to keep the diff smaller, definitely more testing is needed 
before we make any of this public (currently all the module is private)



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