From 6b8a782d50c14c8375a044c17ce89bfdba0b2f82 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Wed, 21 Sep 2022 10:57:22 +0200 Subject: [PATCH] Fix cast for pi function on MySQL 5.7 --- .../org/hibernate/dialect/MySQLDialect.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java index 23a262ea3a..1a3a3e43e6 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java @@ -501,11 +501,22 @@ public class MySQLDialect extends Dialect { .register(); // pi() produces a value with 7 digits unless we're explicit - functionRegistry.patternDescriptorBuilder( "pi", "cast(pi() as double)" ) - .setInvariantType(basicTypeRegistry.resolve( StandardBasicTypes.DOUBLE )) - .setExactArgumentCount(0) - .setArgumentListSignature("") - .register(); + if ( getMySQLVersion().isSameOrAfter( 8 ) ) { + functionRegistry.patternDescriptorBuilder( "pi", "cast(pi() as double)" ) + .setInvariantType( basicTypeRegistry.resolve( StandardBasicTypes.DOUBLE ) ) + .setExactArgumentCount( 0 ) + .setArgumentListSignature( "" ) + .register(); + } + else { + // But before MySQL 8, it's not possible to cast to double. Double has a default precision of 53 + // and since the internal representation of pi has only 15 decimal places, we cast to decimal(53,15) + functionRegistry.patternDescriptorBuilder( "pi", "cast(pi() as decimal(53,15))" ) + .setInvariantType( basicTypeRegistry.resolve( StandardBasicTypes.DOUBLE ) ) + .setExactArgumentCount( 0 ) + .setArgumentListSignature( "" ) + .register(); + } // By default char() produces a binary string, not a character string. // (Note also that char() is actually a variadic function in MySQL.)