alamb commented on code in PR #25744:
URL: https://github.com/apache/datafusion/pull/25744#discussion_r4107216584


##########
datafusion/functions-nested/src/cosine_distance.rs:
##########
@@ -254,3 +258,51 @@ fn dot_and_squares(
     }
     (dot, sq1, sq2)
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use arrow::array::ListArray;
+    use arrow::datatypes::Float64Type;
+
+    #[test]
+    fn test_cosine_distance_identical_vectors_metric_non_negative() {
+        // [1.0, 1.0, 1.0] has dot = 3.0, sq1 = 3.0, sq2 = 3.0.
+        // In IEEE-754 floating point arithmetic, sqrt(3.0) * sqrt(3.0) < 3.0
+        // (specifically 2.9999999999999996), which without clamping causes
+        // dot / (sqrt(sq1) * sqrt(sq2)) = 1.0000000000000002 > 1.0,
+        // resulting in 1.0 - ratio = -2.220446049250313e-16 < 0.0 (violating 
metric non-negativity).
+        let list1: ArrayRef = 
Arc::new(ListArray::from_iter_primitive::<Float64Type, _, _>(vec![

Review Comment:
   Though it is not documented very well -- I tried to improve the docs here
   - https://github.com/apache/datafusion/pull/25756



-- 
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]

Reply via email to