use std::collections::{BTreeMap, HashMap};
use qql_core::error::QqlError;
use qql_plan::{
PlanFacetValue, PlanGroupId, PlanPointId, PlanShardKey, PlanVectorStruct, PlanVectorValue,
};
use crate::executor::response::{
BackendResponse, ExecData, FacetHit, GroupedSearchResult, SearchHit, score_f64,
};
use crate::executor::telemetry::{
HardwareUsage, InferenceUsage, ModelUsage, ServerTelemetry, ServerUsage,
};
use crate::qdrant_grpc::qdrant;
use super::values::qdrant_value_to_json;
fn envelope_err(message: impl Into<String>) -> QqlError {
QqlError::backend("QQL-BACKEND-ENVELOPE", message.into(), None)
}
pub(crate) fn point_id_to_plan(id: Option<qdrant::PointId>) -> Result<PlanPointId, QqlError> {
use qdrant::point_id::PointIdOptions;
let point_id = id.ok_or_else(|| envelope_err("point is missing its id"))?;
match point_id.point_id_options {
Some(PointIdOptions::Num(n)) => Ok(PlanPointId::Number(n)),
Some(PointIdOptions::Uuid(s)) => Ok(PlanPointId::String(s)),
None => Err(envelope_err("point id carries neither num nor uuid")),
}
}
pub(crate) fn shard_key_to_plan(key: &qdrant::ShardKey) -> Result<PlanShardKey, QqlError> {
match &key.key {
Some(qdrant::shard_key::Key::Keyword(keyword)) => {
Ok(PlanShardKey::Keyword(keyword.clone()))
}
Some(qdrant::shard_key::Key::Number(number)) => Ok(PlanShardKey::Number(*number)),
None => Err(envelope_err("shard key carries no value")),
}
}
fn payload_to_typed(
payload: HashMap<String, qdrant::Value>,
) -> Option<HashMap<String, serde_json::Value>> {
Some(
payload
.into_iter()
.map(|(k, v)| (k, qdrant_value_to_json(&v)))
.collect(),
)
}
fn vector_output_to_plan(vo: qdrant::VectorOutput) -> Result<PlanVectorValue, QqlError> {
use qdrant::vector_output;
match vo.vector {
Some(vector_output::Vector::Dense(dense)) => Ok(PlanVectorValue::Dense(dense.data)),
Some(vector_output::Vector::Sparse(sparse)) => Ok(PlanVectorValue::Sparse {
indices: sparse.indices,
values: sparse.values,
}),
Some(vector_output::Vector::MultiDense(multi)) => Ok(PlanVectorValue::MultiDense(
multi.vectors.into_iter().map(|d| d.data).collect(),
)),
None => Err(envelope_err("vector output carries no vector")),
}
}
fn vectors_output_to_owned(vectors: qdrant::VectorsOutput) -> Result<PlanVectorStruct, QqlError> {
use qdrant::vectors_output::VectorsOptions;
match vectors.vectors_options {
Some(VectorsOptions::Vector(vo)) => {
Ok(PlanVectorStruct::Single(vector_output_to_plan(vo)?))
}
Some(VectorsOptions::Vectors(named)) => {
let entries = named
.vectors
.into_iter()
.map(|(name, vo)| Ok((name, vector_output_to_plan(vo)?)))
.collect::<Result<BTreeMap<_, _>, QqlError>>()?;
Ok(PlanVectorStruct::Named(entries))
}
None => Err(envelope_err("vectors output carries no vector options")),
}
}
#[cfg(test)]
pub(crate) fn vectors_output_to_typed(
vectors: &qdrant::VectorsOutput,
) -> Result<PlanVectorStruct, QqlError> {
vectors_output_to_owned(vectors.clone())
}
fn vector_to_typed(
vectors: Option<qdrant::VectorsOutput>,
) -> Result<Option<PlanVectorStruct>, QqlError> {
vectors.map(vectors_output_to_owned).transpose()
}
pub(crate) fn scored_point_to_hit(mut p: qdrant::ScoredPoint) -> Result<SearchHit, QqlError> {
Ok(SearchHit {
id: point_id_to_plan(p.id.take())?,
score: score_f64(p.score),
payload: payload_to_typed(p.payload),
collection: None,
vector: vector_to_typed(p.vectors.take())?,
})
}
pub(crate) fn retrieved_point_to_hit(mut p: qdrant::RetrievedPoint) -> Result<SearchHit, QqlError> {
Ok(SearchHit {
id: point_id_to_plan(p.id.take())?,
score: 0.0,
payload: payload_to_typed(p.payload),
collection: None,
vector: vector_to_typed(p.vectors.take())?,
})
}
fn group_id_to_plan(id: qdrant::GroupId) -> Result<PlanGroupId, QqlError> {
match id.kind {
Some(qdrant::group_id::Kind::UnsignedValue(n)) => Ok(PlanGroupId::Unsigned(n)),
Some(qdrant::group_id::Kind::IntegerValue(i)) => Ok(PlanGroupId::Signed(i)),
Some(qdrant::group_id::Kind::StringValue(s)) => Ok(PlanGroupId::Keyword(s)),
None => Err(envelope_err("group id carries no value")),
}
}
pub(crate) fn point_group_to_typed(
mut g: qdrant::PointGroup,
) -> Result<GroupedSearchResult, QqlError> {
let id =
g.id.take()
.ok_or_else(|| envelope_err("group is missing its id"))?;
Ok(GroupedSearchResult {
group_id: group_id_to_plan(id)?,
hits: g
.hits
.into_iter()
.map(scored_point_to_hit)
.collect::<Result<_, _>>()?,
})
}
pub(crate) fn facet_hit_to_typed(hit: qdrant::FacetHit) -> Result<FacetHit, QqlError> {
use qdrant::facet_value::Variant;
let value = match hit.value.and_then(|v| v.variant) {
Some(Variant::StringValue(s)) => PlanFacetValue::Keyword(s),
Some(Variant::IntegerValue(i)) => PlanFacetValue::Integer(i),
Some(Variant::BoolValue(b)) => PlanFacetValue::Bool(b),
None => return Err(envelope_err("facet hit is missing its value")),
};
Ok(FacetHit {
value,
count: hit.count,
})
}
pub(crate) fn usage_to_telemetry(usage: Option<&qdrant::Usage>) -> Option<ServerUsage> {
let report = usage?;
let hardware = report.hardware.map(|h| HardwareUsage {
cpu: h.cpu,
payload_io_read: h.payload_io_read,
payload_io_write: h.payload_io_write,
payload_index_io_read: h.payload_index_io_read,
payload_index_io_write: h.payload_index_io_write,
vector_io_read: h.vector_io_read,
vector_io_write: h.vector_io_write,
});
let inference = report.inference.as_ref().map(|inf| InferenceUsage {
models: inf
.models
.iter()
.map(|(name, m)| (name.clone(), ModelUsage { tokens: m.tokens }))
.collect(),
});
if hardware.is_none() && inference.is_none() {
return None;
}
Some(ServerUsage {
hardware,
inference,
})
}
pub(crate) fn telemetry_from_proto(
time: f64,
usage: Option<&qdrant::Usage>,
) -> Option<ServerTelemetry> {
Some(ServerTelemetry {
time_s: Some(time),
usage: usage_to_telemetry(usage),
})
}
pub(crate) fn mutation_response_to_typed(resp: qdrant::PointsOperationResponse) -> BackendResponse {
BackendResponse {
data: ExecData::Mutation { affected: None },
telemetry: telemetry_from_proto(resp.time, resp.usage.as_ref()),
}
}
pub(crate) fn collection_mutation_to_typed(time: f64) -> BackendResponse {
BackendResponse {
data: ExecData::Mutation { affected: None },
telemetry: telemetry_from_proto(time, None),
}
}
#[cfg(test)]
pub(crate) fn usage_to_json(usage: Option<&qdrant::Usage>) -> serde_json::Value {
let Some(report) = usage else {
return serde_json::Value::Null;
};
let hardware = report.hardware.as_ref().map(|h| {
serde_json::json!({
"cpu": h.cpu,
"payload_io_read": h.payload_io_read,
"payload_io_write": h.payload_io_write,
"payload_index_io_read": h.payload_index_io_read,
"payload_index_io_write": h.payload_index_io_write,
"vector_io_read": h.vector_io_read,
"vector_io_write": h.vector_io_write,
})
});
let inference = report.inference.as_ref().map(|inf| {
let models: serde_json::Map<String, serde_json::Value> = inf
.models
.iter()
.map(|(name, m)| (name.clone(), serde_json::json!({ "tokens": m.tokens })))
.collect();
serde_json::json!({ "models": models })
});
serde_json::json!({
"hardware": hardware,
"inference": inference,
})
}