Skip to content

Commit

Permalink
Fix leading whitespace in convert function
Browse files Browse the repository at this point in the history
Signed-off-by: Nirmit Shah <[email protected]>
  • Loading branch information
Nirmit Shah committed Mar 2, 2025
1 parent 8b6a640 commit d85a514
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 32 deletions.
4 changes: 2 additions & 2 deletions contrib/babelfishpg_tsql/sql/sys_function_helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10049,9 +10049,9 @@ BEGIN

v_res_length := substring(p_datatype COLLATE "C", MASK_REGEXP)::SMALLINT;
IF v_res_length IS NULL THEN
RETURN v_result;
RETURN ltrim(v_result);
ELSE
RETURN rpad(v_result, v_res_length, ' ');
RETURN rpad(ltrim(v_result), v_res_length, ' ');
END IF;
EXCEPTION
WHEN invalid_parameter_value THEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,79 @@ BEGIN
END;
$$;

CREATE OR REPLACE FUNCTION sys.babelfish_try_conv_float_to_string(IN p_datatype TEXT,
IN p_floatval FLOAT,
IN p_style NUMERIC DEFAULT 0)
RETURNS TEXT
AS
$BODY$
DECLARE
v_style SMALLINT;
v_format VARCHAR COLLATE "C";
v_floatval NUMERIC := abs(p_floatval);
v_digits SMALLINT;
v_integral_digits SMALLINT;
v_decimal_digits SMALLINT;
v_sign SMALLINT := sign(p_floatval);
v_result TEXT;
v_res_length SMALLINT;
MASK_REGEXP CONSTANT VARCHAR COLLATE "C" := '^\s*(?:character varying)\s*\(\s*(\d+|MAX)\s*\)\s*$';
BEGIN
v_style := floor(p_style)::SMALLINT;
IF (v_style = 0) THEN
v_digits := length(v_floatval::NUMERIC::TEXT);
v_decimal_digits := scale(v_floatval);
IF (v_decimal_digits > 0) THEN
v_integral_digits := v_digits - v_decimal_digits - 1;
ELSE
v_integral_digits := v_digits;
END IF;
IF (v_floatval >= 999999.5) THEN
v_format := '9D99999EEEE';
v_result := to_char(v_sign::NUMERIC * ceiling(v_floatval), v_format);
v_result := to_char(substring(v_result, 1, 8)::NUMERIC, 'FM9D99999')::NUMERIC::TEXT || substring(v_result, 9);
ELSE
IF (6 - v_integral_digits < v_decimal_digits) AND (trunc(abs(v_floatval)) != 0) THEN
v_decimal_digits := 6 - v_integral_digits;
ELSIF (6 - v_integral_digits < v_decimal_digits) THEN
v_decimal_digits := 6;
END IF;
v_format := (pow(10, v_integral_digits)-10)::TEXT || 'D';
IF (v_decimal_digits > 0) THEN
v_format := v_format || (pow(10, v_decimal_digits)-1)::TEXT;
END IF;
v_result := to_char(p_floatval, v_format);
END IF;
ELSIF (v_style = 1) THEN
v_format := '9D9999999EEEE';
v_result := to_char(p_floatval, v_format);
ELSIF (v_style = 2) THEN
v_format := '9D999999999999999EEEE';
v_result := to_char(p_floatval, v_format);
ELSIF (v_style = 3) THEN
v_format := '9D9999999999999999EEEE';
v_result := to_char(p_floatval, v_format);
ELSE
RAISE invalid_parameter_value;
END IF;

v_res_length := substring(p_datatype COLLATE "C", MASK_REGEXP)::SMALLINT;
IF v_res_length IS NULL THEN
RETURN ltrim(v_result);
ELSE
RETURN rpad(ltrim(v_result), v_res_length, ' ');
END IF;
EXCEPTION
WHEN invalid_parameter_value THEN
RAISE USING MESSAGE := pg_catalog.format('%s is not a valid style number when converting from FLOAT to a character string.', v_style),
DETAIL := 'Use of incorrect "style" parameter value during conversion process.',
HINT := 'Change "style" parameter to the proper value and try again.';
END;
$BODY$
LANGUAGE plpgsql
STABLE
RETURNS NULL ON NULL INPUT;

-- After upgrade, always run analyze for all babelfish catalogs.
CALL sys.analyze_babelfish_catalogs();

Expand Down
22 changes: 10 additions & 12 deletions test/JDBC/expected/babel_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,21 @@ select CONVERT(varchar(30), CAST(11234561231231.234 AS float), 1);
GO
~~START~~
varchar
1.1234561e+13
1.1234561e+13
~~END~~

select CONVERT(varchar(30), CAST(11234561231231.234 AS float), 2);
GO
~~START~~
varchar
1.123456123123123e+13
1.123456123123123e+13
~~END~~

select CONVERT(varchar(30), CAST(11234561231231.234 AS float), 3);
GO
~~START~~
varchar
1.1234561231231234e+13
1.1234561231231234e+13
~~END~~


Expand Down Expand Up @@ -304,7 +304,7 @@ select TRY_CONVERT(varchar(30), CAST('11234561231231.234'AS float), 1);
GO
~~START~~
varchar
1.1234561e+13
1.1234561e+13
~~END~~

select TRY_CONVERT(varchar(10), CAST(4936.56 AS MONEY), 0);
Expand All @@ -318,17 +318,15 @@ varchar
-- Wrong conversions that return NULL
select TRY_CONVERT(date, 123);
GO
~~START~~
date
<NULL>
~~END~~
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Explicit conversion from data type integer to date is not allowed.)~~

select TRY_CONVERT(time, 123);
GO
~~START~~
time
<NULL>
~~END~~
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Explicit conversion from data type integer to time is not allowed.)~~

select TRY_CONVERT(datetime, 123);
GO
Expand Down
36 changes: 18 additions & 18 deletions test/JDBC/expected/test_conv_float_to_varchar_char-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ GO

~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar#!#varchar
Direct Conversion#!#CAST vs CONVERT#!#Standard positive decimal#!# 123.456#!# 123.456 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Standard positive decimal#!# 123.456#!#123.456 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Negative decimal#!# -123.456#!#-123.456 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Zero value#!# 0#!# 0 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Zero value#!# 0#!#0 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Scientific notation - large#!# 10000000000#!#1e+10 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Scientific notation - small#!# 0#!# 0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Scientific notation - small#!# 0#!#0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Large decimal#!# 9999999999.99#!#1e+10 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Very small decimal#!# 0#!# 0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Very small decimal#!# 0#!#0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#NULL Value#!#<NULL>#!#<NULL>#!#<NULL>
~~END~~

~~START~~
char#!#char
123.456#!# 123.456
123.456#!#123.456
~~END~~

~~START~~
Expand All @@ -71,7 +71,7 @@ char#!#char

~~START~~
char#!#char
0#!# 0
0#!#0
~~END~~

~~START~~
Expand All @@ -81,7 +81,7 @@ char#!#char

~~START~~
char#!#char
0#!# 0.000000
0#!#0.000000
~~END~~

~~START~~
Expand All @@ -91,7 +91,7 @@ char#!#char

~~START~~
char#!#char
0#!# 0.000000
0#!#0.000000
~~END~~

~~START~~
Expand Down Expand Up @@ -126,7 +126,7 @@ varchar

~~START~~
varchar
123.457
123.457
~~END~~


Expand Down Expand Up @@ -182,19 +182,19 @@ GO

~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar#!#varchar
Direct Conversion#!#CAST vs CONVERT#!#Standard positive decimal#!#123.456#!# 123.456 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Standard positive decimal#!#123.456#!#123.456 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Negative decimal#!#-123.456#!#-123.456 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Zero value#!#0#!# 0 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Zero value#!#0#!#0 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Scientific notation - large#!#10000000000#!#1e+10 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Scientific notation - small#!#0#!# 0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Scientific notation - small#!#0#!#0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Large decimal#!#9999999999.99#!#1e+10 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Very small decimal#!#0#!# 0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#Very small decimal#!#0#!#0.000000 #!#<NULL>
Direct Conversion#!#CAST vs CONVERT#!#NULL Value#!#<NULL>#!#<NULL>#!#<NULL>
~~END~~

~~START~~
varchar#!#varchar
123.456#!# 123.456
123.456#!#123.456
~~END~~

~~START~~
Expand All @@ -204,7 +204,7 @@ varchar#!#varchar

~~START~~
varchar#!#varchar
0#!# 0
0#!#0
~~END~~

~~START~~
Expand All @@ -214,7 +214,7 @@ varchar#!#varchar

~~START~~
varchar#!#varchar
0#!# 0.000000
0#!#0.000000
~~END~~

~~START~~
Expand All @@ -224,7 +224,7 @@ varchar#!#varchar

~~START~~
varchar#!#varchar
0#!# 0.000000
0#!#0.000000
~~END~~

~~START~~
Expand Down Expand Up @@ -260,7 +260,7 @@ varchar

~~START~~
varchar
123.457
123.457
~~END~~


Expand Down

0 comments on commit d85a514

Please sign in to comment.