Skip to content

Commit

Permalink
Merge branch 'operator_whitespace' of https://github.com/robverschoor…
Browse files Browse the repository at this point in the history
…/babelfish_extensions into operator_whitespace
  • Loading branch information
robverschoor committed Jan 5, 2024
2 parents c8c83aa + bf7d22d commit 370b87a
Show file tree
Hide file tree
Showing 54 changed files with 2,697 additions and 31 deletions.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/antlr/TSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ DECIMAL: DEC_DIGIT+;
ID: ( [_#] | LETTER) ( [_#$@0-9] | LETTER)*;
BINARY: '0' [Xx] ( HEX_DIGIT | '\\' [\r]? [\n] )*;
FLOAT: DEC_DOT_DEC;
REAL: (DECIMAL | DEC_DOT_DEC) ([Ee] ([+-]? DEC_DIGIT+)?);
REAL: (DECIMAL | DEC_DOT_DEC) ([Ee] ([+-]? DEC_DIGIT*)?);

MONEY: CURRENCY_SYMBOL [ ]* ('+'|'-')? (DECIMAL | DEC_DOT_DEC);

Expand Down
61 changes: 61 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ static void pltsql_ExecutorStart(QueryDesc *queryDesc, int eflags);
static void pltsql_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once);
static void pltsql_ExecutorFinish(QueryDesc *queryDesc);
static void pltsql_ExecutorEnd(QueryDesc *queryDesc);
static bool pltsql_bbfViewHasInsteadofTrigger(Relation view, CmdType event);

static bool plsql_TriggerRecursiveCheck(ResultRelInfo *resultRelInfo);
static bool bbf_check_rowcount_hook(int es_processed);
Expand Down Expand Up @@ -210,6 +211,7 @@ static ExecutorFinish_hook_type prev_ExecutorFinish = NULL;
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
static GetNewObjectId_hook_type prev_GetNewObjectId_hook = NULL;
static inherit_view_constraints_from_table_hook_type prev_inherit_view_constraints_from_table = NULL;
static bbfViewHasInsteadofTrigger_hook_type prev_bbfViewHasInsteadofTrigger_hook = NULL;
static detect_numeric_overflow_hook_type prev_detect_numeric_overflow_hook = NULL;
static match_pltsql_func_call_hook_type prev_match_pltsql_func_call_hook = NULL;
static insert_pltsql_function_defaults_hook_type prev_insert_pltsql_function_defaults_hook = NULL;
Expand Down Expand Up @@ -325,6 +327,9 @@ InstallExtendedHooks(void)
inherit_view_constraints_from_table_hook = preserve_view_constraints_from_base_table;
TriggerRecuresiveCheck_hook = plsql_TriggerRecursiveCheck;

prev_bbfViewHasInsteadofTrigger_hook = bbfViewHasInsteadofTrigger_hook;
bbfViewHasInsteadofTrigger_hook = pltsql_bbfViewHasInsteadofTrigger;

prev_detect_numeric_overflow_hook = detect_numeric_overflow_hook;
detect_numeric_overflow_hook = pltsql_detect_numeric_overflow;

Expand Down Expand Up @@ -440,6 +445,7 @@ UninstallExtendedHooks(void)
ExecutorEnd_hook = prev_ExecutorEnd;
GetNewObjectId_hook = prev_GetNewObjectId_hook;
inherit_view_constraints_from_table_hook = prev_inherit_view_constraints_from_table;
bbfViewHasInsteadofTrigger_hook = prev_bbfViewHasInsteadofTrigger_hook;
detect_numeric_overflow_hook = prev_detect_numeric_overflow_hook;
match_pltsql_func_call_hook = prev_match_pltsql_func_call_hook;
insert_pltsql_function_defaults_hook = prev_insert_pltsql_function_defaults_hook;
Expand Down Expand Up @@ -719,6 +725,61 @@ plsql_TriggerRecursiveCheck(ResultRelInfo *resultRelInfo)
return false;
}

/**
* Hook function to skip rewriting VIEW with base table if the VIEW has an instead of trigger
* Checks if view have an INSTEAD OF trigger at statement level
* If it does, we don't want to treat it as auto-updatable.
* This function also does error checking for recursive triggers
* Reference - src/backend/rewrite/rewriteHandler.c view_has_instead_trigger
*/
static bool
pltsql_bbfViewHasInsteadofTrigger(Relation view, CmdType event)
{
TriggerDesc *trigDesc = view->trigdesc;
if (trigDesc && triggerInvocationSequence)
{
int i;
for (i = 0; i < trigDesc->numtriggers; i++)
{
Trigger *trigger = &trigDesc->triggers[i];
Oid current_tgoid = trigger->tgoid;
Oid prev_tgoid = InvalidOid;
prev_tgoid = lfirst_oid(list_tail(triggerInvocationSequence));
if (prev_tgoid == current_tgoid)
{
return false; /** Loop trigger call by itself*/
}
else if (list_length(triggerInvocationSequence) > TRIGGER_MAX_NEST_LEVEL || list_member_oid(triggerInvocationSequence, current_tgoid))
{
/** Loop trigger call by another trigger */
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("Maximum stored procedure, function, trigger, or view nesting level exceeded (limit %d)", TRIGGER_MAX_NEST_LEVEL)));
}
}
}

switch (event)
{
case CMD_INSERT:
if (trigDesc && trigDesc->trig_insert_instead_statement)
return true;
break;
case CMD_UPDATE:
if (trigDesc && trigDesc->trig_update_instead_statement)
return true;
break;
case CMD_DELETE:
if (trigDesc && trigDesc->trig_delete_instead_statement)
return true;
break;
default:
elog(ERROR, "unrecognized CmdType: %d", (int)event);
break;
}
return false;
}

/*
* Wrapper function that calls the initilization function.
* Calls the pre function call hook on the procname
Expand Down
23 changes: 18 additions & 5 deletions contrib/babelfishpg_tsql/src/pl_exec-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,22 @@ exec_stmt_print(PLtsql_execstate *estate, PLtsql_stmt_print *stmt)
&formattypmod);

if (formatisnull)
extval = "<NULL>";
{
// Printing NULL prints a single space in T-SQL
extval = " ";
}
else
{
extval = convert_value_to_string(estate,
formatdatum,
formattypeid);
}

if (strlen(extval) == 0)
{
// Printing an empty string prints a single space in T-SQL
extval = " ";
}

ereport(INFO, errmsg_internal("%s", extval));

Expand Down Expand Up @@ -928,7 +939,7 @@ exec_stmt_exec(PLtsql_execstate *estate, PLtsql_stmt_exec *stmt)

stmt->is_scalar_func = is_scalar_func;

/* T-SQL doens't allow call prcedure in function */
/* T-SQL doesn't allow call procedure in function */
if (estate->func && estate->func->fn_oid != InvalidOid && estate->func->fn_prokind == PROKIND_FUNCTION && estate->func->fn_is_trigger == PLTSQL_NOT_TRIGGER /* check EXEC is running
* in the body of
* function */
Expand Down Expand Up @@ -2127,11 +2138,13 @@ exec_stmt_exec_sp(PLtsql_execstate *estate, PLtsql_stmt_exec_sp *stmt)
int save_nestlevel;
int scope_level;
InlineCodeBlockArgs *args = NULL;

batch = exec_eval_expr(estate, stmt->query, &isnull, &restype, &restypmod);
if (isnull)
ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("batch string argument of sp_executesql is null")));
{
// When called with a NULL argument, sp_executesql should take no action at all
break;
}

batchstr = convert_value_to_string(estate, batch, restype);

Expand Down
2 changes: 2 additions & 0 deletions contrib/babelfishpg_tsql/src/pltsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
/* Max number of Args allowed for Prepared stmts. */
#define PREPARE_STMT_MAX_ARGS 2100

#define TRIGGER_MAX_NEST_LEVEL 32 /* Maximum allowed trigger nesting level*/

/*
* Compiler's namespace item types
*/
Expand Down
Loading

0 comments on commit 370b87a

Please sign in to comment.