From 91a6f38b06e88345b1b07b712f316abba767faff Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Fri, 12 May 2017 13:53:55 +0530
Subject: [PATCH 1/2] Cleanup

Code refactoring required for hash partitioning patch v3
---
 src/backend/catalog/partition.c  | 82 ++++++++++++++++++++--------------------
 src/backend/commands/tablecmds.c |  7 ++--
 2 files changed, 46 insertions(+), 43 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 8641ae1..f42f287 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -244,8 +244,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)
 				{
@@ -326,8 +325,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);
@@ -1674,10 +1672,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;
 
@@ -1719,40 +1715,46 @@ 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])
+					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
@@ -1760,12 +1762,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);
 		}
 
 		/*
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index cdcb949..49bb4d7 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -459,7 +459,7 @@ static void RangeVarCallbackForAlterRelation(const RangeVar *rv, Oid relid,
 static bool is_partition_attr(Relation rel, AttrNumber attnum, bool *used_in_expr);
 static PartitionSpec *transformPartitionSpec(Relation rel, PartitionSpec *partspec, char *strategy);
 static void ComputePartitionAttrs(Relation rel, List *partParams, AttrNumber *partattrs,
-					  List **partexprs, Oid *partopclass, Oid *partcollation);
+					  List **partexprs, Oid *partopclass, Oid *partcollation, char strategy);
 static void CreateInheritance(Relation child_rel, Relation parent_rel);
 static void RemoveInheritance(Relation child_rel, Relation parent_rel);
 static ObjectAddress ATExecAttachPartition(List **wqueue, Relation rel,
@@ -823,7 +823,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 												&strategy);
 		ComputePartitionAttrs(rel, stmt->partspec->partParams,
 							  partattrs, &partexprs, partopclass,
-							  partcollation);
+							  partcollation, strategy);
 
 		partnatts = list_length(stmt->partspec->partParams);
 		StorePartitionKey(rel, strategy, partnatts, partattrs, partexprs,
@@ -13220,7 +13220,8 @@ transformPartitionSpec(Relation rel, PartitionSpec *partspec, char *strategy)
  */
 static void
 ComputePartitionAttrs(Relation rel, List *partParams, AttrNumber *partattrs,
-					  List **partexprs, Oid *partopclass, Oid *partcollation)
+					  List **partexprs, Oid *partopclass, Oid *partcollation,
+					  char strategy)
 {
 	int			attn;
 	ListCell   *lc;
-- 
2.6.2

