MCPcopy Create free account
hub / github.com/apache/datafusion / compute_array_distance

Function compute_array_distance

datafusion/functions-nested/src/distance.rs:162–234  ·  view source on GitHub ↗

Computes the Euclidean distance between two arrays

(
    arr1: Option<ArrayRef>,
    arr2: Option<ArrayRef>,
)

Source from the content-addressed store, hash-verified

160
161/// Computes the Euclidean distance between two arrays
162fn compute_array_distance(
163 arr1: Option<ArrayRef>,
164 arr2: Option<ArrayRef>,
165) -> Result<Option<f64>> {
166 let value1 = match arr1 {
167 Some(arr) => arr,
168 None => return Ok(None),
169 };
170 let value2 = match arr2 {
171 Some(arr) => arr,
172 None => return Ok(None),
173 };
174
175 let mut value1 = value1;
176 let mut value2 = value2;
177
178 loop {
179 match value1.data_type() {
180 List(_) => {
181 if downcast_arg!(value1, ListArray).null_count() > 0 {
182 return Ok(None);
183 }
184 value1 = downcast_arg!(value1, ListArray).value(0);
185 }
186 LargeList(_) => {
187 if downcast_arg!(value1, LargeListArray).null_count() > 0 {
188 return Ok(None);
189 }
190 value1 = downcast_arg!(value1, LargeListArray).value(0);
191 }
192 _ => break,
193 }
194
195 match value2.data_type() {
196 List(_) => {
197 if downcast_arg!(value2, ListArray).null_count() > 0 {
198 return Ok(None);
199 }
200 value2 = downcast_arg!(value2, ListArray).value(0);
201 }
202 LargeList(_) => {
203 if downcast_arg!(value2, LargeListArray).null_count() > 0 {
204 return Ok(None);
205 }
206 value2 = downcast_arg!(value2, LargeListArray).value(0);
207 }
208 _ => break,
209 }
210 }
211
212 // Check for NULL values inside the arrays
213 if value1.null_count() != 0 || value2.null_count() != 0 {
214 return Ok(None);
215 }
216
217 let values1 = convert_to_f64_array(&value1)?;
218 let values2 = convert_to_f64_array(&value2)?;
219

Callers 1

general_array_distanceFunction · 0.85

Calls 8

convert_to_f64_arrayFunction · 0.85
null_countMethod · 0.80
sumMethod · 0.80
data_typeMethod · 0.45
valueMethod · 0.45
lenMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…