From 8ef84a31e61271c007852ec100be63740b94a5b9 Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Sat, 13 May 2017 18:39:53 +0530
Subject: [PATCH 1/2] Cleanup_v2

Code refactoring required for hash partitioning patch v4
---
 src/backend/catalog/partition.c | 87 ++++++++++++++++++++++-------------------
 1 file changed, 47 insertions(+), 40 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 885c533..5566839 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -252,8 +252,7 @@ RelationBuildPartitionDesc(Relation rel)
 				ListCell   *c;
 				PartitionBoundSpec *spec = lfirst(cell);
 
-				if (spec->strategy != PARTITION_STRATEGY_LIST)
-					elog(ERROR, "invalid strategy in partition bound spec");
+				Assert(spec->strategy == PARTITION_STRATEGY_LIST);
 
 				foreach(c, spec->listdatums)
 				{
@@ -334,8 +333,7 @@ RelationBuildPartitionDesc(Relation rel)
 				PartitionRangeBound *lower,
 						   *upper;
 
-				if (spec->strategy != PARTITION_STRATEGY_RANGE)
-					elog(ERROR, "invalid strategy in partition bound spec");
+				Assert(spec->strategy == PARTITION_STRATEGY_RANGE);
 
 				lower = make_one_range_bound(key, i, spec->lowerdatums,
 											 true);
@@ -1924,10 +1922,8 @@ get_partition_for_tuple(PartitionDispatch *pd,
 	PartitionDispatch parent;
 	Datum		values[PARTITION_MAX_KEYS];
 	bool		isnull[PARTITION_MAX_KEYS];
-	int			cur_offset,
-				cur_index;
-	int			i,
-				result;
+	int			cur_index = -1;
+	int			result;
 	ExprContext *ecxt = GetPerTupleExprContext(estate);
 	TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple;
 
@@ -1969,40 +1965,51 @@ get_partition_for_tuple(PartitionDispatch *pd,
 		ecxt->ecxt_scantuple = slot;
 		FormPartitionKeyDatum(parent, slot, estate, values, isnull);
 
-		if (key->strategy == PARTITION_STRATEGY_RANGE)
-		{
-			/* Disallow nulls in the range partition key of the tuple */
-			for (i = 0; i < key->partnatts; i++)
-				if (isnull[i])
-					ereport(ERROR,
-							(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
-						errmsg("range partition key of row contains null")));
-		}
-
-		/*
-		 * A null partition key is only acceptable if null-accepting list
-		 * partition exists.
-		 */
-		cur_index = -1;
-		if (isnull[0] && partdesc->boundinfo->has_null)
-			cur_index = partdesc->boundinfo->null_index;
-		else if (!isnull[0])
+		switch (key->strategy)
 		{
-			/* Else bsearch in partdesc->boundinfo */
-			bool		equal = false;
+			case PARTITION_STRATEGY_LIST:
+				/*
+				 * A null partition key is only acceptable if null-accepting
+				 * list partition exists.
+				 */
+				if (isnull[0])
+				{
+					if (partdesc->boundinfo->has_null)
+						cur_index = partdesc->boundinfo->null_index;
+				}
+				else
+				{
+					bool		equal = false;
+					int			cur_offset;
 
-			cur_offset = partition_bound_bsearch(key, partdesc->boundinfo,
-												 values, false, &equal);
-			switch (key->strategy)
-			{
-				case PARTITION_STRATEGY_LIST:
+					/* bsearch in partdesc->boundinfo */
+					cur_offset = partition_bound_bsearch(key,
+														 partdesc->boundinfo,
+														 values, false, &equal);
 					if (cur_offset >= 0 && equal)
 						cur_index = partdesc->boundinfo->indexes[cur_offset];
 					else
 						cur_index = -1;
-					break;
+				}
+				break;
+
+			case PARTITION_STRATEGY_RANGE:
+				{
+					bool		equal = false;
+					int			i;
+					int			cur_offset;
 
-				case PARTITION_STRATEGY_RANGE:
+					/* Disallow nulls in the range partition key of the tuple */
+					for (i = 0; i < key->partnatts; i++)
+						if (isnull[i])
+							ereport(ERROR,
+									(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+									 errmsg("range partition key of row contains null")));
+
+					/* bsearch in partdesc->boundinfo */
+					cur_offset = partition_bound_bsearch(key,
+														 partdesc->boundinfo,
+														 values, false, &equal);
 
 					/*
 					 * Offset returned is such that the bound at offset is
@@ -2010,12 +2017,12 @@ get_partition_for_tuple(PartitionDispatch *pd,
 					 * at offset+1 would be the upper bound.
 					 */
 					cur_index = partdesc->boundinfo->indexes[cur_offset + 1];
-					break;
+				}
+				break;
 
-				default:
-					elog(ERROR, "unexpected partition strategy: %d",
-						 (int) key->strategy);
-			}
+			default:
+				elog(ERROR, "unexpected partition strategy: %d",
+					 (int) key->strategy);
 		}
 
 		/*
-- 
2.6.2

