diff --git a/hybridse/src/codegen/udf_ir_builder_test.cc b/hybridse/src/codegen/udf_ir_builder_test.cc index b8388ebe6c3..0c2dc6ccd52 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, degree_to_radius_check) { + auto udf_name = "radians"; + CheckUdf(udf_name, 3.141592653589793238463, 180); + CheckUdf(udf_name, 1.570796326794896619231, 90); + CheckUdf(udf_name, 0, 0); + CheckUdf, Nullable>(udf_name, nullptr, nullptr); +} } // namespace codegen } // namespace hybridse diff --git a/hybridse/src/udf/default_udf_library.cc b/hybridse/src/udf/default_udf_library.cc index 1f5830f5985..ed3418dd90d 100644 --- a/hybridse/src/udf/default_udf_library.cc +++ b/hybridse/src/udf/default_udf_library.cc @@ -1286,6 +1286,19 @@ void DefaultUdfLibrary::InitMathUdf() { @param expr @since 0.5.0)"); + RegisterExternal("RADIANS") + .args( + static_cast(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(); } diff --git a/hybridse/src/udf/udf.cc b/hybridse/src/udf/udf.cc index 31781ea7363..aeac947c227 100644 --- a/hybridse/src/udf/udf.cc +++ b/hybridse/src/udf/udf.cc @@ -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, diff --git a/hybridse/src/udf/udf.h b/hybridse/src/udf/udf.h index 1fca943fd31..139d1196b78 100644 --- a/hybridse/src/udf/udf.h +++ b/hybridse/src/udf/udf.h @@ -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);