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 syntax error issue while using [primary] column name in DML/DDL statements #3132

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
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/antlr/TSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -4831,6 +4831,7 @@ keyword
| PREDICATE
| PREDICT
| PRIMARY_ROLE
| PRIMARY_SQBRACKET
| PRIOR
| PRIORITY
| PRIORITY_LEVEL
Expand Down
205 changes: 205 additions & 0 deletions test/JDBC/expected/BABEL-5422.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
-- Create tables
CREATE TABLE babel_5422_table (
[primary] INT PRIMARY KEY,
Name VARCHAR(50)
);
GO

CREATE TABLE babel_5422_table2 (
[PRIMARY] INT,
Age INT
);
GO

CREATE TABLE #babel_5422_table3 (
[primary_column] INT,
Age INT
);
GO

-- Insert sample data
INSERT INTO babel_5422_table ([primary], Name)
VALUES (1, 'John'), (2, 'Jane'), (3, 'Bob');
GO
~~ROW COUNT: 3~~


INSERT INTO babel_5422_table2 ([primary], Age)
VALUES (1, 25), (3, 30);
GO
~~ROW COUNT: 2~~


INSERT INTO #babel_5422_table3 ([primary_column], Age)
VALUES (1, 30), (3, 40);
GO
~~ROW COUNT: 2~~


-- Aliasing
SELECT [primary] AS PrimaryKey, Name
FROM babel_5422_table;
GO
~~START~~
int#!#varchar
1#!#John
2#!#Jane
3#!#Bob
~~END~~


SELECT [primary_column] AS [Primary], Age
FROM #babel_5422_table3;
GO
~~START~~
int#!#int
1#!#30
3#!#40
~~END~~


-- Joining Tables
SELECT t1.[primary], t1.Name, t2.Age
FROM babel_5422_table t1
JOIN babel_5422_table2 t2 ON t1.[primary] = t2.[primary];
GO
~~START~~
int#!#varchar#!#int
1#!#John#!#25
3#!#Bob#!#30
~~END~~


-- Aggregation
SELECT COUNT([primary]) AS TotalRows
FROM babel_5422_table;
GO
~~START~~
int
3
~~END~~


-- Ordering
SELECT *
FROM babel_5422_table
ORDER BY [primary] DESC;
GO
~~START~~
int#!#varchar
3#!#Bob
2#!#Jane
1#!#John
~~END~~


-- Checking for Existence
IF EXISTS (SELECT 1 FROM babel_5422_table WHERE [primary] = 3)
PRINT 'Row with [primary] = 3 exists';
ELSE
PRINT 'Row with [primary] = 3 does not exist';
GO

-- Using in a CASE Statement
SELECT [primary],
CASE
WHEN [primary] = 1 THEN 'One'
WHEN [primary] = 2 THEN 'Two'
ELSE 'Other'
END AS PrimaryDescription
FROM babel_5422_table;
GO
~~START~~
int#!#varchar
1#!#One
2#!#Two
3#!#Other
~~END~~


-- Using in a Subquery
SELECT Name
FROM babel_5422_table
WHERE [primary] IN (SELECT [primary] FROM babel_5422_table2);
GO
~~START~~
varchar
John
Bob
~~END~~


-- Using in a View
CREATE VIEW babel_5422_view
AS
SELECT [primary], Name
FROM babel_5422_table;
GO

SELECT * FROM babel_5422_view;
GO
~~START~~
int#!#varchar
1#!#John
2#!#Jane
3#!#Bob
~~END~~


-- Using in a Stored Procedure
CREATE PROCEDURE babel_5422_proc
@PrimaryKey INT
AS
BEGIN
SELECT [primary], Name
FROM babel_5422_table
WHERE [primary] = @PrimaryKey;
END
GO

EXEC babel_5422_proc 2;
GO
~~START~~
int#!#varchar
2#!#Jane
~~END~~


-- Using in a User-Defined Function
CREATE FUNCTION babel_5422_func
(@PrimaryKey INT)
RETURNS TABLE
AS
RETURN(
SELECT [primary], Name
FROM babel_5422_table
WHERE [primary] = @PrimaryKey
)
GO

SELECT babel_5422_func(3);
GO
~~START~~
varchar
(3,Bob)
~~END~~


-- Cleanup
DROP FUNCTION babel_5422_func;
GO

DROP PROCEDURE babel_5422_proc;
GO

DROP VIEW babel_5422_view;
GO

DROP TABLE #babel_5422_table3
GO

DROP TABLE babel_5422_table2;
GO

DROP TABLE babel_5422_table;
GO
138 changes: 138 additions & 0 deletions test/JDBC/input/BABEL-5422.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
-- Create tables
CREATE TABLE babel_5422_table (
[primary] INT PRIMARY KEY,
Name VARCHAR(50)
);
GO

CREATE TABLE babel_5422_table2 (
[PRIMARY] INT,
Age INT
);
GO

CREATE TABLE #babel_5422_table3 (
[primary_column] INT,
Age INT
);
GO

-- Insert sample data
INSERT INTO babel_5422_table ([primary], Name)
VALUES (1, 'John'), (2, 'Jane'), (3, 'Bob');
GO

INSERT INTO babel_5422_table2 ([primary], Age)
VALUES (1, 25), (3, 30);
GO

INSERT INTO #babel_5422_table3 ([primary_column], Age)
VALUES (1, 30), (3, 40);
GO

-- Aliasing
SELECT [primary] AS PrimaryKey, Name
FROM babel_5422_table;
GO

SELECT [primary_column] AS [Primary], Age
FROM #babel_5422_table3;
GO

-- Joining Tables
SELECT t1.[primary], t1.Name, t2.Age
FROM babel_5422_table t1
JOIN babel_5422_table2 t2 ON t1.[primary] = t2.[primary];
GO

-- Aggregation
SELECT COUNT([primary]) AS TotalRows
FROM babel_5422_table;
GO

-- Ordering
SELECT *
FROM babel_5422_table
ORDER BY [primary] DESC;
GO

-- Checking for Existence
IF EXISTS (SELECT 1 FROM babel_5422_table WHERE [primary] = 3)
PRINT 'Row with [primary] = 3 exists';
ELSE
PRINT 'Row with [primary] = 3 does not exist';
GO

-- Using in a CASE Statement
SELECT [primary],
CASE
WHEN [primary] = 1 THEN 'One'
WHEN [primary] = 2 THEN 'Two'
ELSE 'Other'
END AS PrimaryDescription
FROM babel_5422_table;
GO

-- Using in a Subquery
SELECT Name
FROM babel_5422_table
WHERE [primary] IN (SELECT [primary] FROM babel_5422_table2);
GO

-- Using in a View
CREATE VIEW babel_5422_view
AS
SELECT [primary], Name
FROM babel_5422_table;
GO

SELECT * FROM babel_5422_view;
GO

-- Using in a Stored Procedure
CREATE PROCEDURE babel_5422_proc
@PrimaryKey INT
AS
BEGIN
SELECT [primary], Name
FROM babel_5422_table
WHERE [primary] = @PrimaryKey;
END
GO

EXEC babel_5422_proc 2;
GO

-- Using in a User-Defined Function
CREATE FUNCTION babel_5422_func
(@PrimaryKey INT)
RETURNS TABLE
AS
RETURN(
SELECT [primary], Name
FROM babel_5422_table
WHERE [primary] = @PrimaryKey
)
GO

SELECT babel_5422_func(3);
GO

-- Cleanup
DROP FUNCTION babel_5422_func;
GO

DROP PROCEDURE babel_5422_proc;
GO

DROP VIEW babel_5422_view;
GO

DROP TABLE #babel_5422_table3
GO

DROP TABLE babel_5422_table2;
GO

DROP TABLE babel_5422_table;
GO
Loading