Skip to content

Commit

Permalink
Fix syntax error when delimited column alias is used without precedin…
Browse files Browse the repository at this point in the history
…g whitespace (#2512)

When the alias is directly adjacent to a keyword (like END) or a non-delimited column name, the error message mentions AS, even though there is no AS keyword in the query.

This bug is caused by ANTLR rewriting, we have added an extra space before 'AS' keyword when rewriting the query which which doesn't explicitly has 'AS' keyword before alias.

# Input
SELECT CASE WHEN 1=1  THEN 1 ELSE 0 END[c]

# previous output (wrong output)               
Msg 33557097, Level 16, State 1, Server persist-rcv-07mar-16-2-0-9, Line 1
syntax error at or near "ENDAS"

# current output (correct output)
~~START~~
int
1
~~END~~

# previous output (wrong output)
SELECT 1c;

Msg 33557097, Level 16, State 1, Server persist-rcv-07mar-16-2-0-9, Line 1
trailing junk after numeric literal at or near "1A"

# current output (correct output)
SELECT 1c;

~~START~~
int
1
~~END~~

    Added Test Cases for this issue

4_X PR: #2504

Task: BABEL-4863

Authored-by: Anikait Agrawal [email protected]
Signed-off-by: Anikait Agrawal <[email protected]>
Co-authored-by: Anikait Agrawal <[email protected]>
  • Loading branch information
Anikait143 and Anikait Agrawal authored Apr 18, 2024
1 parent 8461054 commit 0efbb15
Show file tree
Hide file tree
Showing 35 changed files with 548 additions and 16 deletions.
12 changes: 5 additions & 7 deletions contrib/babelfishpg_tsql/src/tsqlIface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2316,15 +2316,13 @@ static void process_query_specification(
auto column_alias_as = elem->expression_elem()->as_column_alias();
if (!column_alias_as->AS())
{
std::string orig_text = ::getFullText(column_alias_as->column_alias());
std::string repl_text = std::string("AS ");

if (is_quotation_needed_for_column_alias(column_alias_as->column_alias()))
repl_text += std::string("\"") + orig_text + "\"";
{
mutator->add(column_alias_as->start->getStartIndex(), "", " AS \"");
mutator->add(column_alias_as->stop->getStopIndex() + 1, "", "\"");
}
else
repl_text += orig_text;

mutator->add(column_alias_as->start->getStartIndex(), "", "AS ");
mutator->add(column_alias_as->start->getStartIndex(), "", " AS ");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
drop function if exists babel_4863_func
GO
drop function if exists babel_4863_func1
GO
drop procedure if exists babel_4863_proc
GO
drop view if exists babel_4863_view
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
create function babel_4863_func()
returns table as return
(
select '1' as val
)
go

create function babel_4863_func1()
returns table as return
(
SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END
)
GO

CREATE PROCEDURE babel_4863_proc @a INT
AS BEGIN
DECLARE @v1 int = CASE WHEN @a=1 THEN 1 ELSE 0 END;
DECLARE @v2 int = CASE WHEN @a=1 THEN 1 ELSE 0 END;
DECLARE @v3 int = CASE WHEN @a=1 THEN 1 ELSE 0 END;
SELECT @v1, @v2, @v3;
END;
GO

CREATE VIEW babel_4863_view AS
SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END;
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
select babel_4863_func();
GO
~~START~~
varchar
1
~~END~~

select * from babel_4863_func1();
GO
~~START~~
int
1
~~END~~

exec babel_4863_proc 1;
GO
~~START~~
int#!#int#!#int
1#!#1#!#1
~~END~~

exec babel_4863_proc 2;
GO
~~START~~
int#!#int#!#int
0#!#0#!#0
~~END~~

SELECT * FROM babel_4863_view;
GO
~~START~~
int
1
~~END~~

10 changes: 10 additions & 0 deletions test/JDBC/expected/BABEL-4863-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
drop table if exists babel_4863_t1
GO
drop function if exists babel_4863_func
GO
drop function if exists babel_4863_func1
GO
drop procedure if exists babel_4863_proc
GO
drop view if exists babel_4863_view
GO
37 changes: 37 additions & 0 deletions test/JDBC/expected/BABEL-4863-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
create table babel_4863_t1 (a int)
GO
INSERT INTO babel_4863_t1 VALUES(1)
GO
~~ROW COUNT: 1~~


create function babel_4863_func()
returns table as return
(
select 'value' = '1'
)
go

create function babel_4863_func1()
returns table as return
(
SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END[this is a $.^ test],
CASE WHEN 1=1 THEN 1 ELSE 0 END'this is a $.^ test1',
CASE WHEN 1=1 THEN 1 ELSE 0 END"this is a $.^ test2"
)
GO

CREATE PROCEDURE babel_4863_proc @a INT
AS BEGIN
DECLARE @v1 int = CASE WHEN @a=1 THEN 1 ELSE 0 END;
DECLARE @v2 int = CASE WHEN @a=1 THEN 1 ELSE 0 END;
DECLARE @v3 int = CASE WHEN @a=1 THEN 1 ELSE 0 END;
SELECT @v1[this is a $.^ test], @v2'this is a $.^ test1', @v3"this is a $.^ test2";
END;
GO

CREATE VIEW babel_4863_view AS
SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END[this is a $.^ test],
CASE WHEN 1=1 THEN 1 ELSE 0 END'this is a $.^ test1',
CASE WHEN 1=1 THEN 1 ELSE 0 END"this is a $.^ test2";
GO
241 changes: 241 additions & 0 deletions test/JDBC/expected/BABEL-4863-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
SELECT 123abc;
GO
~~START~~
int
123
~~END~~

SELECT 0x0o;
GO
~~START~~
varbinary
00
~~END~~

SELECT 1_2_3;
GO
~~START~~
int
1
~~END~~

SELECT 0.a;
GO
~~START~~
numeric
0
~~END~~

SELECT 0.0a;
GO
~~START~~
numeric
0
~~END~~

SELECT .0a;
GO
~~START~~
numeric
0
~~END~~

SELECT 0.0e1a;
GO
~~START~~
numeric
0
~~END~~

SELECT $1a;
GO
~~START~~
int
1
~~END~~

SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END[c]
GO
~~START~~
int
1
~~END~~

SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END'c'
GO
~~START~~
int
1
~~END~~

SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END"c"
GO
~~START~~
int
1
~~END~~

select a[c] from babel_4863_t1
GO
~~START~~
int
1
~~END~~

select a"c" from babel_4863_t1
GO
~~START~~
int
1
~~END~~

select a'c' from babel_4863_t1
GO
~~START~~
int
1
~~END~~

declare @v int=1
select @v[c]
GO
~~START~~
int
1
~~END~~

declare @v int=1
select @v'c'
GO
~~START~~
int
1
~~END~~

declare @v int=1
select @v"c"
GO
~~START~~
int
1
~~END~~

select babel_4863_func()[a];
GO
~~START~~
varchar
1
~~END~~

select 1[c];
GO
~~START~~
int
1
~~END~~

select 1'c';
GO
~~START~~
int
1
~~END~~

select 1"c";
GO
~~START~~
int
1
~~END~~

select 'abc'[def];
GO
~~START~~
varchar
abc
~~END~~

select 'abc''def';
GO
~~START~~
varchar
abc'def
~~END~~

select 'abc'"def";
GO
~~START~~
varchar
abc
~~END~~

select 123[this is a $.^ test]
GO
~~START~~
int
123
~~END~~

select 123'this is a $.^ test'
GO
~~START~~
int
123
~~END~~

select 123"this is a $.^ test"
GO
~~START~~
int
123
~~END~~

SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END[this is a $.^ test]
GO
~~START~~
int
1
~~END~~

SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END'this is a $.^ test'
GO
~~START~~
int
1
~~END~~

SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END"this is a $.^ test"
GO
~~START~~
int
1
~~END~~

select * from babel_4863_func1();
GO
~~START~~
int#!#int#!#int
1#!#1#!#1
~~END~~

exec babel_4863_proc 1;
GO
~~START~~
int#!#int#!#int
1#!#1#!#1
~~END~~

exec babel_4863_proc 2;
GO
~~START~~
int#!#int#!#int
0#!#0#!#0
~~END~~

SELECT * FROM babel_4863_view;
GO
~~START~~
int#!#int#!#int
1#!#1#!#1
~~END~~

Loading

0 comments on commit 0efbb15

Please sign in to comment.