nevi-me commented on a change in pull request #491:
URL: https://github.com/apache/arrow-rs/pull/491#discussion_r679472996



##########
File path: arrow/src/array/array_map.rs
##########
@@ -0,0 +1,380 @@
+// 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 std::any::Any;
+use std::fmt;
+use std::mem;
+
+use super::make_array;
+use super::{
+    array::print_long_array, raw_pointer::RawPtrBox, Array, ArrayData, 
ArrayRef,
+};
+use crate::datatypes::{ArrowNativeType, DataType};
+use crate::error::ArrowError;
+
+/// A nested array type where each record is a key-value map.
+/// Keys should always be non-null, but values can be null.
+///
+/// [MapArray] is physically a [ListArray] that has a [StructArray]
+/// with 2 child fields.
+pub struct MapArray {
+    data: ArrayData,
+    values: ArrayRef,
+    value_offsets: RawPtrBox<i32>,
+}
+
+impl MapArray {
+    #[cfg(feature = "experimental-api")]
+    /// Returns a reference to the keys of this map.
+    pub fn keys(&self) -> ArrayRef {
+        make_array(self.values.data().child_data()[0].clone())
+    }
+
+    #[cfg(feature = "experimental-api")]
+    /// Returns a reference to the values of this map.
+    pub fn values(&self) -> ArrayRef {
+        make_array(self.values.data().child_data()[1].clone())
+    }
+
+    #[cfg(feature = "experimental-api")]
+    /// Returns the data type of the map's keys.
+    pub fn key_type(&self) -> DataType {
+        self.values.data().child_data()[0].data_type().clone()
+    }
+
+    #[cfg(feature = "experimental-api")]
+    /// Returns the data type of the map's values.
+    pub fn value_type(&self) -> DataType {
+        self.values.data().child_data()[1].data_type().clone()
+    }
+
+    /// Returns ith value of this map array.
+    /// # Safety
+    /// Caller must ensure that the index is within the array bounds
+    pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
+        let end = *self.value_offsets().get_unchecked(i + 1);
+        let start = *self.value_offsets().get_unchecked(i);
+        self.values
+            .slice(start.to_usize().unwrap(), (end - 
start).to_usize().unwrap())
+    }
+
+    /// Returns ith value of this map array.
+    pub fn value(&self, i: usize) -> ArrayRef {
+        let end = self.value_offsets()[i + 1] as usize;
+        let start = self.value_offsets()[i] as usize;
+        self.values.slice(start, end - start)
+    }
+
+    /// Returns the offset values in the offsets buffer
+    #[inline]
+    pub fn value_offsets(&self) -> &[i32] {
+        // Soundness
+        //     pointer alignment & location is ensured by RawPtrBox
+        //     buffer bounds/offset is ensured by the ArrayData instance.
+        unsafe {
+            std::slice::from_raw_parts(
+                self.value_offsets.as_ptr().add(self.data.offset()),
+                self.len() + 1,
+            )
+        }
+    }
+
+    /// Returns the length for value at index `i`.
+    #[inline]
+    pub fn value_length(&self, i: usize) -> i32 {
+        let offsets = self.value_offsets();
+        offsets[i + 1] - offsets[i]
+    }
+}
+
+impl From<ArrayData> for MapArray {
+    fn from(data: ArrayData) -> Self {
+        Self::try_new_from_array_data(data)
+            .expect("Expected infallable creation of MapArray from ArrayData 
failed")
+    }
+}
+
+impl MapArray {
+    fn try_new_from_array_data(data: ArrayData) -> Result<Self, ArrowError> {
+        if data.buffers().len() != 1 {
+            return Err(ArrowError::InvalidArgumentError(
+                format!("MapArray data should contain a single buffer only 
(value offsets), had {}",
+                        data.len())));
+        }
+
+        if data.child_data().len() != 1 {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "MapArray should contain a single child array (values array), 
had {}",
+                data.child_data().len()
+            )));
+        }
+
+        let entries = data.child_data()[0].clone();
+
+        if let DataType::Struct(fields) = entries.data_type() {
+            if fields.len() != 2 {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                "MapArray should contain a struct array with 2 fields, have {} 
fields",
+                fields.len()
+            )));
+            }
+        } else {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "MapArray should contain a struct array child, found {:?}",
+                entries.data_type()
+            )));
+        }
+
+        let values = make_array(entries);
+        let value_offsets = data.buffers()[0].as_ptr();
+
+        let value_offsets = unsafe { RawPtrBox::<i32>::new(value_offsets) };
+        unsafe {
+            if (*value_offsets.as_ptr().offset(0)) != 0 {
+                return Err(ArrowError::InvalidArgumentError(String::from(
+                    "offsets do not start at zero",
+                )));
+            }
+        }
+        Ok(Self {
+            data,
+            values,
+            value_offsets,
+        })
+    }
+}
+
+impl Array for MapArray {
+    fn as_any(&self) -> &Any {
+        self
+    }
+
+    fn data(&self) -> &ArrayData {
+        &self.data
+    }
+
+    /// Returns the total number of bytes of memory occupied by the buffers 
owned by this [MapArray].
+    fn get_buffer_memory_size(&self) -> usize {
+        self.data.get_buffer_memory_size()
+    }
+
+    /// Returns the total number of bytes of memory occupied physically by 
this [MapArray].
+    fn get_array_memory_size(&self) -> usize {
+        self.data.get_array_memory_size() + mem::size_of_val(self)
+    }
+}
+
+impl fmt::Debug for MapArray {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "MapArray\n[\n")?;
+        print_long_array(self, f, |array, index, f| {
+            fmt::Debug::fmt(&array.value(index), f)
+        })?;
+        write!(f, "]")
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use std::sync::Arc;
+
+    use crate::{
+        array::ArrayData,
+        array::{Int32Array, StructArray, UInt32Array},
+        buffer::Buffer,
+        datatypes::Field,
+        datatypes::ToByteSlice,
+    };
+
+    use super::*;
+
+    fn create_from_buffers() -> MapArray {
+        // Construct key and values
+        let keys_data = ArrayData::builder(DataType::Int32)
+            .len(8)
+            .add_buffer(Buffer::from(&[0, 1, 2, 3, 4, 5, 6, 
7].to_byte_slice()))
+            .build();
+        let values_data = ArrayData::builder(DataType::UInt32)
+            .len(8)
+            .add_buffer(Buffer::from(&[0u32, 1, 2, 3, 4, 5, 6, 
7].to_byte_slice()))
+            .build();
+
+        // Construct a buffer for value offsets, for the nested array:
+        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
+        let entry_offsets = Buffer::from(&[0, 3, 6, 8].to_byte_slice());
+
+        let keys = Field::new("keys", DataType::Int32, false);
+        let values = Field::new("values", DataType::UInt32, false);
+        let entry_struct = StructArray::from(vec![
+            (keys, make_array(keys_data)),
+            (values, make_array(values_data)),
+        ]);
+
+        // Construct a map array from the above two
+        let map_data_type = DataType::Map(
+            Box::new(Field::new(
+                "entries",
+                entry_struct.data_type().clone(),
+                true,
+            )),
+            false,
+        );
+        let map_data = ArrayData::builder(map_data_type)
+            .len(3)
+            .add_buffer(entry_offsets)
+            .add_child_data(entry_struct.data().clone())
+            .build();
+        MapArray::from(map_data)
+    }
+
+    #[test]
+    fn test_map_array() {
+        // Construct key and values
+        let key_data = ArrayData::builder(DataType::Int32)
+            .len(8)
+            .add_buffer(Buffer::from(&[0, 1, 2, 3, 4, 5, 6, 
7].to_byte_slice()))
+            .build();
+        let value_data = ArrayData::builder(DataType::UInt32)
+            .len(8)
+            .add_buffer(Buffer::from(&[0u32, 1, 2, 3, 4, 5, 6, 
7].to_byte_slice()))
+            .build();
+
+        // Construct a buffer for value offsets, for the nested array:
+        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
+        let entry_offsets = Buffer::from(&[0, 3, 6, 8].to_byte_slice());
+
+        let keys_field = Field::new("keys", DataType::Int32, false);
+        let values_field = Field::new("values", DataType::UInt32, false);
+        let entry_struct = StructArray::from(vec![
+            (keys_field.clone(), make_array(key_data)),
+            (values_field.clone(), make_array(value_data.clone())),
+        ]);
+
+        // Construct a map array from the above two
+        let map_data_type = DataType::Map(
+            Box::new(Field::new(
+                "entries",
+                entry_struct.data_type().clone(),
+                true,
+            )),
+            false,
+        );
+        let map_data = ArrayData::builder(map_data_type)
+            .len(3)
+            .add_buffer(entry_offsets)
+            .add_child_data(entry_struct.data().clone())
+            .build();
+        let map_array = MapArray::from(map_data);

Review comment:
       I've made some slight differences between the two functions to test null 
values on the one map




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