use crate::bridge::envelope::PhysicalPlan;
use crate::control::server::shared::session::read_set::{EngineTag, ReadKey};
use crate::types::KeyRepr;
use nodedb_physical::physical_plan::{
ColumnarOp, CrdtOp, DocumentOp, GraphOp, KvOp, MetaOp, QueryOp, SpatialOp, TextOp,
TimeseriesOp, VectorOp,
};
pub(crate) fn extract_collection(plan: &PhysicalPlan) -> Option<&str> {
match plan {
PhysicalPlan::Document(DocumentOp::PointGet { collection, .. })
| PhysicalPlan::Vector(VectorOp::Search { collection, .. })
| PhysicalPlan::Document(DocumentOp::RangeScan { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::Read { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::Apply { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::DocUpsert { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::DocDelete { collection, .. })
| PhysicalPlan::Vector(VectorOp::Insert { collection, .. })
| PhysicalPlan::Vector(VectorOp::BatchInsert { collection, .. })
| PhysicalPlan::Vector(VectorOp::MultiSearch { collection, .. })
| PhysicalPlan::Vector(VectorOp::Delete { collection, .. })
| PhysicalPlan::Document(DocumentOp::BatchInsert { collection, .. })
| PhysicalPlan::Document(DocumentOp::PointPut { collection, .. })
| PhysicalPlan::Document(DocumentOp::PointInsert { collection, .. })
| PhysicalPlan::Document(DocumentOp::PointDelete { collection, .. })
| PhysicalPlan::Document(DocumentOp::PointUpdate { collection, .. })
| PhysicalPlan::Document(DocumentOp::Scan { collection, .. })
| PhysicalPlan::Query(QueryOp::Aggregate { collection, .. })
| PhysicalPlan::Query(QueryOp::HashJoin {
left_collection: collection,
..
})
| PhysicalPlan::Query(QueryOp::NestedLoopJoin {
left_collection: collection,
..
})
| PhysicalPlan::Graph(GraphOp::RagFusion { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::SetPolicy { collection, .. })
| PhysicalPlan::Crdt(CrdtOp::GetPolicy { collection, .. })
| PhysicalPlan::Vector(VectorOp::SetParams { collection, .. })
| PhysicalPlan::Text(TextOp::Search { collection, .. })
| PhysicalPlan::Text(TextOp::PhraseSearch { collection, .. })
| PhysicalPlan::Text(TextOp::HybridSearch { collection, .. })
| PhysicalPlan::Text(TextOp::HybridSearchTriple { collection, .. })
| PhysicalPlan::Text(TextOp::BM25ScoreScan { collection, .. })
| PhysicalPlan::Text(TextOp::FtsIndexDoc { collection, .. })
| PhysicalPlan::Text(TextOp::FtsDeleteDoc { collection, .. })
| PhysicalPlan::Text(TextOp::SetAnalyzer { collection, .. })
| PhysicalPlan::Query(QueryOp::PartialAggregate { collection, .. })
| PhysicalPlan::Query(QueryOp::FacetCounts { collection, .. })
| PhysicalPlan::Document(DocumentOp::BulkUpdate { collection, .. })
| PhysicalPlan::Document(DocumentOp::BulkDelete { collection, .. })
| PhysicalPlan::Document(DocumentOp::Upsert { collection, .. })
| PhysicalPlan::Document(DocumentOp::InsertSelect {
target_collection: collection,
..
})
| PhysicalPlan::Document(DocumentOp::Truncate { collection, .. })
| PhysicalPlan::Document(DocumentOp::EstimateCount { collection, .. })
| PhysicalPlan::Columnar(ColumnarOp::Scan { collection, .. })
| PhysicalPlan::Columnar(ColumnarOp::Insert { collection, .. })
| PhysicalPlan::Columnar(ColumnarOp::Update { collection, .. })
| PhysicalPlan::Columnar(ColumnarOp::Delete { collection, .. })
| PhysicalPlan::Timeseries(TimeseriesOp::Scan { collection, .. })
| PhysicalPlan::Timeseries(TimeseriesOp::Ingest { collection, .. })
| PhysicalPlan::Spatial(SpatialOp::Scan { collection, .. })
| PhysicalPlan::Document(DocumentOp::Register { collection, .. })
| PhysicalPlan::Document(DocumentOp::IndexLookup { collection, .. })
| PhysicalPlan::Document(DocumentOp::IndexedFetch { collection, .. })
| PhysicalPlan::Document(DocumentOp::DropIndex { collection, .. }) => {
Some(collection.as_str())
}
PhysicalPlan::Graph(GraphOp::EdgePut { .. })
| PhysicalPlan::Graph(GraphOp::EdgeDelete { .. })
| PhysicalPlan::Graph(GraphOp::Hop { .. })
| PhysicalPlan::Graph(GraphOp::Neighbors { .. })
| PhysicalPlan::Graph(GraphOp::Path { .. })
| PhysicalPlan::Graph(GraphOp::Subgraph { .. })
| PhysicalPlan::Meta(MetaOp::WalAppend { .. })
| PhysicalPlan::Meta(MetaOp::Cancel { .. })
| PhysicalPlan::Meta(MetaOp::TransactionBatch { .. })
| PhysicalPlan::Meta(MetaOp::CreateSnapshot)
| PhysicalPlan::Meta(MetaOp::Compact)
| PhysicalPlan::Meta(MetaOp::Checkpoint)
| PhysicalPlan::Graph(GraphOp::Algo { .. })
| PhysicalPlan::Graph(GraphOp::Match { .. })
| PhysicalPlan::Graph(GraphOp::MatchContinuation { .. })
| PhysicalPlan::Graph(GraphOp::MatchVarLenResume { .. })
| PhysicalPlan::Graph(GraphOp::BspSuperstep(_))
| PhysicalPlan::Graph(GraphOp::WccSuperstep(_)) => None,
PhysicalPlan::Query(QueryOp::Exchange(op)) => extract_collection(&op.child),
PhysicalPlan::Query(QueryOp::ProviderScan { .. }) => None,
PhysicalPlan::Kv(op) => op.collection(),
PhysicalPlan::Document(_)
| PhysicalPlan::Vector(_)
| PhysicalPlan::Graph(_)
| PhysicalPlan::Columnar(_)
| PhysicalPlan::Spatial(_)
| PhysicalPlan::Crdt(_)
| PhysicalPlan::Query(_)
| PhysicalPlan::Meta(_)
| PhysicalPlan::Array(_)
| PhysicalPlan::ClusterArray(_) => None,
}
}
pub(crate) fn plan_engine(plan: &PhysicalPlan) -> EngineTag {
match plan {
PhysicalPlan::Vector(_) => EngineTag::Vector,
PhysicalPlan::Graph(_) => EngineTag::Graph,
PhysicalPlan::Document(_) => EngineTag::Document,
PhysicalPlan::Kv(_) => EngineTag::Kv,
PhysicalPlan::Text(_) => EngineTag::Text,
PhysicalPlan::Columnar(_) => EngineTag::Columnar,
PhysicalPlan::Timeseries(_) => EngineTag::Timeseries,
PhysicalPlan::Spatial(_) => EngineTag::Spatial,
PhysicalPlan::Crdt(_) => EngineTag::Crdt,
PhysicalPlan::Query(_) => EngineTag::Query,
PhysicalPlan::Meta(_) => EngineTag::Meta,
PhysicalPlan::Array(_) => EngineTag::Array,
PhysicalPlan::ClusterArray(_) => EngineTag::ClusterArray,
}
}
pub(crate) fn read_key_of(plan: &PhysicalPlan, found: bool) -> ReadKey {
match plan {
PhysicalPlan::Document(DocumentOp::PointGet { surrogate, .. }) => {
if found {
ReadKey::Point {
repr: KeyRepr::Surrogate(surrogate.as_u32()),
}
} else {
ReadKey::Predicate
}
}
PhysicalPlan::Kv(KvOp::Get { key, .. }) | PhysicalPlan::Kv(KvOp::FieldGet { key, .. }) => {
ReadKey::Point {
repr: KeyRepr::KvKey(key.clone().into_boxed_slice()),
}
}
PhysicalPlan::Document(
DocumentOp::IndexedFetch { path, value, .. }
| DocumentOp::IndexLookup { path, value, .. },
) => ReadKey::IndexEq {
field: path.clone(),
value: value.clone(),
},
PhysicalPlan::Document(DocumentOp::RangeScan {
field,
lower,
upper,
..
}) => ReadKey::IndexRange {
field: field.clone(),
lo: lower
.as_ref()
.map(|b| String::from_utf8_lossy(b).into_owned()),
hi: upper
.as_ref()
.map(|b| String::from_utf8_lossy(b).into_owned()),
},
PhysicalPlan::Document(_)
| PhysicalPlan::Kv(_)
| PhysicalPlan::Vector(_)
| PhysicalPlan::Graph(_)
| PhysicalPlan::Text(_)
| PhysicalPlan::Columnar(_)
| PhysicalPlan::Timeseries(_)
| PhysicalPlan::Spatial(_)
| PhysicalPlan::Crdt(_)
| PhysicalPlan::Query(_)
| PhysicalPlan::Meta(_)
| PhysicalPlan::Array(_)
| PhysicalPlan::ClusterArray(_) => ReadKey::Predicate,
}
}