fix the round() function on Postgres

for some dumb reason, round(x,n) doesn't accept a double on pg
This commit is contained in:
Gavin King 2022-02-04 14:52:13 +01:00
parent f7d5bc857b
commit 2f08812187
4 changed files with 13 additions and 11 deletions

View File

@ -72,7 +72,6 @@ import java.sql.Types;
import jakarta.persistence.TemporalType;
import static org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers.useArgType;
import static org.hibernate.type.SqlTypes.*;
/**
@ -249,13 +248,9 @@ public class DerbyDialect extends Dialect {
functionFactory.leftRight_substrLength();
functionFactory.characterLength_length( SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
functionFactory.power_expLn();
functionFactory.round_floor();
functionFactory.bitLength_pattern( "length(?1)*8" );
queryEngine.getSqmFunctionRegistry().patternDescriptorBuilder( "round", "floor(?1*1e?2+0.5)/1e?2")
.setReturnTypeResolver( useArgType(1) )
.setExactArgumentCount( 2 )
.register();
queryEngine.getSqmFunctionRegistry().register(
"concat",
new CastingConcatFunction(

View File

@ -404,6 +404,7 @@ public class PostgreSQLDialect extends Dialect {
CommonFunctionFactory functionFactory = new CommonFunctionFactory(queryEngine);
functionFactory.round_floor(); //Postgres round(x,n) does not accept double
functionFactory.cot();
functionFactory.radians();
functionFactory.degrees();
@ -602,13 +603,9 @@ public class PostgreSQLDialect extends Dialect {
return false;
}
/**
* Workaround for postgres bug #1453
* <p/>
* {@inheritDoc}
*/
@Override
public String getSelectClauseNullString(int sqlType) {
// Workaround for postgres bug #1453
return "null::" + getRawTypeName( sqlType );
}

View File

@ -1883,6 +1883,14 @@ public class CommonFunctionFactory {
.register();
}
public void round_floor() {
functionRegistry.patternDescriptorBuilder( "round", "floor(?1*1e?2+0.5)/1e?2")
.setReturnTypeResolver( useArgType(1) )
.setExactArgumentCount( 2 )
.setParameterTypes(NUMERIC, INTEGER)
.register();
}
public void square() {
functionRegistry.namedDescriptorBuilder( "square" )
.setExactArgumentCount( 1 )

View File

@ -282,6 +282,8 @@ public class FunctionTests {
.list();
session.createQuery("select ceiling(e.theDouble), floor(e.theDouble) from EntityOfBasics e")
.list();
session.createQuery("select round(e.theDouble, 2) from EntityOfBasics e")
.list();
session.createQuery("select round(cast(e.theDouble as BigDecimal), 3) from EntityOfBasics e")
.list();
assertThat( session.createQuery("select abs(-2)").getSingleResult(), is(2) );