jonahgao commented on code in PR #9284: URL: https://github.com/apache/arrow-datafusion/pull/9284#discussion_r1497125082
########## datafusion/functions/src/core/nvl.rs: ########## @@ -0,0 +1,290 @@ +// 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, Result, DataFusionError}; +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; +use arrow::compute::kernels::zip::zip; +use arrow::compute::{and, is_not_null}; +use arrow::array::{Array, BooleanArray}; + +#[derive(Debug)] +pub(super) struct NVLFunc { + signature: Signature, + aliases: Vec<String>, +} + +/// 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 +pub static SUPPORTED_NVL_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 NVLFunc { + pub fn new() -> Self { + Self { + signature: + Signature::uniform(2, SUPPORTED_NVL_TYPES.to_vec(), + Volatility::Immutable, + ), + aliases: vec![String::from("ifnull")], + } + } +} + +impl ScalarUDFImpl for NVLFunc { + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn name(&self) -> &str { + "nvl" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { + // NVL has two args and they might get coerced, get a preview of this + let coerced_types = datafusion_expr::type_coercion::functions::data_types(arg_types, &self.signature); + coerced_types.map(|typs| typs[0].clone()) + .map_err(|e| e.context("Failed to coerce arguments for NVL") + ) + } + + fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { + nvl_func(args) + } + + fn aliases(&self) -> &[String] { + &self.aliases + } +} + +fn nvl_func(args: &[ColumnarValue]) -> Result<ColumnarValue> { + if args.len() != 2 { + return internal_err!( + "{:?} args were supplied but NVL/IFNULL takes exactly two args", + args.len() + ); + } + let (lhs, rhs) = (&args[0], &args[1]); + match (lhs, rhs) { + (ColumnarValue::Array(lhs), ColumnarValue::Scalar(rhs)) => { + let size = lhs.len(); + let mut current_value = rhs.to_array_of_size(size)?; + let remainder = BooleanArray::from(vec![true; size]); + let to_apply = and(&remainder, &is_not_null(lhs.as_ref())?)?; Review Comment: Is this "and" operation necessary, as the remainder is always true ? -- 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