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;
|
2023-09-12 15:43:32 +00:00
|
|
|
use crate::idx::planner::plan::{IndexOperator, 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;
|
2023-10-25 10:24:04 +00:00
|
|
|
use crate::sql::{Array, Cond, Expression, Idiom, Operator, Part, Subquery, Table, Value, With};
|
2023-06-19 18:41:13 +00:00
|
|
|
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-09-12 15:43:32 +00:00
|
|
|
/// Traverse all the conditions and extract every expression
|
2023-06-23 20:26:19 +00:00
|
|
|
/// 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,
|
2023-07-21 18:41:36 +00:00
|
|
|
cond: &'a Option<Cond>,
|
2023-09-12 15:43:32 +00:00
|
|
|
with: &'a Option<With>,
|
2023-11-08 10:06:52 +00:00
|
|
|
) -> Result<Option<(Node, IndexesMap, Vec<IndexRef>)>, Error> {
|
|
|
|
let mut b = TreeBuilder::new(ctx, opt, txn, table, with);
|
2023-06-19 18:41:13 +00:00
|
|
|
if let Some(cond) = cond {
|
2023-11-08 10:06:52 +00:00
|
|
|
let node = b.eval_value(&cond.0).await?;
|
|
|
|
Ok(Some((node, b.index_map, b.with_indexes)))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-09-12 15:43:32 +00:00
|
|
|
with: &'a Option<With>,
|
2023-06-19 18:41:13 +00:00
|
|
|
indexes: Option<Arc<[DefineIndexStatement]>>,
|
2023-11-08 10:06:52 +00:00
|
|
|
resolved_expressions: HashMap<Arc<Expression>, ResolvedExpression>,
|
|
|
|
resolved_idioms: HashMap<Arc<Idiom>, Arc<Idiom>>,
|
|
|
|
idioms_indexes: HashMap<Arc<Idiom>, Option<Arc<Vec<IndexRef>>>>,
|
|
|
|
index_map: IndexesMap,
|
2023-09-12 15:43:32 +00:00
|
|
|
with_indexes: Vec<IndexRef>,
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TreeBuilder<'a> {
|
2023-11-08 10:06:52 +00:00
|
|
|
fn new(
|
|
|
|
ctx: &'a Context<'_>,
|
|
|
|
opt: &'a Options,
|
|
|
|
txn: &'a Transaction,
|
|
|
|
table: &'a Table,
|
|
|
|
with: &'a Option<With>,
|
|
|
|
) -> Self {
|
|
|
|
let with_indexes = match with {
|
|
|
|
Some(With::Index(ixs)) => Vec::with_capacity(ixs.len()),
|
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
Self {
|
|
|
|
ctx,
|
|
|
|
opt,
|
|
|
|
txn,
|
|
|
|
table,
|
|
|
|
with,
|
|
|
|
indexes: None,
|
|
|
|
resolved_expressions: Default::default(),
|
|
|
|
resolved_idioms: Default::default(),
|
|
|
|
idioms_indexes: Default::default(),
|
|
|
|
index_map: Default::default(),
|
|
|
|
with_indexes,
|
2023-09-12 15:43:32 +00:00
|
|
|
}
|
2023-11-08 10:06:52 +00:00
|
|
|
}
|
|
|
|
async fn lazy_cache_indexes(&mut self) -> Result<(), Error> {
|
2023-06-19 18:41:13 +00:00
|
|
|
if self.indexes.is_none() {
|
|
|
|
let indexes = self
|
|
|
|
.txn
|
|
|
|
.clone()
|
|
|
|
.lock()
|
|
|
|
.await
|
2023-09-01 11:52:02 +00:00
|
|
|
.all_tb_indexes(self.opt.ns(), self.opt.db(), &self.table.0)
|
2023-06-19 18:41:13 +00:00
|
|
|
.await?;
|
|
|
|
self.indexes = Some(indexes);
|
|
|
|
}
|
2023-11-08 10:06:52 +00:00
|
|
|
Ok(())
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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> {
|
2023-09-07 23:36:39 +00:00
|
|
|
match v {
|
|
|
|
Value::Expression(e) => self.eval_expression(e).await,
|
|
|
|
Value::Idiom(i) => self.eval_idiom(i).await,
|
2023-09-12 20:26:03 +00:00
|
|
|
Value::Strand(_) | Value::Number(_) | Value::Bool(_) | Value::Thing(_) => {
|
2023-10-26 21:34:28 +00:00
|
|
|
Ok(Node::Computed(v.to_owned()))
|
2023-09-12 20:26:03 +00:00
|
|
|
}
|
2023-10-26 21:34:28 +00:00
|
|
|
Value::Array(a) => self.eval_array(a).await,
|
2023-09-07 23:36:39 +00:00
|
|
|
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-09-07 23:36:39 +00:00
|
|
|
self.eval_value(&v).await
|
2023-06-30 16:51:03 +00:00
|
|
|
}
|
2023-09-07 23:36:39 +00:00
|
|
|
_ => Ok(Node::Unsupported(format!("Unsupported value: {}", v))),
|
|
|
|
}
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 21:34:28 +00:00
|
|
|
async fn eval_array(&mut self, a: &Array) -> Result<Node, Error> {
|
|
|
|
let mut values = Vec::with_capacity(a.len());
|
2023-09-12 20:26:03 +00:00
|
|
|
for v in &a.0 {
|
2023-10-26 21:34:28 +00:00
|
|
|
values.push(v.compute(self.ctx, self.opt, self.txn, None).await?);
|
2023-09-12 20:26:03 +00:00
|
|
|
}
|
2023-10-26 21:34:28 +00:00
|
|
|
Ok(Node::Computed(Value::Array(Array::from(values))))
|
2023-09-12 20:26:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 18:41:13 +00:00
|
|
|
async fn eval_idiom(&mut self, i: &Idiom) -> Result<Node, Error> {
|
2023-11-08 10:06:52 +00:00
|
|
|
let mut res = Node::NonIndexedField;
|
|
|
|
// Check if the idiom has already been resolved
|
|
|
|
if let Some(i) = self.resolved_idioms.get(i) {
|
|
|
|
if let Some(Some(irs)) = self.idioms_indexes.get(i).cloned() {
|
|
|
|
return Ok(Node::IndexedField(i.clone(), irs));
|
|
|
|
}
|
|
|
|
return Ok(res);
|
|
|
|
};
|
|
|
|
|
2023-10-25 10:24:04 +00:00
|
|
|
// Compute the idiom value if it is a param
|
|
|
|
if let Some(Part::Start(x)) = i.0.first() {
|
|
|
|
if x.is_param() {
|
|
|
|
let v = i.compute(self.ctx, self.opt, self.txn, None).await?;
|
|
|
|
return self.eval_value(&v).await;
|
|
|
|
}
|
|
|
|
}
|
2023-11-08 10:06:52 +00:00
|
|
|
|
|
|
|
self.lazy_cache_indexes().await?;
|
|
|
|
|
|
|
|
let i = Arc::new(i.clone());
|
|
|
|
self.resolved_idioms.insert(i.clone(), i.clone());
|
|
|
|
|
|
|
|
// Try to detect if it matches an index
|
|
|
|
if let Some(irs) = self.resolve_indexes(&i) {
|
|
|
|
res = Node::IndexedField(i.clone(), irs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_indexes(&mut self, i: &Arc<Idiom>) -> Option<Arc<Vec<IndexRef>>> {
|
|
|
|
let mut res = None;
|
|
|
|
if let Some(indexes) = &self.indexes {
|
|
|
|
let mut irs = Vec::new();
|
|
|
|
for ix in indexes.as_ref() {
|
|
|
|
if ix.cols.len() == 1 && ix.cols[0].eq(i) {
|
|
|
|
let ixr = self.index_map.definitions.len() as IndexRef;
|
|
|
|
if let Some(With::Index(ixs)) = self.with {
|
|
|
|
if ixs.contains(&ix.name.0) {
|
|
|
|
self.with_indexes.push(ixr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.index_map.definitions.push(ix.clone());
|
|
|
|
irs.push(ixr);
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 16:36:43 +00:00
|
|
|
if !irs.is_empty() {
|
2023-11-08 10:06:52 +00:00
|
|
|
res = Some(Arc::new(irs));
|
2023-09-12 16:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-08 10:06:52 +00:00
|
|
|
self.idioms_indexes.insert(i.clone(), res.clone());
|
|
|
|
res
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn eval_expression(&mut self, e: &Expression) -> Result<Node, Error> {
|
2023-06-20 23:31:23 +00:00
|
|
|
match e {
|
|
|
|
Expression::Unary {
|
|
|
|
..
|
2023-09-07 23:36:39 +00:00
|
|
|
} => Ok(Node::Unsupported("unary expressions not supported".to_string())),
|
2023-06-20 23:31:23 +00:00
|
|
|
Expression::Binary {
|
|
|
|
l,
|
|
|
|
o,
|
|
|
|
r,
|
|
|
|
} => {
|
2023-11-08 10:06:52 +00:00
|
|
|
// Did we already compute the same expression?
|
|
|
|
if let Some(re) = self.resolved_expressions.get(e).cloned() {
|
|
|
|
return Ok(re.into());
|
2023-06-23 20:26:19 +00:00
|
|
|
}
|
2023-11-08 10:06:52 +00:00
|
|
|
let left = Arc::new(self.eval_value(l).await?);
|
|
|
|
let right = Arc::new(self.eval_value(r).await?);
|
2023-06-23 20:26:19 +00:00
|
|
|
let mut io = None;
|
2023-09-12 16:36:43 +00:00
|
|
|
if let Some((id, irs)) = left.is_indexed_field() {
|
2023-10-26 21:34:28 +00:00
|
|
|
io = self.lookup_index_option(
|
|
|
|
irs.as_slice(),
|
|
|
|
o,
|
|
|
|
id,
|
|
|
|
&right,
|
|
|
|
e,
|
|
|
|
IdiomPosition::Left,
|
|
|
|
);
|
2023-09-12 16:36:43 +00:00
|
|
|
} else if let Some((id, irs)) = right.is_indexed_field() {
|
2023-10-26 21:34:28 +00:00
|
|
|
io = self.lookup_index_option(
|
|
|
|
irs.as_slice(),
|
|
|
|
o,
|
|
|
|
id,
|
|
|
|
&left,
|
|
|
|
e,
|
|
|
|
IdiomPosition::Right,
|
|
|
|
);
|
2023-06-21 18:31:15 +00:00
|
|
|
};
|
2023-11-08 10:06:52 +00:00
|
|
|
let exp = Arc::new(e.clone());
|
|
|
|
let re = ResolvedExpression {
|
|
|
|
exp: exp.clone(),
|
|
|
|
io: io.clone(),
|
|
|
|
left: left.clone(),
|
|
|
|
right: right.clone(),
|
|
|
|
};
|
|
|
|
self.resolved_expressions.insert(exp.clone(), re.clone());
|
|
|
|
Ok(re.into())
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:31:15 +00:00
|
|
|
fn lookup_index_option(
|
|
|
|
&mut self,
|
2023-09-12 16:36:43 +00:00
|
|
|
irs: &[IndexRef],
|
2023-06-21 18:31:15 +00:00
|
|
|
op: &Operator,
|
2023-11-08 10:06:52 +00:00
|
|
|
id: Arc<Idiom>,
|
2023-09-12 20:26:03 +00:00
|
|
|
n: &Node,
|
2023-06-23 20:26:19 +00:00
|
|
|
e: &Expression,
|
2023-10-26 21:34:28 +00:00
|
|
|
p: IdiomPosition,
|
2023-06-21 18:31:15 +00:00
|
|
|
) -> Option<IndexOption> {
|
2023-09-12 20:26:03 +00:00
|
|
|
for ir in irs {
|
2023-11-08 10:06:52 +00:00
|
|
|
if let Some(ix) = self.index_map.definitions.get(*ir as usize) {
|
2023-09-12 20:26:03 +00:00
|
|
|
let op = match &ix.index {
|
2023-10-26 21:34:28 +00:00
|
|
|
Index::Idx => Self::eval_index_operator(op, n, p),
|
|
|
|
Index::Uniq => Self::eval_index_operator(op, n, p),
|
2023-09-12 20:26:03 +00:00
|
|
|
Index::Search {
|
|
|
|
..
|
2023-10-26 21:34:28 +00:00
|
|
|
} => Self::eval_matches_operator(op, n),
|
|
|
|
Index::MTree(_) => Self::eval_knn_operator(op, n),
|
2023-09-12 20:26:03 +00:00
|
|
|
};
|
|
|
|
if let Some(op) = op {
|
2023-11-08 10:06:52 +00:00
|
|
|
let io = IndexOption::new(*ir, id, op);
|
|
|
|
self.index_map.options.push((Arc::new(e.clone()), io.clone()));
|
2023-09-12 20:26:03 +00:00
|
|
|
return Some(io);
|
2023-06-21 18:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2023-10-26 21:34:28 +00:00
|
|
|
fn eval_matches_operator(op: &Operator, n: &Node) -> Option<IndexOperator> {
|
|
|
|
if let Some(v) = n.is_computed() {
|
|
|
|
if let Operator::Matches(mr) = op {
|
|
|
|
return Some(IndexOperator::Matches(v.clone().to_raw_string(), *mr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2023-06-21 18:31:15 +00:00
|
|
|
|
2023-10-26 21:34:28 +00:00
|
|
|
fn eval_knn_operator(op: &Operator, n: &Node) -> Option<IndexOperator> {
|
|
|
|
if let Operator::Knn(k) = op {
|
|
|
|
if let Node::Computed(Value::Array(a)) = n {
|
|
|
|
return Some(IndexOperator::Knn(a.clone(), *k));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_index_operator(op: &Operator, n: &Node, p: IdiomPosition) -> Option<IndexOperator> {
|
|
|
|
if let Some(v) = n.is_computed() {
|
|
|
|
match (op, v, p) {
|
|
|
|
(Operator::Equal, v, _) => Some(IndexOperator::Equality(v.clone())),
|
|
|
|
(Operator::Contain, v, IdiomPosition::Left) => {
|
|
|
|
Some(IndexOperator::Equality(v.clone()))
|
|
|
|
}
|
|
|
|
(Operator::ContainAny, Value::Array(a), IdiomPosition::Left) => {
|
|
|
|
Some(IndexOperator::Union(a.clone()))
|
|
|
|
}
|
|
|
|
(Operator::ContainAll, Value::Array(a), IdiomPosition::Left) => {
|
|
|
|
Some(IndexOperator::Union(a.clone()))
|
|
|
|
}
|
|
|
|
(
|
|
|
|
Operator::LessThan
|
|
|
|
| Operator::LessThanOrEqual
|
|
|
|
| Operator::MoreThan
|
|
|
|
| Operator::MoreThanOrEqual,
|
|
|
|
v,
|
|
|
|
p,
|
|
|
|
) => Some(IndexOperator::RangePart(p.transform(op), v.clone())),
|
2023-09-12 20:26:03 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2023-09-12 15:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 18:41:13 +00:00
|
|
|
async fn eval_subquery(&mut self, s: &Subquery) -> Result<Node, Error> {
|
2023-09-07 23:36:39 +00:00
|
|
|
match s {
|
|
|
|
Subquery::Value(v) => self.eval_value(v).await,
|
|
|
|
_ => Ok(Node::Unsupported(format!("Unsupported subquery: {}", s))),
|
|
|
|
}
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 15:43:32 +00:00
|
|
|
pub(super) type IndexRef = u16;
|
2023-11-08 10:06:52 +00:00
|
|
|
/// For each expression a possible index option
|
2023-06-23 20:26:19 +00:00
|
|
|
#[derive(Default)]
|
2023-11-08 10:06:52 +00:00
|
|
|
pub(super) struct IndexesMap {
|
|
|
|
pub(super) options: Vec<(Arc<Expression>, IndexOption)>,
|
|
|
|
pub(super) definitions: Vec<DefineIndexStatement>,
|
2023-06-23 20:26:19 +00:00
|
|
|
}
|
|
|
|
|
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-11-08 10:06:52 +00:00
|
|
|
left: Arc<Node>,
|
|
|
|
right: Arc<Node>,
|
2023-09-12 15:43:32 +00:00
|
|
|
exp: Arc<Expression>,
|
2023-06-19 18:41:13 +00:00
|
|
|
},
|
2023-11-08 10:06:52 +00:00
|
|
|
IndexedField(Arc<Idiom>, Arc<Vec<IndexRef>>),
|
2023-06-19 18:41:13 +00:00
|
|
|
NonIndexedField,
|
2023-10-26 21:34:28 +00:00
|
|
|
Computed(Value),
|
2023-09-07 23:36:39 +00:00
|
|
|
Unsupported(String),
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
2023-10-26 21:34:28 +00:00
|
|
|
pub(super) fn is_computed(&self) -> Option<&Value> {
|
|
|
|
if let Node::Computed(v) = self {
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:06:52 +00:00
|
|
|
pub(super) fn is_indexed_field(&self) -> Option<(Arc<Idiom>, Arc<Vec<IndexRef>>)> {
|
2023-09-12 16:36:43 +00:00
|
|
|
if let Node::IndexedField(id, irs) = self {
|
2023-11-08 10:06:52 +00:00
|
|
|
Some((id.clone(), irs.clone()))
|
2023-06-19 18:41:13 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-08 10:06:52 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum IdiomPosition {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
}
|
|
|
|
impl IdiomPosition {
|
|
|
|
// Reverses the operator for non commutative operators
|
|
|
|
fn transform(&self, op: &Operator) -> Operator {
|
|
|
|
match self {
|
|
|
|
IdiomPosition::Left => op.clone(),
|
|
|
|
IdiomPosition::Right => match op {
|
|
|
|
Operator::LessThan => Operator::MoreThan,
|
|
|
|
Operator::LessThanOrEqual => Operator::MoreThanOrEqual,
|
|
|
|
Operator::MoreThan => Operator::LessThan,
|
|
|
|
Operator::MoreThanOrEqual => Operator::LessThanOrEqual,
|
|
|
|
_ => op.clone(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct ResolvedExpression {
|
|
|
|
exp: Arc<Expression>,
|
|
|
|
io: Option<IndexOption>,
|
|
|
|
left: Arc<Node>,
|
|
|
|
right: Arc<Node>,
|
|
|
|
}
|
|
|
|
impl From<ResolvedExpression> for Node {
|
|
|
|
fn from(re: ResolvedExpression) -> Self {
|
|
|
|
Node::Expression {
|
|
|
|
io: re.io,
|
|
|
|
left: re.left,
|
|
|
|
right: re.right,
|
|
|
|
exp: re.exp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|