From 686a140d8090f4af3012a40b771d620d7fe54bc8 Mon Sep 17 00:00:00 2001 From: Emmanuel Keller Date: Wed, 6 Mar 2024 23:41:48 +0000 Subject: [PATCH] Support of IN operator in the planner (#3640) --- core/src/idx/planner/tree.rs | 11 ++++---- lib/tests/planner.rs | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/core/src/idx/planner/tree.rs b/core/src/idx/planner/tree.rs index dfad4088..07956cef 100644 --- a/core/src/idx/planner/tree.rs +++ b/core/src/idx/planner/tree.rs @@ -316,12 +316,11 @@ impl<'a> TreeBuilder<'a> { (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::ContainAny | Operator::ContainAll | Operator::Inside, + Value::Array(a), + IdiomPosition::Left, + ) => Some(IndexOperator::Union(a.clone())), ( Operator::LessThan | Operator::LessThanOrEqual diff --git a/lib/tests/planner.rs b/lib/tests/planner.rs index f498485a..80fa1557 100644 --- a/lib/tests/planner.rs +++ b/lib/tests/planner.rs @@ -1246,3 +1246,58 @@ async fn select_with_uuid_value() -> Result<(), Error> { Ok(()) } + +#[tokio::test] +async fn select_with_in_operator() -> Result<(), Error> { + let dbs = new_ds().await?; + let ses = Session::owner().with_ns("test").with_db("test"); + + let sql = " + DEFINE INDEX user_email_idx ON user FIELDS email; + CREATE user:1 CONTENT { email: 'a@b' }; + CREATE user:2 CONTENT { email: 'c@d' }; + SELECT * FROM user WHERE email IN ['a@b', 'e@f'] EXPLAIN; + SELECT * FROM user WHERE email INSIDE ['a@b', 'e@f'] EXPLAIN; + SELECT * FROM user WHERE email IN ['a@b', 'e@f']; + SELECT * FROM user WHERE email INSIDE ['a@b', 'e@f']; + "; + let mut res = dbs.execute(&sql, &ses, None).await?; + + assert_eq!(res.len(), 7); + skip_ok(&mut res, 3)?; + + for _ in 0..2 { + let tmp = res.remove(0).result?; + let val = Value::parse( + r#"[ + { + detail: { + plan: { + index: 'user_email_idx', + operator: 'union', + value: ['a@b', 'e@f'] + }, + table: 'user' + }, + operation: 'Iterate Index' + } + ]"#, + ); + assert_eq!(format!("{:#}", tmp), format!("{:#}", val)); + } + + for _ in 0..2 { + let tmp = res.remove(0).result?; + let val = Value::parse( + r#"[ + { + 'id': user:1, + 'email': 'a@b' + } + ]"#, + ); + assert_eq!(format!("{:#}", tmp), format!("{:#}", val)); + } + + Ok(()) +}