Skip to content

Commit

Permalink
Fix for both table and column aliases contains single and multibyte c…
Browse files Browse the repository at this point in the history
…haracters. (babelfish-for-postgresql#1912)

This pull request fix the implementation for column and table aliases contain both single byte and multibyte characters whose length is more than 64 bytes. A valid identifier can have 128 characters. It also fixed the issue with aliases contain multibyte characters with 128 characters.

Task: BABEL-4231
Signed-off-by: Riya Jain <[email protected]>
  • Loading branch information
riyajain39 authored Oct 13, 2023
1 parent 08c886f commit fe2b8ea
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 20 deletions.
11 changes: 5 additions & 6 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,11 @@ extract_identifier(const char *start)
* greater than 1 */

/*
* valid identifier cannot be longer than 258 (2*128+2) bytes. SQL server
* allows up to 128 bascially. And escape character can take additional
* one byte for each character in worst case. And additional 2 byes for
* delimiter
* Reaching here implies of valid identifier. It means we can reach
* identifier's end in both the cases of single and multibyte characters.
* If the identifier is not valid, the scanner should have already reported a syntax error.
*/
while (i < 258)
while (true)
{
char c = start[i];

Expand Down Expand Up @@ -1759,7 +1758,7 @@ pre_transform_target_entry(ResTarget *res, ParseState *pstate,
memcpy(alias + (alias_len) - 32,
identifier_name + (alias_len) - 32,
32);
alias[alias_len+1] = '\0';
alias[alias_len] = '\0';
}
/* Identifier is not truncated. */
else
Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/expected/BABEL-4231-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,6 @@ DROP VIEW view_babel_4231_40;
GO
DROP TABLE table18_babel_4231;
GO

DROP VIEW view_babel_4231_41;
GO
14 changes: 8 additions & 6 deletions test/JDBC/expected/BABEL-4231-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,22 @@ CREATE VIEW view_babel_4231_35 AS SELECT * FROM table17_babel_4231 AS "αΒβΓ
GO

-- tsql
-- table aliases which are not delimited by double quote, square bracket and length is less than 64
-- column aliases which are not delimited by double quote, square bracket and length is less than 64
CREATE VIEW view_babel_4231_36 AS SELECT 1 AS ABCD;
GO

-- tsql
-- table aliases with single byte characters which are not delimited by double quote, square bracket and length is more than or equals to 64
-- column aliases with single byte characters which are not delimited by double quote, square bracket and length is more than or equals to 64
CREATE VIEW view_babel_4231_37 AS SELECT 1 AS ABCDbdjaBFWFGRUWlgrefwfiwuegfdvfwefgvggfedfudywtitewutgfdWUIF;
GO

-- tsql
-- table aliases with single byte characters which are delimited by single quote and length is more than or equals to 64
-- column aliases with single byte characters which are delimited by single quote and length is more than or equals to 64
CREATE VIEW view_babel_4231_38 AS SELECT 1 AS 'ABCDbdjaBFWFGRUWlgrefwfiwuegfdvfwefgvggfedfudywtitewutgfdWUIF';
GO

-- tsql
-- table aliases with single byte characters which are delimited by single quote and length is less than 64
-- column aliases with single byte characters which are delimited by single quote and length is less than 64
CREATE VIEW view_babel_4231_39 AS SELECT 1 AS N'ANfjws';
GO

Expand All @@ -264,5 +264,7 @@ GO
CREATE VIEW view_babel_4231_40 AS SELECT 您您对您对您对您对您对您对您对您对您对您您您.* FROM table18_babel_4231 AS 您您对您对您对您对您对您对您对您对您对您您您;
GO



-- tsql
-- column aliases with multibyte characters which are delimited by single quote and length is more than 128
CREATE VIEW view_babel_4231_41 AS SELECT 1 AS '您对“数据一览“中的车型,颜色,内饰,选装, 您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,';
GO
18 changes: 18 additions & 0 deletions test/JDBC/expected/BABEL-4231-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,21 @@ text
SELECT "您您对您对您对您对您d60211ff7d947ff09db87babbf0cb9de".a,<newline> "您您对您对您对您对您d60211ff7d947ff09db87babbf0cb9de".b<newline> FROM master_dbo.table18_babel_4231 "您您对您对您对您对您d60211ff7d947ff09db87babbf0cb9de";
~~END~~


-- psql
SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'view_babel_4231_41';
GO
~~START~~
text
SELECT 1 AS "您对“数据一览“中的e06f302024afe951b33a0978fde84988";
~~END~~


-- tsql
select 1 as '您对“数据一览“中的车型,颜色,内饰,选装, 您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,';
GO
~~START~~
int
1
~~END~~

5 changes: 4 additions & 1 deletion test/JDBC/input/BABEL-4231-vu-cleanup.mix
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,7 @@ GO
DROP VIEW view_babel_4231_40;
GO
DROP TABLE table18_babel_4231;
GO
GO

DROP VIEW view_babel_4231_41;
GO
14 changes: 8 additions & 6 deletions test/JDBC/input/BABEL-4231-vu-prepare.mix
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,22 @@ CREATE VIEW view_babel_4231_35 AS SELECT * FROM table17_babel_4231 AS "αΒβΓ
GO

-- tsql
-- table aliases which are not delimited by double quote, square bracket and length is less than 64
-- column aliases which are not delimited by double quote, square bracket and length is less than 64
CREATE VIEW view_babel_4231_36 AS SELECT 1 AS ABCD;
GO

-- tsql
-- table aliases with single byte characters which are not delimited by double quote, square bracket and length is more than or equals to 64
-- column aliases with single byte characters which are not delimited by double quote, square bracket and length is more than or equals to 64
CREATE VIEW view_babel_4231_37 AS SELECT 1 AS ABCDbdjaBFWFGRUWlgrefwfiwuegfdvfwefgvggfedfudywtitewutgfdWUIF;
GO

-- tsql
-- table aliases with single byte characters which are delimited by single quote and length is more than or equals to 64
-- column aliases with single byte characters which are delimited by single quote and length is more than or equals to 64
CREATE VIEW view_babel_4231_38 AS SELECT 1 AS 'ABCDbdjaBFWFGRUWlgrefwfiwuegfdvfwefgvggfedfudywtitewutgfdWUIF';
GO

-- tsql
-- table aliases with single byte characters which are delimited by single quote and length is less than 64
-- column aliases with single byte characters which are delimited by single quote and length is less than 64
CREATE VIEW view_babel_4231_39 AS SELECT 1 AS N'ANfjws';
GO

Expand All @@ -264,5 +264,7 @@ GO
CREATE VIEW view_babel_4231_40 AS SELECT 您您对您对您对您对您对您对您对您对您对您您您.* FROM table18_babel_4231 AS 您您对您对您对您对您对您对您对您对您对您您您;
GO



-- tsql
-- column aliases with multibyte characters which are delimited by single quote and length is more than 128
CREATE VIEW view_babel_4231_41 AS SELECT 1 AS '您对“数据一览“中的车型,颜色,内饰,选装, 您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,';
GO
10 changes: 9 additions & 1 deletion test/JDBC/input/BABEL-4231-vu-verify.mix
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,12 @@ GO

-- psql
SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'view_babel_4231_40';
GO
GO

-- psql
SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'view_babel_4231_41';
GO

-- tsql
select 1 as '您对“数据一览“中的车型,颜色,内饰,选装, 您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,您对“数据一览“中的车型,颜色,内饰,选装,';
GO

0 comments on commit fe2b8ea

Please sign in to comment.