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 jakarta.persistence.TemporalType;
import static org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers.useArgType;
import static org.hibernate.type.SqlTypes.*; import static org.hibernate.type.SqlTypes.*;
/** /**
@ -249,13 +248,9 @@ public class DerbyDialect extends Dialect {
functionFactory.leftRight_substrLength(); functionFactory.leftRight_substrLength();
functionFactory.characterLength_length( SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); functionFactory.characterLength_length( SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
functionFactory.power_expLn(); functionFactory.power_expLn();
functionFactory.round_floor();
functionFactory.bitLength_pattern( "length(?1)*8" ); 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( queryEngine.getSqmFunctionRegistry().register(
"concat", "concat",
new CastingConcatFunction( new CastingConcatFunction(

View File

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

View File

@ -1883,6 +1883,14 @@ public class CommonFunctionFactory {
.register(); .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() { public void square() {
functionRegistry.namedDescriptorBuilder( "square" ) functionRegistry.namedDescriptorBuilder( "square" )
.setExactArgumentCount( 1 ) .setExactArgumentCount( 1 )

View File

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