@@ -358,13 +358,13 @@ public MethodInfo getMethodInfo()
358358 labkeyMethod .put ("rand" , new JdbcMethod ("rand" , JdbcType .DOUBLE , 0 , 1 ));
359359 labkeyMethod .put ("repeat" , new JdbcMethod ("repeat" , JdbcType .VARCHAR , 2 , 2 ));
360360 labkeyMethod .put ("round" , new Method ("round" , JdbcType .DOUBLE , 1 , 2 )
361- {
362- @ Override
363- public MethodInfo getMethodInfo ()
364- {
365- return new RoundInfo ();
366- }
367- });
361+ {
362+ @ Override
363+ public MethodInfo getMethodInfo ()
364+ {
365+ return new RoundInfo ();
366+ }
367+ });
368368 labkeyMethod .put ("rtrim" , new JdbcMethod ("rtrim" , JdbcType .VARCHAR , 1 , 1 ));
369369 labkeyMethod .put ("second" , new JdbcMethod ("second" , JdbcType .INTEGER , 1 , 1 ));
370370 labkeyMethod .put ("sign" , new JdbcMethod ("sign" , JdbcType .DOUBLE , 1 , 1 ));
@@ -813,30 +813,43 @@ public SQLFragment getSQL(SqlDialect dialect, SQLFragment[] arguments)
813813 return super .getSQL (dialect , new SQLFragment [] {arguments [0 ]});
814814 }
815815
816- int i = Integer .MIN_VALUE ;
817- try
816+ if (supportsRoundDouble )
818817 {
819- i = Integer . parseInt ( arguments [ 1 ]. getSQL () );
818+ return super . getSQL (dialect , arguments );
820819 }
821- catch ( NumberFormatException x )
820+ else if ( dialect . isPostgreSQL () )
822821 {
823- /* fall through */
822+ // Postgres ROUND() requires NUMERIC argument and will error with DOUBLE
823+ // This CAST works because in postgres NUMERIC(unspecified) => up to 131072 digits before the decimal point; up to 16383 digits after the decimal point
824+ // This is not SQL standard behavior
825+ SQLFragment numeric = new SQLFragment ();
826+ numeric .append ("CAST((" ).append (arguments [0 ]).append (") AS NUMERIC)" );
827+ return super .getSQL (dialect , new SQLFragment [] {numeric , arguments [1 ]});
824828 }
825-
826- if (supportsRoundDouble || i == Integer .MIN_VALUE )
827- return super .getSQL (dialect , arguments );
828-
829- // fall back, only supports simple integer
830- SQLFragment scaled = new SQLFragment ();
831- scaled .append ("(" );
832- scaled .append (arguments [0 ]);
833- scaled .append (")*" ).appendValue (Math .pow (10 ,i ));
834- SQLFragment ret = super .getSQL (dialect , new SQLFragment [] {scaled });
835- ret .append ("/" );
836- ret .appendValue (Math .pow (10 ,i ));
837- return ret ;
838- }
839- }
829+ else
830+ {
831+ // NOTE: At the moment there appear to be no Dialects that use this code path (but it still works).
832+ // Leave for completeness?
833+ int n = Integer .MIN_VALUE ;
834+ try
835+ {
836+ n = Integer .parseInt (arguments [1 ].getSQL ());
837+ }
838+ catch (NumberFormatException x )
839+ {
840+ /* fall through */
841+ }
842+ // If 2nd argument isn't a constant, just do the default thing. This may work or it may cause a server parse error.
843+ if (n == Integer .MIN_VALUE )
844+ return super .getSQL (dialect , arguments );
845+ var scale = Math .pow (10 ,n );
846+ SQLFragment scaled = new SQLFragment ().append ("(" ).append (arguments [0 ]).append (")*" ).appendValue (scale );
847+ SQLFragment ret = super .getSQL (dialect , new SQLFragment []{scaled });
848+ ret .append ("/" ).appendValue (scale );
849+ return ret ;
850+ }
851+ }
852+ }
840853
841854
842855 static class AgeMethodInfo extends AbstractMethodInfo
0 commit comments