WRITE_*_ARRAY macros for outfuncs.c

Started by Peter Eisentrautabout 7 years ago2 messages
#1Peter Eisentraut
peter.eisentraut@2ndquadrant.com
1 attachment(s)

In readfuncs.c, we have READ_ATTRNUMBER_ARRAY, READ_OID_ARRAY,
READ_INT_ARRAY, READ_BOOL_ARRAY, but the writing side in outfuncs.c is
coded by hand in each case. Any reason for this?

Here is a patch that adds WRITE_ATTRNUMBER_ARRAY, WRITE_OID_ARRAY,
WRITE_INT_ARRAY, WRITE_BOOL_ARRAY. That seems much nicer.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-Add-WRITE_-_ARRAY-macros.patchtext/plain; charset=UTF-8; name=0001-Add-WRITE_-_ARRAY-macros.patch; x-mac-creator=0; x-mac-type=0Download
From 62239b0d488a00bcca1229332caad98960240761 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Thu, 13 Dec 2018 21:18:18 +0100
Subject: [PATCH] Add WRITE_*_ARRAY macros

---
 src/backend/nodes/outfuncs.c | 248 ++++++++++-------------------------
 1 file changed, 67 insertions(+), 181 deletions(-)

diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index 6edc7f2359..be6b4ca2f4 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -110,6 +110,34 @@ static void outChar(StringInfo str, char c);
 	(appendStringInfoString(str, " :" CppAsString(fldname) " "), \
 	 outBitmapset(str, node->fldname))
 
+#define WRITE_ATTRNUMBER_ARRAY(fldname, len) \
+	do { \
+		appendStringInfoString(str, " :" CppAsString(fldname) " "); \
+		for (int i = 0; i < len; i++) \
+			appendStringInfo(str, " %d", node->fldname[i]); \
+	} while(0)
+
+#define WRITE_OID_ARRAY(fldname, len) \
+	do { \
+		appendStringInfoString(str, " :" CppAsString(fldname) " "); \
+		for (int i = 0; i < len; i++) \
+			appendStringInfo(str, " %u", node->fldname[i]); \
+	} while(0)
+
+#define WRITE_INT_ARRAY(fldname, len) \
+	do { \
+		appendStringInfoString(str, " :" CppAsString(fldname) " "); \
+		for (int i = 0; i < len; i++) \
+			appendStringInfo(str, " %d", node->fldname[i]); \
+	} while(0)
+
+#define WRITE_BOOL_ARRAY(fldname, len) \
+	do { \
+		appendStringInfoString(str, " :" CppAsString(fldname) " "); \
+		for (int i = 0; i < len; i++) \
+			appendStringInfo(str, " %s", booltostr(node->fldname[i])); \
+	} while(0)
+
 
 #define booltostr(x)  ((x) ? "true" : "false")
 
@@ -411,55 +439,30 @@ _outAppend(StringInfo str, const Append *node)
 static void
 _outMergeAppend(StringInfo str, const MergeAppend *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("MERGEAPPEND");
 
 	_outPlanInfo(str, (const Plan *) node);
 
 	WRITE_NODE_FIELD(mergeplans);
-
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :sortColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->sortColIdx[i]);
-
-	appendStringInfoString(str, " :sortOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->sortOperators[i]);
-
-	appendStringInfoString(str, " :collations");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->collations[i]);
-
-	appendStringInfoString(str, " :nullsFirst");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %s", booltostr(node->nullsFirst[i]));
-
+	WRITE_ATTRNUMBER_ARRAY(sortColIdx, node->numCols);
+	WRITE_OID_ARRAY(sortOperators, node->numCols);
+	WRITE_OID_ARRAY(collations, node->numCols);
+	WRITE_BOOL_ARRAY(nullsFirst, node->numCols);
 	WRITE_NODE_FIELD(part_prune_info);
 }
 
 static void
 _outRecursiveUnion(StringInfo str, const RecursiveUnion *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("RECURSIVEUNION");
 
 	_outPlanInfo(str, (const Plan *) node);
 
 	WRITE_INT_FIELD(wtParam);
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :dupColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->dupColIdx[i]);
-
-	appendStringInfoString(str, " :dupOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->dupOperators[i]);
-
+	WRITE_ATTRNUMBER_ARRAY(dupColIdx, node->numCols);
+	WRITE_OID_ARRAY(dupOperators, node->numCols);
 	WRITE_LONG_FIELD(numGroups);
 }
 
@@ -501,8 +504,6 @@ _outGather(StringInfo str, const Gather *node)
 static void
 _outGatherMerge(StringInfo str, const GatherMerge *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("GATHERMERGE");
 
 	_outPlanInfo(str, (const Plan *) node);
@@ -510,23 +511,10 @@ _outGatherMerge(StringInfo str, const GatherMerge *node)
 	WRITE_INT_FIELD(num_workers);
 	WRITE_INT_FIELD(rescan_param);
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :sortColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->sortColIdx[i]);
-
-	appendStringInfoString(str, " :sortOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->sortOperators[i]);
-
-	appendStringInfoString(str, " :collations");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->collations[i]);
-
-	appendStringInfoString(str, " :nullsFirst");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %s", booltostr(node->nullsFirst[i]));
-
+	WRITE_ATTRNUMBER_ARRAY(sortColIdx, node->numCols);
+	WRITE_OID_ARRAY(sortOperators, node->numCols);
+	WRITE_OID_ARRAY(collations, node->numCols);
+	WRITE_BOOL_ARRAY(nullsFirst, node->numCols);
 	WRITE_BITMAPSET_FIELD(initParam);
 }
 
@@ -748,7 +736,6 @@ static void
 _outMergeJoin(StringInfo str, const MergeJoin *node)
 {
 	int			numCols;
-	int			i;
 
 	WRITE_NODE_TYPE("MERGEJOIN");
 
@@ -759,21 +746,10 @@ _outMergeJoin(StringInfo str, const MergeJoin *node)
 
 	numCols = list_length(node->mergeclauses);
 
-	appendStringInfoString(str, " :mergeFamilies");
-	for (i = 0; i < numCols; i++)
-		appendStringInfo(str, " %u", node->mergeFamilies[i]);
-
-	appendStringInfoString(str, " :mergeCollations");
-	for (i = 0; i < numCols; i++)
-		appendStringInfo(str, " %u", node->mergeCollations[i]);
-
-	appendStringInfoString(str, " :mergeStrategies");
-	for (i = 0; i < numCols; i++)
-		appendStringInfo(str, " %d", node->mergeStrategies[i]);
-
-	appendStringInfoString(str, " :mergeNullsFirst");
-	for (i = 0; i < numCols; i++)
-		appendStringInfo(str, " %s", booltostr(node->mergeNullsFirst[i]));
+	WRITE_OID_ARRAY(mergeFamilies, numCols);
+	WRITE_OID_ARRAY(mergeCollations, numCols);
+	WRITE_INT_ARRAY(mergeStrategies, numCols);
+	WRITE_BOOL_ARRAY(mergeNullsFirst, numCols);
 }
 
 static void
@@ -789,8 +765,6 @@ _outHashJoin(StringInfo str, const HashJoin *node)
 static void
 _outAgg(StringInfo str, const Agg *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("AGG");
 
 	_outPlanInfo(str, (const Plan *) node);
@@ -798,15 +772,8 @@ _outAgg(StringInfo str, const Agg *node)
 	WRITE_ENUM_FIELD(aggstrategy, AggStrategy);
 	WRITE_ENUM_FIELD(aggsplit, AggSplit);
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :grpColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->grpColIdx[i]);
-
-	appendStringInfoString(str, " :grpOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->grpOperators[i]);
-
+	WRITE_ATTRNUMBER_ARRAY(grpColIdx, node->numCols);
+	WRITE_OID_ARRAY(grpOperators, node->numCols);
 	WRITE_LONG_FIELD(numGroups);
 	WRITE_BITMAPSET_FIELD(aggParams);
 	WRITE_NODE_FIELD(groupingSets);
@@ -816,33 +783,17 @@ _outAgg(StringInfo str, const Agg *node)
 static void
 _outWindowAgg(StringInfo str, const WindowAgg *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("WINDOWAGG");
 
 	_outPlanInfo(str, (const Plan *) node);
 
 	WRITE_UINT_FIELD(winref);
 	WRITE_INT_FIELD(partNumCols);
-
-	appendStringInfoString(str, " :partColIdx");
-	for (i = 0; i < node->partNumCols; i++)
-		appendStringInfo(str, " %d", node->partColIdx[i]);
-
-	appendStringInfoString(str, " :partOperations");
-	for (i = 0; i < node->partNumCols; i++)
-		appendStringInfo(str, " %u", node->partOperators[i]);
-
+	WRITE_ATTRNUMBER_ARRAY(partColIdx, node->partNumCols);
+	WRITE_OID_ARRAY(partOperators, node->partNumCols);
 	WRITE_INT_FIELD(ordNumCols);
-
-	appendStringInfoString(str, " :ordColIdx");
-	for (i = 0; i < node->ordNumCols; i++)
-		appendStringInfo(str, " %d", node->ordColIdx[i]);
-
-	appendStringInfoString(str, " :ordOperations");
-	for (i = 0; i < node->ordNumCols; i++)
-		appendStringInfo(str, " %u", node->ordOperators[i]);
-
+	WRITE_ATTRNUMBER_ARRAY(ordColIdx, node->ordNumCols);
+	WRITE_OID_ARRAY(ordOperators, node->ordNumCols);
 	WRITE_INT_FIELD(frameOptions);
 	WRITE_NODE_FIELD(startOffset);
 	WRITE_NODE_FIELD(endOffset);
@@ -856,21 +807,13 @@ _outWindowAgg(StringInfo str, const WindowAgg *node)
 static void
 _outGroup(StringInfo str, const Group *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("GROUP");
 
 	_outPlanInfo(str, (const Plan *) node);
 
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :grpColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->grpColIdx[i]);
-
-	appendStringInfoString(str, " :grpOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->grpOperators[i]);
+	WRITE_ATTRNUMBER_ARRAY(grpColIdx, node->numCols);
+	WRITE_OID_ARRAY(grpOperators, node->numCols);
 }
 
 static void
@@ -884,49 +827,27 @@ _outMaterial(StringInfo str, const Material *node)
 static void
 _outSort(StringInfo str, const Sort *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("SORT");
 
 	_outPlanInfo(str, (const Plan *) node);
 
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :sortColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->sortColIdx[i]);
-
-	appendStringInfoString(str, " :sortOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->sortOperators[i]);
-
-	appendStringInfoString(str, " :collations");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->collations[i]);
-
-	appendStringInfoString(str, " :nullsFirst");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %s", booltostr(node->nullsFirst[i]));
+	WRITE_ATTRNUMBER_ARRAY(sortColIdx, node->numCols);
+	WRITE_OID_ARRAY(sortOperators, node->numCols);
+	WRITE_OID_ARRAY(collations, node->numCols);
+	WRITE_BOOL_ARRAY(nullsFirst, node->numCols);
 }
 
 static void
 _outUnique(StringInfo str, const Unique *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("UNIQUE");
 
 	_outPlanInfo(str, (const Plan *) node);
 
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :uniqColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->uniqColIdx[i]);
-
-	appendStringInfoString(str, " :uniqOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->uniqOperators[i]);
+	WRITE_ATTRNUMBER_ARRAY(uniqColIdx, node->numCols);
+	WRITE_OID_ARRAY(uniqOperators, node->numCols);
 }
 
 static void
@@ -945,8 +866,6 @@ _outHash(StringInfo str, const Hash *node)
 static void
 _outSetOp(StringInfo str, const SetOp *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("SETOP");
 
 	_outPlanInfo(str, (const Plan *) node);
@@ -954,15 +873,8 @@ _outSetOp(StringInfo str, const SetOp *node)
 	WRITE_ENUM_FIELD(cmd, SetOpCmd);
 	WRITE_ENUM_FIELD(strategy, SetOpStrategy);
 	WRITE_INT_FIELD(numCols);
-
-	appendStringInfoString(str, " :dupColIdx");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %d", node->dupColIdx[i]);
-
-	appendStringInfoString(str, " :dupOperators");
-	for (i = 0; i < node->numCols; i++)
-		appendStringInfo(str, " %u", node->dupOperators[i]);
-
+	WRITE_ATTRNUMBER_ARRAY(dupColIdx, node->numCols);
+	WRITE_OID_ARRAY(dupOperators, node->numCols);
 	WRITE_INT_FIELD(flagColIdx);
 	WRITE_INT_FIELD(firstFlag);
 	WRITE_LONG_FIELD(numGroups);
@@ -1026,8 +938,6 @@ _outPartitionPruneInfo(StringInfo str, const PartitionPruneInfo *node)
 static void
 _outPartitionedRelPruneInfo(StringInfo str, const PartitionedRelPruneInfo *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("PARTITIONEDRELPRUNEINFO");
 
 	WRITE_UINT_FIELD(rtindex);
@@ -1035,19 +945,9 @@ _outPartitionedRelPruneInfo(StringInfo str, const PartitionedRelPruneInfo *node)
 	WRITE_BITMAPSET_FIELD(present_parts);
 	WRITE_INT_FIELD(nparts);
 	WRITE_INT_FIELD(nexprs);
-
-	appendStringInfoString(str, " :subplan_map");
-	for (i = 0; i < node->nparts; i++)
-		appendStringInfo(str, " %d", node->subplan_map[i]);
-
-	appendStringInfoString(str, " :subpart_map");
-	for (i = 0; i < node->nparts; i++)
-		appendStringInfo(str, " %d", node->subpart_map[i]);
-
-	appendStringInfoString(str, " :hasexecparam");
-	for (i = 0; i < node->nexprs; i++)
-		appendStringInfo(str, " %s", booltostr(node->hasexecparam[i]));
-
+	WRITE_INT_ARRAY(subplan_map, node->nparts);
+	WRITE_INT_ARRAY(subpart_map, node->nparts);
+	WRITE_BOOL_ARRAY(hasexecparam, node->nexprs);
 	WRITE_BOOL_FIELD(do_initial_prune);
 	WRITE_BOOL_FIELD(do_exec_prune);
 	WRITE_BITMAPSET_FIELD(execparamids);
@@ -2409,15 +2309,9 @@ _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
 	WRITE_UINT_FIELD(con_relid);
 	WRITE_UINT_FIELD(ref_relid);
 	WRITE_INT_FIELD(nkeys);
-	appendStringInfoString(str, " :conkey");
-	for (i = 0; i < node->nkeys; i++)
-		appendStringInfo(str, " %d", node->conkey[i]);
-	appendStringInfoString(str, " :confkey");
-	for (i = 0; i < node->nkeys; i++)
-		appendStringInfo(str, " %d", node->confkey[i]);
-	appendStringInfoString(str, " :conpfeqop");
-	for (i = 0; i < node->nkeys; i++)
-		appendStringInfo(str, " %u", node->conpfeqop[i]);
+	WRITE_ATTRNUMBER_ARRAY(conkey, node->nkeys);
+	WRITE_ATTRNUMBER_ARRAY(confkey, node->nkeys);
+	WRITE_OID_ARRAY(conpfeqop, node->nkeys);
 	WRITE_INT_FIELD(nmatched_ec);
 	WRITE_INT_FIELD(nmatched_rcols);
 	WRITE_INT_FIELD(nmatched_ri);
@@ -3629,23 +3523,15 @@ _outConstraint(StringInfo str, const Constraint *node)
 static void
 _outForeignKeyCacheInfo(StringInfo str, const ForeignKeyCacheInfo *node)
 {
-	int			i;
-
 	WRITE_NODE_TYPE("FOREIGNKEYCACHEINFO");
 
 	WRITE_OID_FIELD(conoid);
 	WRITE_OID_FIELD(conrelid);
 	WRITE_OID_FIELD(confrelid);
 	WRITE_INT_FIELD(nkeys);
-	appendStringInfoString(str, " :conkey");
-	for (i = 0; i < node->nkeys; i++)
-		appendStringInfo(str, " %d", node->conkey[i]);
-	appendStringInfoString(str, " :confkey");
-	for (i = 0; i < node->nkeys; i++)
-		appendStringInfo(str, " %d", node->confkey[i]);
-	appendStringInfoString(str, " :conpfeqop");
-	for (i = 0; i < node->nkeys; i++)
-		appendStringInfo(str, " %u", node->conpfeqop[i]);
+	WRITE_ATTRNUMBER_ARRAY(conkey, node->nkeys);
+	WRITE_ATTRNUMBER_ARRAY(confkey, node->nkeys);
+	WRITE_OID_ARRAY(conpfeqop, node->nkeys);
 }
 
 static void
-- 
2.20.1

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#1)
Re: WRITE_*_ARRAY macros for outfuncs.c

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

In readfuncs.c, we have READ_ATTRNUMBER_ARRAY, READ_OID_ARRAY,
READ_INT_ARRAY, READ_BOOL_ARRAY, but the writing side in outfuncs.c is
coded by hand in each case. Any reason for this?

Here is a patch that adds WRITE_ATTRNUMBER_ARRAY, WRITE_OID_ARRAY,
WRITE_INT_ARRAY, WRITE_BOOL_ARRAY. That seems much nicer.

LGTM.

regards, tom lane