|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! [ScalarUDFImpl] definitions for array_distance function. |
| 19 | +
|
| 20 | +use crate::utils::{downcast_arg, make_scalar_function}; |
| 21 | +use arrow_array::{ |
| 22 | + Array, ArrayRef, Float64Array, LargeListArray, ListArray, OffsetSizeTrait, |
| 23 | +}; |
| 24 | +use arrow_schema::DataType; |
| 25 | +use arrow_schema::DataType::{FixedSizeList, Float64, LargeList, List}; |
| 26 | +use core::any::type_name; |
| 27 | +use datafusion_common::cast::{ |
| 28 | + as_float32_array, as_float64_array, as_generic_list_array, as_int32_array, |
| 29 | + as_int64_array, |
| 30 | +}; |
| 31 | +use datafusion_common::DataFusionError; |
| 32 | +use datafusion_common::{exec_err, Result}; |
| 33 | +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; |
| 34 | +use std::any::Any; |
| 35 | +use std::sync::Arc; |
| 36 | + |
| 37 | +make_udf_expr_and_func!( |
| 38 | + ArrayDistance, |
| 39 | + array_distance, |
| 40 | + array, |
| 41 | + "returns the Euclidean distance between two numeric arrays.", |
| 42 | + array_distance_udf |
| 43 | +); |
| 44 | + |
| 45 | +#[derive(Debug)] |
| 46 | +pub(super) struct ArrayDistance { |
| 47 | + signature: Signature, |
| 48 | + aliases: Vec<String>, |
| 49 | +} |
| 50 | + |
| 51 | +impl ArrayDistance { |
| 52 | + pub fn new() -> Self { |
| 53 | + Self { |
| 54 | + signature: Signature::variadic_any(Volatility::Immutable), |
| 55 | + aliases: vec!["list_distance".to_string()], |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl ScalarUDFImpl for ArrayDistance { |
| 61 | + fn as_any(&self) -> &dyn Any { |
| 62 | + self |
| 63 | + } |
| 64 | + |
| 65 | + fn name(&self) -> &str { |
| 66 | + "array_distance" |
| 67 | + } |
| 68 | + |
| 69 | + fn signature(&self) -> &Signature { |
| 70 | + &self.signature |
| 71 | + } |
| 72 | + |
| 73 | + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { |
| 74 | + match arg_types[0] { |
| 75 | + List(_) | LargeList(_) | FixedSizeList(_, _) => Ok(Float64), |
| 76 | + _ => exec_err!("The array_distance function can only accept List/LargeList/FixedSizeList."), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { |
| 81 | + make_scalar_function(array_distance_inner)(args) |
| 82 | + } |
| 83 | + |
| 84 | + fn aliases(&self) -> &[String] { |
| 85 | + &self.aliases |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +pub fn array_distance_inner(args: &[ArrayRef]) -> Result<ArrayRef> { |
| 90 | + if args.len() != 2 { |
| 91 | + return exec_err!("array_distance expects exactly two arguments"); |
| 92 | + } |
| 93 | + |
| 94 | + match (&args[0].data_type(), &args[1].data_type()) { |
| 95 | + (List(_), List(_)) => general_array_distance::<i32>(args), |
| 96 | + (LargeList(_), LargeList(_)) => general_array_distance::<i64>(args), |
| 97 | + (array_type1, array_type2) => { |
| 98 | + exec_err!("array_distance does not support types '{array_type1:?}' and '{array_type2:?}'") |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn general_array_distance<O: OffsetSizeTrait>(arrays: &[ArrayRef]) -> Result<ArrayRef> { |
| 104 | + let list_array1 = as_generic_list_array::<O>(&arrays[0])?; |
| 105 | + let list_array2 = as_generic_list_array::<O>(&arrays[1])?; |
| 106 | + |
| 107 | + let result = list_array1 |
| 108 | + .iter() |
| 109 | + .zip(list_array2.iter()) |
| 110 | + .map(|(arr1, arr2)| compute_array_distance(arr1, arr2)) |
| 111 | + .collect::<Result<Float64Array>>()?; |
| 112 | + |
| 113 | + Ok(Arc::new(result) as ArrayRef) |
| 114 | +} |
| 115 | + |
| 116 | +/// Computes the Euclidean distance between two arrays |
| 117 | +fn compute_array_distance( |
| 118 | + arr1: Option<ArrayRef>, |
| 119 | + arr2: Option<ArrayRef>, |
| 120 | +) -> Result<Option<f64>> { |
| 121 | + let value1 = match arr1 { |
| 122 | + Some(arr) => arr, |
| 123 | + None => return Ok(None), |
| 124 | + }; |
| 125 | + let value2 = match arr2 { |
| 126 | + Some(arr) => arr, |
| 127 | + None => return Ok(None), |
| 128 | + }; |
| 129 | + |
| 130 | + let mut value1 = value1; |
| 131 | + let mut value2 = value2; |
| 132 | + |
| 133 | + loop { |
| 134 | + match value1.data_type() { |
| 135 | + List(_) => { |
| 136 | + if downcast_arg!(value1, ListArray).null_count() > 0 { |
| 137 | + return Ok(None); |
| 138 | + } |
| 139 | + value1 = downcast_arg!(value1, ListArray).value(0); |
| 140 | + } |
| 141 | + LargeList(_) => { |
| 142 | + if downcast_arg!(value1, LargeListArray).null_count() > 0 { |
| 143 | + return Ok(None); |
| 144 | + } |
| 145 | + value1 = downcast_arg!(value1, LargeListArray).value(0); |
| 146 | + } |
| 147 | + _ => break, |
| 148 | + } |
| 149 | + |
| 150 | + match value2.data_type() { |
| 151 | + List(_) => { |
| 152 | + if downcast_arg!(value2, ListArray).null_count() > 0 { |
| 153 | + return Ok(None); |
| 154 | + } |
| 155 | + value2 = downcast_arg!(value2, ListArray).value(0); |
| 156 | + } |
| 157 | + LargeList(_) => { |
| 158 | + if downcast_arg!(value2, LargeListArray).null_count() > 0 { |
| 159 | + return Ok(None); |
| 160 | + } |
| 161 | + value2 = downcast_arg!(value2, LargeListArray).value(0); |
| 162 | + } |
| 163 | + _ => break, |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Check for NULL values inside the arrays |
| 168 | + if value1.null_count() != 0 || value2.null_count() != 0 { |
| 169 | + return Ok(None); |
| 170 | + } |
| 171 | + |
| 172 | + let values1 = convert_to_f64_array(&value1)?; |
| 173 | + let values2 = convert_to_f64_array(&value2)?; |
| 174 | + |
| 175 | + if values1.len() != values2.len() { |
| 176 | + return exec_err!("Both arrays must have the same length"); |
| 177 | + } |
| 178 | + |
| 179 | + let sum_squares: f64 = values1 |
| 180 | + .iter() |
| 181 | + .zip(values2.iter()) |
| 182 | + .map(|(v1, v2)| { |
| 183 | + let diff = v1.unwrap_or(0.0) - v2.unwrap_or(0.0); |
| 184 | + diff * diff |
| 185 | + }) |
| 186 | + .sum(); |
| 187 | + |
| 188 | + Ok(Some(sum_squares.sqrt())) |
| 189 | +} |
| 190 | + |
| 191 | +/// Converts an array of any numeric type to a Float64Array. |
| 192 | +fn convert_to_f64_array(array: &ArrayRef) -> Result<Float64Array> { |
| 193 | + match array.data_type() { |
| 194 | + DataType::Float64 => Ok(as_float64_array(array)?.clone()), |
| 195 | + DataType::Float32 => { |
| 196 | + let array = as_float32_array(array)?; |
| 197 | + let converted: Float64Array = |
| 198 | + array.iter().map(|v| v.map(|v| v as f64)).collect(); |
| 199 | + Ok(converted) |
| 200 | + } |
| 201 | + DataType::Int64 => { |
| 202 | + let array = as_int64_array(array)?; |
| 203 | + let converted: Float64Array = |
| 204 | + array.iter().map(|v| v.map(|v| v as f64)).collect(); |
| 205 | + Ok(converted) |
| 206 | + } |
| 207 | + DataType::Int32 => { |
| 208 | + let array = as_int32_array(array)?; |
| 209 | + let converted: Float64Array = |
| 210 | + array.iter().map(|v| v.map(|v| v as f64)).collect(); |
| 211 | + Ok(converted) |
| 212 | + } |
| 213 | + _ => exec_err!("Unsupported array type for conversion to Float64Array"), |
| 214 | + } |
| 215 | +} |
0 commit comments