Skip to content

Commit

Permalink
HHH-15518 bless degrees() and radians() as standard
Browse files Browse the repository at this point in the history
- add a test
- emulate them on HANA and Oracle
  • Loading branch information
gavinking committed Oct 6, 2022
1 parent 1c3819a commit 4451611
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
Expand Up @@ -1002,6 +1002,8 @@ Of course, we also have a number of functions for working with numeric values.
| `pi` | π | `pi` | ✗
| `sin()`, `cos()`, `tan()`, `asin()`, `acos()`, `atan()`, `atan2()`
| Basic trigonometric functions | `sin(theta)`, `cos(theta)`, `atan2(opposite, adjacent)` | ✗
| `degrees()` | Convert radians to degrees | `degrees(x)` | ✗
| `radians()` | Convert degrees to radians | `radians(x)` | ✗
| `least()` | Return the smallest of the given arguments | `least(x, y, z)` |✗
| `greatest()` | Return the largest of the given arguments | `greatest(x, y, z)` | ✗
|===
Expand Down
Expand Up @@ -377,6 +377,9 @@ public void initializeFunctionRegistry(QueryEngine queryEngine) {
functionFactory.inverseDistributionOrderedSetAggregates();
functionFactory.hypotheticalOrderedSetAggregates_windowEmulation();

functionFactory.radians_acos();
functionFactory.degrees_acos();

queryEngine.getSqmFunctionRegistry().register( "timestampadd",
new IntegralTimestampaddFunction( this, typeConfiguration ) );
}
Expand Down
Expand Up @@ -787,6 +787,8 @@ else if ( isNumericType(sqlType) ) {
* <li> <code>round(arg0, arg1)</code>
* <li> <code>least(arg0, arg1, ...)</code>
* <li> <code>greatest(arg0, arg1, ...)</code>
* <li> <code>degrees(arg)</code>
* <li> <code>radians(arg)</code>
* </ul>
*
* <ul>
Expand Down
Expand Up @@ -174,6 +174,9 @@ public void initializeFunctionRegistry(QueryEngine queryEngine) {
functionFactory.monthsBetween();
functionFactory.everyAny_minMaxCase();

functionFactory.radians_acos();
functionFactory.degrees_acos();

functionFactory.median();
functionFactory.stddev();
functionFactory.stddevPopSamp();
Expand Down
Expand Up @@ -80,14 +80,6 @@ public void cot() {
.register();
}

public void degrees() {
functionRegistry.namedDescriptorBuilder( "degrees" )
.setExactArgumentCount( 1 )
.setParameterTypes(NUMERIC)
.setInvariantType(doubleType)
.register();
}

/**
* For databases where the first parameter is the base
*/
Expand Down Expand Up @@ -178,6 +170,36 @@ public void radians() {
.register();
}

/**
* For Oracle, HANA
*/
public void radians_acos() {
functionRegistry.patternDescriptorBuilder( "radians", "(?1*acos(-1)/180)" )
.setInvariantType(doubleType)
.setExactArgumentCount(1)
.setParameterTypes(NUMERIC)
.register();
}

public void degrees() {
functionRegistry.namedDescriptorBuilder( "degrees" )
.setExactArgumentCount( 1 )
.setParameterTypes(NUMERIC)
.setInvariantType(doubleType)
.register();
}

/**
* For Oracle, HANA
*/
public void degrees_acos() {
functionRegistry.patternDescriptorBuilder( "degrees", "(?1/acos(-1)*180)" )
.setInvariantType(doubleType)
.setExactArgumentCount(1)
.setParameterTypes(NUMERIC)
.register();
}

public void sinh() {
functionRegistry.namedDescriptorBuilder( "sinh" )
.setExactArgumentCount( 1 )
Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.Distinct;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.FunctionExpression;
import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.expression.SqlTupleContainer;
import org.hibernate.sql.ast.tree.expression.Star;
Expand Down
Expand Up @@ -921,6 +921,22 @@ public void testPi(SessionFactoryScope scope) {
);
}

@Test
public void testDegreesRadians(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
assertThat(
session.createQuery("select degrees(pi)", Double.class).getSingleResult(),
IsCloseTo.closeTo( 180.0, 1e-9 )
);
assertThat(
session.createQuery("select radians(180.0)", Double.class).getSingleResult(),
IsCloseTo.closeTo( Math.PI, 1e-9 )
);
}
);
}

@Test
public void testLog(SessionFactoryScope scope) {
scope.inTransaction(
Expand Down

0 comments on commit 4451611

Please sign in to comment.