Skip to content

Commit 385728b

Browse files
committed
Fix validation and constraint creation
1 parent f00f0d1 commit 385728b

File tree

5 files changed

+28
-56
lines changed

5 files changed

+28
-56
lines changed

range.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ BEGIN
164164
/* Notify backend about changes */
165165
PERFORM @extschema@.on_create_partitions(parent_relid);
166166

167-
/* Relocate data if asked to */
167+
/* Relocate data if asked to
168168
IF partition_data = true THEN
169169
PERFORM @extschema@.set_enable_parent(parent_relid, false);
170170
PERFORM @extschema@.partition_data(parent_relid);
171171
ELSE
172172
PERFORM @extschema@.set_enable_parent(parent_relid, true);
173-
END IF;
173+
END IF; */
174174

175175
RETURN p_count;
176176
END

src/include/partition_creation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Oid create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type,
3030

3131
/* Create one RANGE partition */
3232
Oid create_single_range_partition_internal(Oid parent_relid,
33+
Oid value_type,
3334
const Bound *start_value,
3435
const Bound *end_value,
3536
RangeVar *partition_rv,

src/init.c

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ static int cmp_range_entries(const void *p1, const void *p2, void *arg);
7777

7878
static bool validate_range_constraint(const Expr *expr,
7979
const PartRelationInfo *prel,
80-
const AttrNumber part_attno,
8180
Datum *lower, Datum *upper,
8281
bool *lower_null, bool *upper_null);
8382
static bool validate_range_opexpr(const Expr *expr,
8483
const PartRelationInfo *prel,
8584
const TypeCacheEntry *tce,
86-
const AttrNumber part_attno,
8785
Datum *lower, Datum *upper,
8886
bool *lower_null, bool *upper_null);
8987

@@ -93,7 +91,6 @@ static bool validate_hash_constraint(const Expr *expr,
9391

9492
static bool read_opexpr_const(const OpExpr *opexpr,
9593
const PartRelationInfo *prel,
96-
const AttrNumber part_attno,
9794
Datum *val);
9895

9996
static int oid_cmp(const void *p1, const void *p2);
@@ -403,7 +400,7 @@ fill_prel_with_partitions(const Oid *partitions,
403400
Datum lower, upper;
404401
bool lower_null, upper_null;
405402

406-
if (validate_range_constraint(con_expr, prel, 0,
403+
if (validate_range_constraint(con_expr, prel,
407404
&lower, &upper,
408405
&lower_null, &upper_null))
409406
{
@@ -917,12 +914,11 @@ cmp_range_entries(const void *p1, const void *p2, void *arg)
917914
return cmp_bounds(flinfo, &v1->min, &v2->min);
918915
}
919916

920-
/* Validates a single expression of kind VAR >= CONST or VAR < CONST */
917+
/* Validates a single expression of kind EXPRESSION >= CONST or EXPRESSION < CONST */
921918
static bool
922919
validate_range_opexpr(const Expr *expr,
923920
const PartRelationInfo *prel,
924921
const TypeCacheEntry *tce,
925-
const AttrNumber part_attno,
926922
Datum *lower, Datum *upper,
927923
bool *lower_null, bool *upper_null)
928924
{
@@ -940,7 +936,7 @@ validate_range_opexpr(const Expr *expr,
940936
opexpr = (const OpExpr *) expr;
941937

942938
/* Try reading Const value */
943-
if (!read_opexpr_const(opexpr, prel, part_attno, &val))
939+
if (!read_opexpr_const(opexpr, prel, &val))
944940
return false;
945941

946942
/* Examine the strategy (expect '>=' OR '<') */
@@ -979,16 +975,15 @@ validate_range_opexpr(const Expr *expr,
979975
/*
980976
* Validates range constraint. It MUST have one of the following formats:
981977
*
982-
* VARIABLE >= CONST AND VARIABLE < CONST
983-
* VARIABLE >= CONST
984-
* VARIABLE < CONST
978+
* EXPRESSION >= CONST AND EXPRESSION < CONST
979+
* EXPRESSION >= CONST
980+
* EXPRESSION < CONST
985981
*
986982
* Writes 'lower' & 'upper' and 'lower_null' & 'upper_null' values on success.
987983
*/
988984
static bool
989985
validate_range_constraint(const Expr *expr,
990986
const PartRelationInfo *prel,
991-
const AttrNumber part_attno,
992987
Datum *lower, Datum *upper,
993988
bool *lower_null, bool *upper_null)
994989
{
@@ -1015,7 +1010,7 @@ validate_range_constraint(const Expr *expr,
10151010
const OpExpr *opexpr = (const OpExpr *) lfirst(lc);
10161011

10171012
/* Exit immediately if something is wrong */
1018-
if (!validate_range_opexpr((const Expr *) opexpr, prel, tce, part_attno,
1013+
if (!validate_range_opexpr((const Expr *) opexpr, prel, tce,
10191014
lower, upper, lower_null, upper_null))
10201015
return false;
10211016
}
@@ -1025,55 +1020,29 @@ validate_range_constraint(const Expr *expr,
10251020
}
10261021

10271022
/* It might be just an OpExpr clause */
1028-
else return validate_range_opexpr(expr, prel, tce, part_attno,
1023+
else return validate_range_opexpr(expr, prel, tce,
10291024
lower, upper, lower_null, upper_null);
10301025
}
10311026

10321027
/*
10331028
* Reads const value from expressions of kind:
1034-
* 1) VAR >= CONST OR VAR < CONST
1029+
* 1) EXPRESSION >= CONST OR EXPRESSION < CONST
10351030
* 2) RELABELTYPE(VAR) >= CONST OR RELABELTYPE(VAR) < CONST
10361031
*/
10371032
static bool
10381033
read_opexpr_const(const OpExpr *opexpr,
10391034
const PartRelationInfo *prel,
1040-
const AttrNumber part_attno,
10411035
Datum *val)
10421036
{
1043-
const Node *left;
10441037
const Node *right;
1045-
const Var *part_attr; /* partitioned column */
10461038
const Const *constant;
10471039
bool cast_success;
10481040

10491041
if (list_length(opexpr->args) != 2)
10501042
return false;
10511043

1052-
left = linitial(opexpr->args);
10531044
right = lsecond(opexpr->args);
10541045

1055-
/* VAR is a part of RelabelType node */
1056-
if (IsA(left, RelabelType) && IsA(right, Const))
1057-
{
1058-
Var *var = (Var *) ((RelabelType *) left)->arg;
1059-
1060-
if (IsA(var, Var))
1061-
part_attr = var;
1062-
else
1063-
return false;
1064-
}
1065-
/* left arg is of type VAR */
1066-
else if (IsA(left, Var) && IsA(right, Const))
1067-
{
1068-
part_attr = (Var *) left;
1069-
}
1070-
/* Something is wrong, retreat! */
1071-
else return false;
1072-
1073-
/* VAR.attno == partitioned attribute number */
1074-
if (part_attr->varoattno != part_attno)
1075-
return false;
1076-
10771046
/* CONST is NOT NULL */
10781047
if (((Const *) right)->constisnull)
10791048
return false;

src/partition_creation.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ static RangeVar *makeRangeVarFromRelid(Oid relid);
9696
/* Create one RANGE partition [start_value, end_value) */
9797
Oid
9898
create_single_range_partition_internal(Oid parent_relid,
99+
Oid value_type,
99100
const Bound *start_value,
100101
const Bound *end_value,
101102
RangeVar *partition_rv,
102103
char *tablespace)
103104
{
104-
Oid partition_relid,
105-
value_type;
105+
Oid partition_relid;
106106
Constraint *check_constr;
107107
Node *expr;
108108
init_callback_params callback_params;
@@ -123,7 +123,7 @@ create_single_range_partition_internal(Oid parent_relid,
123123
partition_relid = create_single_partition_internal(parent_relid,
124124
partition_rv,
125125
tablespace,
126-
&value_type,
126+
NULL,
127127
&expr);
128128

129129
/* Build check constraint for RANGE partition */
@@ -559,6 +559,7 @@ spawn_partitions_val(Oid parent_relid, /* parent's Oid */
559559
bounds[1] = MakeBound(should_append ? cur_leading_bound : cur_following_bound);
560560

561561
last_partition = create_single_range_partition_internal(parent_relid,
562+
value_type,
562563
&bounds[0], &bounds[1],
563564
NULL, NULL);
564565

@@ -691,11 +692,15 @@ create_single_partition_internal(Oid parent_relid,
691692
parent_nsp_name = get_namespace_name(parent_nsp);
692693

693694
/* Fetch expression for constraint */
694-
if (expr && expr_type)
695+
if (expr_type)
696+
{
697+
*expr_type = DatumGetObjectId(config_values[Anum_pathman_config_atttype - 1]);
698+
}
699+
700+
if (expr)
695701
{
696702
char *expr_string;
697703

698-
*expr_type = DatumGetObjectId(config_values[Anum_pathman_config_atttype - 1]);
699704
expr_string = TextDatumGetCString(config_values[Anum_pathman_config_expression - 1]);
700705
*expr = get_raw_expression(parent_relid, expr_string, NULL, NULL);
701706
pfree(expr_string);
@@ -1154,11 +1159,6 @@ build_raw_range_check_tree(Node *raw_expression,
11541159
*right_arg = makeNode(A_Expr);
11551160
A_Const *left_const = makeNode(A_Const),
11561161
*right_const = makeNode(A_Const);
1157-
ColumnRef *col_ref = makeNode(ColumnRef);
1158-
1159-
/* Partitioned column */
1160-
//col_ref->fields = list_make1(makeString(attname));
1161-
col_ref->location = -1;
11621162

11631163
and_oper->boolop = AND_EXPR;
11641164
and_oper->args = NIL;
@@ -1174,7 +1174,7 @@ build_raw_range_check_tree(Node *raw_expression,
11741174

11751175
left_arg->name = list_make1(makeString(">="));
11761176
left_arg->kind = AEXPR_OP;
1177-
left_arg->lexpr = (Node *) col_ref;
1177+
left_arg->lexpr = raw_expression;
11781178
left_arg->rexpr = (Node *) left_const;
11791179
left_arg->location = -1;
11801180

@@ -1191,7 +1191,7 @@ build_raw_range_check_tree(Node *raw_expression,
11911191

11921192
right_arg->name = list_make1(makeString("<"));
11931193
right_arg->kind = AEXPR_OP;
1194-
right_arg->lexpr = (Node *) col_ref;
1194+
right_arg->lexpr = raw_expression;
11951195
right_arg->rexpr = (Node *) right_const;
11961196
right_arg->location = -1;
11971197

src/pl_range_funcs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ PG_FUNCTION_INFO_V1( validate_interval_value );
8181
Datum
8282
create_single_range_partition_pl(PG_FUNCTION_ARGS)
8383
{
84-
Oid parent_relid;
84+
Oid parent_relid,
85+
value_type;
8586

8687
/* RANGE boundaries + value type */
8788
Bound start,
@@ -101,7 +102,7 @@ create_single_range_partition_pl(PG_FUNCTION_ARGS)
101102

102103
/* Fetch mandatory args */
103104
parent_relid = PG_GETARG_OID(0);
104-
//value_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
105+
value_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
105106

106107
start = PG_ARGISNULL(1) ?
107108
MakeBoundInf(MINUS_INFINITY) :
@@ -132,6 +133,7 @@ create_single_range_partition_pl(PG_FUNCTION_ARGS)
132133

133134
/* Create a new RANGE partition and return its Oid */
134135
partition_relid = create_single_range_partition_internal(parent_relid,
136+
value_type,
135137
&start,
136138
&end,
137139
partition_name_rv,

0 commit comments

Comments
 (0)