From 3bf64687db473674e04a105face0a5a588b70aec Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Tue, 6 Jun 2017 10:52:39 +0530
Subject: [PATCH 1/2] Cleanup_v5

Code refactoring required for hash partitioning patch v13.
---
 src/backend/catalog/partition.c | 93 ++++++++++++++++++++++-------------------
 1 file changed, 51 insertions(+), 42 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 5c5a9e1..8d03292 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -1924,10 +1924,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;
 
@@ -1970,61 +1968,72 @@ 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;
 				}
-			}
-		}
-
-		/*
-		 * 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;
+				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:
+			case PARTITION_STRATEGY_RANGE:
+				{
+					bool		equal = false;
+					int			i;
+					int			cur_offset;
 
 					/*
+					 * 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

