HHH-15518 bless degrees() and radians() as standard

- add a test
- emulate them on HANA and Oracle
This commit is contained in:
Gavin King 2022-10-06 20:13:21 +02:00
parent 1c3819a77b
commit 4451611311
7 changed files with 56 additions and 9 deletions

View File

@ -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)` | ✗
|===

View File

@ -377,6 +377,9 @@ public abstract class AbstractHANADialect extends Dialect {
functionFactory.inverseDistributionOrderedSetAggregates();
functionFactory.hypotheticalOrderedSetAggregates_windowEmulation();
functionFactory.radians_acos();
functionFactory.degrees_acos();
queryEngine.getSqmFunctionRegistry().register( "timestampadd",
new IntegralTimestampaddFunction( this, typeConfiguration ) );
}

View File

@ -787,6 +787,8 @@ public abstract class Dialect implements ConversionContext {
* <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>

View File

@ -174,6 +174,9 @@ public class OracleDialect extends Dialect {
functionFactory.monthsBetween();
functionFactory.everyAny_minMaxCase();
functionFactory.radians_acos();
functionFactory.degrees_acos();
functionFactory.median();
functionFactory.stddev();
functionFactory.stddevPopSamp();

View File

@ -80,14 +80,6 @@ public class CommonFunctionFactory {
.register();
}
public void degrees() {
functionRegistry.namedDescriptorBuilder( "degrees" )
.setExactArgumentCount( 1 )
.setParameterTypes(NUMERIC)
.setInvariantType(doubleType)
.register();
}
/**
* For databases where the first parameter is the base
*/
@ -178,6 +170,36 @@ public class CommonFunctionFactory {
.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 )

View File

@ -27,7 +27,6 @@ import org.hibernate.sql.ast.spi.SqlAppender;
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;

View File

@ -921,6 +921,22 @@ public class StandardFunctionTests {
);
}
@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(