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

Add type::is::nil function #3989

Closed
wants to merge 1 commit into from
Closed
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl_module_def!(
"geometry" => run,
"int" => run,
"line" => run,
"nil" => run,
"none" => run,
"null" => run,
"multiline" => run,
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