Abhisheklearn12 commented on code in PR #9379:
URL: https://github.com/apache/arrow-rs/pull/9379#discussion_r2789472604


##########
arrow-json/src/reader/run_end_array.rs:
##########
@@ -0,0 +1,150 @@
+// 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::reader::tape::Tape;
+use crate::reader::{ArrayDecoder, DecoderContext};
+use arrow_array::Array;
+use arrow_data::transform::MutableArrayData;
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType};
+
+pub struct RunEndEncodedArrayDecoder {
+    data_type: DataType,
+    decoder: Box<dyn ArrayDecoder>,
+}
+
+impl RunEndEncodedArrayDecoder {
+    pub fn new(
+        ctx: &DecoderContext,
+        data_type: &DataType,
+        is_nullable: bool,
+    ) -> Result<Self, ArrowError> {
+        let values_field = match data_type {
+            DataType::RunEndEncoded(_, v) => v,
+            _ => unreachable!(),
+        };
+        let decoder = ctx.make_decoder(
+            values_field.data_type(),
+            values_field.is_nullable() || is_nullable,
+        )?;
+
+        Ok(Self {
+            data_type: data_type.clone(),
+            decoder,
+        })
+    }
+}
+
+impl ArrayDecoder for RunEndEncodedArrayDecoder {
+    fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayData, 
ArrowError> {
+        let run_ends_type = match &self.data_type {
+            DataType::RunEndEncoded(r, _) => r.data_type(),
+            _ => unreachable!(),
+        };
+
+        let len = pos.len();
+        if len == 0 {
+            let empty_run_ends = new_empty_run_ends(run_ends_type);

Review Comment:
   Yeah, `new_empty_array` handles this cleanly. The decode call was there to 
get a typed empty values array but that's redundant when `new_empty_array` 
already constructs the full REE structure. So, will simplify it.                
                                                                                
                              
                                                  



##########
arrow-json/src/reader/run_end_array.rs:
##########
@@ -0,0 +1,150 @@
+// 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::reader::tape::Tape;
+use crate::reader::{ArrayDecoder, DecoderContext};
+use arrow_array::Array;
+use arrow_data::transform::MutableArrayData;
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType};
+
+pub struct RunEndEncodedArrayDecoder {
+    data_type: DataType,
+    decoder: Box<dyn ArrayDecoder>,
+}
+
+impl RunEndEncodedArrayDecoder {
+    pub fn new(
+        ctx: &DecoderContext,
+        data_type: &DataType,
+        is_nullable: bool,
+    ) -> Result<Self, ArrowError> {
+        let values_field = match data_type {
+            DataType::RunEndEncoded(_, v) => v,
+            _ => unreachable!(),
+        };
+        let decoder = ctx.make_decoder(
+            values_field.data_type(),
+            values_field.is_nullable() || is_nullable,
+        )?;
+
+        Ok(Self {
+            data_type: data_type.clone(),
+            decoder,
+        })
+    }
+}
+
+impl ArrayDecoder for RunEndEncodedArrayDecoder {
+    fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayData, 
ArrowError> {
+        let run_ends_type = match &self.data_type {
+            DataType::RunEndEncoded(r, _) => r.data_type(),
+            _ => unreachable!(),
+        };
+
+        let len = pos.len();
+        if len == 0 {
+            let empty_run_ends = new_empty_run_ends(run_ends_type);
+            let empty_values = self.decoder.decode(tape, &[])?;
+            let data = ArrayDataBuilder::new(self.data_type.clone())
+                .len(0)
+                .add_child_data(empty_run_ends)
+                .add_child_data(empty_values);
+            // Safety:
+            // Valid by construction
+            return Ok(unsafe { data.build_unchecked() });
+        }
+
+        let flat_data = self.decoder.decode(tape, pos)?;
+
+        let mut run_ends: Vec<i64> = Vec::new();
+        let mut mutable = MutableArrayData::new(vec![&flat_data], false, len);
+
+        let mut run_start = 0;
+        for i in 1..len {
+            if !same_run(&flat_data, run_start, i) {
+                run_ends.push(i as i64);
+                mutable.extend(0, run_start, run_start + 1);
+                run_start = i;
+            }
+        }
+        run_ends.push(len as i64);
+        mutable.extend(0, run_start, run_start + 1);
+
+        let values_data = mutable.freeze();
+        let run_ends_data = build_run_ends_array(run_ends_type, run_ends)?;
+
+        let data = ArrayDataBuilder::new(self.data_type.clone())
+            .len(len)
+            .add_child_data(run_ends_data)
+            .add_child_data(values_data);
+
+        // Safety:
+        // Valid by construction

Review Comment:
   The `run_ends` are built strictly increasing from the encoding loop, with 
the last value always equal to `len`, and `values` has the same length as 
`run_ends`, so the REE layout invariants are satisfied. I can spell that out in 
the comment instead if you'd prefer.   



##########
arrow-json/src/reader/run_end_array.rs:
##########
@@ -0,0 +1,150 @@
+// 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::reader::tape::Tape;
+use crate::reader::{ArrayDecoder, DecoderContext};
+use arrow_array::Array;
+use arrow_data::transform::MutableArrayData;
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType};
+
+pub struct RunEndEncodedArrayDecoder {
+    data_type: DataType,
+    decoder: Box<dyn ArrayDecoder>,
+}
+
+impl RunEndEncodedArrayDecoder {
+    pub fn new(
+        ctx: &DecoderContext,
+        data_type: &DataType,
+        is_nullable: bool,
+    ) -> Result<Self, ArrowError> {
+        let values_field = match data_type {
+            DataType::RunEndEncoded(_, v) => v,
+            _ => unreachable!(),
+        };
+        let decoder = ctx.make_decoder(
+            values_field.data_type(),
+            values_field.is_nullable() || is_nullable,
+        )?;
+
+        Ok(Self {
+            data_type: data_type.clone(),
+            decoder,
+        })
+    }
+}
+
+impl ArrayDecoder for RunEndEncodedArrayDecoder {
+    fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayData, 
ArrowError> {
+        let run_ends_type = match &self.data_type {
+            DataType::RunEndEncoded(r, _) => r.data_type(),
+            _ => unreachable!(),
+        };
+
+        let len = pos.len();
+        if len == 0 {
+            let empty_run_ends = new_empty_run_ends(run_ends_type);
+            let empty_values = self.decoder.decode(tape, &[])?;
+            let data = ArrayDataBuilder::new(self.data_type.clone())
+                .len(0)
+                .add_child_data(empty_run_ends)
+                .add_child_data(empty_values);
+            // Safety:
+            // Valid by construction
+            return Ok(unsafe { data.build_unchecked() });
+        }
+
+        let flat_data = self.decoder.decode(tape, pos)?;
+
+        let mut run_ends: Vec<i64> = Vec::new();
+        let mut mutable = MutableArrayData::new(vec![&flat_data], false, len);
+
+        let mut run_start = 0;
+        for i in 1..len {
+            if !same_run(&flat_data, run_start, i) {
+                run_ends.push(i as i64);
+                mutable.extend(0, run_start, run_start + 1);
+                run_start = i;
+            }
+        }
+        run_ends.push(len as i64);
+        mutable.extend(0, run_start, run_start + 1);
+
+        let values_data = mutable.freeze();
+        let run_ends_data = build_run_ends_array(run_ends_type, run_ends)?;
+
+        let data = ArrayDataBuilder::new(self.data_type.clone())
+            .len(len)
+            .add_child_data(run_ends_data)
+            .add_child_data(values_data);
+
+        // Safety:
+        // Valid by construction
+        Ok(unsafe { data.build_unchecked() })
+    }
+}
+
+fn same_run(data: &ArrayData, i: usize, j: usize) -> bool {
+    let null_i = data.is_null(i);
+    let null_j = data.is_null(j);
+    if null_i != null_j {
+        return false;
+    }
+    if null_i {
+        return true;
+    }
+    data.slice(i, 1) == data.slice(j, 1)
+}
+
+fn build_run_ends_array(dt: &DataType, run_ends: Vec<i64>) -> 
Result<ArrayData, ArrowError> {

Review Comment:
   good idea,  I'll make the decoder generic over `R: RunEndIndexType` and 
dispatch in `make_decoder`, that removes the `i64` conversion entirely.



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