2023-06-30 16:51:03 +00:00
|
|
|
use crate::ctx::Context;
|
2023-06-19 18:41:13 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::err::Error;
|
|
|
|
use crate::idx::planner::plan::IndexOption;
|
2023-06-21 18:31:15 +00:00
|
|
|
use crate::sql::index::Index;
|
2023-06-19 18:41:13 +00:00
|
|
|
use crate::sql::statements::DefineIndexStatement;
|
|
|
|
use crate::sql::{Cond, Expression, Idiom, Operator, Subquery, Table, Value};
|
|
|
|
use async_recursion::async_recursion;
|
2023-06-23 20:26:19 +00:00
|
|
|
use std::collections::HashMap;
|
2023-06-19 18:41:13 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-06-21 18:31:15 +00:00
|
|
|
pub(super) struct Tree {}
|
2023-06-19 18:41:13 +00:00
|
|
|
|
|
|
|
impl Tree {
|
2023-06-23 20:26:19 +00:00
|
|
|
/// Traverse the all the conditions and extract every expression
|
|
|
|
/// that can be resolved by an index.
|
2023-06-19 18:41:13 +00:00
|
|
|
pub(super) async fn build<'a>(
|
2023-06-30 16:51:03 +00:00
|
|
|
ctx: &'a Context<'_>,
|
2023-06-19 18:41:13 +00:00
|
|
|
opt: &'a Options,
|
|
|
|
txn: &'a Transaction,
|
|
|
|
table: &'a Table,
|
|
|
|
cond: &Option<Cond>,
|
|
|
|
) -> Result<Option<(Node, IndexMap)>, Error> {
|
|
|
|
let mut b = TreeBuilder {
|
2023-06-30 16:51:03 +00:00
|
|
|
ctx,
|
2023-06-19 18:41:13 +00:00
|
|
|
opt,
|
|
|
|
txn,
|
|
|
|
table,
|
|
|
|
indexes: None,
|
|
|
|
index_map: IndexMap::default(),
|
|
|
|
};
|
|
|
|
let mut res = None;
|
|
|
|
if let Some(cond) = cond {
|
|
|
|
res = Some((b.eval_value(&cond.0).await?, b.index_map));
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TreeBuilder<'a> {
|
2023-06-30 16:51:03 +00:00
|
|
|
ctx: &'a Context<'a>,
|
2023-06-19 18:41:13 +00:00
|
|
|
opt: &'a Options,
|
|
|
|
txn: &'a Transaction,
|
|
|
|
table: &'a Table,
|
|
|
|
indexes: Option<Arc<[DefineIndexStatement]>>,
|
|
|
|
index_map: IndexMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TreeBuilder<'a> {
|
|
|
|
async fn find_index(&mut self, i: &Idiom) -> Result<Option<DefineIndexStatement>, Error> {
|
|
|
|
if self.indexes.is_none() {
|
|
|
|
let indexes = self
|
|
|
|
.txn
|
|
|
|
.clone()
|
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.all_ix(self.opt.ns(), self.opt.db(), &self.table.0)
|
|
|
|
.await?;
|
|
|
|
self.indexes = Some(indexes);
|
|
|
|
}
|
|
|
|
if let Some(indexes) = &self.indexes {
|
|
|
|
for ix in indexes.as_ref() {
|
|
|
|
if ix.cols.len() == 1 && ix.cols[0].eq(i) {
|
|
|
|
return Ok(Some(ix.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
|
|
|
|
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
|
|
|
|
async fn eval_value(&mut self, v: &Value) -> Result<Node, Error> {
|
|
|
|
Ok(match v {
|
|
|
|
Value::Expression(e) => self.eval_expression(e).await?,
|
|
|
|
Value::Idiom(i) => self.eval_idiom(i).await?,
|
|
|
|
Value::Strand(_) => Node::Scalar(v.to_owned()),
|
|
|
|
Value::Number(_) => Node::Scalar(v.to_owned()),
|
|
|
|
Value::Bool(_) => Node::Scalar(v.to_owned()),
|
|
|
|
Value::Subquery(s) => self.eval_subquery(s).await?,
|
2023-06-30 16:51:03 +00:00
|
|
|
Value::Param(p) => {
|
2023-07-06 14:57:42 +00:00
|
|
|
let v = p.compute(self.ctx, self.opt, self.txn, None).await?;
|
2023-06-30 16:51:03 +00:00
|
|
|
self.eval_value(&v).await?
|
|
|
|
}
|
2023-06-19 18:41:13 +00:00
|
|
|
_ => Node::Unsupported,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn eval_idiom(&mut self, i: &Idiom) -> Result<Node, Error> {
|
|
|
|
Ok(if let Some(ix) = self.find_index(i).await? {
|
2023-06-21 18:31:15 +00:00
|
|
|
Node::IndexedField(i.to_owned(), ix)
|
2023-06-19 18:41:13 +00:00
|
|
|
} else {
|
|
|
|
Node::NonIndexedField
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn eval_expression(&mut self, e: &Expression) -> Result<Node, Error> {
|
2023-06-20 23:31:23 +00:00
|
|
|
match e {
|
|
|
|
Expression::Unary {
|
|
|
|
..
|
2023-06-21 07:45:39 +00:00
|
|
|
} => Err(Error::FeatureNotYetImplemented {
|
|
|
|
feature: "unary expressions in index",
|
|
|
|
}),
|
2023-06-20 23:31:23 +00:00
|
|
|
Expression::Binary {
|
|
|
|
l,
|
|
|
|
o,
|
|
|
|
r,
|
|
|
|
} => {
|
|
|
|
let left = self.eval_value(l).await?;
|
|
|
|
let right = self.eval_value(r).await?;
|
2023-06-23 20:26:19 +00:00
|
|
|
if let Some(io) = self.index_map.0.get(e) {
|
|
|
|
return Ok(Node::Expression {
|
|
|
|
io: Some(io.clone()),
|
|
|
|
left: Box::new(left),
|
|
|
|
right: Box::new(right),
|
|
|
|
exp: e.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let mut io = None;
|
2023-06-21 18:31:15 +00:00
|
|
|
if let Some((id, ix)) = left.is_indexed_field() {
|
2023-06-23 20:26:19 +00:00
|
|
|
io = self.lookup_index_option(ix, o, id, &right, e);
|
2023-06-21 18:31:15 +00:00
|
|
|
} else if let Some((id, ix)) = right.is_indexed_field() {
|
2023-06-23 20:26:19 +00:00
|
|
|
io = self.lookup_index_option(ix, o, id, &left, e);
|
2023-06-21 18:31:15 +00:00
|
|
|
};
|
2023-06-20 23:31:23 +00:00
|
|
|
Ok(Node::Expression {
|
2023-06-23 20:26:19 +00:00
|
|
|
io,
|
2023-06-20 23:31:23 +00:00
|
|
|
left: Box::new(left),
|
|
|
|
right: Box::new(right),
|
2023-06-23 20:26:19 +00:00
|
|
|
exp: e.clone(),
|
2023-06-20 23:31:23 +00:00
|
|
|
})
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:31:15 +00:00
|
|
|
fn lookup_index_option(
|
|
|
|
&mut self,
|
|
|
|
ix: &DefineIndexStatement,
|
|
|
|
op: &Operator,
|
|
|
|
id: &Idiom,
|
|
|
|
v: &Node,
|
2023-06-23 20:26:19 +00:00
|
|
|
e: &Expression,
|
2023-06-21 18:31:15 +00:00
|
|
|
) -> Option<IndexOption> {
|
|
|
|
if let Some(v) = v.is_scalar() {
|
2023-06-23 20:26:19 +00:00
|
|
|
let (found, mr, qs) = match &ix.index {
|
|
|
|
Index::Idx => (Operator::Equal.eq(op), None, None),
|
|
|
|
Index::Uniq => (Operator::Equal.eq(op), None, None),
|
2023-06-21 18:31:15 +00:00
|
|
|
Index::Search {
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
if let Operator::Matches(mr) = op {
|
2023-06-23 20:26:19 +00:00
|
|
|
(true, *mr, Some(v.clone().to_raw_string()))
|
2023-06-21 18:31:15 +00:00
|
|
|
} else {
|
2023-06-23 20:26:19 +00:00
|
|
|
(false, None, None)
|
2023-06-21 18:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-23 20:26:19 +00:00
|
|
|
};
|
|
|
|
if found {
|
|
|
|
let io = IndexOption::new(ix.clone(), id.clone(), op.to_owned(), v.clone(), qs, mr);
|
|
|
|
self.index_map.0.insert(e.clone(), io.clone());
|
2023-06-21 18:31:15 +00:00
|
|
|
return Some(io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-06-19 18:41:13 +00:00
|
|
|
async fn eval_subquery(&mut self, s: &Subquery) -> Result<Node, Error> {
|
|
|
|
Ok(match s {
|
|
|
|
Subquery::Value(v) => self.eval_value(v).await?,
|
|
|
|
_ => Node::Unsupported,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:26:19 +00:00
|
|
|
/// For each expression the a possible index option
|
|
|
|
#[derive(Default)]
|
|
|
|
pub(super) struct IndexMap(HashMap<Expression, IndexOption>);
|
|
|
|
|
|
|
|
impl IndexMap {
|
|
|
|
pub(super) fn consume(self) -> HashMap<Expression, IndexOption> {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 18:41:13 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
|
|
|
pub(super) enum Node {
|
|
|
|
Expression {
|
2023-06-23 20:26:19 +00:00
|
|
|
io: Option<IndexOption>,
|
2023-06-19 18:41:13 +00:00
|
|
|
left: Box<Node>,
|
|
|
|
right: Box<Node>,
|
2023-06-23 20:26:19 +00:00
|
|
|
exp: Expression,
|
2023-06-19 18:41:13 +00:00
|
|
|
},
|
2023-06-21 18:31:15 +00:00
|
|
|
IndexedField(Idiom, DefineIndexStatement),
|
2023-06-19 18:41:13 +00:00
|
|
|
NonIndexedField,
|
|
|
|
Scalar(Value),
|
|
|
|
Unsupported,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
|
|
|
pub(super) fn is_scalar(&self) -> Option<&Value> {
|
|
|
|
if let Node::Scalar(v) = self {
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:31:15 +00:00
|
|
|
pub(super) fn is_indexed_field(&self) -> Option<(&Idiom, &DefineIndexStatement)> {
|
|
|
|
if let Node::IndexedField(id, ix) = self {
|
|
|
|
Some((id, ix))
|
2023-06-19 18:41:13 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|