Jefffrey commented on code in PR #22013: URL: https://github.com/apache/datafusion/pull/22013#discussion_r3185615028
########## datafusion/functions-nested/src/array_normalize.rs: ########## @@ -0,0 +1,207 @@ +// 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. + +//! [`ScalarUDFImpl`] definitions for array_normalize function. + +use crate::utils::make_scalar_function; +use arrow::array::{Array, ArrayRef, Float64Array, GenericListArray, OffsetSizeTrait}; +use arrow::buffer::{NullBuffer, OffsetBuffer}; +use arrow::datatypes::{ + DataType, + DataType::{FixedSizeList, LargeList, List, Null}, + Field, +}; +use datafusion_common::cast::{as_float64_array, as_generic_list_array}; +use datafusion_common::utils::{ListCoercion, coerced_type_with_base_type_only}; +use datafusion_common::{Result, internal_err, plan_err, utils::take_function_args}; +use datafusion_expr::{ + ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature, + Volatility, +}; +use datafusion_macros::user_doc; +use std::sync::Arc; + +make_udf_expr_and_func!( + ArrayNormalize, + array_normalize, + array, + "returns the L2-normalized vector for a numeric array.", + array_normalize_udf +); + +#[user_doc( + doc_section(label = "Array Functions"), + description = "Returns the L2-normalized vector for the input numeric array, computed as `array[i] / sqrt(sum(array[i]^2))` per element. Returns NULL if the input is NULL, contains NULL elements, or has zero magnitude (all elements are zero). Returns an empty array for an empty input array.", + syntax_example = "array_normalize(array)", + sql_example = r#"```sql +> select array_normalize([3.0, 4.0]); ++-----------------------------+ +| array_normalize(List([3.0,4.0])) | ++-----------------------------+ +| [0.6, 0.8] | ++-----------------------------+ +```"#, + argument( + name = "array", + description = "Array expression. Can be a constant, column, or function, and any combination of array operators." + ) +)] +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct ArrayNormalize { + signature: Signature, + aliases: Vec<String>, +} + +impl Default for ArrayNormalize { + fn default() -> Self { + Self::new() + } +} + +impl ArrayNormalize { + pub fn new() -> Self { + Self { + signature: Signature::user_defined(Volatility::Immutable), + aliases: vec!["list_normalize".to_string()], + } + } +} + +impl ScalarUDFImpl for ArrayNormalize { + fn name(&self) -> &str { + "array_normalize" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { + // After `coerce_types`, `arg_types[0]` is one of List(Float64) or LargeList(Float64). + Ok(arg_types[0].clone()) + } + + fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { + let [arg_type] = take_function_args(self.name(), arg_types)?; + let coercion = Some(&ListCoercion::FixedSizedListToList); + + if !matches!(arg_type, Null | List(_) | LargeList(_) | FixedSizeList(..)) { + return plan_err!("{} does not support type {arg_type}", self.name()); + } + + let coerced = if matches!(arg_type, Null) { + List(Arc::new(Field::new_list_field(DataType::Float64, true))) + } else { + coerced_type_with_base_type_only(arg_type, &DataType::Float64, coercion) + }; + + Ok(vec![coerced]) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { + make_scalar_function(array_normalize_inner)(&args.args) + } + + fn aliases(&self) -> &[String] { + &self.aliases + } + + fn documentation(&self) -> Option<&Documentation> { + self.doc() + } +} + +fn array_normalize_inner(args: &[ArrayRef]) -> Result<ArrayRef> { + let [array] = take_function_args("array_normalize", args)?; + match array.data_type() { + List(_) => general_array_normalize::<i32>(args), + LargeList(_) => general_array_normalize::<i64>(args), + arg_type => internal_err!( + "array_normalize received unexpected type after coercion: {arg_type}" + ), + } +} + +fn general_array_normalize<O: OffsetSizeTrait>(arrays: &[ArrayRef]) -> Result<ArrayRef> { + let list_array = as_generic_list_array::<O>(&arrays[0])?; + let values = as_float64_array(list_array.values())?; + let offsets = list_array.value_offsets(); + + let mut new_values: Vec<f64> = Vec::with_capacity(values.len()); + let mut new_offsets: Vec<O> = Vec::with_capacity(list_array.len() + 1); + new_offsets.push(O::usize_as(0)); + let mut validity: Vec<bool> = Vec::with_capacity(list_array.len()); Review Comment: Use [`NullBufferBuilder`](https://docs.rs/arrow/latest/arrow/array/struct.NullBufferBuilder.html) here instead. One benefit is when finishing it, it may output `None` if there are no nulls (currently we always provide a null buffer even if there are no nulls) ########## datafusion/functions-nested/src/array_normalize.rs: ########## @@ -0,0 +1,207 @@ +// 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. + +//! [`ScalarUDFImpl`] definitions for array_normalize function. + +use crate::utils::make_scalar_function; +use arrow::array::{Array, ArrayRef, Float64Array, GenericListArray, OffsetSizeTrait}; +use arrow::buffer::{NullBuffer, OffsetBuffer}; +use arrow::datatypes::{ + DataType, + DataType::{FixedSizeList, LargeList, List, Null}, + Field, +}; +use datafusion_common::cast::{as_float64_array, as_generic_list_array}; +use datafusion_common::utils::{ListCoercion, coerced_type_with_base_type_only}; +use datafusion_common::{Result, internal_err, plan_err, utils::take_function_args}; +use datafusion_expr::{ + ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature, + Volatility, +}; +use datafusion_macros::user_doc; +use std::sync::Arc; + +make_udf_expr_and_func!( + ArrayNormalize, + array_normalize, + array, + "returns the L2-normalized vector for a numeric array.", + array_normalize_udf +); + +#[user_doc( + doc_section(label = "Array Functions"), + description = "Returns the L2-normalized vector for the input numeric array, computed as `array[i] / sqrt(sum(array[i]^2))` per element. Returns NULL if the input is NULL, contains NULL elements, or has zero magnitude (all elements are zero). Returns an empty array for an empty input array.", + syntax_example = "array_normalize(array)", + sql_example = r#"```sql +> select array_normalize([3.0, 4.0]); ++-----------------------------+ +| array_normalize(List([3.0,4.0])) | ++-----------------------------+ +| [0.6, 0.8] | ++-----------------------------+ +```"#, + argument( + name = "array", + description = "Array expression. Can be a constant, column, or function, and any combination of array operators." + ) +)] +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct ArrayNormalize { + signature: Signature, + aliases: Vec<String>, +} + +impl Default for ArrayNormalize { + fn default() -> Self { + Self::new() + } +} + +impl ArrayNormalize { + pub fn new() -> Self { + Self { + signature: Signature::user_defined(Volatility::Immutable), + aliases: vec!["list_normalize".to_string()], + } + } +} + +impl ScalarUDFImpl for ArrayNormalize { + fn name(&self) -> &str { + "array_normalize" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { + // After `coerce_types`, `arg_types[0]` is one of List(Float64) or LargeList(Float64). + Ok(arg_types[0].clone()) + } + + fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { + let [arg_type] = take_function_args(self.name(), arg_types)?; + let coercion = Some(&ListCoercion::FixedSizedListToList); + + if !matches!(arg_type, Null | List(_) | LargeList(_) | FixedSizeList(..)) { + return plan_err!("{} does not support type {arg_type}", self.name()); + } + + let coerced = if matches!(arg_type, Null) { + List(Arc::new(Field::new_list_field(DataType::Float64, true))) + } else { + coerced_type_with_base_type_only(arg_type, &DataType::Float64, coercion) + }; + + Ok(vec![coerced]) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { + make_scalar_function(array_normalize_inner)(&args.args) + } + + fn aliases(&self) -> &[String] { + &self.aliases + } + + fn documentation(&self) -> Option<&Documentation> { + self.doc() + } +} + +fn array_normalize_inner(args: &[ArrayRef]) -> Result<ArrayRef> { + let [array] = take_function_args("array_normalize", args)?; + match array.data_type() { + List(_) => general_array_normalize::<i32>(args), + LargeList(_) => general_array_normalize::<i64>(args), + arg_type => internal_err!( + "array_normalize received unexpected type after coercion: {arg_type}" + ), + } +} + +fn general_array_normalize<O: OffsetSizeTrait>(arrays: &[ArrayRef]) -> Result<ArrayRef> { + let list_array = as_generic_list_array::<O>(&arrays[0])?; + let values = as_float64_array(list_array.values())?; + let offsets = list_array.value_offsets(); + + let mut new_values: Vec<f64> = Vec::with_capacity(values.len()); + let mut new_offsets: Vec<O> = Vec::with_capacity(list_array.len() + 1); Review Comment: I think it might be simpler to use [`OffsetBufferBuilder`](https://docs.rs/arrow/latest/arrow/array/struct.OffsetBufferBuilder.html) here -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
