use std::collections::HashMap;
use std::fmt::Write as _;
use serde::ser::{SerializeMap, Serializer};
use serde::{Deserialize, Serialize};
use qql_plan::{
PlanFacetValue, PlanGroupId, PlanPointId, PlanShardKey, PlanVectorStruct, QuotaConfig,
};
use crate::schema::CollectionInfo;
use crate::telemetry::{PhaseTimings, ServerTelemetry, ServerUsage};
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ExecResponse {
pub ok: bool,
pub operation: String,
pub message: String,
pub data: Option<ExecData>,
#[serde(skip_serializing_if = "Option::is_none")]
pub telemetry: Option<ServerTelemetry>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ExecutionReport {
pub ok: bool,
pub results: Vec<ExecResponse>,
pub succeeded: usize,
pub failed: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub telemetry: Option<ServerTelemetry>,
}
impl ExecutionReport {
pub fn from_results(results: Vec<ExecResponse>) -> Self {
let succeeded = results.iter().filter(|r| r.ok).count();
let failed = results.len() - succeeded;
let telemetry =
ServerTelemetry::aggregate(results.iter().filter_map(|r| r.telemetry.as_ref()));
Self {
ok: failed == 0,
results,
succeeded,
failed,
telemetry,
}
}
pub fn single(resp: ExecResponse) -> Self {
let ok = resp.ok;
let telemetry = resp.telemetry.clone();
Self {
ok,
results: vec![resp],
succeeded: if ok { 1 } else { 0 },
failed: if ok { 0 } else { 1 },
telemetry,
}
}
pub fn first(&self) -> Option<&ExecResponse> {
self.results.first()
}
pub fn hits(&self, stmt: usize) -> Option<Vec<SearchHit>> {
self.results.get(stmt).and_then(|r| r.hits())
}
pub fn ids(&self, stmt: usize) -> Vec<u64> {
self.results.get(stmt).map(|r| r.ids()).unwrap_or_default()
}
pub fn count(&self, stmt: usize) -> Option<u64> {
self.results.get(stmt).and_then(|r| r.count())
}
pub fn facet(&self, stmt: usize) -> Option<Vec<(PlanFacetValue, u64)>> {
self.results.get(stmt).and_then(|r| r.facet())
}
pub fn first_count(&self) -> Option<u64> {
self.count(0)
}
pub fn first_hits(&self) -> Option<Vec<SearchHit>> {
self.hits(0)
}
pub fn first_ids(&self) -> Vec<u64> {
self.ids(0)
}
pub fn first_facet(&self) -> Option<Vec<(PlanFacetValue, u64)>> {
self.facet(0)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct AnalyzeReport {
pub ok: bool,
pub plan: String,
pub phases: PhaseTimings,
#[serde(skip_serializing_if = "Option::is_none")]
pub server_time_s: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<ServerUsage>,
pub results: Vec<ExecResponse>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum OnError {
#[default]
Stop,
Continue,
}
pub fn score_f64(v: f32) -> f64 {
let mut buf = ScoreBuf::new();
if write!(buf, "{v}").is_err() {
return v as f64;
}
buf.as_str().parse().unwrap_or(v as f64)
}
struct ScoreBuf {
buf: [u8; 64],
len: usize,
}
impl ScoreBuf {
fn new() -> Self {
Self {
buf: [0; 64],
len: 0,
}
}
fn as_str(&self) -> &str {
std::str::from_utf8(&self.buf[..self.len]).unwrap_or("")
}
}
impl std::fmt::Write for ScoreBuf {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
let bytes = s.as_bytes();
let end = self.len + bytes.len();
if end > self.buf.len() {
return Err(std::fmt::Error);
}
self.buf[self.len..end].copy_from_slice(bytes);
self.len = end;
Ok(())
}
}
pub fn score_from_wire(v: f64) -> f64 {
score_f64(v as f32)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SearchHit {
pub id: PlanPointId,
pub score: f64,
pub payload: Option<HashMap<String, serde_json::Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub collection: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub vector: Option<PlanVectorStruct>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GroupedSearchResult {
#[serde(rename = "id")]
pub group_id: PlanGroupId,
pub hits: Vec<SearchHit>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FacetHit {
pub value: PlanFacetValue,
pub count: u64,
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
pub enum ExecData {
Hits(Vec<SearchHit>),
Groups(Vec<GroupedSearchResult>),
Count(u64),
Facet(Vec<FacetHit>),
Mutation {
affected: Option<u64>,
},
Collections(Vec<String>),
Collection(CollectionInfo),
ShardKeys(Vec<PlanShardKey>),
Quotas(QuotaConfig),
}
impl Serialize for ExecData {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ExecData::Hits(hits) => hits.serialize(serializer),
ExecData::Groups(groups) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("groups", groups)?;
map.end()
}
ExecData::Count(count) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("count", count)?;
map.end()
}
ExecData::Facet(hits) => hits.serialize(serializer),
ExecData::Mutation {
affected: Some(count),
} => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("count", count)?;
map.end()
}
ExecData::Mutation { affected: None } => serializer.serialize_none(),
ExecData::Collections(collections) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("collections", collections)?;
map.end()
}
ExecData::Collection(info) => info.serialize(serializer),
ExecData::ShardKeys(keys) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("shard_keys", keys)?;
map.end()
}
ExecData::Quotas(config) => config.serialize(serializer),
}
}
}
impl ExecData {
pub fn hits(&self) -> Option<&[SearchHit]> {
match self {
ExecData::Hits(hits) => Some(hits),
_ => None,
}
}
pub fn groups(&self) -> Option<&[GroupedSearchResult]> {
match self {
ExecData::Groups(groups) => Some(groups),
_ => None,
}
}
pub fn count(&self) -> Option<u64> {
match self {
ExecData::Count(count) => Some(*count),
ExecData::Mutation { affected } => *affected,
_ => None,
}
}
pub fn mutation_affected(&self) -> Option<Option<u64>> {
match self {
ExecData::Mutation { affected } => Some(*affected),
_ => None,
}
}
pub fn facet(&self) -> Option<&[FacetHit]> {
match self {
ExecData::Facet(hits) => Some(hits),
_ => None,
}
}
pub fn collections(&self) -> Option<&[String]> {
match self {
ExecData::Collections(collections) => Some(collections),
_ => None,
}
}
pub fn collection(&self) -> Option<&CollectionInfo> {
match self {
ExecData::Collection(info) => Some(info),
_ => None,
}
}
pub fn shard_keys(&self) -> Option<&[PlanShardKey]> {
match self {
ExecData::ShardKeys(keys) => Some(keys),
_ => None,
}
}
pub fn quotas(&self) -> Option<&QuotaConfig> {
match self {
ExecData::Quotas(config) => Some(config),
_ => None,
}
}
pub fn is_empty(&self) -> bool {
match self {
ExecData::Hits(hits) => hits.is_empty(),
ExecData::Groups(groups) => groups.is_empty(),
ExecData::Count(_)
| ExecData::Mutation { .. }
| ExecData::Collection(_)
| ExecData::Quotas(_) => false,
ExecData::Facet(hits) => hits.is_empty(),
ExecData::Collections(collections) => collections.is_empty(),
ExecData::ShardKeys(keys) => keys.is_empty(),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct BackendResponse {
pub data: ExecData,
#[serde(skip_serializing_if = "Option::is_none")]
pub telemetry: Option<ServerTelemetry>,
}
impl ExecResponse {
pub fn hits_ref(&self) -> Option<&[SearchHit]> {
self.data.as_ref().and_then(ExecData::hits)
}
pub fn hits(&self) -> Option<Vec<SearchHit>> {
self.hits_ref().map(<[SearchHit]>::to_vec)
}
pub fn ids(&self) -> Vec<u64> {
let Some(hits) = self.hits_ref() else {
return Vec::new();
};
hits.iter()
.filter_map(|hit| match &hit.id {
PlanPointId::Number(id) => Some(*id),
PlanPointId::String(id) => id.parse().ok(),
})
.collect()
}
pub fn count(&self) -> Option<u64> {
self.data.as_ref().and_then(ExecData::count)
}
pub fn facet(&self) -> Option<Vec<(PlanFacetValue, u64)>> {
let entries = self.data.as_ref()?.facet()?;
Some(
entries
.iter()
.map(|entry| (entry.value.clone(), entry.count))
.collect(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn score_f64_rounds_once_at_ingestion() {
assert_eq!(score_f64(0.95f32), 0.95f64);
let hit = SearchHit {
id: PlanPointId::Number(1),
score: score_f64(0.95f32),
payload: None,
collection: None,
vector: None,
};
let value = serde_json::to_value(&hit).expect("hit serializes");
assert_eq!(value["score"], serde_json::json!(0.95));
}
#[test]
fn score_f64_matches_heap_round_trip() {
for v in [
0.95f32,
0.75,
0.1,
0.99,
1.0,
-1.0,
0.0,
-0.0,
1e-5,
1e-4,
1e21,
12345.67,
0.30000004,
f32::MIN_POSITIVE,
f32::EPSILON,
3.4028235e38,
1.1754944e-38,
1e-45,
2.5e-7,
std::f32::consts::PI,
100.0,
f32::INFINITY,
f32::NEG_INFINITY,
] {
let expected: f64 = v.to_string().parse().unwrap_or(v as f64);
assert_eq!(score_f64(v), expected, "score_f64({v}) diverged");
}
assert!(score_f64(f32::NAN).is_nan());
let expected: f64 = f32::NAN.to_string().parse().unwrap_or(f32::NAN as f64);
assert!(expected.is_nan());
}
#[test]
fn score_from_wire_narrows_once() {
assert_eq!(super::score_from_wire(0.95), 0.95f64);
assert_eq!(super::score_from_wire(0.949999988079071), 0.95f64);
}
}