From 70eeb8ba3aa080d32cadfb0c51b56904268fbf46 Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Fri, 19 May 2017 14:18:14 +0530
Subject: [PATCH 1/2] Cleanup_v3

Code refactoring required for hash partitioning patch v10.
---
 src/backend/catalog/partition.c | 100 ++++++++++++++++++++++------------------
 1 file changed, 54 insertions(+), 46 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 7304f6c..66c1071 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -250,8 +250,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)
 				{
@@ -331,8 +330,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);
@@ -1923,10 +1921,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,62 +1965,74 @@ get_partition_for_tuple(PartitionDispatch *pd,
 		ecxt->ecxt_scantuple = slot;
 		FormPartitionKeyDatum(parent, slot, estate, values, isnull);
 
-		if (key->strategy == PARTITION_STRATEGY_RANGE)
+		switch (key->strategy)
 		{
-			/*
-			 * Since we cannot route tuples with NULL partition keys through
-			 * a range-partitioned table, simply return that no partition
-			 * exists
-			 */
-			for (i = 0; i < key->partnatts; i++)
-			{
-				if (isnull[i])
+			case PARTITION_STRATEGY_LIST:
+
+				/*
+				 * A null partition key is only acceptable if null-accepting
+				 * list partition exists.
+				 */
+				if (isnull[0])
 				{
-					*failed_at = parent;
-					*failed_slot = slot;
-					result = -1;
-					goto error_exit;
+					if (partition_bound_accepts_nulls(partdesc->boundinfo))
+						cur_index = partdesc->boundinfo->null_index;
 				}
-			}
-		}
+				else
+				{
+					bool		equal = false;
+					int			cur_offset;
 
-		/*
-		 * A null partition key is only acceptable if null-accepting list
-		 * partition exists.
-		 */
-		cur_index = -1;
-		if (isnull[0] && partition_bound_accepts_nulls(partdesc->boundinfo))
-			cur_index = partdesc->boundinfo->null_index;
-		else if (!isnull[0])
-		{
-			/* Else bsearch in partdesc->boundinfo */
-			bool		equal = false;
+					/* bsearch in partdesc->boundinfo */
+					cur_offset = partition_bound_bsearch(key,
+														 partdesc->boundinfo,
+														 values, false, &equal);
 
-			cur_offset = partition_bound_bsearch(key, partdesc->boundinfo,
-												 values, false, &equal);
-			switch (key->strategy)
-			{
-				case PARTITION_STRATEGY_LIST:
 					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:
+					/*
+					 * Since we cannot route tuples with NULL partition keys
+					 * through a range-partitioned table, simply return that no
+					 * partition exists.
+					 */
+					for (i = 0; i < key->partnatts; i++)
+					{
+						if (isnull[i])
+						{
+							*failed_at = parent;
+							*failed_slot = slot;
+							result = -1;
+							goto error_exit;
+						}
+					}
 
+					/* 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
 					 * found to be less or equal with the tuple. So, the bound
 					 * 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

