From 8c658358c22ed51dea35b21495cfddb2a0ee2459 Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Mon, 22 May 2017 10:16:00 +0530
Subject: [PATCH 1/2] Cleanup_v4

Code refactoring required for hash partitioning patch v11.

Changes :
 Removed hunk as suggested by Robert in message-id :
 CA%2BTgmoabcyyYPe_TRiHyXb%2BAa2rQ%2BzVC9ZHHLASPHmmYbp4sig%40mail.gmail.com
---
 src/backend/catalog/partition.c | 94 +++++++++++++++++++++++------------------
 1 file changed, 52 insertions(+), 42 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 7304f6c..763a24b 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -1923,10 +1923,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 +1967,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:
+			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

