Skip to content

Commit

Permalink
Adding fix for edge cases in Space function (#2807)
Browse files Browse the repository at this point in the history
* Adding fix for edge cases in Space function (#2742)

Currently for string function SPACE() we only handle positive integers and throws error for input 0 or negative number.

This commit will handle those edge cases and when a negative number is passed we will return NULL and when 0 is passed we will return an empty string. Additionally, this commit adds handling when input integer is greater than 8000.

Task: BABEL-4811
Signed-off-by: Tanya Gupta <[email protected]>
  • Loading branch information
tanyagupta17 authored Jul 30, 2024
1 parent 1596e1f commit ad03399
Show file tree
Hide file tree
Showing 34 changed files with 734 additions and 6 deletions.
9 changes: 6 additions & 3 deletions contrib/babelfishpg_tsql/sql/sys_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1510,10 +1510,13 @@ STRICT
LANGUAGE SQL IMMUTABLE;

CREATE OR REPLACE FUNCTION sys.space(IN number INTEGER, OUT result SYS.VARCHAR) AS $$
-- sys.varchar has default length of 1, so we have to pass in 'number' to be the
-- type modifier.
BEGIN
EXECUTE pg_catalog.format(E'SELECT repeat(\' \', %s)::SYS.VARCHAR(%s)', number, number) INTO result;
IF number < 0 THEN
result := NULL;
ELSE
-- TSQL has a limitation of 8000 character spaces for space function.
result := PG_CATALOG.repeat(' ',least(number, 8000));
END IF;
END;
$$
STRICT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10010,6 +10010,19 @@ BEGIN
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION sys.space(IN number INTEGER, OUT result SYS.VARCHAR) AS $$
BEGIN
IF number < 0 THEN
result := NULL;
ELSE
-- TSQL has a limitation of 8000 character spaces for space function.
result := PG_CATALOG.repeat(' ',least(number, 8000));
END IF;
END;
$$
STRICT
LANGUAGE plpgsql;
-- Drops the temporary procedure used by the upgrade script.
-- Please have this be one of the last statements executed in this upgrade script.
DROP PROCEDURE sys.babelfish_drop_deprecated_object(varchar, varchar, varchar);
Expand Down
50 changes: 50 additions & 0 deletions test/JDBC/expected/space-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
DROP FUNCTION babel_4811_vu_prepare_f1
GO

DROP FUNCTION babel_4811_vu_prepare_f2
GO

DROP FUNCTION babel_4811_vu_prepare_f3
GO

DROP PROCEDURE babel_4811_vu_prepare_p1
GO

DROP PROCEDURE babel_4811_vu_prepare_p2
GO

DROP VIEW babel_4811_vu_prepare_v1
GO

DROP VIEW babel_4811_vu_prepare_v2
GO

DROP VIEW babel_4811_vu_prepare_v3
GO

DROP VIEW babel_4811_vu_prepare_v4
GO

DROP VIEW babel_4811_vu_prepare_v5
GO

DROP VIEW babel_4811_vu_prepare_v6
GO

DROP VIEW babel_4811_vu_prepare_v7
GO

DROP VIEW babel_4811_vu_prepare_v8
GO

DROP VIEW babel_4811_vu_prepare_v9
GO

DROP TABLE babel_4811_vu_prepare_t1
GO

DROP TABLE babel_4811_vu_prepare_t2
GO

DROP TABLE babel_4811_vu_prepare_t3
GO
91 changes: 91 additions & 0 deletions test/JDBC/expected/space-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
CREATE TABLE babel_4811_vu_prepare_t1(number int)
GO
INSERT INTO babel_4811_vu_prepare_t1 VALUES(5)
GO
~~ROW COUNT: 1~~


CREATE TABLE babel_4811_vu_prepare_t2(number int)
GO
INSERT INTO babel_4811_vu_prepare_t2 VALUES(-10)
GO
~~ROW COUNT: 1~~


CREATE TABLE babel_4811_vu_prepare_t3(number int)
GO
INSERT INTO babel_4811_vu_prepare_t3 VALUES(0)
GO
~~ROW COUNT: 1~~


CREATE FUNCTION babel_4811_vu_prepare_f1(@number int)
returns int
BEGIN
RETURN DATALENGTH(SPACE(@number))
END
GO

CREATE FUNCTION babel_4811_vu_prepare_f2(@number int)
returns varchar(20)
BEGIN
RETURN '|' + SPACE(@number) + '|'
END
GO

CREATE FUNCTION babel_4811_vu_prepare_f3()
returns table
AS
RETURN (select '|' + SPACE(number) + '|' as result from babel_4811_vu_prepare_t1)
GO

CREATE PROCEDURE babel_4811_vu_prepare_p1 (@number AS INT)
AS
BEGIN
SELECT DATALENGTH(SPACE(@number))
END;
GO

CREATE PROCEDURE babel_4811_vu_prepare_p2 (@number AS INT)
AS
BEGIN
SELECT '|' + SPACE(@number) + '|' AS result
END;
GO

CREATE VIEW babel_4811_vu_prepare_v1 AS
SELECT DATALENGTH(SPACE(10)) as result
GO


CREATE VIEW babel_4811_vu_prepare_v2 AS
SELECT DATALENGTH(SPACE(0)) as result
GO

CREATE VIEW babel_4811_vu_prepare_v3 AS
SELECT DATALENGTH(SPACE(-10)) as result
GO

CREATE VIEW babel_4811_vu_prepare_v4 AS
SELECT DATALENGTH(SPACE(number)) as result FROM babel_4811_vu_prepare_t1
GO

CREATE VIEW babel_4811_vu_prepare_v5 AS
SELECT DATALENGTH(SPACE(number)) as result FROM babel_4811_vu_prepare_t2
GO

CREATE VIEW babel_4811_vu_prepare_v6 AS
SELECT DATALENGTH(SPACE(number)) as result FROM babel_4811_vu_prepare_t3
GO

CREATE VIEW babel_4811_vu_prepare_v7 AS
SELECT '|' + SPACE(10) + '|' AS result
GO

CREATE VIEW babel_4811_vu_prepare_v8 AS
SELECT '|' + SPACE(0) + '|' AS result
GO

CREATE VIEW babel_4811_vu_prepare_v9 AS
SELECT '|' + SPACE(-10) + '|' AS result
GO
Loading

0 comments on commit ad03399

Please sign in to comment.