This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-rust.git
The following commit(s) were added to refs/heads/main by this push:
new 3b86352e [core] Avoid score normalization overflow in hybrid search
(#1484)
3b86352e is described below
commit 3b86352ed0964cefe33b04e6acbee6752bfa1e6f
Author: Jingsong Lee <[email protected]>
AuthorDate: Sun Nov 22 10:23:48 2026 +0800
[core] Avoid score normalization overflow in hybrid search (#1484)
---
crates/paimon/src/table/hybrid_search_builder.rs | 36 +++++++++++++++++++-----
crates/paimon/src/table/pk_search_ranker.rs | 28 ++++++++++++++----
2 files changed, 51 insertions(+), 13 deletions(-)
diff --git a/crates/paimon/src/table/hybrid_search_builder.rs
b/crates/paimon/src/table/hybrid_search_builder.rs
index 1eda3c33..0cfbe7c6 100644
--- a/crates/paimon/src/table/hybrid_search_builder.rs
+++ b/crates/paimon/src/table/hybrid_search_builder.rs
@@ -1355,20 +1355,27 @@ fn weighted_score(route_results:
&[WeightedRouteResult], limit: usize) -> Scored
min = min.min(*score);
max = max.max(*score);
}
- let range = max - min;
-
for (row_id, score) in ranked {
- let normalized = if range > 0.0 {
- (score - min) / range
- } else {
- 1.0
- };
+ let normalized = normalize_route_score(score, min, max);
add_score(&mut scores, row_id, route_result.weight * normalized);
}
}
top_k(scores, limit)
}
+/// Min-max normalizes a route score. A route with no spread maps to one.
+pub(super) fn normalize_route_score(score: f32, min: f32, max: f32) -> f32 {
+ let range = max - min;
+ if range.is_infinite() && min.is_finite() && max.is_finite() {
+ return ((f64::from(score) - f64::from(min)) / (f64::from(max) -
f64::from(min))) as f32;
+ }
+ if range > 0.0 {
+ (score - min) / range
+ } else {
+ 1.0
+ }
+}
+
fn ranked_row_ids(result: &ScoredRowIds) -> Vec<(u64, f32)> {
let mut best_scores = HashMap::new();
for (&row_id, &score) in result.row_ids.iter().zip(&result.scores) {
@@ -1459,6 +1466,21 @@ mod tests {
assert!((scores[&3] - 0.0).abs() < 1e-6);
}
+ #[test]
+ fn test_weighted_score_handles_finite_scores_with_overflowing_range() {
+ let ranked = rank_results(
+ HybridSearchRanker::WeightedScore,
+ &[
+ route_result(vec![1, 2, 3], vec![-f32::MAX, 0.0, f32::MAX],
4.0),
+ route_result(vec![1, 2, 3], vec![1.0, 0.0, 0.0], 1.0),
+ ],
+ 2,
+ );
+
+ assert_eq!(ranked.row_ids, vec![3, 2]);
+ assert_eq!(ranked.scores, vec![4.0, 2.0]);
+ }
+
#[test]
fn test_mrr_uses_reciprocal_rank_without_constant() {
let ranked = rank_results(
diff --git a/crates/paimon/src/table/pk_search_ranker.rs
b/crates/paimon/src/table/pk_search_ranker.rs
index e3003a3d..68bad568 100644
--- a/crates/paimon/src/table/pk_search_ranker.rs
+++ b/crates/paimon/src/table/pk_search_ranker.rs
@@ -31,6 +31,7 @@
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
+use crate::table::hybrid_search_builder::normalize_route_score;
use crate::table::pk_search_position::PrimaryKeySearchPosition;
/// Reciprocal-rank-fusion smoothing constant. Matches Java's
@@ -170,13 +171,8 @@ pub(crate) fn weighted_score(
min = min.min(position.score());
max = max.max(position.score());
}
- let range = max - min;
for position in ranking.positions() {
- let normalized: f64 = if range > 0.0 {
- ((position.score() - min) / range) as f64
- } else {
- 1.0
- };
+ let normalized = f64::from(normalize_route_score(position.score(),
min, max));
*fused_scores.entry(position.clone()).or_insert(0.0) +=
ranking.weight() * normalized;
}
}
@@ -361,6 +357,26 @@ mod tests {
assert_eq!(scores, vec![2.0, 1.0, 0.0]);
}
+ #[test]
+ fn weighted_score_handles_finite_scores_with_overflowing_range() {
+ let ranked = weighted_score(
+ &[ranking(
+ vec![
+ position("a", 0, -f32::MAX),
+ position("b", 0, 0.0),
+ position("c", 0, f32::MAX),
+ ],
+ 4.0,
+ )],
+ 2,
+ )
+ .unwrap();
+
+ assert_eq!(file_names(&ranked), vec!["c", "b"]);
+ assert_eq!(ranked[0].score(), 4.0);
+ assert_eq!(ranked[1].score(), 2.0);
+ }
+
#[test]
fn rrf_k_matches_java_value() {
assert_eq!(DEFAULT_RRF_K, 60);