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

fix: built-in function: support char_length() and character_length() function #1895

Merged
merged 21 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions hybridse/src/codegen/udf_ir_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,13 @@ TEST_F(UdfIRBuilderTest, degrees) {
CheckUdf<double, double>(udf_name, -90.0, -pi/2);
CheckUdf<Nullable<double>, Nullable<double>>(udf_name, nullptr, nullptr);
}
TEST_F(UdfIRBuilderTest, char_length_udf_test) {
auto udf_name = "char_length";
CheckUdf<int32_t, StringRef>(udf_name, 10, StringRef("Spark SQL "));
Ivyee17 marked this conversation as resolved.
Show resolved Hide resolved
CheckUdf<int32_t, StringRef>(udf_name, 10, StringRef("Spark SQL\n"));
CheckUdf<int32_t, Nullable<StringRef>>(udf_name, 0, StringRef(""));
CheckUdf<int32_t, Nullable<StringRef>>(udf_name, 0, nullptr);
}
TEST_F(UdfIRBuilderTest, degree_to_radius_check) {
auto udf_name = "radians";
CheckUdf<double, double>(udf_name, 3.141592653589793238463, 180);
Expand Down
13 changes: 13 additions & 0 deletions hybridse/src/udf/default_udf_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,19 @@ void DefaultUdfLibrary::InitStringUdf() {
@since 0.4.0)");
RegisterAlias("lower", "lcase");
RegisterAlias("upper", "ucase");
RegisterExternal("char_length")
.args<StringRef>(static_cast<int32_t (*)(StringRef*)>(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() {
Expand Down
8 changes: 8 additions & 0 deletions hybridse/src/udf/udf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Ivyee17 marked this conversation as resolved.
Show resolved Hide resolved
}

float Cotf(float x) { return cosf(x) / sinf(x); }

double degree_to_radius(double degree) {
Expand Down
1 change: 1 addition & 0 deletions hybridse/src/udf/udf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down