From 78fd5aff21f692f583955a281e841b1e8fd6925f Mon Sep 17 00:00:00 2001 From: Xue Yu <278006819@qq.com> Date: Wed, 3 Apr 2019 03:47:49 +0800 Subject: [PATCH] support radians and degrees in sql (#7336) * support radians and degrees in sql * update test case --- docs/content/querying/sql.md | 2 ++ .../calcite/planner/DruidOperatorTable.java | 2 ++ .../druid/sql/calcite/CalciteQueryTest.java | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/docs/content/querying/sql.md b/docs/content/querying/sql.md index bb9a993cfb4..eaf25ed26b1 100644 --- a/docs/content/querying/sql.md +++ b/docs/content/querying/sql.md @@ -162,6 +162,8 @@ Numeric functions will return 64 bit integers or 64 bit floats, depending on the |`ACOS(expr)`|Arc cosine of expr.| |`ATAN(expr)`|Arc tangent of expr.| |`ATAN2(y, x)`|Angle theta from the conversion of rectangular coordinates (x, y) to polar * coordinates (r, theta).| +|`DEGREES(expr)`|Converts an angle measured in radians to an approximately equivalent angle measured in degrees| +|`RADIANS(expr)`|Converts an angle measured in degrees to an approximately equivalent angle measured in radians| ### String functions diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidOperatorTable.java b/sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidOperatorTable.java index c908a2d5266..a1f6e159e08 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidOperatorTable.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidOperatorTable.java @@ -131,6 +131,8 @@ public class DruidOperatorTable implements SqlOperatorTable .add(new DirectOperatorConversion(SqlStdOperatorTable.ACOS, "acos")) .add(new DirectOperatorConversion(SqlStdOperatorTable.ATAN, "atan")) .add(new DirectOperatorConversion(SqlStdOperatorTable.ATAN2, "atan2")) + .add(new DirectOperatorConversion(SqlStdOperatorTable.RADIANS, "toRadians")) + .add(new DirectOperatorConversion(SqlStdOperatorTable.DEGREES, "toDegrees")) .add(new UnaryPrefixOperatorConversion(SqlStdOperatorTable.NOT, "!")) .add(new UnaryPrefixOperatorConversion(SqlStdOperatorTable.UNARY_MINUS, "-")) .add(new UnaryFunctionOperatorConversion(SqlStdOperatorTable.IS_NULL, "isnull")) diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java index 687887cc5b6..472ff39b136 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java @@ -7669,4 +7669,28 @@ public class CalciteQueryTest extends BaseCalciteQueryTest ) ); } + + @Test + public void testRadiansAndDegrees() throws Exception + { + testQuery( + "SELECT RADIANS(m1 * 15)/DEGREES(m2) FROM numfoo WHERE dim1 = '1'", + ImmutableList.of( + newScanQueryBuilder() + .dataSource(CalciteTests.DATASOURCE3) + .intervals(querySegmentSpec(Filtration.eternity())) + .virtualColumns( + expressionVirtualColumn("v0", "(toRadians((\"m1\" * 15)) / toDegrees(\"m2\"))", ValueType.DOUBLE) + ) + .columns("v0") + .filters(selector("dim1", "1", null)) + .resultFormat(ScanQuery.RESULT_FORMAT_COMPACTED_LIST) + .context(QUERY_CONTEXT_DEFAULT) + .build() + ), + ImmutableList.of( + new Object[]{Math.toRadians(60) / Math.toDegrees(4)} + ) + ); + } }