Skip to main content

surql_parser/upstream/sql/
scoring.rs

1use revision::revisioned;
2use std::hash::{Hash, Hasher};
3use surrealdb_types::{SqlFormat, ToSql, write_sql};
4#[revisioned(revision = 1)]
5#[derive(Clone, Debug, PartialOrd)]
6pub enum Scoring {
7	Bm { k1: f32, b: f32 },
8	Vs,
9}
10impl Eq for Scoring {}
11impl PartialEq for Scoring {
12	fn eq(&self, other: &Self) -> bool {
13		match (self, other) {
14			(
15				Scoring::Bm { k1, b },
16				Scoring::Bm {
17					k1: other_k1,
18					b: other_b,
19				},
20			) => k1.to_bits() == other_k1.to_bits() && b.to_bits() == other_b.to_bits(),
21			(Scoring::Vs, Scoring::Vs) => true,
22			_ => false,
23		}
24	}
25}
26impl Hash for Scoring {
27	fn hash<H: Hasher>(&self, state: &mut H) {
28		match self {
29			Scoring::Bm { k1, b } => {
30				k1.to_bits().hash(state);
31				b.to_bits().hash(state);
32			}
33			Scoring::Vs => 0.hash(state),
34		}
35	}
36}
37impl Default for Scoring {
38	fn default() -> Self {
39		Self::Bm { k1: 1.2, b: 0.75 }
40	}
41}
42impl ToSql for Scoring {
43	fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
44		match self {
45			Self::Bm { k1, b } => write_sql!(f, sql_fmt, "BM25({},{})", k1, b),
46			Self::Vs => write_sql!(f, sql_fmt, "VS"),
47		}
48	}
49}
50impl From<Scoring> for crate::compat::catalog::Scoring {
51	fn from(v: Scoring) -> Self {
52		match v {
53			Scoring::Bm { k1, b } => crate::compat::catalog::Scoring::Bm { k1, b },
54			Scoring::Vs => crate::compat::catalog::Scoring::Vs,
55		}
56	}
57}
58impl From<crate::compat::catalog::Scoring> for Scoring {
59	fn from(v: crate::compat::catalog::Scoring) -> Self {
60		match v {
61			crate::compat::catalog::Scoring::Bm { k1, b } => Self::Bm { k1, b },
62			crate::compat::catalog::Scoring::Vs => Self::Vs,
63		}
64	}
65}