alamb commented on code in PR #25744:
URL: https://github.com/apache/datafusion/pull/25744#discussion_r4106998573
##########
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).
Review Comment:
I think this is referring to the old implementation which will become
outdated when this PR is merged. this information is useful for a code review
or commit comments, but not the comments in the code
##########
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:
is there some reason we can't test this with .slt tests ? The preference is
generally for .slt tests as they cover the end user API unless there is some
corner case that can't be easily reached with sql
--
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]