Skip to content

Commit

Permalink
Redoing unquoted string handling (#1900)
Browse files Browse the repository at this point in the history
Re-implements unquoted string handling, as the previous fix (babelfish-for-postgresql/postgresql_modified_for_babelfish#206 , #1821) did not address all use cases, notably strings longer than 63 characters; also all unquoted strings were downcased. 
This fix rolls back the previous code changes in the backend and implements the the unquoted string handling fully in the Babelfish extension (`tsqlIface.cpp`), restructuring various parts of ANTLR tree rewriting in the process, and adding many test cases.

Engine PR: babelfish-for-postgresql/postgresql_modified_for_babelfish#235
 
Issues Resolved:
BABEL-334 Support unquoted string parameter values in procedure calls/declarations
BABEL-2883 Error raised when invoking sproc w string parameter using argument in square brackets
BABEL-4452 Incorrect results with long unquoted string parameter values in procedure calls/declarations

Signed-off-by: Rob Verschoor [[email protected]](mailto:[email protected])
  • Loading branch information
robverschoor authored Oct 18, 2023
1 parent 817b371 commit d41258d
Show file tree
Hide file tree
Showing 16 changed files with 2,173 additions and 801 deletions.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/sql/sys_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4050,7 +4050,7 @@ BEGIN
column_length := 256;
ELSIF column_data_type IS NULL THEN

-- Check if it's a user-defined data type
-- Check if it ia user-defined data type
SELECT sys.translate_pg_type_to_tsql(typbasetype), typlen, typtypmod
INTO column_data_type, typelen, typemod
FROM pg_type
Expand Down
137 changes: 0 additions & 137 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ static bool pltsql_bbfViewHasInsteadofTrigger(Relation view, CmdType event);
static bool plsql_TriggerRecursiveCheck(ResultRelInfo *resultRelInfo);
static bool bbf_check_rowcount_hook(int es_processed);

static void declare_parameter_unquoted_string(Node *paramDft, ObjectType objtype);
static void declare_parameter_unquoted_string_reset(Node *paramDft);

static Node* call_argument_unquoted_string(Node *arg);
static void call_argument_unquoted_string_reset(Node *colref_arg);
static char *get_local_schema_for_bbf_functions(Oid proc_nsp_oid);

/*****************************************
Expand Down Expand Up @@ -214,10 +209,6 @@ static validate_var_datatype_scale_hook_type prev_validate_var_datatype_scale_ho
static modify_RangeTblFunction_tupdesc_hook_type prev_modify_RangeTblFunction_tupdesc_hook = NULL;
static fill_missing_values_in_copyfrom_hook_type prev_fill_missing_values_in_copyfrom_hook = NULL;
static check_rowcount_hook_type prev_check_rowcount_hook = NULL;
static declare_parameter_unquoted_string_hook_type prev_declare_parameter_unquoted_string_hook = NULL;
static declare_parameter_unquoted_string_reset_hook_type prev_declare_parameter_unquoted_string_reset_hook = NULL;
static call_argument_unquoted_string_hook_type prev_call_argument_unquoted_string_hook = NULL;
static call_argument_unquoted_string_reset_hook_type prev_call_argument_unquoted_string_reset_hook = NULL;
static bbfCustomProcessUtility_hook_type prev_bbfCustomProcessUtility_hook = NULL;
static bbfSelectIntoUtility_hook_type prev_bbfSelectIntoUtility_hook = NULL;
static bbfSelectIntoAddIdentity_hook_type prev_bbfSelectIntoAddIdentity_hook = NULL;
Expand Down Expand Up @@ -349,16 +340,6 @@ InstallExtendedHooks(void)
prev_check_rowcount_hook = check_rowcount_hook;
check_rowcount_hook = bbf_check_rowcount_hook;

prev_declare_parameter_unquoted_string_hook = declare_parameter_unquoted_string_hook;
declare_parameter_unquoted_string_hook = declare_parameter_unquoted_string;
prev_declare_parameter_unquoted_string_reset_hook = declare_parameter_unquoted_string_reset_hook;
declare_parameter_unquoted_string_reset_hook = declare_parameter_unquoted_string_reset;

prev_call_argument_unquoted_string_hook = call_argument_unquoted_string_hook;
call_argument_unquoted_string_hook = call_argument_unquoted_string;
prev_call_argument_unquoted_string_reset_hook = call_argument_unquoted_string_reset_hook;
call_argument_unquoted_string_reset_hook = call_argument_unquoted_string_reset;

prev_bbfCustomProcessUtility_hook = bbfCustomProcessUtility_hook;
bbfCustomProcessUtility_hook = pltsql_bbfCustomProcessUtility;

Expand Down Expand Up @@ -442,10 +423,6 @@ UninstallExtendedHooks(void)
modify_RangeTblFunction_tupdesc_hook = prev_modify_RangeTblFunction_tupdesc_hook;
fill_missing_values_in_copyfrom_hook = prev_fill_missing_values_in_copyfrom_hook;
check_rowcount_hook = prev_check_rowcount_hook;
declare_parameter_unquoted_string_hook = prev_declare_parameter_unquoted_string_hook;
declare_parameter_unquoted_string_reset_hook = prev_declare_parameter_unquoted_string_reset_hook;
call_argument_unquoted_string_hook = prev_call_argument_unquoted_string_hook;
call_argument_unquoted_string_reset_hook = prev_call_argument_unquoted_string_reset_hook;
bbfCustomProcessUtility_hook = prev_bbfCustomProcessUtility_hook;
bbfSelectIntoUtility_hook = prev_bbfSelectIntoUtility_hook;
bbfSelectIntoAddIdentity_hook = prev_bbfSelectIntoAddIdentity_hook;
Expand Down Expand Up @@ -4149,120 +4126,6 @@ static void pltsql_bbfSelectIntoAddIdentity(IntoClause *into, List *tableElts)
}
}


/*
* Hook functions for handling unquoted string defaults in parameter definitions
* for T-SQL CREATE PROCEDURE/CREATE FUNCTION.
*/
static void declare_parameter_unquoted_string (Node *paramDft, ObjectType objtype)
{
if (sql_dialect == SQL_DIALECT_TSQL &&
(objtype == OBJECT_PROCEDURE || objtype == OBJECT_FUNCTION) &&
nodeTag(paramDft) == T_ColumnRef)
{
/*
* The node could be for a variable, which should not be treated as a
* an unquoted string, so verify it does not start with '@'.
* This will cause parameter defaults with local variables to
* fail rather than to return the local variable name as a string,
* which is identical to Babelfish behaviour before the fix
* for unquoted string parameter.
*/
ColumnRef *colref = (ColumnRef *) paramDft;
Node *colnameField = (Node *) linitial(colref->fields);
char *colname = strVal(colnameField);
if (colname[0] != '@')
{
paramDft->type = T_TSQL_UnquotedString;
}
}
return;
}

static void declare_parameter_unquoted_string_reset (Node *paramDft)
{
/*
* In the case of an unquoted string, restore the original node type
* or we may run into an unknown node type downstream.
*/
if (nodeTag(paramDft) == T_ColumnRef)
{
paramDft->type = T_ColumnRef;
}
return;
}

/*
* Hook functions for handling unquoted string arguments in
* for T-SQL procedure calls.
*/
static Node* call_argument_unquoted_string (Node *arg)
{
/*
* Intercept unquoted string arguments in T-SQL procedure calls.
* These arrive here as nodetype=T_ColumnRef. Temporarily change
* the node type to T_TSQL_UnquotedString, which is picked up and
* handled in transformExprRecurse().
*/
Node *colref_arg = NULL; /* Points to temporarily modified node, if any. */
if (sql_dialect == SQL_DIALECT_TSQL)
{
if (nodeTag(arg) == T_ColumnRef)
{
/*
* We get here for unnamed argument syntax, i.e.
* exec myproc mystring
* */
colref_arg = arg;
}
else if (nodeTag(arg) == T_NamedArgExpr)
{
/*
* We get here for named argument syntax, i.e.
* exec myproc @p=mystring
*/
NamedArgExpr *na = (NamedArgExpr *) arg;
Assert(na->arg);
if (nodeTag((Node *) na->arg) == T_ColumnRef)
{
colref_arg = (Node *) na->arg;
}
}
/*
* The argument could be a variable, which should not be treated
* as an unquoted string, so verify it does not start with '@'.
*/
if (colref_arg)
{
ColumnRef *colref = (ColumnRef *) colref_arg;
Node *colnameField = (Node *) linitial(colref->fields);
char *colname = strVal(colnameField);
if (colname[0] != '@')
{
colref_arg->type = T_TSQL_UnquotedString;
}
}
}
return colref_arg;
}


static void call_argument_unquoted_string_reset (Node *colref_arg)
{
/*
* In case of an unquoted string, restore original node type
* or we may run into an unknown node type downstream.
*/
if (colref_arg)
{
if (nodeTag(colref_arg) == T_TSQL_UnquotedString)
{
colref_arg->type = T_ColumnRef;
}
}
return;
}

static char *
get_local_schema_for_bbf_functions(Oid proc_nsp_oid)
{
Expand Down
Loading

0 comments on commit d41258d

Please sign in to comment.