diff --git a/hybridse/src/codegen/udf_ir_builder_test.cc b/hybridse/src/codegen/udf_ir_builder_test.cc index 0c2dc6ccd52..7e031c8d53f 100644 --- a/hybridse/src/codegen/udf_ir_builder_test.cc +++ b/hybridse/src/codegen/udf_ir_builder_test.cc @@ -1007,6 +1007,13 @@ TEST_F(UdfIRBuilderTest, degrees) { CheckUdf(udf_name, -90.0, -pi/2); CheckUdf, Nullable>(udf_name, nullptr, nullptr); } +TEST_F(UdfIRBuilderTest, char_length_udf_test) { + auto udf_name = "char_length"; + CheckUdf(udf_name, 10, StringRef("Spark SQL ")); + CheckUdf(udf_name, 10, StringRef("Spark SQL\n")); + CheckUdf>(udf_name, 0, StringRef("")); + CheckUdf>(udf_name, 0, nullptr); +} TEST_F(UdfIRBuilderTest, degree_to_radius_check) { auto udf_name = "radians"; CheckUdf(udf_name, 3.141592653589793238463, 180); diff --git a/hybridse/src/udf/default_udf_library.cc b/hybridse/src/udf/default_udf_library.cc index ed3418dd90d..f04b4fc951e 100644 --- a/hybridse/src/udf/default_udf_library.cc +++ b/hybridse/src/udf/default_udf_library.cc @@ -895,6 +895,19 @@ void DefaultUdfLibrary::InitStringUdf() { @since 0.4.0)"); RegisterAlias("lower", "lcase"); RegisterAlias("upper", "ucase"); + RegisterExternal("char_length") + .args(static_cast(udf::v1::char_length)) + .doc(R"( + @brief Returns the length of the string. It is measured in characters and multibyte character string is not supported. + + Example: + + @code{.sql} + SELECT CHAR_LENGTH('Spark SQL '); + --output 10 + @endcode + @since 0.6.0)"); + RegisterAlias("character_length", "char_length"); } void DefaultUdfLibrary::InitMathUdf() { diff --git a/hybridse/src/udf/udf.cc b/hybridse/src/udf/udf.cc index aeac947c227..b20105113f8 100644 --- a/hybridse/src/udf/udf.cc +++ b/hybridse/src/udf/udf.cc @@ -157,6 +157,14 @@ int32_t weekofyear(Date *date) { } } +int32_t char_length(StringRef *str) { + if (nullptr == str) { + return 0; + } + int32_t res = str->size_; + return res; +} + float Cotf(float x) { return cosf(x) / sinf(x); } double degree_to_radius(double degree) { diff --git a/hybridse/src/udf/udf.h b/hybridse/src/udf/udf.h index 139d1196b78..f1147cf5969 100644 --- a/hybridse/src/udf/udf.h +++ b/hybridse/src/udf/udf.h @@ -224,6 +224,7 @@ int32_t weekofyear(int64_t ts); int32_t weekofyear(Timestamp *ts); int32_t weekofyear(Date *ts); +int32_t char_length(StringRef *str); double degree_to_radius(double degree); float Cotf(float x);