Commit f0a0c17c authored by Robert Haas's avatar Robert Haas

Refactor get_partition_for_tuple a bit.

Pending patches for both default partitioning and hash partitioning
find the current coding pattern to be inconvenient.  Change it so that
we switch on the partitioning method first and then do whatever is
needed.

Amul Sul, reviewed by Jeevan Ladhe, with a few adjustments by me.

Discussion: http://postgr.es/m/CAAJ_b97mTb=dG2pv6+1ougxEVZFVnZJajW+0QHj46mEE7WsoOQ@mail.gmail.com
Discussion: http://postgr.es/m/CAOgcT0M37CAztEinpvjJc18EdHfm23fw0EG9-36Ya=+rEFUqaQ@mail.gmail.com
parent 3ca930fc
...@@ -1920,10 +1920,7 @@ get_partition_for_tuple(PartitionDispatch *pd, ...@@ -1920,10 +1920,7 @@ get_partition_for_tuple(PartitionDispatch *pd,
PartitionDispatch parent; PartitionDispatch parent;
Datum values[PARTITION_MAX_KEYS]; Datum values[PARTITION_MAX_KEYS];
bool isnull[PARTITION_MAX_KEYS]; bool isnull[PARTITION_MAX_KEYS];
int cur_offset, int result;
cur_index;
int i,
result;
ExprContext *ecxt = GetPerTupleExprContext(estate); ExprContext *ecxt = GetPerTupleExprContext(estate);
TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple; TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple;
...@@ -1935,6 +1932,7 @@ get_partition_for_tuple(PartitionDispatch *pd, ...@@ -1935,6 +1932,7 @@ get_partition_for_tuple(PartitionDispatch *pd,
PartitionDesc partdesc = parent->partdesc; PartitionDesc partdesc = parent->partdesc;
TupleTableSlot *myslot = parent->tupslot; TupleTableSlot *myslot = parent->tupslot;
TupleConversionMap *map = parent->tupmap; TupleConversionMap *map = parent->tupmap;
int cur_index = -1;
if (myslot != NULL && map != NULL) if (myslot != NULL && map != NULL)
{ {
...@@ -1966,12 +1964,38 @@ get_partition_for_tuple(PartitionDispatch *pd, ...@@ -1966,12 +1964,38 @@ get_partition_for_tuple(PartitionDispatch *pd,
ecxt->ecxt_scantuple = slot; ecxt->ecxt_scantuple = slot;
FormPartitionKeyDatum(parent, slot, estate, values, isnull); FormPartitionKeyDatum(parent, slot, estate, values, isnull);
if (key->strategy == PARTITION_STRATEGY_RANGE) /* Route as appropriate based on partitioning strategy. */
switch (key->strategy)
{ {
/* case PARTITION_STRATEGY_LIST:
* Since we cannot route tuples with NULL partition keys through a
* range-partitioned table, simply return that no partition exists if (isnull[0])
*/ {
if (partition_bound_accepts_nulls(partdesc->boundinfo))
cur_index = partdesc->boundinfo->null_index;
}
else
{
bool equal = false;
int cur_offset;
cur_offset = partition_bound_bsearch(key,
partdesc->boundinfo,
values,
false,
&equal);
if (cur_offset >= 0 && equal)
cur_index = partdesc->boundinfo->indexes[cur_offset];
}
break;
case PARTITION_STRATEGY_RANGE:
{
bool equal = false;
int cur_offset;
int i;
/* No range includes NULL. */
for (i = 0; i < key->partnatts; i++) for (i = 0; i < key->partnatts; i++)
{ {
if (isnull[i]) if (isnull[i])
...@@ -1982,46 +2006,26 @@ get_partition_for_tuple(PartitionDispatch *pd, ...@@ -1982,46 +2006,26 @@ get_partition_for_tuple(PartitionDispatch *pd,
goto error_exit; goto error_exit;
} }
} }
}
/* cur_offset = partition_bound_bsearch(key,
* A null partition key is only acceptable if null-accepting list partdesc->boundinfo,
* partition exists. values,
*/ false,
cur_index = -1; &equal);
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;
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;
case PARTITION_STRATEGY_RANGE:
/* /*
* Offset returned is such that the bound at offset is * The offset returned is such that the bound at cur_offset
* found to be less or equal with the tuple. So, the bound * is less than or equal to the tuple value, so the bound
* at offset+1 would be the upper bound. * at offset+1 is the upper bound.
*/ */
cur_index = partdesc->boundinfo->indexes[cur_offset + 1]; cur_index = partdesc->boundinfo->indexes[cur_offset + 1];
}
break; break;
default: default:
elog(ERROR, "unexpected partition strategy: %d", elog(ERROR, "unexpected partition strategy: %d",
(int) key->strategy); (int) key->strategy);
} }
}
/* /*
* cur_index < 0 means we failed to find a partition of this parent. * cur_index < 0 means we failed to find a partition of this parent.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment