Skip to content

Commit

Permalink
Fix cast for pi function on MySQL 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Sep 21, 2022
1 parent aae3513 commit 6b8a782
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,22 @@ public void initializeFunctionRegistry(QueryEngine queryEngine) {
.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.)
Expand Down

0 comments on commit 6b8a782

Please sign in to comment.