SteNicholas commented on code in PR #1486:
URL: https://github.com/apache/paimon-rust/pull/1486#discussion_r4657813998
##########
crates/paimon/src/table/pk_search_ranker.rs:
##########
@@ -172,10 +184,39 @@ pub(crate) fn weighted_score(
max = max.max(position.score());
}
for position in ranking.positions() {
- let normalized = f64::from(normalize_route_score(position.score(),
min, max));
+ let normalized = if ranking.cosine_distance_scores && max > min {
+ (f64::from(position.score()) - f64::from(min)) /
(f64::from(max) - f64::from(min))
Review Comment:
**加入一条只命中第三行的全文检索路线,会让近似匹配超过精确匹配。** 查询为 `[1, 0]`,行 0 为 `[1, 2^-30]`,行 1
等于查询,行 2 为 `[0, 1]`,测试中均补零至 4 维。余弦路线取 3 个候选,精排因子为 2,三行距离分别为
`2^-61`、`0`、`1`。单独运行这条路线时,`execute_read()` 返回行 1。加入权重 `0.5`、只命中行 2 的全文路线后,它返回行
0。归一化时 `1 - 2^-61` 在 `f64` 中已舍入到 `1`,前两行因此得到相同总分。
请在按物理位置打破平局前保留足以区分这两个贡献的精度。对这个用例,其他路线对前两行的贡献相同,可以直接比较原始余弦距离。建议把加入第三行全文命中前后的
Top‑1 都纳入回归测试。
##########
crates/paimon/src/table/pk_search_ranker.rs:
##########
@@ -172,10 +184,39 @@ pub(crate) fn weighted_score(
max = max.max(position.score());
}
for position in ranking.positions() {
- let normalized = f64::from(normalize_route_score(position.score(),
min, max));
+ let normalized = if ranking.cosine_distance_scores && max > min {
+ (f64::from(position.score()) - f64::from(min)) /
(f64::from(max) - f64::from(min))
+ } else {
+ f64::from(normalize_route_score(position.score(), min, max))
+ };
*fused_scores.entry(position.clone()).or_insert(0.0) +=
ranking.weight() * normalized;
}
}
+ if rankings
+ .iter()
+ .any(|ranking| ranking.cosine_distance_scores)
+ {
+ // Min-max normalization is monotone within one route. Use its input
+ // order in that case and the f64 fused totals for multiple routes.
+ let mut ranked = fused_scores
+ .into_iter()
+ .map(|(position, score)| {
+ let order = if rankings.len() == 1 {
+ f64::from(position.score())
+ } else {
+ score
+ };
+ Ok((position.with_score(score as f32)?, order))
+ })
+ .collect::<crate::Result<Vec<_>>>()?;
+ ranked.sort_by(|(left, left_order), (right, right_order)| {
+ right_order
+ .total_cmp(left_order)
Review Comment:
**交换三条等权余弦路线的声明顺序,会改变 `weighted_score` 的 Top‑1。** 我用行 0 `A = [1, 0, 0, 0]`、行
1 `B = [0, 1, 0, 0]` 和行 2 `[-1, -1, 15, 0]` 做了端到端测试。每条路线取全部 3 个候选,精排因子为 2。令 `M
= [1, 1, 0, 0]`,按 `[A, M, B]` 查询返回行 0,按 `[B, M, A]` 查询返回行 1。前两行得到相同的三项贡献
`1`、`1` 和 `0.06224118620351357`。累加顺序让两个 `f64` 总分相差一个 ULP,这里的比较就跳过了物理位置的平局规则。
请让相同贡献集合的求和结果与路线顺序无关,并用这两种排列验证物理位置较小的行 0 始终胜出。
##########
crates/paimon/src/table/de_vector_read.rs:
##########
@@ -3229,21 +3556,11 @@ fn compute_raw_vector_score_from_values(
debug_assert_eq!(query.len(), end - start);
match metric {
RawVectorMetric::L2 => compute_raw_vector_l2_score(query,
&values.values()[start..end]),
- RawVectorMetric::Cosine => {
- let mut dot = 0.0;
- let mut norm_b = 0.0;
- for (q, value_index) in query.iter().zip(start..end) {
- let stored = values.value(value_index);
- dot += q * stored;
- norm_b += stored * stored;
- }
- let denominator = query_l2_squared_norm.sqrt() * norm_b.sqrt();
- if denominator == 0.0 {
- 0.0
- } else {
- dot / denominator
- }
- }
+ RawVectorMetric::Cosine => cosine_similarity(
Review Comment:
**把每行的同一个向量包装成单元素列表,精确 Top‑1 会从行 11 变成行 10。** 取 `t = 2^-27`,查询为 `[1, t,
1]`,行 10 为 `[1, 0, -1]`,行 11 为 `[1, t, -1]`。普通向量在单查询和四查询批处理中都返回行 11,分数为
`2^-55`。使用单元素嵌入列表时,`ListScorer` 的 `Independent` 和 `MaxSim` 都返回行 10,分数为
`0`。列表路径仍通过 `multi_vector::cosine` 顺序累加 `1 + 2^-54 - 1`,丢失了行 11 的正点积。
请让列表精排也复用能保留抵消余项的点积计算,并保留 MaxSim 跨查询求和的精度。用同一批数据断言普通向量和单元素列表选中相同的行,就能覆盖这个差异。
--
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]