diff --git a/be/src/exprs/string_functions.cpp b/be/src/exprs/string_functions.cpp index 92860d8959..e64b745f00 100644 --- a/be/src/exprs/string_functions.cpp +++ b/be/src/exprs/string_functions.cpp @@ -99,6 +99,15 @@ BooleanVal StringFunctions::ends_with( return BooleanVal(str_sp.ends_with(suffix_sp)); } +BooleanVal StringFunctions::null_or_empty( + FunctionContext* context, const StringVal& str) { + if (str.is_null || str.len == 0) { + return 1; + } else { + return 0; + } +} + StringVal StringFunctions::space(FunctionContext* context, const IntVal& len) { if (len.is_null){ return StringVal::null(); diff --git a/be/src/exprs/string_functions.h b/be/src/exprs/string_functions.h index 502d55520b..31139d3e3c 100644 --- a/be/src/exprs/string_functions.h +++ b/be/src/exprs/string_functions.h @@ -55,6 +55,8 @@ public: static doris_udf::BooleanVal ends_with( doris_udf::FunctionContext* context, const doris_udf::StringVal& str, const doris_udf::StringVal& suffix); + static doris_udf::BooleanVal null_or_empty( + doris_udf::FunctionContext* context, const doris_udf::StringVal& str); static doris_udf::StringVal space( doris_udf::FunctionContext* context, const doris_udf::IntVal& len); static doris_udf::StringVal repeat( diff --git a/be/test/exprs/string_functions_test.cpp b/be/test/exprs/string_functions_test.cpp index 8a2cd8108a..059bb899c6 100644 --- a/be/test/exprs/string_functions_test.cpp +++ b/be/test/exprs/string_functions_test.cpp @@ -223,6 +223,26 @@ TEST_F(StringFunctionsTest, starts_with) { ASSERT_EQ(nullRet, StringFunctions::starts_with(context, StringVal::null(), StringVal::null())); } +TEST_F(StringFunctionsTest, null_or_empty) { + doris_udf::FunctionContext* context = new doris_udf::FunctionContext(); + doris_udf::BooleanVal falseRet = doris_udf::BooleanVal(false); + doris_udf::BooleanVal trueRet = doris_udf::BooleanVal(true); + + ASSERT_EQ(trueRet, StringFunctions::null_or_empty(context, StringVal(""))); + + ASSERT_EQ(falseRet, StringFunctions::null_or_empty(context, StringVal(" "))); + + ASSERT_EQ(falseRet, StringFunctions::null_or_empty(context, StringVal("hello"))); + + ASSERT_EQ(falseRet, StringFunctions::null_or_empty(context, StringVal("doris"))); + + ASSERT_EQ(falseRet, StringFunctions::null_or_empty(context, StringVal("111"))); + + ASSERT_EQ(falseRet, StringFunctions::null_or_empty(context, StringVal("."))); + + ASSERT_EQ(trueRet, StringFunctions::null_or_empty(context, StringVal::null())); +} + } int main(int argc, char** argv) { diff --git a/docs/documentation/cn/sql-reference/sql-functions/string-functions/null_or_empty.md b/docs/documentation/cn/sql-reference/sql-functions/string-functions/null_or_empty.md new file mode 100644 index 0000000000..b1debd351a --- /dev/null +++ b/docs/documentation/cn/sql-reference/sql-functions/string-functions/null_or_empty.md @@ -0,0 +1,53 @@ + + +# null_or_empty +## description +### Syntax + +`BOOLEAN NULL_OR_EMPTY (VARCHAR str)` + +如果字符串为空字符串或者NULL,返回true。否则,返回false。 + +## example + +``` +MySQL [(none)]> select null_or_empty(null); ++---------------------+ +| null_or_empty(NULL) | ++---------------------+ +| 1 | ++---------------------+ + +MySQL [(none)]> select null_or_empty(""); ++-------------------+ +| null_or_empty('') | ++-------------------+ +| 1 | ++-------------------+ + +MySQL [(none)]> select null_or_empty("a"); ++--------------------+ +| null_or_empty('a') | ++--------------------+ +| 0 | ++--------------------+ +``` +##keyword +NULL_OR_EMPTY \ No newline at end of file diff --git a/docs/documentation/en/sql-reference/sql-functions/string-functions/null_or_empty.md b/docs/documentation/en/sql-reference/sql-functions/string-functions/null_or_empty.md new file mode 100644 index 0000000000..847f2244a4 --- /dev/null +++ b/docs/documentation/en/sql-reference/sql-functions/string-functions/null_or_empty.md @@ -0,0 +1,53 @@ + + +# null_or_empty +## description +### Syntax + +`BOOLEAN NULL_OR_EMPTY (VARCHAR str)` + +It returns true if the string is an empty string or NULL. Otherwise it returns false. + +## example + +``` +MySQL [(none)]> select null_or_empty(null); ++---------------------+ +| null_or_empty(NULL) | ++---------------------+ +| 1 | ++---------------------+ + +MySQL [(none)]> select null_or_empty(""); ++-------------------+ +| null_or_empty('') | ++-------------------+ +| 1 | ++-------------------+ + +MySQL [(none)]> select null_or_empty("a"); ++--------------------+ +| null_or_empty('a') | ++--------------------+ +| 0 | ++--------------------+ +``` +##keyword +NULL_OR_EMPTY \ No newline at end of file diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index c234a404da..aebba11e17 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -516,6 +516,8 @@ visible_functions = [ '_ZN5doris15StringFunctions9ends_withEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'], [['starts_with'], 'BOOLEAN', ['VARCHAR', 'VARCHAR'], '_ZN5doris15StringFunctions11starts_withEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'], + [['null_or_empty'], 'BOOLEAN', ['VARCHAR'], + '_ZN5doris15StringFunctions13null_or_emptyEPN9doris_udf15FunctionContextERKNS1_9StringValE'], [['space'], 'VARCHAR', ['INT'], '_ZN5doris15StringFunctions5spaceEPN9doris_udf15FunctionContextERKNS1_6IntValE'], [['repeat'], 'VARCHAR', ['VARCHAR', 'INT'], @@ -727,7 +729,8 @@ non_null_result_with_null_param_functions = [ 'if', 'hll_hash', 'concat_ws', - 'ifnull' + 'ifnull', + 'null_or_empty' ] invisible_functions = [