You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/09/03 15:34:10 UTC

[GitHub] [arrow] jorgecarleitao commented on a change in pull request #8102: ARROW-9902: [Rust] [DataFusion] Add array() built-in function

jorgecarleitao commented on a change in pull request #8102:
URL: https://github.com/apache/arrow/pull/8102#discussion_r483070474



##########
File path: rust/datafusion/src/physical_plan/array_expressions.rs
##########
@@ -0,0 +1,106 @@
+// 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.
+
+//! Array expressions
+
+use crate::error::{ExecutionError, Result};
+use arrow::array::*;
+use arrow::datatypes::DataType;
+use std::sync::Arc;
+
+macro_rules! downcast_vec {
+    ($ARGS:expr, $ARRAY_TYPE:ident) => {{
+        $ARGS
+            .iter()
+            .map(|e| match e.as_any().downcast_ref::<$ARRAY_TYPE>() {
+                Some(array) => Ok(array),
+                _ => Err(ExecutionError::General("failed to downcast".to_string())),
+            })
+    }};
+}
+
+macro_rules! array {
+    ($ARGS:expr, $ARRAY_TYPE:ident, $BUILDER_TYPE:ident) => {{
+        // downcast all arguments to their common format
+        let args =
+            downcast_vec!($ARGS, $ARRAY_TYPE).collect::<Result<Vec<&$ARRAY_TYPE>>>()?;
+
+        let mut builder = FixedSizeListBuilder::<$BUILDER_TYPE>::new(
+            <$BUILDER_TYPE>::new(args[0].len()),
+            args.len() as i32,
+        );
+        // for each entry in the array
+        for index in 0..args[0].len() {
+            for arg in &args {
+                if arg.is_null(index) {
+                    builder.values().append_null()?;
+                } else {
+                    builder.values().append_value(arg.value(index))?;
+                }
+            }
+            builder.append(true)?;
+        }
+        Ok(Arc::new(builder.finish()))
+    }};
+}
+
+/// put values in an array.
+pub fn array(args: &[ArrayRef]) -> Result<ArrayRef> {
+    // do not accept 0 arguments.
+    if args.len() == 0 {
+        return Err(ExecutionError::InternalError(
+            "array requires at least one argument".to_string(),
+        ));
+    }
+
+    match args[0].data_type() {

Review comment:
       Type coercion should do it, but while adding a test to this, I found a bug in the coercion logic. So, this PR will have to wait a bit until the logic is fixed. Spot-on, @andygrove !




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org