Skip to content

Commit 1204842

Browse files
committed
refactoring of function select_range_partitions()
1 parent 0b5edec commit 1204842

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

src/pg_pathman.c

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -455,41 +455,43 @@ select_range_partitions(const Datum value,
455455
const int strategy,
456456
WrapperNode *result)
457457
{
458-
const RangeEntry *current_re;
459-
bool lossy = false,
460-
is_less,
461-
is_greater;
458+
bool lossy = false,
459+
is_less,
460+
is_greater;
462461

463462
#ifdef USE_ASSERT_CHECKING
464-
bool found = false;
465-
int counter = 0;
463+
bool found = false;
464+
int counter = 0;
466465
#endif
467466

468-
int i,
469-
startidx = 0,
470-
endidx = nranges - 1,
471-
cmp_min,
472-
cmp_max;
467+
int startidx = 0,
468+
endidx = nranges - 1,
469+
cmp_min,
470+
cmp_max,
471+
i;
472+
473+
Bound value_bound = MakeBound(value); /* convert value to Bound */
474+
473475

474476
/* Initial value (no missing partitions found) */
475477
result->found_gap = false;
476478

477-
/* Check boundaries */
479+
/* Check 'ranges' array */
478480
if (nranges == 0)
479481
{
480482
result->rangeset = NIL;
481483
return;
482484
}
485+
486+
/* Check corner cases */
483487
else
484488
{
485489
Assert(ranges);
486490
Assert(cmp_func);
487491

488-
/* Corner cases */
489-
cmp_min = IsInfinite(&ranges[startidx].min) ?
490-
1 : DatumGetInt32(FunctionCall2(cmp_func, value, BoundGetValue(&ranges[startidx].min)));
491-
cmp_max = IsInfinite(&ranges[endidx].max) ?
492-
-1 : DatumGetInt32(FunctionCall2(cmp_func, value, BoundGetValue(&ranges[endidx].max)));
492+
/* Compare 'value' to absolute MIN and MAX bounds */
493+
cmp_min = cmp_bounds(cmp_func, &value_bound, &ranges[startidx].min);
494+
cmp_max = cmp_bounds(cmp_func, &value_bound, &ranges[endidx].max);
493495

494496
if ((cmp_min <= 0 && strategy == BTLessStrategyNumber) ||
495497
(cmp_min < 0 && (strategy == BTLessEqualStrategyNumber ||
@@ -529,21 +531,16 @@ select_range_partitions(const Datum value,
529531
/* Binary search */
530532
while (true)
531533
{
534+
Assert(ranges);
532535
Assert(cmp_func);
533536

537+
/* Calculate new pivot */
534538
i = startidx + (endidx - startidx) / 2;
535539
Assert(i >= 0 && i < nranges);
536540

537-
current_re = &ranges[i];
538-
539-
cmp_min = IsInfinite(&current_re->min) ?
540-
1 :
541-
FunctionCall2(cmp_func, value,
542-
BoundGetValue(&current_re->min));
543-
cmp_max = IsInfinite(&current_re->max) ?
544-
-1 :
545-
FunctionCall2(cmp_func, value,
546-
BoundGetValue(&current_re->max));
541+
/* Compare 'value' to current MIN and MAX bounds */
542+
cmp_min = cmp_bounds(cmp_func, &value_bound, &ranges[i].min);
543+
cmp_max = cmp_bounds(cmp_func, &value_bound, &ranges[i].max);
547544

548545
is_less = (cmp_min < 0 || (cmp_min == 0 && strategy == BTLessStrategyNumber));
549546
is_greater = (cmp_max > 0 || (cmp_max >= 0 && strategy != BTLessStrategyNumber));
@@ -556,13 +553,14 @@ select_range_partitions(const Datum value,
556553
lossy = false;
557554
else
558555
lossy = true;
556+
559557
#ifdef USE_ASSERT_CHECKING
560558
found = true;
561559
#endif
562560
break;
563561
}
564562

565-
/* If we still haven't found partition then it doesn't exist */
563+
/* Indices have met, looks like there's no partition */
566564
if (startidx >= endidx)
567565
{
568566
result->rangeset = NIL;
@@ -579,6 +577,7 @@ select_range_partitions(const Datum value,
579577
Assert(++counter < 100);
580578
}
581579

580+
/* Should've been found by now */
582581
Assert(found);
583582

584583
/* Filter partitions */
@@ -638,8 +637,8 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
638637

639638
*alwaysTrue = false;
640639
/*
641-
* TODO: use faster algorithm using knowledge that we enumerate indexes
642-
* sequntially.
640+
* TODO: use faster algorithm using knowledge
641+
* that we enumerate indexes sequntially.
643642
*/
644643
found = irange_list_find(wrap->rangeset, index, &lossy);
645644

@@ -670,16 +669,19 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
670669

671670
arg = wrapper_make_expression((WrapperNode *) lfirst(lc),
672671
index, &childAlwaysTrue);
672+
673673
#ifdef USE_ASSERT_CHECKING
674674
/*
675-
* We shouldn't get there for always true clause under OR and
676-
* always false clause under AND.
675+
* We shouldn't get there for always true clause
676+
* under OR and always false clause under AND.
677677
*/
678678
if (expr->boolop == OR_EXPR)
679679
Assert(!childAlwaysTrue);
680+
680681
if (expr->boolop == AND_EXPR)
681682
Assert(arg || childAlwaysTrue);
682683
#endif
684+
683685
if (arg)
684686
args = lappend(args, arg);
685687
}

src/relation_info.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ cmp_bounds(FmgrInfo *cmp_func, const Bound *b1, const Bound *b2)
7979
{
8080
if (IsMinusInfinity(b1) || IsPlusInfinity(b2))
8181
return -1;
82+
8283
if (IsMinusInfinity(b2) || IsPlusInfinity(b1))
8384
return 1;
8485

8586
Assert(cmp_func);
8687

87-
return FunctionCall2(cmp_func, BoundGetValue(b1), BoundGetValue(b2));
88+
return DatumGetInt32(FunctionCall2(cmp_func,
89+
BoundGetValue(b1),
90+
BoundGetValue(b2)));
8891
}
8992

9093

0 commit comments

Comments
 (0)