Skip to content

Commit

Permalink
Fix the remaining issues about default keyword in proc/func call (bab…
Browse files Browse the repository at this point in the history
…elfish-for-postgresql#259)

1. The error message should be the same as sql server
2. Function call with not exist default param, then should use NULL value
3. Support call by name default as well

Task: BABEL-335
Signed-off-by: Zhibai Song <[email protected]>
  • Loading branch information
forestkeeper authored and Jason Teng committed Dec 21, 2023
1 parent 621a1c3 commit 8f1cd92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/backend/optimizer/util/clauses.c
Original file line number Diff line number Diff line change
Expand Up @@ -4314,9 +4314,24 @@ fetch_function_defaults(HeapTuple func_tuple)
List *defaults;
Datum proargdefaults;
char *str;
bool isnull;

/* The error cases here shouldn't happen, but check anyway */
proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
Anum_pg_proc_proargdefaults,
&isnull);
if (isnull)
{
/* This error msg is not expected in Tsql for 2 reasons
1. Procedure call will throw a specific error msg later
2. Func call even without defining default value, it
won't error out, it'll use NULL instead */
if (sql_dialect == SQL_DIALECT_TSQL)
return NIL;

elog(ERROR, "not enough default arguments");
}

proargdefaults = SysCacheGetAttrNotNull(PROCOID, func_tuple,
Anum_pg_proc_proargdefaults);
str = TextDatumGetCString(proargdefaults);
defaults = castNode(List, stringToNode(str));
pfree(str);
Expand Down
12 changes: 11 additions & 1 deletion src/backend/parser/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -3204,13 +3204,23 @@ transformCallStmt(ParseState *pstate, CallStmt *stmt)
targs = NIL;
foreach(lc, stmt->funccall->args)
{
if (sql_dialect == SQL_DIALECT_TSQL && nodeTag((Node*)lfirst(lc)) == T_SetToDefault)
if (sql_dialect == SQL_DIALECT_TSQL &&
(nodeTag((Node*)lfirst(lc)) == T_SetToDefault))
{
// For Tsql Default in function call, we set it to UNKNOWN in parser stage
// In analyzer it'll use other types to detect the right func candidate
((SetToDefault *)lfirst(lc))->typeId = UNKNOWNOID;
targs = lappend(targs, (Node *) lfirst(lc));
}
else if (sql_dialect == SQL_DIALECT_TSQL &&
nodeTag((Node*)lfirst(lc)) == T_NamedArgExpr &&
nodeTag(((NamedArgExpr*)lfirst(lc))->arg) == T_SetToDefault)
{
// For Tsql Default in function call, we set it to UNKNOWN in parser stage
// In analyzer it'll use other types to detect the right func candidate
((SetToDefault *)((NamedArgExpr*)lfirst(lc))->arg)->typeId = UNKNOWNOID;
targs = lappend(targs, (Node *) lfirst(lc));
}
else
{
targs = lappend(targs, transformExpr(pstate,
Expand Down

0 comments on commit 8f1cd92

Please sign in to comment.