Skip to content

Commit

Permalink
Redoing unquoted string handling (#235)
Browse files Browse the repository at this point in the history
Re-implements unquoted string handling, as the previous fix (#206 , babelfish-for-postgresql/babelfish_extensions#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 and implements the the unquoted string handling fully in the Babelfish extension (`tsqlIface.cpp`).

Extension PR: babelfish-for-postgresql/babelfish_extensions#1900
 
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 8361f89 commit 2d7aaea
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 79 deletions.
15 changes: 2 additions & 13 deletions src/backend/commands/functioncmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@

check_lang_as_clause_hook_type check_lang_as_clause_hook = NULL;
write_stored_proc_probin_hook_type write_stored_proc_probin_hook = NULL;
declare_parameter_unquoted_string_hook_type declare_parameter_unquoted_string_hook = NULL;
declare_parameter_unquoted_string_reset_hook_type declare_parameter_unquoted_string_reset_hook = NULL;

/*
* Examine the RETURNS clause of the CREATE FUNCTION statement
Expand Down Expand Up @@ -412,23 +410,14 @@ interpret_function_parameter_list(ParseState *pstate,
if (fp->defexpr)
{
Node *def;
Node *paramDft;

if (!isinput)
ereport(ERROR,
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
errmsg("only input parameters can have default values")));

paramDft = fp->defexpr;
if (declare_parameter_unquoted_string_hook)
declare_parameter_unquoted_string_hook(paramDft, objtype);

def = transformExpr(pstate,
paramDft,
EXPR_KIND_FUNCTION_DEFAULT);

if (declare_parameter_unquoted_string_reset_hook)
declare_parameter_unquoted_string_reset_hook(paramDft);
def = transformExpr(pstate, fp->defexpr,
EXPR_KIND_FUNCTION_DEFAULT);

def = coerce_to_specific_type(pstate, def, toid, "DEFAULT");
assign_expr_collations(pstate, def);
Expand Down
14 changes: 1 addition & 13 deletions src/backend/parser/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ pre_transform_setop_tree_hook_type pre_transform_setop_tree_hook = NULL;
/* Hook to reset a query's targetlist after modification in pre_transfrom_sort_clause */
pre_transform_setop_sort_clause_hook_type pre_transform_setop_sort_clause_hook = NULL;

/* Hooks for handling unquoted string argumentss in T-SQL procedure calls */
call_argument_unquoted_string_hook_type call_argument_unquoted_string_hook = NULL;
call_argument_unquoted_string_reset_hook_type call_argument_unquoted_string_reset_hook = NULL;

static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
Expand Down Expand Up @@ -3123,17 +3119,9 @@ transformCallStmt(ParseState *pstate, CallStmt *stmt)
targs = NIL;
foreach(lc, stmt->funccall->args)
{
Node *colref_arg = NULL;
Node *arg = lfirst(lc);
if (call_argument_unquoted_string_hook)
colref_arg = (*call_argument_unquoted_string_hook)(arg);

targs = lappend(targs, transformExpr(pstate,
arg,
(Node *) lfirst(lc),
EXPR_KIND_CALL_ARGUMENT));

if (call_argument_unquoted_string_reset_hook)
call_argument_unquoted_string_reset_hook(colref_arg);
}

node = ParseFuncOrColumn(pstate,
Expand Down
34 changes: 0 additions & 34 deletions src/backend/parser/parse_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,40 +125,6 @@ transformExprRecurse(ParseState *pstate, Node *expr)

switch (nodeTag(expr))
{
case T_TSQL_UnquotedString:
Assert(sql_dialect == SQL_DIALECT_TSQL);
{
/*
* This means the node is an unquoted string argument in a T-SQL procedure
* call or in a parameter default declaration in a T-SQL CREATE PROCEDURE/
* CREATE FUNCTION statement.
* Such arguments show up in the parse tree as T_ColumnRef nodes, which
* are intercepted in transformCallStmt() and interpret_function_parameter_list()
* and temporarily changed to node type T_TSQL_UnquotedString.
* Below, a constant T_String node is created for this argument instead
* of a ColumnRef node, which means it will be processed as a string -- as is
* the T-SQL semantic.
*/
A_Const newConst;

/*
* Get the string argument, which is pretending to be a column name at this point.
*/
ColumnRef *cref = (ColumnRef *) expr;
Node *colnameField = (Node *) linitial(cref->fields);
char *colname = strVal(colnameField);

/*
* Create a new node of type string
*/
newConst.type = T_String;
newConst.isnull = false;
newConst.location = cref->location;
newConst.val.sval = *(makeString(colname));
result = (Node *) make_const(pstate, &newConst);
}
break;

case T_ColumnRef:
result = transformColumnRef(pstate, (ColumnRef *) expr);
break;
Expand Down
5 changes: 0 additions & 5 deletions src/include/commands/defrem.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ typedef void (*write_stored_proc_probin_hook_type)(CreateFunctionStmt *stmt, Oid
extern PGDLLIMPORT check_lang_as_clause_hook_type check_lang_as_clause_hook;
extern PGDLLIMPORT write_stored_proc_probin_hook_type write_stored_proc_probin_hook;

typedef void (*declare_parameter_unquoted_string_hook_type)(Node *paramDft, ObjectType objtype);
extern PGDLLIMPORT declare_parameter_unquoted_string_hook_type declare_parameter_unquoted_string_hook;
typedef void (*declare_parameter_unquoted_string_reset_hook_type)(Node *paramDft);
extern PGDLLIMPORT declare_parameter_unquoted_string_reset_hook_type declare_parameter_unquoted_string_reset_hook;

/* commands/operatorcmds.c */
extern ObjectAddress DefineOperator(List *names, List *parameters);
extern void RemoveOperatorById(Oid operOid);
Expand Down
7 changes: 1 addition & 6 deletions src/include/nodes/nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,7 @@ typedef enum NodeTag
/*
* Additional TAG FOR VALUE NODES (value.h)
*/
T_TSQL_HexString,

/*
* Additional tag for handling unquoted TSQL strings
*/
T_TSQL_UnquotedString
T_TSQL_HexString

} NodeTag;

Expand Down
8 changes: 0 additions & 8 deletions src/include/parser/analyze.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ extern PGDLLIMPORT pre_transform_setop_tree_hook_type pre_transform_setop_tree_h
typedef void (*pre_transform_setop_sort_clause_hook_type) (ParseState *pstate, Query *qry, List *sortClause, Query *leftmostQuery);
extern PGDLLIMPORT pre_transform_setop_sort_clause_hook_type pre_transform_setop_sort_clause_hook;

/* Hooks for handling unquoted string argumentss in T-SQL procedure calls */
typedef Node* (*call_argument_unquoted_string_hook_type)(Node *arg);
extern PGDLLIMPORT call_argument_unquoted_string_hook_type call_argument_unquoted_string_hook;

typedef void (*call_argument_unquoted_string_reset_hook_type)(Node *colref_arg);
extern PGDLLIMPORT call_argument_unquoted_string_reset_hook_type call_argument_unquoted_string_reset_hook;


extern Query *parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);
extern Query *parse_analyze(RawStmt *parseTree, const char *sourceText,
Expand Down

0 comments on commit 2d7aaea

Please sign in to comment.