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 radians() function #788 #1897

Merged
merged 15 commits into from
May 28, 2022
Merged
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, degree_to_radius_check) {
auto udf_name = "radians";
CheckUdf<double, double>(udf_name, 3.141592653589793238463, 180);
CheckUdf<double, double>(udf_name, 1.570796326794896619231, 90);
CheckUdf<double, double>(udf_name, 0, 0);
CheckUdf<Nullable<double>, Nullable<double>>(udf_name, nullptr, nullptr);
}
} // namespace codegen
} // namespace hybridse

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 @@ -1286,6 +1286,19 @@ void DefaultUdfLibrary::InitMathUdf() {
@param expr

@since 0.5.0)");
RegisterExternal("RADIANS")
.args<double>(
static_cast<double (*)(double)>(udf::v1::degree_to_radius))
.doc(R"(
@brief Returns the argument X, converted from degrees to radians. (Note that π radians equals 180 degrees.)

Example:

@code{.sql}
SELECT RADIANS(90);
--output 1.570796326794896619231
@endcode
@since 0.6.0)");
InitTrigonometricUdf();
}

Expand Down
6 changes: 5 additions & 1 deletion hybridse/src/udf/udf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ int32_t weekofyear(Date *date) {

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

double Degrees(double x) { return x * (180 / 3.141592653589793238463L); }
double degree_to_radius(double degree) {
return degree/180.0L*M_PI;
}

double Degrees(double x) { return x * (180 / M_PI); }

void date_format(Timestamp *timestamp,
StringRef *format,
Expand Down
2 changes: 2 additions & 0 deletions hybridse/src/udf/udf.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ int32_t weekofyear(int64_t ts);
int32_t weekofyear(Timestamp *ts);
int32_t weekofyear(Date *ts);

double degree_to_radius(double degree);

float Cotf(float x);
double Degrees(double x);

Expand Down