@@ -455,41 +455,43 @@ select_range_partitions(const Datum value,
455
455
const int strategy ,
456
456
WrapperNode * result )
457
457
{
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 ;
462
461
463
462
#ifdef USE_ASSERT_CHECKING
464
- bool found = false;
465
- int counter = 0 ;
463
+ bool found = false;
464
+ int counter = 0 ;
466
465
#endif
467
466
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
+
473
475
474
476
/* Initial value (no missing partitions found) */
475
477
result -> found_gap = false;
476
478
477
- /* Check boundaries */
479
+ /* Check 'ranges' array */
478
480
if (nranges == 0 )
479
481
{
480
482
result -> rangeset = NIL ;
481
483
return ;
482
484
}
485
+
486
+ /* Check corner cases */
483
487
else
484
488
{
485
489
Assert (ranges );
486
490
Assert (cmp_func );
487
491
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 );
493
495
494
496
if ((cmp_min <= 0 && strategy == BTLessStrategyNumber ) ||
495
497
(cmp_min < 0 && (strategy == BTLessEqualStrategyNumber ||
@@ -529,21 +531,16 @@ select_range_partitions(const Datum value,
529
531
/* Binary search */
530
532
while (true)
531
533
{
534
+ Assert (ranges );
532
535
Assert (cmp_func );
533
536
537
+ /* Calculate new pivot */
534
538
i = startidx + (endidx - startidx ) / 2 ;
535
539
Assert (i >= 0 && i < nranges );
536
540
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 );
547
544
548
545
is_less = (cmp_min < 0 || (cmp_min == 0 && strategy == BTLessStrategyNumber ));
549
546
is_greater = (cmp_max > 0 || (cmp_max >= 0 && strategy != BTLessStrategyNumber ));
@@ -556,13 +553,14 @@ select_range_partitions(const Datum value,
556
553
lossy = false;
557
554
else
558
555
lossy = true;
556
+
559
557
#ifdef USE_ASSERT_CHECKING
560
558
found = true;
561
559
#endif
562
560
break ;
563
561
}
564
562
565
- /* If we still haven't found partition then it doesn't exist */
563
+ /* Indices have met, looks like there's no partition */
566
564
if (startidx >= endidx )
567
565
{
568
566
result -> rangeset = NIL ;
@@ -579,6 +577,7 @@ select_range_partitions(const Datum value,
579
577
Assert (++ counter < 100 );
580
578
}
581
579
580
+ /* Should've been found by now */
582
581
Assert (found );
583
582
584
583
/* Filter partitions */
@@ -638,8 +637,8 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
638
637
639
638
* alwaysTrue = false;
640
639
/*
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.
643
642
*/
644
643
found = irange_list_find (wrap -> rangeset , index , & lossy );
645
644
@@ -670,16 +669,19 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
670
669
671
670
arg = wrapper_make_expression ((WrapperNode * ) lfirst (lc ),
672
671
index , & childAlwaysTrue );
672
+
673
673
#ifdef USE_ASSERT_CHECKING
674
674
/*
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.
677
677
*/
678
678
if (expr -> boolop == OR_EXPR )
679
679
Assert (!childAlwaysTrue );
680
+
680
681
if (expr -> boolop == AND_EXPR )
681
682
Assert (arg || childAlwaysTrue );
682
683
#endif
684
+
683
685
if (arg )
684
686
args = lappend (args , arg );
685
687
}
0 commit comments