Skip to content

Commit 6b8a782

Browse files
committed
Fix cast for pi function on MySQL 5.7
1 parent aae3513 commit 6b8a782

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,22 @@ public void initializeFunctionRegistry(QueryEngine queryEngine) {
501501
.register();
502502

503503
// pi() produces a value with 7 digits unless we're explicit
504-
functionRegistry.patternDescriptorBuilder( "pi", "cast(pi() as double)" )
505-
.setInvariantType(basicTypeRegistry.resolve( StandardBasicTypes.DOUBLE ))
506-
.setExactArgumentCount(0)
507-
.setArgumentListSignature("")
508-
.register();
504+
if ( getMySQLVersion().isSameOrAfter( 8 ) ) {
505+
functionRegistry.patternDescriptorBuilder( "pi", "cast(pi() as double)" )
506+
.setInvariantType( basicTypeRegistry.resolve( StandardBasicTypes.DOUBLE ) )
507+
.setExactArgumentCount( 0 )
508+
.setArgumentListSignature( "" )
509+
.register();
510+
}
511+
else {
512+
// But before MySQL 8, it's not possible to cast to double. Double has a default precision of 53
513+
// and since the internal representation of pi has only 15 decimal places, we cast to decimal(53,15)
514+
functionRegistry.patternDescriptorBuilder( "pi", "cast(pi() as decimal(53,15))" )
515+
.setInvariantType( basicTypeRegistry.resolve( StandardBasicTypes.DOUBLE ) )
516+
.setExactArgumentCount( 0 )
517+
.setArgumentListSignature( "" )
518+
.register();
519+
}
509520

510521
// By default char() produces a binary string, not a character string.
511522
// (Note also that char() is actually a variadic function in MySQL.)

0 commit comments

Comments
 (0)