use crate::filter::{point_id_req_typed, top_level_filter};
use crate::semantic::PlanShardKey;
use crate::types::*;
use alloc::format;
use qql_core::ast::{
ClearPayloadStmt, DeletePayloadStmt, DeleteStmt, DeleteVectorStmt, PointEntry, PointSelector,
UpdatePayloadStmt, UpdateVectorStmt, UpsertPoint, UpsertStmt,
};
use qql_core::error::QqlError;
pub fn lower_upsert_request(stmt: &UpsertStmt) -> Result<UpsertRequest, QqlError> {
Ok(UpsertRequest {
points: stmt
.points
.iter()
.filter_map(|point| match point {
PointEntry::Inline(inline) => Some(lower_upsert_point(inline)),
PointEntry::Param(..) | PointEntry::PositionalParam(..) => None,
})
.collect(),
update_filter: stmt
.update_filter
.as_ref()
.map(top_level_filter)
.transpose()?,
update_mode: stmt.update_mode.map(|mode| match mode {
qql_core::ast::UpsertUpdateMode::InsertOnly => UpdateMode::InsertOnly,
qql_core::ast::UpsertUpdateMode::UpdateOnly => UpdateMode::UpdateOnly,
qql_core::ast::UpsertUpdateMode::Upsert => UpdateMode::Upsert,
}),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
})
}
fn lower_upsert_point(point: &UpsertPoint) -> UpsertPointRequest {
let mut req = UpsertPointRequest {
id: PlanPointId::from(&point.id),
vector: None,
payload: None,
};
if let Some(ref vectors) = point.vectors {
req.vector = Some(PlanPointVectors::from(vectors));
}
if !point.payload.is_empty() {
req.payload = Some(point.payload.clone());
}
req
}
pub fn lower_delete_request(stmt: &DeleteStmt) -> Result<DeleteRequest, QqlError> {
Ok(match &stmt.selector {
PointSelector::Id(id) => DeleteRequest {
points: Some(vec![point_id_req_typed(id)]),
filter: None,
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Ids(ids) => DeleteRequest {
points: Some(ids.iter().map(point_id_req_typed).collect()),
filter: None,
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Filter(filter) => DeleteRequest {
points: None,
filter: Some(top_level_filter(filter)?),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
})
}
pub fn lower_update_vector_request(stmt: &UpdateVectorStmt) -> UpdateVectorRequest {
UpdateVectorRequest {
points: stmt
.points
.iter()
.map(|point| UpdateVectorPoint {
id: PlanPointId::from(&point.id),
vector: PlanPointVectors::from(&point.vectors),
})
.collect(),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
}
}
pub fn lower_update_payload_request(
stmt: &UpdatePayloadStmt,
) -> Result<UpdatePayloadRequest, QqlError> {
let payload = stmt.payload.clone();
Ok(match &stmt.selector {
PointSelector::Id(id) => UpdatePayloadRequest {
points: Some(vec![point_id_req_typed(id)]),
filter: None,
payload,
key: stmt.key.clone(),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Ids(ids) => UpdatePayloadRequest {
points: Some(ids.iter().map(point_id_req_typed).collect()),
filter: None,
payload,
key: stmt.key.clone(),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Filter(filter) => UpdatePayloadRequest {
points: None,
filter: Some(top_level_filter(filter)?),
payload,
key: stmt.key.clone(),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
})
}
pub fn lower_clear_payload_request(
stmt: &ClearPayloadStmt,
) -> Result<ClearPayloadRequest, QqlError> {
Ok(match &stmt.selector {
PointSelector::Id(id) => ClearPayloadRequest {
points: Some(vec![point_id_req_typed(id)]),
filter: None,
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Ids(ids) => ClearPayloadRequest {
points: Some(ids.iter().map(point_id_req_typed).collect()),
filter: None,
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Filter(filter) => ClearPayloadRequest {
points: None,
filter: Some(top_level_filter(filter)?),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
})
}
pub fn lower_delete_payload_request(
stmt: &DeletePayloadStmt,
) -> Result<DeletePayloadRequest, QqlError> {
Ok(match &stmt.selector {
PointSelector::Id(id) => DeletePayloadRequest {
keys: stmt.keys.clone(),
points: Some(vec![point_id_req_typed(id)]),
filter: None,
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Ids(ids) => DeletePayloadRequest {
keys: stmt.keys.clone(),
points: Some(ids.iter().map(point_id_req_typed).collect()),
filter: None,
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Filter(filter) => DeletePayloadRequest {
keys: stmt.keys.clone(),
points: None,
filter: Some(top_level_filter(filter)?),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
})
}
pub fn lower_delete_vector_request(
stmt: &DeleteVectorStmt,
) -> Result<DeleteVectorRequest, QqlError> {
Ok(match &stmt.selector {
PointSelector::Id(id) => DeleteVectorRequest {
points: Some(vec![point_id_req_typed(id)]),
filter: None,
vector: stmt.vector_names.clone(),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Ids(ids) => DeleteVectorRequest {
points: Some(ids.iter().map(point_id_req_typed).collect()),
filter: None,
vector: stmt.vector_names.clone(),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
PointSelector::Filter(filter) => DeleteVectorRequest {
points: None,
filter: Some(top_level_filter(filter)?),
vector: stmt.vector_names.clone(),
shard_key: stmt.shard_key.as_ref().map(PlanShardKey::from),
},
})
}
fn uuid_to_u128(s: &str) -> Option<u128> {
let clean: String = s.chars().filter(|c| *c != '-').collect();
if clean.len() == 32 {
u128::from_str_radix(&clean, 16).ok()
} else {
None
}
}
fn increment_uuid_point_id(s: &str) -> Option<String> {
let val = uuid_to_u128(s)?;
let next = val.checked_add(1)?;
Some(format!(
"{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
(next >> 96) as u32,
((next >> 80) & 0xffff) as u16,
((next >> 64) & 0xffff) as u16,
((next >> 48) & 0xffff) as u16,
(next & 0xffff_ffff_ffff) as u64
))
}
pub fn validate_scroll_after(after: Option<&qql_core::ast::PointId>) -> Result<(), QqlError> {
let Some(id) = after else {
return Ok(());
};
match id {
qql_core::ast::PointId::Number(n) if *n == u64::MAX => Err(QqlError::validation(
"QQL-PLAN-SCROLL-AFTER",
"SCROLL AFTER is already the maximum point ID; there is no next page",
None,
)),
qql_core::ast::PointId::Number(_) => Ok(()),
qql_core::ast::PointId::String(s) => match uuid_to_u128(s) {
Some(v) if v == u128::MAX => Err(QqlError::validation(
"QQL-PLAN-SCROLL-AFTER",
"SCROLL AFTER is already the maximum UUID; there is no next page",
None,
)),
_ => Ok(()),
},
qql_core::ast::PointId::Param(..) | qql_core::ast::PointId::PositionalParam(..) => Ok(()),
}
}
pub fn lower_scroll_request(
limit: u64,
filter: Option<&qql_core::ast::FilterExpr>,
after: Option<&qql_core::ast::PointId>,
order_by: Option<&qql_core::ast::ScrollOrderBy>,
shard_key: Option<qql_core::ast::ShardKey>,
with_payload: Option<&qql_core::ast::PayloadSelector>,
with_vector: Option<&qql_core::ast::VectorSelector>,
) -> Result<ScrollRequest, QqlError> {
let with_payload = match with_payload {
None => Some(PayloadSelectorReq::All(true)),
Some(qql_core::ast::PayloadSelector::All) => Some(PayloadSelectorReq::All(true)),
Some(qql_core::ast::PayloadSelector::None) => Some(PayloadSelectorReq::All(false)),
Some(qql_core::ast::PayloadSelector::Include(fields)) => {
Some(PayloadSelectorReq::Include {
include: fields.clone(),
})
}
Some(qql_core::ast::PayloadSelector::Exclude(fields)) => {
Some(PayloadSelectorReq::Exclude {
exclude: fields.clone(),
})
}
};
let with_vector = match with_vector {
Some(qql_core::ast::VectorSelector::All) => Some(VectorSelectorReq::All(true)),
Some(qql_core::ast::VectorSelector::None) => Some(VectorSelectorReq::All(false)),
Some(qql_core::ast::VectorSelector::Names(names)) => {
Some(VectorSelectorReq::Names(names.clone()))
}
None => Some(VectorSelectorReq::All(false)),
};
Ok(ScrollRequest {
filter: filter.map(top_level_filter).transpose()?,
offset: after.map(|id| match id {
qql_core::ast::PointId::Number(n) => PlanPointId::Number(n.saturating_add(1)),
qql_core::ast::PointId::String(s) => {
if let Some(next) = increment_uuid_point_id(s) {
PlanPointId::String(next)
} else {
PlanPointId::String(s.clone())
}
}
other => point_id_req_typed(other),
}),
limit: Some(limit),
with_payload,
with_vector,
order_by: order_by.map(|order| OrderByQuery {
key: order.field.clone(),
direction: Some(match order.direction {
qql_core::ast::OrderDirection::Asc => "asc".into(),
qql_core::ast::OrderDirection::Desc => "desc".into(),
}),
start_from: order.start_from.clone(),
}),
shard_key: shard_key.as_ref().map(PlanShardKey::from),
})
}
pub fn planned_to_update_operation(
op: &crate::plan::PlannedOperation,
) -> Option<(String, UpdateOperation)> {
use crate::plan::PlannedOperation;
match op {
PlannedOperation::Upsert {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::Upsert {
upsert: request.clone(),
},
)),
PlannedOperation::Delete {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::Delete {
delete: request.clone(),
},
)),
PlannedOperation::UpdatePayload {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::SetPayload {
set_payload: request.clone(),
},
)),
PlannedOperation::OverwritePayload {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::Overwrite {
overwrite_payload: request.clone(),
},
)),
PlannedOperation::ClearPayload {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::ClearPayload {
clear_payload: request.clone(),
},
)),
PlannedOperation::DeletePayload {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::DeletePayload {
delete_payload: request.clone(),
},
)),
PlannedOperation::UpdateVectors {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::UpdateVectors {
update_vectors: request.clone(),
},
)),
PlannedOperation::DeleteVectors {
collection,
request,
..
} => Some((
collection.clone(),
UpdateOperation::DeleteVectors {
delete_vectors: request.clone(),
},
)),
_ => None,
}
}
pub fn planned_to_update_operation_owned(
op: crate::plan::PlannedOperation,
) -> Option<(String, UpdateOperation)> {
use crate::plan::PlannedOperation;
match op {
PlannedOperation::Upsert {
collection,
request,
..
} => Some((collection, UpdateOperation::Upsert { upsert: request })),
PlannedOperation::Delete {
collection,
request,
..
} => Some((collection, UpdateOperation::Delete { delete: request })),
PlannedOperation::UpdatePayload {
collection,
request,
..
} => Some((
collection,
UpdateOperation::SetPayload {
set_payload: request,
},
)),
PlannedOperation::OverwritePayload {
collection,
request,
..
} => Some((
collection,
UpdateOperation::Overwrite {
overwrite_payload: request,
},
)),
PlannedOperation::ClearPayload {
collection,
request,
..
} => Some((
collection,
UpdateOperation::ClearPayload {
clear_payload: request,
},
)),
PlannedOperation::DeletePayload {
collection,
request,
..
} => Some((
collection,
UpdateOperation::DeletePayload {
delete_payload: request,
},
)),
PlannedOperation::UpdateVectors {
collection,
request,
..
} => Some((
collection,
UpdateOperation::UpdateVectors {
update_vectors: request,
},
)),
PlannedOperation::DeleteVectors {
collection,
request,
..
} => Some((
collection,
UpdateOperation::DeleteVectors {
delete_vectors: request,
},
)),
_ => None,
}
}
pub fn update_operation_into_planned(
collection: &str,
op: UpdateOperation,
) -> crate::plan::PlannedOperation {
use crate::plan::PlannedOperation;
match op {
UpdateOperation::Upsert { upsert } => PlannedOperation::Upsert {
collection: collection.to_owned(),
request: upsert,
wait: true,
},
UpdateOperation::Delete { delete } => PlannedOperation::Delete {
collection: collection.to_owned(),
request: delete,
wait: true,
},
UpdateOperation::SetPayload { set_payload } => PlannedOperation::UpdatePayload {
collection: collection.to_owned(),
request: set_payload,
wait: true,
},
UpdateOperation::Overwrite { overwrite_payload } => PlannedOperation::OverwritePayload {
collection: collection.to_owned(),
request: overwrite_payload,
wait: true,
},
UpdateOperation::ClearPayload { clear_payload } => PlannedOperation::ClearPayload {
collection: collection.to_owned(),
request: clear_payload,
wait: true,
},
UpdateOperation::DeletePayload { delete_payload } => PlannedOperation::DeletePayload {
collection: collection.to_owned(),
request: delete_payload,
wait: true,
},
UpdateOperation::UpdateVectors { update_vectors } => PlannedOperation::UpdateVectors {
collection: collection.to_owned(),
request: update_vectors,
wait: true,
},
UpdateOperation::DeleteVectors { delete_vectors } => PlannedOperation::DeleteVectors {
collection: collection.to_owned(),
request: delete_vectors,
wait: true,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use qql_core::ast::PointId;
#[test]
fn scroll_after_accepts_incrementable_cursors() {
assert!(validate_scroll_after(None).is_ok());
assert!(validate_scroll_after(Some(&PointId::Number(42))).is_ok());
assert!(
validate_scroll_after(Some(&PointId::String(
"00000000-0000-0000-0000-00000000002a".into()
)))
.is_ok()
);
assert!(validate_scroll_after(Some(&PointId::param("after"))).is_ok());
assert!(validate_scroll_after(Some(&PointId::positional_param(0))).is_ok());
}
#[test]
fn scroll_after_passes_through_opaque_strings() {
assert!(validate_scroll_after(Some(&PointId::String("id-with-quote".into()))).is_ok());
}
#[test]
fn scroll_after_rejects_maximum_ids() {
let err = validate_scroll_after(Some(&PointId::Number(u64::MAX)))
.expect_err("u64::MAX has no successor");
assert_eq!(err.code, "QQL-PLAN-SCROLL-AFTER");
let err = validate_scroll_after(Some(&PointId::String(
"ffffffff-ffff-ffff-ffff-ffffffffffff".into(),
)))
.expect_err("UUID-max has no successor");
assert_eq!(err.code, "QQL-PLAN-SCROLL-AFTER");
assert!(increment_uuid_point_id("ffffffff-ffff-ffff-ffff-ffffffffffff").is_none());
}
}