code to deparse parameter in postgres_fdw is duplicated

Started by Ashutosh Bapatabout 10 years ago1 messages
#1Ashutosh Bapat
ashutosh.bapat@enterprisedb.com
1 attachment(s)

Hi,
The code to deparse a Var or Param node as a parameter in the query looks
same except the member names for getting type and typmod. PFA patch to push
that into a function to deduplicate it. Regression and tests in
contrib/postgres_fdw run clean with this patch.

--
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company

Attachments:

pg_dep_par.patchtext/x-patch; charset=US-ASCII; name=pg_dep_par.patchDownload
diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index e59af2c..2ff5eb4 100644
--- a/contrib/postgres_fdw/deparse.c
+++ b/contrib/postgres_fdw/deparse.c
@@ -132,20 +132,21 @@ static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context);
 static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node,
 						 deparse_expr_cxt *context);
 static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context);
 static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context);
 static void deparseNullTest(NullTest *node, deparse_expr_cxt *context);
 static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context);
 static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod,
 				 deparse_expr_cxt *context);
 static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod,
 					   deparse_expr_cxt *context);
+static void deparseNodeAsParam(Node *node, deparse_expr_cxt *context);
 
 
 /*
  * Examine each qual clause in input_conds, and classify them into two groups,
  * which are returned as two lists:
  *	- remote_conds contains expressions that can be evaluated remotely
  *	- local_conds contains expressions that can't be evaluated remotely
  */
 void
 classifyConditions(PlannerInfo *root,
@@ -1302,48 +1303,21 @@ deparseVar(Var *node, deparse_expr_cxt *context)
 {
 	StringInfo	buf = context->buf;
 
 	if (node->varno == context->foreignrel->relid &&
 		node->varlevelsup == 0)
 	{
 		/* Var belongs to foreign table */
 		deparseColumnRef(buf, node->varno, node->varattno, context->root);
 	}
 	else
-	{
-		/* Treat like a Param */
-		if (context->params_list)
-		{
-			int			pindex = 0;
-			ListCell   *lc;
-
-			/* find its index in params_list */
-			foreach(lc, *context->params_list)
-			{
-				pindex++;
-				if (equal(node, (Node *) lfirst(lc)))
-					break;
-			}
-			if (lc == NULL)
-			{
-				/* not in list, so add it */
-				pindex++;
-				*context->params_list = lappend(*context->params_list, node);
-			}
-
-			printRemoteParam(pindex, node->vartype, node->vartypmod, context);
-		}
-		else
-		{
-			printRemotePlaceholder(node->vartype, node->vartypmod, context);
-		}
-	}
+		deparseNodeAsParam((Node *)node, context);
 }
 
 /*
  * Deparse given constant value into context->buf.
  *
  * This function has to be kept in sync with ruleutils.c's get_const_expr.
  */
 static void
 deparseConst(Const *node, deparse_expr_cxt *context)
 {
@@ -1430,55 +1404,63 @@ deparseConst(Const *node, deparse_expr_cxt *context)
 			needlabel = true;
 			break;
 	}
 	if (needlabel)
 		appendStringInfo(buf, "::%s",
 						 deparse_type_name(node->consttype,
 										   node->consttypmod));
 }
 
 /*
- * Deparse given Param node.
- *
+ * Deparses a Param or Var node as a parameter.
  * If we're generating the query "for real", add the Param to
  * context->params_list if it's not already present, and then use its index
  * in that list as the remote parameter number.  During EXPLAIN, there's
  * no need to identify a parameter number.
  */
 static void
-deparseParam(Param *node, deparse_expr_cxt *context)
+deparseNodeAsParam(Node *node, deparse_expr_cxt *context)
 {
+	Assert(IsA(node, Param) || IsA(node, Var));
+
 	if (context->params_list)
 	{
 		int			pindex = 0;
 		ListCell   *lc;
 
 		/* find its index in params_list */
 		foreach(lc, *context->params_list)
 		{
 			pindex++;
 			if (equal(node, (Node *) lfirst(lc)))
 				break;
 		}
 		if (lc == NULL)
 		{
 			/* not in list, so add it */
 			pindex++;
 			*context->params_list = lappend(*context->params_list, node);
 		}
 
-		printRemoteParam(pindex, node->paramtype, node->paramtypmod, context);
+		printRemoteParam(pindex, exprType(node), exprTypmod(node), context);
 	}
 	else
-	{
-		printRemotePlaceholder(node->paramtype, node->paramtypmod, context);
-	}
+		printRemotePlaceholder(exprType(node), exprTypmod(node), context);
+}
+
+/*
+ * Deparse given Param node.
+ */
+static void
+deparseParam(Param *node, deparse_expr_cxt *context)
+{
+	deparseNodeAsParam((Node *)node, context);
 }
 
 /*
  * Deparse an array subscript expression.
  */
 static void
 deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context)
 {
 	StringInfo	buf = context->buf;
 	ListCell   *lowlist_item;