You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "wjones127 (via GitHub)" <gi...@apache.org> on 2023/03/24 02:40:49 UTC

[GitHub] [arrow-rs] wjones127 commented on a diff in pull request #3925: feat: add take for MapArray

wjones127 commented on code in PR #3925:
URL: https://github.com/apache/arrow-rs/pull/3925#discussion_r1147061867


##########
arrow-array/src/array/map_array.rs:
##########
@@ -251,6 +251,69 @@ impl std::fmt::Debug for MapArray {
     }
 }
 
+impl TryFrom<&ListArray> for MapArray {
+    type Error = ArrowError;
+    fn try_from(value: &ListArray) -> Result<Self, Self::Error> {
+        let field = match value.data_type() {
+            DataType::List(field) => {
+                if let DataType::Struct(fields) = field.data_type() {
+                    if fields.len() != 2 {
+                        Err(ArrowError::InvalidArgumentError(
+                            "List item must be a struct type with two fields".to_string(),
+                        ))
+                    } else {
+                        Ok(field)
+                    }
+                } else {
+                    Err(ArrowError::InvalidArgumentError(
+                        "List item must be a struct type to convert to a list"
+                            .to_string(),
+                    ))
+                }
+            }
+            _ => unreachable!("This should be a list type."),
+        }?;
+        let old_data = value.to_data();
+        let array_data = unsafe {
+            ArrayData::new_unchecked(
+                DataType::Map(field.clone(), false),
+                old_data.len(),
+                Some(old_data.null_count()),
+                old_data.nulls().map(|nulls|nulls.buffer().clone()),
+                old_data.offset(),
+                old_data.buffers().to_vec(),
+                old_data.child_data().to_vec(),
+            )
+        };

Review Comment:
   Is there a better way to create a copy of `ArrayData` that's the same except for the type?



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