Skip to content

Commit

Permalink
Add type::is::nil function
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed May 7, 2024
1 parent b9f02d1 commit 6ba1341
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/fnc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"type::is::geometry" => r#type::is::geometry,
"type::is::int" => r#type::is::int,
"type::is::line" => r#type::is::line,
"type::is::nil" => r#type::is::nil,
"type::is::none" => r#type::is::none,
"type::is::null" => r#type::is::null,
"type::is::multiline" => r#type::is::multiline,
Expand Down
5 changes: 5 additions & 0 deletions core/src/fnc/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ pub mod is {
Ok(matches!(arg, Value::Geometry(Geometry::Line(_))).into())
}

pub fn nil((arg,): (Value,)) -> Result<Value, Error> {
let result = arg.is_none() || arg.is_null();
Ok(result.into())
}

pub fn none((arg,): (Value,)) -> Result<Value, Error> {
Ok(arg.is_none().into())
}
Expand Down
1 change: 1 addition & 0 deletions core/src/syn/parser/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ pub(crate) static PATHS: phf::Map<UniCase<&'static str>, PathKind> = phf_map! {
UniCase::ascii("type::is::geometry") => PathKind::Function,
UniCase::ascii("type::is::int") => PathKind::Function,
UniCase::ascii("type::is::line") => PathKind::Function,
UniCase::ascii("type::is::nil") => PathKind::Function,
UniCase::ascii("type::is::null") => PathKind::Function,
UniCase::ascii("type::is::none") => PathKind::Function,
UniCase::ascii("type::is::multiline") => PathKind::Function,
Expand Down
1 change: 1 addition & 0 deletions lib/fuzz/fuzz_targets/fuzz_executor.dict
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@
"type::is::geometry("
"type::is::int("
"type::is::line("
"type::is::nil("
"type::is::none("
"type::is::null("
"type::is::multiline("
Expand Down
1 change: 1 addition & 0 deletions lib/fuzz/fuzz_targets/fuzz_sql_parser.dict
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@
"type::is::geometry("
"type::is::int("
"type::is::line("
"type::is::nil("
"type::is::none("
"type::is::null("
"type::is::multiline("
Expand Down
27 changes: 27 additions & 0 deletions lib/tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5222,6 +5222,33 @@ async fn function_type_is_line() -> Result<(), Error> {
Ok(())
}

#[tokio::test]
async fn function_type_is_nil() -> Result<(), Error> {
let sql = r#"
RETURN type::is::nil(none);
RETURN type::is::nil(null);
RETURN type::is::nil("123");
"#;
let dbs = new_ds().await?;
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 3);
//
let tmp = res.remove(0).result?;
let val = Value::from(true);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::from(true);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::from(false);
assert_eq!(tmp, val);
//
Ok(())
}

#[tokio::test]
async fn function_type_is_none() -> Result<(), Error> {
let sql = r#"
Expand Down

0 comments on commit 6ba1341

Please sign in to comment.