From 7ed9597684eb87e312f006ea5054adc4e7f2f640 Mon Sep 17 00:00:00 2001 From: Halil Ozan Akgul Date: Mon, 16 Aug 2021 17:10:29 +0300 Subject: [PATCH] Adds SQL-standard function body support to ruleutils_14.c Relevant PG commit: e717a9a18b2e34c9c40e5259ad4d31cd7e420750 --- .../distributed/deparser/ruleutils_14.c | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/deparser/ruleutils_14.c b/src/backend/distributed/deparser/ruleutils_14.c index 6354bf8a8..68669b21a 100644 --- a/src/backend/distributed/deparser/ruleutils_14.c +++ b/src/backend/distributed/deparser/ruleutils_14.c @@ -182,6 +182,10 @@ typedef struct List *outer_tlist; /* referent for OUTER_VAR Vars */ List *inner_tlist; /* referent for INNER_VAR Vars */ List *index_tlist; /* referent for INDEX_VAR Vars */ + /* Special namespace representing a function signature: */ + char *funcname; + int numargs; + char **argnames; } deparse_namespace; /* Callback signature for resolve_special_varno() */ @@ -2358,7 +2362,10 @@ get_basic_select_query(Query *query, deparse_context *context, /* * Build up the query string - first we say SELECT */ - appendStringInfoString(buf, "SELECT"); + if (query->isReturn) + appendStringInfoString(buf, "RETURN"); + else + appendStringInfoString(buf, "SELECT"); /* Add the DISTINCT clause if given */ if (query->distinctClause != NIL) @@ -4594,6 +4601,50 @@ get_parameter(Param *param, deparse_context *context) return; } + /* + * If it's an external parameter, see if the outermost namespace provides + * function argument names. + */ + if (param->paramkind == PARAM_EXTERN) + { + dpns = lfirst(list_tail(context->namespaces)); + if (dpns->argnames) + { + char *argname = dpns->argnames[param->paramid - 1]; + + if (argname) + { + bool should_qualify = false; + ListCell *lc; + + /* + * Qualify the parameter name if there are any other deparse + * namespaces with range tables. This avoids qualifying in + * trivial cases like "RETURN a + b", but makes it safe in all + * other cases. + */ + foreach(lc, context->namespaces) + { + deparse_namespace *dpns = lfirst(lc); + + if (list_length(dpns->rtable_names) > 0) + { + should_qualify = true; + break; + } + } + if (should_qualify) + { + appendStringInfoString(context->buf, quote_identifier(dpns->funcname)); + appendStringInfoChar(context->buf, '.'); + } + + appendStringInfoString(context->buf, quote_identifier(argname)); + return; + } + } + } + /* * Not PARAM_EXEC, or couldn't find referent: for base types just print $N. * For composite types, add cast to the parameter to ease remote node detect