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

Fix the issue of preserving case of column names in SELECT corrname.colname. #1866

Merged
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
62 changes: 61 additions & 1 deletion contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,9 @@ pre_transform_target_entry(ResTarget *res, ParseState *pstate,
int alias_len = 0;
const char *colname_start;
const char *identifier_name = NULL;
int open_square_bracket = 0;
riyajain39 marked this conversation as resolved.
Show resolved Hide resolved
int double_quotes = 0;
const char *last_dot;

if (res->name == NULL && res->location != -1 &&
IsA(res->val, ColumnRef))
Expand All @@ -1600,12 +1603,69 @@ pre_transform_target_entry(ResTarget *res, ParseState *pstate,
* sourcetext
*/
if (list_length(cref->fields) == 1 &&
IsA(linitial(cref->fields), String))
IsA(linitial(cref->fields), String))
{
identifier_name = strVal(linitial(cref->fields));
alias_len = strlen(identifier_name);
colname_start = pstate->p_sourcetext + res->location;
}
/*
* This condition will preserve the case of column name when there are more than
* one cref->fields. For instance, Queries like
* 1. select [database].[schema].[table].[column] from table.
* 2. select [schema].[table].[column] from table.
* 3. select [t].[column] from table as t
* Case 1: Handle the cases when column name is passed with no delimiters
* For example, select ABC from table
* Case 2: Handle the cases when column name is delimited with dq.
* In such cases, we are checking if no. of dq are even or not. When dq are odd,
* we are not tracing number of sqb and sq within dq.
* For instance, Queries like select "AF bjs'vs] " from table.
* Case 3: Handle the case when column name is delimited with sqb. When number of sqb
* are zero, it means we are out of sqb.
*/
if(list_length(cref->fields) > 1 &&
IsA(llast(cref->fields), String))
{
identifier_name = strVal(llast(cref->fields));
alias_len = strlen(identifier_name);
colname_start = pstate->p_sourcetext + res->location;
while(true)
{
if(open_square_bracket == 0 && *colname_start == '"')
{
double_quotes++;
}
/* To check how many open sqb are present in sourcetext. */
else if(double_quotes % 2 == 0 && *colname_start == '[')
{
open_square_bracket++;
}
else if(double_quotes % 2 == 0 && *colname_start == ']')
{
open_square_bracket--;
}
/*
* last_dot pointer is to trace the last dot in the sourcetext,
* as last dot indicates the starting of column name.
*/
else if(open_square_bracket == 0 && double_quotes % 2 == 0 && *colname_start == '.')
{
last_dot = colname_start;
}
/*
* If there is no open sqb, there are even no. of sq or dq and colname_start is at
* space or comma, it means colname_start is at the end of column name.
*/
else if(open_square_bracket == 0 && double_quotes % 2 == 0 && (*colname_start == ' ' || *colname_start == ','))
{
last_dot++;
colname_start = last_dot;
break;
}
colname_start++;
}
}
}
else if (res->name != NULL && res->name_location != -1)
{
Expand Down
60 changes: 60 additions & 0 deletions test/JDBC/expected/BABEL-4279-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- tsql
DROP VIEW test_babel_4279_v1;
GO

DROP VIEW test_babel_4279_v2;
GO

DROP VIEW test_babel_4279_v3;
GO

DROP TABLE test_babel_4279_t1;
GO

USE [test_babel_4279_d.1];
GO

DROP VIEW test_babel_4279_sv1;
GO

DROP TABLE test_babel_4279_s1.test_babel_4279_st1;
GO

DROP SCHEMA test_babel_4279_s1;
GO

USE MASTER;
GO

DROP DATABASE [test_babel_4279_d.1];
GO

DROP VIEW test_babel_4279_v4;
GO

DROP VIEW test_babel_4279_v5;
GO

DROP TABLE test_babel_4279_t2;
GO

DROP VIEW test_babel_4279_v6;
GO

DROP TABLE "tngdf'".[sc,sdg"fdsngjds'];
GO

DROP SCHEMA "tngdf'";
GO

DROP VIEW test_babel_4279_v7;
GO

DROP TABLE test_babel_4279_t3;
GO

DROP VIEW test_babel_4279_v8;
GO

DROP TABLE test_babel_4279_t4;
GO
60 changes: 60 additions & 0 deletions test/JDBC/expected/BABEL-4279-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- tsql
CREATE TABLE test_babel_4279_t1([ABC.nfds] INT, [DEf.j] INT);
GO

CREATE VIEW test_babel_4279_v1 AS SELECT test_babel_4279_t1.[ABC.nfds] from test_babel_4279_t1;
GO

CREATE VIEW test_babel_4279_v2 AS SELECT [test_babel_4279_t1].[ABC.nfds] ,test_babel_4279_t1.[DEf.j] from test_babel_4279_t1;
GO

CREATE DATABASE [test_babel_4279_d.1];
GO

USE [test_babel_4279_d.1];
GO

CREATE SCHEMA test_babel_4279_s1;
GO

CREATE TABLE test_babel_4279_s1.test_babel_4279_st1([ABC.nfds] INT, [DEf.j] INT);
GO

CREATE VIEW test_babel_4279_sv1 AS SELECT [test_babel_4279_s1].[test_babel_4279_st1].[ABC.nfds] from test_babel_4279_s1.test_babel_4279_st1;
GO

USE MASTER
GO

CREATE VIEW test_babel_4279_v3 AS SELECT [test_babel_4279_d.1].[test_babel_4279_s1].[test_babel_4279_st1].[ABC.nfds] from [test_babel_4279_d.1].[test_babel_4279_s1].[test_babel_4279_st1];
GO

CREATE TABLE test_babel_4279_t2(您您对您对您对您对您对您对您对您对您对您您您 INT, 对您对您对您对您对您对您对您 INT);
GO

CREATE VIEW test_babel_4279_v4 AS SELECT test_babel_4279_t2.[您您对您对您对您对您对您对您对您对您对您您您] from test_babel_4279_t2;
GO

CREATE VIEW test_babel_4279_v5 AS SELECT ぁあ.[您您对您对您对您对您对您对您对您对您对您您您] from test_babel_4279_t2 AS ぁあ;
GO

CREATE SCHEMA "tngdf'";
GO

CREATE TABLE "tngdf'".[sc,sdg"fdsngjds']("AB[C" INT);
GO

CREATE VIEW test_babel_4279_v6 AS SELECT "tngdf'".[sc,sdg"fdsngjds']."AB[C" from "tngdf'".[sc,sdg"fdsngjds'];
GO

CREATE TABLE test_babel_4279_t3(ABCD INT);
GO

CREATE VIEW test_babel_4279_v7 AS SELECT test_babel_4279_t3.ABCD FROM test_babel_4279_t3;
GO

CREATE TABLE test_babel_4279_t4([ぁあ'"] INT);
GO

CREATE VIEW test_babel_4279_v8 AS SELECT test_babel_4279_t4.[ぁあ'"] FROM test_babel_4279_t4;
GO
72 changes: 72 additions & 0 deletions test/JDBC/expected/BABEL-4279-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- psql
SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v1';
GO
~~START~~
text
SELECT test_babel_4279_t1."abc.nfds" AS "ABC.nfds"<newline> FROM master_dbo.test_babel_4279_t1;
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v2';
GO
~~START~~
text
SELECT test_babel_4279_t1."abc.nfds" AS "ABC.nfds",<newline> test_babel_4279_t1."def.j" AS "DEf.j"<newline> FROM master_dbo.test_babel_4279_t1;
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_sv1';
GO
~~START~~
text
SELECT test_babel_4279_st1."abc.nfds" AS "ABC.nfds"<newline> FROM "test_babel_4279_d.1_test_babel_4279_s1".test_babel_4279_st1;
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v3';
GO
~~START~~
text
SELECT test_babel_4279_st1."abc.nfds" AS "ABC.nfds"<newline> FROM "test_babel_4279_d.1_test_babel_4279_s1".test_babel_4279_st1;
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v4';
GO
~~START~~
text
SELECT test_babel_4279_t2."您您对您对您对您对您d60211ff7d947ff09db87babbf0cb9de"<newline> FROM master_dbo.test_babel_4279_t2;
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v5';
GO
~~START~~
text
SELECT "ぁあ"."您您对您对您对您对您d60211ff7d947ff09db87babbf0cb9de"<newline> FROM master_dbo.test_babel_4279_t2 "ぁあ";
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v6';
GO
~~START~~
text
SELECT "sc,sdg""fdsngjds'"."ab[c" AS "AB[C"<newline> FROM "master_tngdf'"."sc,sdg""fdsngjds'";
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v7';
GO
~~START~~
text
SELECT test_babel_4279_t3.abcd AS "ABCD"<newline> FROM master_dbo.test_babel_4279_t3;
~~END~~


SELECT pg_catalog.pg_get_viewdef(oid, true) FROM pg_class WHERE relname = 'test_babel_4279_v8';
GO
~~START~~
text
SELECT test_babel_4279_t4."ぁあ'"""<newline> FROM master_dbo.test_babel_4279_t4;
~~END~~

Loading
Loading