alamb commented on code in PR #9364:
URL: https://github.com/apache/arrow-datafusion/pull/9364#discussion_r1504079813


##########
datafusion/sqllogictest/test_files/nvl2.slt:
##########
@@ -0,0 +1,120 @@
+# 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.
+
+# 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.
+
+statement ok
+CREATE TABLE test(
+  int_field  INT,
+  bool_field BOOLEAN,
+  text_field TEXT,
+  more_ints  INT
+) as VALUES
+  (1,    true,  'abc',  2),
+  (2,    false, 'def',  2),
+  (3,    NULL,  'ghij', 3),
+  (NULL, NULL,   NULL,  4),
+  (4,    false, 'zxc',  5),
+  (NULL, true,   NULL,  6)
+;
+
+# Arrays tests
+query I

Review Comment:
   I would recommend adding an ORDER BY to these queries to ensure the order is 
the same as listed in the tests
   
   for example
   
   ```sql
   SELECT NVL2(int_field, 2, 3) FROM test ORDER BY more_ints;
   ```



##########
datafusion/functions/src/core/nvl2.rs:
##########
@@ -0,0 +1,321 @@
+// 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::datatypes::DataType;
+use datafusion_common::{internal_err, plan_datafusion_err, DataFusionError, 
Result};
+use datafusion_expr::{utils, ColumnarValue, ScalarUDFImpl, Signature, 
Volatility};
+use arrow::compute::kernels::zip::zip;
+use arrow::compute::is_not_null;
+use arrow::array::Array;
+
+#[derive(Debug)]
+pub(super) struct NVL2Func {
+    signature: Signature,
+}
+
+/// Currently supported types by the nvl/ifnull function.
+/// The order of these types correspond to the order on which coercion applies
+/// This should thus be from least informative to most informative
+static SUPPORTED_NVL2_TYPES: &[DataType] = &[

Review Comment:
   I don't think we need this list of data types, do we? Why wouldn't NVL2 
support any data type? Its logic isn't specific to the type (only if the 
argument is null). 
   
   It seems like we could remove the list from `nvl` as well 



##########
datafusion/functions/src/core/nvl2.rs:
##########
@@ -0,0 +1,321 @@
+// 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::datatypes::DataType;
+use datafusion_common::{internal_err, plan_datafusion_err, DataFusionError, 
Result};
+use datafusion_expr::{utils, ColumnarValue, ScalarUDFImpl, Signature, 
Volatility};
+use arrow::compute::kernels::zip::zip;
+use arrow::compute::is_not_null;
+use arrow::array::Array;
+
+#[derive(Debug)]
+pub(super) struct NVL2Func {
+    signature: Signature,
+}
+
+/// Currently supported types by the nvl/ifnull function.
+/// The order of these types correspond to the order on which coercion applies
+/// This should thus be from least informative to most informative
+static SUPPORTED_NVL2_TYPES: &[DataType] = &[
+    DataType::Boolean,
+    DataType::UInt8,
+    DataType::UInt16,
+    DataType::UInt32,
+    DataType::UInt64,
+    DataType::Int8,
+    DataType::Int16,
+    DataType::Int32,
+    DataType::Int64,
+    DataType::Float32,
+    DataType::Float64,
+    DataType::Utf8,
+    DataType::LargeUtf8,
+];
+
+impl NVL2Func {
+    pub fn new() -> Self {
+        Self {
+            signature:
+            Signature::uniform(3, SUPPORTED_NVL2_TYPES.to_vec(),
+                Volatility::Immutable,
+            ),
+        }
+    }
+}
+
+impl ScalarUDFImpl for NVL2Func {
+    fn as_any(&self) -> &dyn std::any::Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "nvl2"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        if arg_types.len() != 3 {
+            return Err(plan_datafusion_err!(
+                "{}",
+                utils::generate_signature_error_msg(
+                    self.name(),
+                    self.signature().clone(),
+                    arg_types,
+                )
+            ));
+        }
+        Ok(arg_types[1].clone())
+    }
+
+    fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
+        nvl2_func(args)
+    }
+}
+
+fn nvl2_func(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    if args.len() != 3 {
+        return internal_err!(
+            "{:?} args were supplied but NVL2 takes exactly three args",
+            args.len()
+        );
+    }
+    let mut len = 1;
+    let mut is_array = false;
+    for arg in args {
+        if let ColumnarValue::Array(array) = arg {
+            len = array.len();
+            is_array = true;
+            break;
+        }
+    }
+    if is_array {
+        let args = args.iter().map(|arg| match arg {
+            ColumnarValue::Scalar(scalar) => {
+                scalar.to_array_of_size(len)
+            }
+            ColumnarValue::Array(array) => {
+                Ok(array.clone())
+            }
+        }).collect::<Result<Vec<_>>>()?;
+        let to_apply = is_not_null(&args[0])?;
+        let value = zip(&to_apply, &args[1], &args[2])?;
+        Ok(ColumnarValue::Array(value))
+    } else {
+        let mut current_value = &args[1];
+        match &args[0] {
+            ColumnarValue::Array(_) => {
+                internal_err!("except Scalar value, but got Array")
+            }
+            ColumnarValue::Scalar(scalar) => {
+                if scalar.is_null() {
+                    current_value = &args[2];
+                }
+                Ok(current_value.clone())
+            }
+        }
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use std::sync::Arc;
+
+    use arrow::array::*;
+
+    use super::*;
+    use datafusion_common::{Result, ScalarValue};
+
+    #[test]
+    fn nvl2_int32() -> Result<()> {

Review Comment:
   Do these tests add any additional coverage over what is in the `nvl2.slt` 
file? I feel like they are the same test logic encoded twice (as in it seems 
like any bug would cause both the slt and the rust tests to fail)
   
   If they are the same, I think we should remove the rust based tests and stay 
with slt



-- 
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-unsubscr...@arrow.apache.org

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

Reply via email to