From a0028e2c7a633caab8b6054790656320e19b9eb1 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 2 Sep 2024 10:15:13 +0100 Subject: [PATCH] Fix allowing `any` to be used for optional arguments (#4659) --- core/src/sql/function.rs | 1 + sdk/tests/function.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/core/src/sql/function.rs b/core/src/sql/function.rs index 6b55be2d..5a528c99 100644 --- a/core/src/sql/function.rs +++ b/core/src/sql/function.rs @@ -275,6 +275,7 @@ impl Function { // Check for any final optional arguments val.args.iter().rev().for_each(|(_, kind)| match kind { Kind::Option(_) if min_args_len == 0 => {} + Kind::Any if min_args_len == 0 => {} _ => min_args_len += 1, }); // Check the necessary arguments are passed diff --git a/sdk/tests/function.rs b/sdk/tests/function.rs index 1f3bfd5d..0d123beb 100644 --- a/sdk/tests/function.rs +++ b/sdk/tests/function.rs @@ -6340,17 +6340,20 @@ pub async fn function_http_disabled() -> Result<(), Error> { #[tokio::test] async fn function_custom_optional_args() -> Result<(), Error> { let sql = r#" + DEFINE FUNCTION fn::any_arg($a: any) { $a || 'test' }; DEFINE FUNCTION fn::zero_arg() { [] }; DEFINE FUNCTION fn::one_arg($a: bool) { [$a] }; DEFINE FUNCTION fn::last_option($a: bool, $b: option) { [$a, $b] }; DEFINE FUNCTION fn::middle_option($a: bool, $b: option, $c: bool) { [$a, $b, $c] }; + RETURN fn::any_arg(); RETURN fn::zero_arg(); RETURN fn::one_arg(); RETURN fn::last_option(); RETURN fn::middle_option(); RETURN fn::zero_arg(true); + RETURN fn::any_arg('other'); RETURN fn::one_arg(true); RETURN fn::last_option(true); RETURN fn::last_option(true, false); @@ -6376,6 +6379,14 @@ async fn function_custom_optional_args() -> Result<(), Error> { assert_eq!(tmp, val); // let tmp = test.next()?.result?; + let val = Value::None; + assert_eq!(tmp, val); + // + let tmp = test.next()?.result?; + let val = Value::parse("'test'"); + assert_eq!(tmp, val); + // + let tmp = test.next()?.result?; let val = Value::parse("[]"); assert_eq!(tmp, val); // @@ -6416,6 +6427,10 @@ async fn function_custom_optional_args() -> Result<(), Error> { } // let tmp = test.next()?.result?; + let val = Value::parse("'other'"); + assert_eq!(tmp, val); + // + let tmp = test.next()?.result?; let val = Value::parse("[true]"); assert_eq!(tmp, val); //