2023-06-19 18:41:13 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::Transaction;
|
|
|
|
use crate::doc::CursorDoc;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::err::Error;
|
2023-04-25 10:13:04 +00:00
|
|
|
use crate::sql::value::TryAdd;
|
|
|
|
use crate::sql::value::TryDiv;
|
|
|
|
use crate::sql::value::TryMul;
|
2023-06-20 23:31:23 +00:00
|
|
|
use crate::sql::value::TryNeg;
|
2023-04-25 10:13:04 +00:00
|
|
|
use crate::sql::value::TryPow;
|
|
|
|
use crate::sql::value::TrySub;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::sql::value::Value;
|
2023-06-19 18:41:13 +00:00
|
|
|
use crate::sql::Expression;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2023-06-20 23:31:23 +00:00
|
|
|
pub fn neg(a: Value) -> Result<Value, Error> {
|
|
|
|
a.try_neg()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn not(a: Value) -> Result<Value, Error> {
|
|
|
|
super::not::not((a,))
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn or(a: Value, b: Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(match a.is_truthy() {
|
|
|
|
true => a,
|
|
|
|
false => b,
|
|
|
|
})
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn and(a: Value, b: Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(match a.is_truthy() {
|
|
|
|
true => b,
|
|
|
|
false => a,
|
|
|
|
})
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 21:48:10 +00:00
|
|
|
pub fn tco(a: Value, b: Value) -> Result<Value, Error> {
|
|
|
|
Ok(match a.is_truthy() {
|
|
|
|
true => a,
|
|
|
|
false => b,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nco(a: Value, b: Value) -> Result<Value, Error> {
|
|
|
|
Ok(match a.is_some() {
|
|
|
|
true => a,
|
|
|
|
false => b,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn add(a: Value, b: Value) -> Result<Value, Error> {
|
2023-04-25 10:13:04 +00:00
|
|
|
a.try_add(b)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn sub(a: Value, b: Value) -> Result<Value, Error> {
|
2023-04-25 10:13:04 +00:00
|
|
|
a.try_sub(b)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn mul(a: Value, b: Value) -> Result<Value, Error> {
|
2023-04-25 10:13:04 +00:00
|
|
|
a.try_mul(b)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn div(a: Value, b: Value) -> Result<Value, Error> {
|
2023-04-25 10:13:04 +00:00
|
|
|
a.try_div(b)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 14:56:07 +00:00
|
|
|
pub fn pow(a: Value, b: Value) -> Result<Value, Error> {
|
2023-04-25 10:13:04 +00:00
|
|
|
a.try_pow(b)
|
2022-12-18 14:56:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn exact(a: &Value, b: &Value) -> Result<Value, Error> {
|
|
|
|
Ok(Value::from(a == b))
|
2021-05-17 17:00:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn equal(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.equal(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn not_equal(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok((!a.equal(b)).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn all_equal(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.all_equal(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn any_equal(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.any_equal(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn like(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.fuzzy(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn not_like(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok((!a.fuzzy(b)).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn all_like(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.all_fuzzy(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn any_like(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.any_fuzzy(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn less_than(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.lt(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn less_than_or_equal(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.le(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn more_than(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.gt(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn more_than_or_equal(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.ge(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn contain(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.contains(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn not_contain(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok((!a.contains(b)).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn contain_all(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.contains_all(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn contain_any(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.contains_any(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn contain_none(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok((!a.contains_any(b)).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn inside(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(b.contains(a).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn not_inside(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok((!b.contains(a)).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn inside_all(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(b.contains_all(a).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn inside_any(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(b.contains_any(a).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn inside_none(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok((!b.contains_any(a)).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 20:41:57 +00:00
|
|
|
pub fn outside(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok((!a.intersects(b)).into())
|
2022-04-29 20:41:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:37:46 +00:00
|
|
|
pub fn intersects(a: &Value, b: &Value) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(a.intersects(b).into())
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 14:57:42 +00:00
|
|
|
pub(crate) async fn matches(
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
txn: &Transaction,
|
|
|
|
doc: Option<&CursorDoc<'_>>,
|
2023-07-20 12:56:32 +00:00
|
|
|
exp: &Expression,
|
2023-07-06 14:57:42 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
if let Some(doc) = doc {
|
|
|
|
if let Some(thg) = doc.rid {
|
2023-07-20 12:56:32 +00:00
|
|
|
if let Some(pla) = ctx.get_query_planner() {
|
|
|
|
if let Some(exe) = pla.get_query_executor(&thg.tb) {
|
|
|
|
// If we find the expression in `pre_match`,
|
|
|
|
// it means that we are using an Iterator::Index
|
|
|
|
// and we are iterating over documents that already matches the expression.
|
|
|
|
if let Some(ir) = doc.ir {
|
|
|
|
if let Some(e) = exe.get_iterator_expression(ir) {
|
|
|
|
if e.eq(exp) {
|
|
|
|
return Ok(Value::Bool(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Evaluate the matches
|
|
|
|
return exe.matches(txn, thg, exp).await;
|
|
|
|
}
|
2023-07-06 14:57:42 +00:00
|
|
|
}
|
2023-06-19 18:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Value::Bool(false))
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn or_true() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::from(2);
|
2021-03-29 15:43:37 +00:00
|
|
|
let res = or(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn or_false_one() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(0);
|
|
|
|
let two = Value::from(1);
|
2021-03-29 15:43:37 +00:00
|
|
|
let res = or(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn or_false_two() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::from(0);
|
2021-03-29 15:43:37 +00:00
|
|
|
let res = or(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn and_true() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::from(2);
|
2021-03-29 15:43:37 +00:00
|
|
|
let res = and(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("2", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn and_false_one() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(0);
|
|
|
|
let two = Value::from(1);
|
2021-03-29 15:43:37 +00:00
|
|
|
let res = and(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("0", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn and_false_two() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::from(0);
|
2021-03-29 15:43:37 +00:00
|
|
|
let res = and(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("0", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
2022-12-02 21:48:10 +00:00
|
|
|
#[test]
|
|
|
|
fn tco_true() {
|
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::from(2);
|
|
|
|
let res = tco(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tco_false_one() {
|
|
|
|
let one = Value::from(0);
|
|
|
|
let two = Value::from(1);
|
|
|
|
let res = tco(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tco_false_two() {
|
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::from(0);
|
|
|
|
let res = tco(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nco_true() {
|
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::from(2);
|
|
|
|
let res = nco(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nco_false_one() {
|
|
|
|
let one = Value::None;
|
|
|
|
let two = Value::from(1);
|
|
|
|
let res = nco(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nco_false_two() {
|
|
|
|
let one = Value::from(1);
|
|
|
|
let two = Value::None;
|
|
|
|
let res = nco(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[test]
|
|
|
|
fn add_basic() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(5);
|
|
|
|
let two = Value::from(4);
|
|
|
|
let res = add(one, two);
|
2021-03-29 15:43:37 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("9", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sub_basic() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(5);
|
|
|
|
let two = Value::from(4);
|
|
|
|
let res = sub(one, two);
|
2021-03-29 15:43:37 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mul_basic() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(5);
|
|
|
|
let two = Value::from(4);
|
|
|
|
let res = mul(one, two);
|
2021-03-29 15:43:37 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
|
|
|
assert_eq!("20", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-05-23 06:10:09 +00:00
|
|
|
fn div_int() {
|
2022-01-13 17:37:46 +00:00
|
|
|
let one = Value::from(5);
|
|
|
|
let two = Value::from(4);
|
|
|
|
let res = div(one, two);
|
2021-03-29 15:43:37 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
2023-05-23 06:10:09 +00:00
|
|
|
assert_eq!("1", format!("{}", out));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn div_float() {
|
|
|
|
let one = Value::from(5.0);
|
|
|
|
let two = Value::from(4.0);
|
|
|
|
let res = div(one, two);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap();
|
2023-06-09 15:23:30 +00:00
|
|
|
assert_eq!("1.25f", format!("{}", out));
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
}
|