Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Tanzeel Khan <[email protected]>
  • Loading branch information
tanscorpio7 committed Jan 2, 2024
1 parent 80a07d3 commit e833135
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 12 deletions.
1 change: 1 addition & 0 deletions contrib/babelfishpg_tds/error_mapping.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,4 @@ XX000 ERRCODE_INTERNAL_ERROR "The table-valued parameter \"%s\" must be declared
22023 ERRCODE_INVALID_PARAMETER_VALUE "\'%s\' is not a recognized %s option" SQL_ERROR_155 15
22023 ERRCODE_INVALID_PARAMETER_VALUE "The datepart %s is not supported by date function %s for data type %s." SQL_ERROR_9810 16
22008 ERRCODE_DATETIME_VALUE_OUT_OF_RANGE "Adding a value to a \'%s\' column caused an overflow." SQL_ERROR_517 16
34000 ERRCODE_UNDEFINED_CURSOR "cursor \"%s\" does not exist" SQL_ERROR_16916 16
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/err_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ is_ignorable_error(int pg_error_code, uint8_t override_flag)
case SQL_ERROR_9810:
case SQL_ERROR_535:
case SQL_ERROR_15003:
case SQL_ERROR_16916:
{
elog(DEBUG1, "TSQL TXN is_ignorable_error %d", latest_error_code);
return true;
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/err_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,4 @@ uint8_t override_txn_behaviour(PLtsql_stmt *stmt);
#define SQL_ERROR_16950 16950
#define SQL_ERROR_18456 18456
#define SQL_ERROR_15003 15003
#define SQL_ERROR_16916 16916
15 changes: 12 additions & 3 deletions contrib/babelfishpg_tsql/src/iterative_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ dispatch_stmt(PLtsql_execstate *estate, PLtsql_stmt *stmt)
exec_stmt_open(estate, (PLtsql_stmt_open *) stmt);
break;
case PLTSQL_STMT_FETCH:
((PLtsql_stmt_fetch *) stmt)->cursor_portal_name = NULL;
if (pltsql_explain_only)
{
ereport(ERROR,
Expand Down Expand Up @@ -1288,6 +1289,17 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
if (TransactionIdIsValid(GetCurrentTransactionIdIfAny()))
add_failed_transaction(GetCurrentTransactionIdIfAny());

/* Incase of failed cursor fetch Unpin Curosr Portal */
if (stmt->cmd_type == PLTSQL_STMT_FETCH &&
((PLtsql_stmt_fetch *) stmt)->cursor_portal_name != NULL)
{
Portal portal;
portal = SPI_cursor_find(((PLtsql_stmt_fetch *) stmt)->cursor_portal_name);

if (portal != NULL && portal->status == PORTAL_FAILED)
UnpinPortal(portal);
}

/*
* Non TDS clients will just throw the error in all cases
*/
Expand Down Expand Up @@ -1365,7 +1377,6 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
*/
HOLD_INTERRUPTS();
elog(DEBUG1, "TSQL TXN TSQL semantics : Rollback current transaction");
AtCleanup_Portals();
/* Hold portals to make sure that cursors work */
HoldPinnedPortals();
AbortCurrentTransaction();
Expand All @@ -1381,7 +1392,6 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
*/
HOLD_INTERRUPTS();
elog(DEBUG1, "TSQL TXN TSQL semantics : Rollback internal transaction");
AtCleanup_Portals();
HoldPinnedPortals();
pltsql_rollback_txn();
estate->tsql_trigger_flags &= ~TSQL_TRAN_STARTED;
Expand All @@ -1403,7 +1413,6 @@ dispatch_stmt_handle_error(PLtsql_execstate *estate,
{
HOLD_INTERRUPTS();
elog(DEBUG1, "TSQL TXN TSQL semantics : Rollback implicit transaction");
AtCleanup_Portals();
pltsql_rollback_txn();
MemoryContextSwitchTo(cur_ctxt);
RESUME_INTERRUPTS();
Expand Down
2 changes: 2 additions & 0 deletions contrib/babelfishpg_tsql/src/pl_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -6102,6 +6102,8 @@ exec_stmt_fetch(PLtsql_execstate *estate, PLtsql_stmt_fetch *stmt)
(errcode(ERRCODE_UNDEFINED_CURSOR),
errmsg("cursor \"%s\" does not exist", pltsql_demangle_curname(curname))));

stmt->cursor_portal_name = curname;

/* Calculate position for FETCH_RELATIVE or FETCH_ABSOLUTE */
if (stmt->expr)
{
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/pltsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ typedef struct PLtsql_stmt_fetch
PLtsql_expr *expr; /* count, if expression */
bool is_move; /* is this a fetch or move? */
bool returns_multiple_rows; /* can return more than one row? */
char *cursor_portal_name;
} PLtsql_stmt_fetch;

/*
Expand Down
42 changes: 36 additions & 6 deletions test/JDBC/expected/BABEL_4585.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@ GO
~~ROW COUNT: 2~~


SELECT DATEDIFF(Year, GetDate(), id) FROM babel_4585_t1
SELECT DATEDIFF(Year, CAST('2024-01-01' AS DATETIME), id) FROM babel_4585_t1
GO
~~START~~
int
-23
-24
~~ERROR (Code: 517)~~

~~ERROR (Message: data out of range for datetime)~~


DECLARE cursor_4585_1 CURSOR FOR SELECT DATEDIFF(Year, GetDate(), id) FROM babel_4585_t1;
DECLARE cursor_4585_1 CURSOR FOR SELECT DATEDIFF(Year, CAST('2024-01-01' AS DATETIME), id) FROM babel_4585_t1
OPEN cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
CLOSE cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
CLOSE cursor_4585_1;
OPEN cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
CLOSE cursor_4585_1;
DEALLOCATE cursor_4585_1;
GO
~~START~~
int
-23
-24
~~END~~

~~START~~
Expand All @@ -31,7 +38,30 @@ int

~~ERROR (Message: data out of range for datetime)~~

~~ERROR (Code: 33557097)~~
~~ERROR (Code: 16916)~~

~~ERROR (Message: cursor "cursor_4585_1" does not exist)~~

~~ERROR (Code: 16916)~~

~~ERROR (Message: cursor "cursor_4585_1" does not exist)~~

~~START~~
int
-24
~~END~~

~~START~~
int
~~ERROR (Code: 517)~~

~~ERROR (Message: data out of range for datetime)~~

~~ERROR (Code: 16916)~~

~~ERROR (Message: cursor "cursor_4585_1" does not exist)~~

~~ERROR (Code: 16916)~~

~~ERROR (Message: cursor "cursor_4585_1" does not exist)~~

Expand Down
13 changes: 10 additions & 3 deletions test/JDBC/input/BABEL_4585.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ CREATE TABLE babel_4585_t1 (id VARCHAR(10));
INSERT INTO babel_4585_t1 VALUES ('2000-12-12'), ('1000-01-01');
GO

SELECT DATEDIFF(Year, GetDate(), id) FROM babel_4585_t1
SELECT DATEDIFF(Year, CAST('2024-01-01' AS DATETIME), id) FROM babel_4585_t1
GO

DECLARE cursor_4585_1 CURSOR FOR SELECT DATEDIFF(Year, GetDate(), id) FROM babel_4585_t1;
DECLARE cursor_4585_1 CURSOR FOR SELECT DATEDIFF(Year, CAST('2024-01-01' AS DATETIME), id) FROM babel_4585_t1
OPEN cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
CLOSE cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
CLOSE cursor_4585_1;
OPEN cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
FETCH NEXT FROM cursor_4585_1;
CLOSE cursor_4585_1;
DEALLOCATE cursor_4585_1;
GO

CREATE TABLE babel_4585_2 (id VARBINARY(10));
Expand Down

0 comments on commit e833135

Please sign in to comment.