Skip to content

Commit 9cbefb5

Browse files
committed
rename Infinitable to Bound, added plus and minus infinity, added a Bound comparison function
1 parent 833707c commit 9cbefb5

File tree

7 files changed

+176
-140
lines changed

7 files changed

+176
-140
lines changed

src/init.c

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,12 @@ fill_prel_with_partitions(const Oid *partitions,
383383
&lower_null, &upper_null))
384384
{
385385
prel->ranges[i].child_oid = partitions[i];
386-
// prel->ranges[i].min = lower;
387-
// prel->ranges[i].max = upper;
388-
// prel->ranges[i].infinite_min = lower_null;
389-
// prel->ranges[i].infinite_max = upper_null;
390-
(&prel->ranges[i].min)->value = lower;
391-
MakeInfinitable(&prel->ranges[i].min, lower, lower_null);
392-
MakeInfinitable(&prel->ranges[i].max, upper, upper_null);
386+
MakeBound(&prel->ranges[i].min,
387+
lower,
388+
lower_null ? MINUS_INFINITY : FINITE);
389+
MakeBound(&prel->ranges[i].max,
390+
upper,
391+
upper_null ? PLUS_INFINITY : FINITE);
393392
}
394393
else
395394
{
@@ -417,11 +416,15 @@ fill_prel_with_partitions(const Oid *partitions,
417416
if (prel->parttype == PT_RANGE)
418417
{
419418
MemoryContext old_mcxt;
419+
FmgrInfo flinfo;
420+
421+
/* Prepare function info */
422+
fmgr_info(prel->cmp_proc, &flinfo);
420423

421424
/* Sort partitions by RangeEntry->min asc */
422425
qsort_arg((void *) prel->ranges, PrelChildrenCount(prel),
423426
sizeof(RangeEntry), cmp_range_entries,
424-
(void *) &prel->cmp_proc);
427+
(void *) &flinfo);
425428

426429
/* Initialize 'prel->children' array */
427430
for (i = 0; i < PrelChildrenCount(prel); i++)
@@ -434,15 +437,15 @@ fill_prel_with_partitions(const Oid *partitions,
434437
// prel->ranges[i].max = datumCopy(prel->ranges[i].max,
435438
// prel->attbyval,
436439
// prel->attlen);
437-
CopyInfinitable(&(prel->ranges[i].max),
440+
CopyBound(&(prel->ranges[i].max),
438441
&(prel->ranges[i].max),
439442
prel->attbyval,
440443
prel->attlen);
441444

442445
// prel->ranges[i].min = datumCopy(prel->ranges[i].min,
443446
// prel->attbyval,
444447
// prel->attlen);
445-
CopyInfinitable(&prel->ranges[i].min,
448+
CopyBound(&prel->ranges[i].min,
446449
&prel->ranges[i].min,
447450
prel->attbyval,
448451
prel->attlen);
@@ -876,25 +879,26 @@ cmp_range_entries(const void *p1, const void *p2, void *arg)
876879
{
877880
const RangeEntry *v1 = (const RangeEntry *) p1;
878881
const RangeEntry *v2 = (const RangeEntry *) p2;
879-
880-
Oid cmp_proc_oid = *(Oid *) arg;
881-
882-
/* If range is half open */
883-
if (IsInfinite(&v1->min))
884-
{
885-
// if (IsInfinite(&v2->min))
886-
// return Int32GetDatum(0);
887-
return Int32GetDatum(-1);
888-
}
889-
if (IsInfinite(&v2->min))
890-
{
891-
return Int32GetDatum(1);
892-
}
893-
894-
/* Else if range is closed */
895-
return OidFunctionCall2(cmp_proc_oid,
896-
InfinitableGetValue(&v1->min),
897-
InfinitableGetValue(&v2->min));
882+
FmgrInfo *flinfo = (FmgrInfo *) arg;
883+
884+
return cmp_bounds(flinfo, &v1->min, &v2->min);
885+
886+
// /* If range is half open */
887+
// if (IsInfinite(&v1->min))
888+
// {
889+
// // if (IsInfinite(&v2->min))
890+
// // return Int32GetDatum(0);
891+
// return Int32GetDatum(-1);
892+
// }
893+
// if (IsInfinite(&v2->min))
894+
// {
895+
// return Int32GetDatum(1);
896+
// }
897+
898+
// /* Else if range is closed */
899+
// return OidFunctionCall2(cmp_proc_oid,
900+
// BoundGetValue(&v1->min),
901+
// BoundGetValue(&v2->min));
898902
}
899903

900904
/*

src/partition_creation.c

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ static void copy_foreign_keys(Oid parent_relid, Oid partition_oid);
7676
/* Create one RANGE partition [start_value, end_value) */
7777
Oid
7878
create_single_range_partition_internal(Oid parent_relid,
79-
const Infinitable *start_value,
80-
const Infinitable *end_value,
79+
const Bound *start_value,
80+
const Bound *end_value,
8181
Oid value_type,
8282
RangeVar *partition_rv,
8383
char *tablespace)
@@ -276,8 +276,8 @@ create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type)
276276
/* TODO */
277277
// bound_min = PrelGetRangesArray(prel)[0].min;
278278
// bound_max = PrelGetRangesArray(prel)[PrelLastChild(prel)].max;
279-
bound_min = InfinitableGetValue(&ranges[0].min);
280-
bound_max = InfinitableGetValue(&ranges[PrelLastChild(prel)].max);
279+
bound_min = BoundGetValue(&ranges[0].min);
280+
bound_max = BoundGetValue(&ranges[PrelLastChild(prel)].max);
281281

282282
/* Copy datums on order to protect them from cache invalidation */
283283
bound_min = datumCopy(bound_min, prel->attbyval, prel->attlen);
@@ -501,7 +501,7 @@ spawn_partitions_val(Oid parent_relid, /* parent's Oid */
501501
check_lt(&cmp_value_bound_finfo, value, cur_leading_bound))
502502
{
503503
Datum args[2];
504-
Infinitable bounds[2];
504+
Bound bounds[2];
505505

506506
/* Assign the 'following' boundary to current 'leading' value */
507507
cur_following_bound = cur_leading_bound;
@@ -514,8 +514,8 @@ spawn_partitions_val(Oid parent_relid, /* parent's Oid */
514514
args[0] = should_append ? cur_following_bound : cur_leading_bound;
515515
args[1] = should_append ? cur_leading_bound : cur_following_bound;
516516

517-
MakeInfinitable(&bounds[0], args[0], false);
518-
MakeInfinitable(&bounds[1], args[1], false);
517+
MakeBound(&bounds[0], args[0], FINITE);
518+
MakeBound(&bounds[1], args[1], FINITE);
519519

520520
last_partition = create_single_range_partition_internal(parent_relid,
521521
&bounds[0], &bounds[1],
@@ -781,8 +781,8 @@ copy_foreign_keys(Oid parent_relid, Oid partition_oid)
781781
/* Build RANGE check constraint expression tree */
782782
Node *
783783
build_raw_range_check_tree(char *attname,
784-
const Infinitable *start_value,
785-
const Infinitable *end_value,
784+
const Bound *start_value,
785+
const Bound *end_value,
786786
Oid value_type)
787787
{
788788
BoolExpr *and_oper = makeNode(BoolExpr);
@@ -805,7 +805,7 @@ build_raw_range_check_tree(char *attname,
805805
{
806806
/* Left boundary */
807807
left_const->val = *makeString(
808-
datum_to_cstring(InfinitableGetValue(start_value), value_type));
808+
datum_to_cstring(BoundGetValue(start_value), value_type));
809809
left_const->location = -1;
810810

811811
left_arg->name = list_make1(makeString(">="));
@@ -821,7 +821,7 @@ build_raw_range_check_tree(char *attname,
821821
{
822822
/* Right boundary */
823823
right_const->val = *makeString(
824-
datum_to_cstring(InfinitableGetValue(end_value), value_type));
824+
datum_to_cstring(BoundGetValue(end_value), value_type));
825825
right_const->location = -1;
826826

827827
right_arg->name = list_make1(makeString("<"));
@@ -842,8 +842,8 @@ build_raw_range_check_tree(char *attname,
842842
Constraint *
843843
build_range_check_constraint(Oid child_relid,
844844
char *attname,
845-
const Infinitable *start_value,
846-
const Infinitable *end_value,
845+
const Bound *start_value,
846+
const Bound *end_value,
847847
Oid value_type)
848848
{
849849
Constraint *range_constr;
@@ -879,8 +879,8 @@ build_range_check_constraint(Oid child_relid,
879879
/* Check if range overlaps with any partitions */
880880
bool
881881
check_range_available(Oid parent_relid,
882-
const Infinitable *start_value,
883-
const Infinitable *end_value,
882+
const Bound *start,
883+
const Bound *end,
884884
Oid value_type,
885885
bool raise_error)
886886
{
@@ -913,30 +913,32 @@ check_range_available(Oid parent_relid,
913913
* range ends in plus infinity then the left boundary of the first
914914
* range is on the left. Otherwise compare specific values
915915
*/
916-
c1 = (IsInfinite(start_value) || IsInfinite(&ranges[i].max)) ?
917-
-1 :
918-
FunctionCall2(&cmp_func,
919-
InfinitableGetValue(start_value),
920-
InfinitableGetValue(&ranges[i].max));
916+
// c1 = (IsInfinite(start) || IsInfinite(&ranges[i].max)) ?
917+
// -1 :
918+
// FunctionCall2(&cmp_func,
919+
// BoundGetValue(start),
920+
// BoundGetValue(&ranges[i].max));
921921
/*
922922
* Similary check that right boundary of the range we're checking is on
923923
* the right of the beginning of the current one
924924
*/
925-
c2 = (IsInfinite(end_value) || IsInfinite(&ranges[i].min)) ?
926-
1 :
927-
FunctionCall2(&cmp_func,
928-
InfinitableGetValue(end_value),
929-
InfinitableGetValue(&ranges[i].min));
925+
// c2 = (IsInfinite(end) || IsInfinite(&ranges[i].min)) ?
926+
// 1 :
927+
// FunctionCall2(&cmp_func,
928+
// BoundGetValue(end),
929+
// BoundGetValue(&ranges[i].min));
930+
931+
c1 = cmp_bounds(&cmp_func, start, &ranges[i].max);
932+
c2 = cmp_bounds(&cmp_func, end, &ranges[i].min);
930933

931934
/* There's someone! */
932935
if (c1 < 0 && c2 > 0)
933936
{
934937
if (raise_error)
935-
/* TODO: print infinity */
936938
elog(ERROR, "specified range [%s, %s) overlaps "
937939
"with existing partitions",
938-
datum_to_cstring(InfinitableGetValue(start_value), value_type),
939-
datum_to_cstring(InfinitableGetValue(end_value), value_type));
940+
!IsInfinite(start) ? datum_to_cstring(BoundGetValue(start), value_type) : "NULL",
941+
!IsInfinite(end) ? datum_to_cstring(BoundGetValue(end), value_type) : "NULL");
940942
else
941943
return false;
942944
}
@@ -1002,8 +1004,8 @@ invoke_init_callback_internal(init_callback_params *cb_params)
10021004
{
10031005
char *start_value,
10041006
*end_value;
1005-
Infinitable sv_datum = cb_params->params.range_params.start_value,
1006-
ev_datum = cb_params->params.range_params.end_value;
1007+
Bound sv_datum = cb_params->params.range_params.start_value,
1008+
ev_datum = cb_params->params.range_params.end_value;
10071009
Oid type = cb_params->params.range_params.value_type;
10081010

10091011
/* Convert min & max to CSTRING */
@@ -1023,7 +1025,7 @@ invoke_init_callback_internal(init_callback_params *cb_params)
10231025
JSB_INIT_VAL(&key, WJB_KEY, "range_min");
10241026
if (!IsInfinite(&sv_datum))
10251027
{
1026-
start_value = datum_to_cstring(InfinitableGetValue(&sv_datum), type);
1028+
start_value = datum_to_cstring(BoundGetValue(&sv_datum), type);
10271029
JSB_INIT_VAL(&val, WJB_VALUE, start_value);
10281030
}
10291031
else
@@ -1033,7 +1035,7 @@ invoke_init_callback_internal(init_callback_params *cb_params)
10331035
JSB_INIT_VAL(&key, WJB_KEY, "range_max");
10341036
if (!IsInfinite(&ev_datum))
10351037
{
1036-
end_value = datum_to_cstring(InfinitableGetValue(&ev_datum), type);
1038+
end_value = datum_to_cstring(BoundGetValue(&ev_datum), type);
10371039
JSB_INIT_VAL(&val, WJB_VALUE, end_value);
10381040
}
10391041
else

src/partition_creation.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ Oid create_partitions_for_value(Oid relid, Datum value, Oid value_type);
1818
Oid create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type);
1919

2020
Oid create_single_range_partition_internal(Oid parent_relid,
21-
const Infinitable *start_value,
22-
const Infinitable *end_value,
21+
const Bound *start_value,
22+
const Bound *end_value,
2323
Oid value_type,
2424
RangeVar *partition_rv,
2525
char *tablespace);
2626

2727
Constraint * build_range_check_constraint(Oid child_relid,
2828
char *attname,
29-
const Infinitable *start_value,
30-
const Infinitable *end_value,
29+
const Bound *start_value,
30+
const Bound *end_value,
3131
Oid value_type);
3232

3333
Node * build_raw_range_check_tree(char *attname,
34-
const Infinitable *start_value,
35-
const Infinitable *end_value,
34+
const Bound *start_value,
35+
const Bound *end_value,
3636
Oid value_type);
3737

3838
bool check_range_available(Oid parent_relid,
39-
const Infinitable *start_value,
40-
const Infinitable *end_value,
39+
const Bound *start_value,
40+
const Bound *end_value,
4141
Oid value_type,
4242
bool raise_error);
4343

@@ -69,7 +69,7 @@ typedef struct
6969

7070
struct
7171
{
72-
Infinitable start_value,
72+
Bound start_value,
7373
end_value;
7474
Oid value_type;
7575
} range_params;

src/pg_pathman.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ select_range_partitions(const Datum value,
447447

448448
/* Corner cases */
449449
cmp_min = IsInfinite(&ranges[startidx].min) ?
450-
1 : DatumGetInt32(FunctionCall2(cmp_func, value, InfinitableGetValue(&ranges[startidx].min)));
450+
1 : DatumGetInt32(FunctionCall2(cmp_func, value, BoundGetValue(&ranges[startidx].min)));
451451
cmp_max = IsInfinite(&ranges[endidx].max) ?
452-
-1 : DatumGetInt32(FunctionCall2(cmp_func, value, InfinitableGetValue(&ranges[endidx].max)));
452+
-1 : DatumGetInt32(FunctionCall2(cmp_func, value, BoundGetValue(&ranges[endidx].max)));
453453

454454
if ((cmp_min <= 0 && strategy == BTLessStrategyNumber) ||
455455
(cmp_min < 0 && (strategy == BTLessEqualStrategyNumber ||
@@ -499,9 +499,9 @@ select_range_partitions(const Datum value,
499499
// cmp_min = FunctionCall2(cmp_func, value, current_re->min);
500500
// cmp_max = FunctionCall2(cmp_func, value, current_re->max);
501501
cmp_min = IsInfinite(&current_re->min) ?
502-
1 : FunctionCall2(cmp_func, value, InfinitableGetValue(&current_re->min));
502+
1 : FunctionCall2(cmp_func, value, BoundGetValue(&current_re->min));
503503
cmp_max = IsInfinite(&current_re->max) ?
504-
-1 : FunctionCall2(cmp_func, value, InfinitableGetValue(&current_re->max));
504+
-1 : FunctionCall2(cmp_func, value, BoundGetValue(&current_re->max));
505505

506506
is_less = (cmp_min < 0 || (cmp_min == 0 && strategy == BTLessStrategyNumber));
507507
is_greater = (cmp_max > 0 || (cmp_max >= 0 && strategy != BTLessStrategyNumber));

src/pl_funcs.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,17 @@ show_partition_list_internal(PG_FUNCTION_ARGS)
397397

398398
re = &PrelGetRangesArray(prel)[usercxt->child_number];
399399

400-
/* TODO: infinite */
401-
rmin = CStringGetTextDatum(datum_to_cstring(InfinitableGetValue(&re->min),
402-
prel->atttype));
403-
rmax = CStringGetTextDatum(datum_to_cstring(InfinitableGetValue(&re->max),
404-
prel->atttype));
400+
/* Lower bound text */
401+
rmin = !IsInfinite(&re->min) ?
402+
CStringGetTextDatum(
403+
datum_to_cstring(BoundGetValue(&re->min), prel->atttype)) :
404+
CStringGetTextDatum("NULL");
405+
406+
/* Upper bound text */
407+
rmax = !IsInfinite(&re->max) ?
408+
CStringGetTextDatum(
409+
datum_to_cstring(BoundGetValue(&re->max), prel->atttype)) :
410+
CStringGetTextDatum("NULL");
405411

406412
values[Anum_pathman_pl_partition - 1] = re->child_oid;
407413
values[Anum_pathman_pl_range_min - 1] = rmin;
@@ -781,8 +787,8 @@ invoke_on_partition_created_callback(PG_FUNCTION_ARGS)
781787
{
782788
// Datum sv_datum,
783789
// ev_datum;
784-
Infinitable start,
785-
end;
790+
Bound start,
791+
end;
786792
Oid value_type;
787793

788794
if (PG_ARGISNULL(ARG_RANGE_START) || PG_ARGISNULL(ARG_RANGE_END))
@@ -791,12 +797,12 @@ invoke_on_partition_created_callback(PG_FUNCTION_ARGS)
791797
/* Fetch start & end values for RANGE + their type */
792798
// sv_datum = PG_GETARG_DATUM(ARG_RANGE_START);
793799
// ev_datum = PG_GETARG_DATUM(ARG_RANGE_END);
794-
MakeInfinitable(&start,
795-
PG_GETARG_DATUM(ARG_RANGE_START),
796-
PG_ARGISNULL(ARG_RANGE_START));
797-
MakeInfinitable(&end,
798-
PG_GETARG_DATUM(ARG_RANGE_END),
799-
PG_ARGISNULL(ARG_RANGE_END));
800+
MakeBound(&start,
801+
PG_GETARG_DATUM(ARG_RANGE_START),
802+
PG_ARGISNULL(ARG_RANGE_START) ? MINUS_INFINITY : FINITE);
803+
MakeBound(&end,
804+
PG_GETARG_DATUM(ARG_RANGE_END),
805+
PG_ARGISNULL(ARG_RANGE_END) ? PLUS_INFINITY : FINITE);
800806
value_type = get_fn_expr_argtype(fcinfo->flinfo, ARG_RANGE_START);
801807

802808
MakeInitCallbackRangeParams(&callback_params,

0 commit comments

Comments
 (0)