Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fix for edge cases in Space function #2807

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading