Skip to content

Commit

Permalink
[extension] Add missing commits in 2_6 (#1891)
Browse files Browse the repository at this point in the history
* Add null check in exec_eval_datum for table types (#1778)

In cases of nested TVF, exec_eval_datum was returning an incorrect value of isnull for actual null values. We match the check in bbf_table_var_lookup to get the correct value.

Task: BABEL-4337
Signed-off-by: Tim Chang <[email protected]>
(cherry picked from commit 4c5e722)

* Update psqlodbc driver used in github action (#1863)

* Update psqlodbc driver used in github action

Signed-off-by: Alex Kasko <[email protected]>
(cherry picked from commit 2cb3d95)

* Drop Table Variables even when AbortCurTransaction is true (#1737)

When there is an error, AbortCurTransaction could be set to true.
During clean up phase, logic tries to drop table variables and as part of the drop,
it acquires exclusive lock on the relation which eventually calls
AssignTransactionId. This is not allowed in AssignTransactionId and error
handling is not able to recover from there.

It has been decided to temporarily set AbortCurTransaction=false in
clean up phase in order for drop to continue.

Task: BABEL-4336
Signed-off-by: Kristian Lejao <[email protected]>
(cherry picked from commit c365072)

---------

Co-authored-by: Tim Chang <[email protected]>
Co-authored-by: staticlibs <[email protected]>
  • Loading branch information
3 people authored Oct 9, 2023
1 parent 6476882 commit 5b9af03
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 66 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/odbc-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ jobs:
if: steps.install-extensions.outcome == 'success' && steps.install-unix-odbc-driver.outcome=='success'
run: |
cd ~
wget https://ftp.postgresql.org/pub/odbc/versions/src/psqlodbc-12.01.0000.tar.gz
tar -zxvf psqlodbc-12.01.0000.tar.gz
cd psqlodbc-12.01.0000
wget https://ftp.postgresql.org/pub/odbc/versions/src/psqlodbc-16.00.0000.tar.gz
tar -zxvf psqlodbc-16.00.0000.tar.gz
cd psqlodbc-16.00.0000
./configure
sudo make
sudo make install
echo '[ODBC_Driver_12_for_PostgreSQL]' | sudo tee -a /etc/odbcinst.ini > /dev/null
echo 'Description=ODBC Driver 12 for PostgreSQL Server' | sudo tee -a /etc/odbcinst.ini > /dev/null
echo '[ODBC_Driver_16_for_PostgreSQL]' | sudo tee -a /etc/odbcinst.ini > /dev/null
echo 'Description=ODBC Driver 16 for PostgreSQL Server' | sudo tee -a /etc/odbcinst.ini > /dev/null
echo 'Driver=/usr/local/lib/psqlodbcw.so' | sudo tee -a /etc/odbcinst.ini > /dev/null
echo 'UsageCount=1' | sudo tee -a /etc/odbcinst.ini > /dev/null
Expand All @@ -81,7 +81,7 @@ jobs:
MSSQL_BABEL_DB_USER=jdbc_user \
MSSQL_BABEL_DB_PASSWORD=12345678 \
MSSQL_BABEL_DB_NAME=master \
PSQL_ODBC_DRIVER_NAME=ODBC_Driver_12_for_PostgreSQL \
PSQL_ODBC_DRIVER_NAME=ODBC_Driver_16_for_PostgreSQL \
PSQL_BABEL_DB_SERVER=localhost \
PSQL_BABEL_DB_PORT=5432 \
PSQL_BABEL_DB_USER=jdbc_user \
Expand Down
16 changes: 15 additions & 1 deletion contrib/babelfishpg_tsql/src/pl_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -6939,7 +6939,7 @@ exec_eval_datum(PLtsql_execstate *estate,
*typeid = tbl->tbltypeid;
*typetypmod = -1;
*value = CStringGetDatum(tbl->tblname);
*isnull = false;
*isnull = !tbl->tblname ? true : false;
break;
}

Expand Down Expand Up @@ -10097,9 +10097,17 @@ pltsql_clean_table_variables(PLtsql_execstate *estate, PLtsql_function *func)
bool old_pltsql_explain_only = pltsql_explain_only;
const char *query_fmt = "DROP TABLE %s";
const char *query;
bool old_abort_curr_txn = AbortCurTransaction;

PG_TRY();
{
/*
* Temporarily set this to false to allow DROP to continue.
* Othewise, DROP would not be allowed to acquire xlock on the
* relation.
*/
AbortCurTransaction = false;

foreach(lc, func->table_varnos)
{
n = lfirst_int(lc);
Expand All @@ -10126,9 +10134,15 @@ pltsql_clean_table_variables(PLtsql_execstate *estate, PLtsql_function *func)
append_explain_info(NULL, query);
}
}

Assert(!AbortCurTransaction); /* engine should not change this value */
AbortCurTransaction = old_abort_curr_txn;
}
PG_CATCH();
{
Assert(!AbortCurTransaction); /* engine should not change this value */
AbortCurTransaction = old_abort_curr_txn;

pltsql_explain_only = old_pltsql_explain_only; /* Recover EXPLAIN ONLY
* mode */
PG_RE_THROW();
Expand Down
10 changes: 10 additions & 0 deletions test/JDBC/expected/table-variable-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ drop schema table_variable_vu_schema
go
drop function table_variable_vu_func2
go

-- BABEL-4337 - nested tv
DROP FUNCTION tv_nested_func2
GO

DROP FUNCTION tv_nested_func1
GO

DROP TYPE tv_nested_type
GO
8 changes: 8 additions & 0 deletions test/JDBC/expected/table-variable-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,11 @@ BEGIN
RETURN
END
go

-- BABEL-4337 - nested TV, null check in tblname
CREATE TYPE tv_nested_type AS TABLE (a INT)
GO
CREATE FUNCTION tv_nested_func1 (@t tv_nested_type readonly) RETURNS @a TABLE (y INT) AS BEGIN; INSERT INTO @a SELECT x FROM @t; RETURN; END;
GO
CREATE FUNCTION tv_nested_func2 (@t tv_nested_type readonly) RETURNS @a TABLE (x INT) AS BEGIN; INSERT INTO @a SELECT y FROM tv_nested_func1(@t); RETURN; END;
GO
10 changes: 10 additions & 0 deletions test/JDBC/expected/table-variable-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,13 @@ bit
1
~~END~~


-- BABEL-4337 - check nested TV for null; should not crash but throw an error
SELECT * FROM tv_nested_func2(NULL)
go
~~START~~
int
~~ERROR (Code: 33557097)~~

~~ERROR (Message: table variable underlying typename is NULL. refname: @t)~~

69 changes: 55 additions & 14 deletions test/JDBC/expected/table_variable_xact_errors.out
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,13 @@ GO


-------------------------------------------------------------------------------
-- Cursor not explicitly closed
-- Cursor not explicitly closed
-------------------------------------------------------------------------------
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @v INT
DECLARE CUR_NETWORK CURSOR LOCAL FOR SELECT STATION_INT FROM @STATION_INTS_TABLE
OPEN CUR_NETWORK
FETCH NEXT FROM CUR_NETWORK INTO @v
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @v INT
DECLARE CUR_NETWORK CURSOR LOCAL FOR SELECT STATION_INT FROM @STATION_INTS_TABLE
OPEN CUR_NETWORK
FETCH NEXT FROM CUR_NETWORK INTO @v
GO

select 123
Expand All @@ -476,16 +476,16 @@ int
~~END~~


CREATE FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery] ()
RETURNS NVARCHAR(MAX) AS

CREATE FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery] ()
RETURNS NVARCHAR(MAX) AS
BEGIN
DECLARE @TSQL NVARCHAR(MAX)
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @TSQL NVARCHAR(MAX)
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @STATION_INT INT SET @TSQL = ''
DECLARE CUR_NETWORK CURSOR LOCAL FOR
SELECT STATION_INT FROM @STATION_INTS_TABLE OPEN CUR_NETWORK
DECLARE CUR_NETWORK CURSOR LOCAL FOR
SELECT STATION_INT FROM @STATION_INTS_TABLE OPEN CUR_NETWORK
FETCH NEXT FROM CUR_NETWORK INTO @STATION_INT

RETURN @TSQL
END
GO
Expand Down Expand Up @@ -534,7 +534,7 @@ nvarchar
~~ERROR (Message: Throw error)~~


DROP FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery]
DROP FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery]
GO


Expand Down Expand Up @@ -569,3 +569,44 @@ nvarchar
DROP FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery]
GO


-------------------------------------------------------------------------------
-- BABEL-4267: Error should not cause crash
-------------------------------------------------------------------------------
CREATE PROCEDURE usp_PopulateDiscount
AS
DECLARE @Lookup TABLE (StartDate DATETIME NOT NULL)
INSERT INTO @Lookup SELECT GETDATE()
BEGIN TRANSACTION
DELETE trgt FROM Discount trgt -- Discount does not exist
COMMIT
go

EXECUTE usp_PopulateDiscount
go
~~ROW COUNT: 1~~

~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "discount" does not exist)~~


CREATE PROCEDURE test
AS
BEGIN TRY
DECLARE @tv1 TABLE(c1 INT PRIMARY KEY, b INT IDENTITY, c CHAR(15) DEFAULT 'Whoops!')
SELECT 1/0
END TRY
BEGIN CATCH
BEGIN TRANSACTION
INSERT INTO @tv1 VALUES(1, 3, 'Three') -- invalid syntax, should do a clean shutdown
COMMIT
END CATCH;
GO

exec test
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: INSERT has more expressions than target columns)~~

Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,13 @@ GO


-------------------------------------------------------------------------------
-- Cursor not explicitly closed
-- Cursor not explicitly closed
-------------------------------------------------------------------------------
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @v INT
DECLARE CUR_NETWORK CURSOR LOCAL FOR SELECT STATION_INT FROM @STATION_INTS_TABLE
OPEN CUR_NETWORK
FETCH NEXT FROM CUR_NETWORK INTO @v
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @v INT
DECLARE CUR_NETWORK CURSOR LOCAL FOR SELECT STATION_INT FROM @STATION_INTS_TABLE
OPEN CUR_NETWORK
FETCH NEXT FROM CUR_NETWORK INTO @v
GO

select 123
Expand All @@ -478,16 +478,16 @@ int
~~END~~


CREATE FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery] ()
RETURNS NVARCHAR(MAX) AS

CREATE FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery] ()
RETURNS NVARCHAR(MAX) AS
BEGIN
DECLARE @TSQL NVARCHAR(MAX)
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @TSQL NVARCHAR(MAX)
DECLARE @STATION_INTS_TABLE TABLE (STATION_INT INT)
DECLARE @STATION_INT INT SET @TSQL = ''
DECLARE CUR_NETWORK CURSOR LOCAL FOR
SELECT STATION_INT FROM @STATION_INTS_TABLE OPEN CUR_NETWORK
DECLARE CUR_NETWORK CURSOR LOCAL FOR
SELECT STATION_INT FROM @STATION_INTS_TABLE OPEN CUR_NETWORK
FETCH NEXT FROM CUR_NETWORK INTO @STATION_INT

RETURN @TSQL
END
GO
Expand Down Expand Up @@ -536,7 +536,7 @@ nvarchar
~~ERROR (Message: Throw error)~~


DROP FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery]
DROP FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery]
GO


Expand Down Expand Up @@ -571,3 +571,52 @@ nvarchar
DROP FUNCTION [dbo].[WOSQL_BuildRevenueDetailOLUQuery]
GO


-------------------------------------------------------------------------------
-- BABEL-4267: Error should not cause crash
-------------------------------------------------------------------------------
CREATE PROCEDURE usp_PopulateDiscount
AS
DECLARE @Lookup TABLE (StartDate DATETIME NOT NULL)
INSERT INTO @Lookup SELECT GETDATE()
BEGIN TRANSACTION
DELETE trgt FROM Discount trgt -- Discount does not exist
COMMIT
go
~~ERROR (Code: 2714)~~

~~ERROR (Message: function "usp_populatediscount" already exists with same argument types)~~


EXECUTE usp_PopulateDiscount
go
~~ROW COUNT: 1~~

~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "discount" does not exist)~~


CREATE PROCEDURE test
AS
BEGIN TRY
DECLARE @tv1 TABLE(c1 INT PRIMARY KEY, b INT IDENTITY, c CHAR(15) DEFAULT 'Whoops!')
SELECT 1/0
END TRY
BEGIN CATCH
BEGIN TRANSACTION
INSERT INTO @tv1 VALUES(1, 3, 'Three') -- invalid syntax, should do a clean shutdown
COMMIT
END CATCH;
GO
~~ERROR (Code: 2714)~~

~~ERROR (Message: function "test" already exists with same argument types)~~


exec test
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: INSERT has more expressions than target columns)~~

Loading

0 comments on commit 5b9af03

Please sign in to comment.