Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query planner should evaluate non boolean expressions #4022

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 23 additions & 7 deletions core/src/idx/planner/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,26 @@ impl<'a> TreeBuilder<'a> {
| Value::Uuid(_)
| Value::Constant(_)
| Value::Geometry(_)
| Value::Datetime(_) => Ok(Node::Computed(Arc::new(v.to_owned()))),
| Value::Datetime(_)
| Value::Param(_)
| Value::Function(_) => Ok(Node::Computable),
Value::Array(a) => self.eval_array(stk, a).await,
Value::Subquery(s) => self.eval_subquery(stk, s).await,
Value::Param(p) => {
let v = stk.run(|stk| p.compute(stk, self.ctx, self.opt, self.txn, None)).await?;
stk.run(|stk| self.eval_value(stk, group, &v)).await
}
_ => Ok(Node::Unsupported(format!("Unsupported value: {}", v))),
}
}

async fn compute(&self, stk: &mut Stk, v: &Value, n: Node) -> Result<Node, Error> {
Ok(if n == Node::Computable {
match v.compute(stk, self.ctx, self.opt, self.txn, None).await {
Ok(v) => Node::Computed(Arc::new(v)),
Err(_) => Node::Unsupported(format!("Unsupported value: {}", v)),
}
} else {
n
})
}

async fn eval_array(&mut self, stk: &mut Stk, a: &Array) -> Result<Node, Error> {
let mut values = Vec::with_capacity(a.len());
for v in &a.0 {
Expand Down Expand Up @@ -289,9 +298,15 @@ impl<'a> TreeBuilder<'a> {
if let Some(re) = self.resolved_expressions.get(e).cloned() {
return Ok(re.into());
}
let left = stk.run(|stk| self.eval_value(stk, group, l)).await?;
let right = stk.run(|stk| self.eval_value(stk, group, r)).await?;
// If both values are computable, then we can delegate the computation to the parent
if left == Node::Computable && right == Node::Computable {
return Ok(Node::Computable);
}
let exp = Arc::new(e.clone());
let left = Arc::new(stk.run(|stk| self.eval_value(stk, group, l)).await?);
let right = Arc::new(stk.run(|stk| self.eval_value(stk, group, r)).await?);
let left = Arc::new(self.compute(stk, l, left).await?);
let right = Arc::new(self.compute(stk, r, right).await?);
let mut io = None;
if let Some((id, local_irs, remote_irs)) = left.is_indexed_field() {
io = self.lookup_index_options(
Expand Down Expand Up @@ -552,6 +567,7 @@ pub(super) enum Node {
IndexedField(Idiom, Vec<IndexRef>),
RecordField(Idiom, RecordOptions),
NonIndexedField(Idiom),
Computable,
Computed(Arc<Value>),
Unsupported(String),
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/syn/lexer/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum Error {
InvalidRange,
#[error("expected uuid-strand to end")]
ExpectedStrandEnd,
#[error("missing a uuid seperator")]
#[error("missing a uuid separator")]
MissingSeperator,
}

Expand Down
73 changes: 73 additions & 0 deletions lib/tests/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2527,3 +2527,76 @@ async fn select_with_exact_operator() -> Result<(), Error> {
//
Ok(())
}

#[tokio::test]
async fn select_with_non_boolean_expression() -> Result<(), Error> {
let dbs = new_ds().await?;
let ses = Session::owner().with_ns("test").with_db("test");
//
let sql = "
DEFINE INDEX idx ON t FIELDS v;
CREATE t:1 set v = 1;
CREATE t:2 set v = 2;
LET $p1 = 1;
LET $p3 = 3;
SELECT * FROM t WHERE v > math::max([0, 1]);
SELECT * FROM t WHERE v > math::max([0, 1]) EXPLAIN;
SELECT * FROM t WHERE v > 3 - math::max([0, 2]);
SELECT * FROM t WHERE v > 3 - math::max([0, 2]) EXPLAIN;
SELECT * FROM t WHERE v > 3 - math::max([0, 1]) - 1;
SELECT * FROM t WHERE v > 3 - math::max([0, 1]) - 1 EXPLAIN;
SELECT * FROM t WHERE v > 3 - ( math::max([0, 1]) + 1 );
SELECT * FROM t WHERE v > 3 - ( math::max([0, 1]) + 1 ) EXPLAIN;
SELECT * FROM t WHERE v > $p3 - ( math::max([0, $p1]) + $p1 );
SELECT * FROM t WHERE v > $p3 - ( math::max([0, $p1]) + $p1 ) EXPLAIN;
";
let mut res = dbs.execute(&sql, &ses, None).await?;
//
assert_eq!(res.len(), 15);
skip_ok(&mut res, 5)?;
//
for i in 0..5 {
let tmp = res.remove(0).result?;
let val = Value::parse(
r#"[
{
id: t:2,
v: 2
}
]"#,
);
assert_eq!(format!("{:#}", tmp), format!("{:#}", val), "{i}");
//
let tmp = res.remove(0).result?;
let val = Value::parse(
r#"[
{
detail: {
plan: {
from: {
inclusive: false,
value: 1
},
index: 'idx',
to: {
inclusive: false,
value: NONE
}
},
table: 't'
},
operation: 'Iterate Index'
},
{
detail: {
type: 'Memory'
},
operation: 'Collector'
}
]"#,
);
assert_eq!(format!("{:#}", tmp), format!("{:#}", val), "{i}");
}
//
Ok(())
}