Skip to content

Commit 11b1d5e

Browse files
committed
light refactoring (utils.c etc)
1 parent 6e63ce0 commit 11b1d5e

File tree

10 files changed

+105
-114
lines changed

10 files changed

+105
-114
lines changed

src/include/partition_creation.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
/* ACL privilege for partition creation */
22-
#define ACL_SPAWN_PARTITIONS ACL_INSERT
22+
#define ACL_SPAWN_PARTITIONS ACL_INSERT
2323

2424

2525
/* Create RANGE partitions to store some value */
@@ -87,10 +87,14 @@ typedef struct
8787

8888
/* Expression parsing functions */
8989
PartExpressionInfo *get_part_expression_info(Oid relid,
90-
const char *expr_string, bool check_hash_func, bool make_plan);
91-
92-
Node *get_raw_expression(Oid relid, const char *expr, char **query_string_out,
93-
Node **parsetree);
90+
const char *expr_string,
91+
bool check_hash_func,
92+
bool make_plan);
93+
94+
Node *parse_partitioning_expression(Oid relid,
95+
const char *expression,
96+
char **query_string_out,
97+
Node **parsetree_out);
9498

9599
/* Update triggers */
96100
void create_single_update_trigger_internal(Oid partition_relid,

src/include/relation_info.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#include "nodes/bitmapset.h"
1919
#include "nodes/nodes.h"
2020
#include "nodes/primnodes.h"
21+
#include "nodes/value.h"
2122
#include "port/atomics.h"
2223
#include "storage/lock.h"
2324
#include "utils/datum.h"
24-
#include "nodes/primnodes.h"
25+
#include "utils/lsyscache.h"
2526

2627

2728
/* Range bound */
@@ -237,6 +238,21 @@ PrelLastChild(const PartRelationInfo *prel)
237238
return PrelChildrenCount(prel) - 1; /* last partition */
238239
}
239240

241+
static inline List *
242+
PrelExpressionColumnNames(const PartRelationInfo *prel)
243+
{
244+
List *columns = NIL;
245+
int j = -1;
246+
247+
while ((j = bms_next_member(prel->expr_atts, j)) >= 0)
248+
{
249+
char *attname = get_attname(prel->key, j);
250+
columns = lappend(columns, makeString(attname));
251+
}
252+
253+
return columns;
254+
}
255+
240256

241257
const PartRelationInfo *refresh_pathman_relation_info(Oid relid,
242258
Datum *values,

src/include/utils.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
bool clause_contains_params(Node *clause);
2929
bool is_date_type_internal(Oid typid);
3030
bool check_security_policy_internal(Oid relid, Oid role);
31-
bool match_expr_to_operand(Node *operand, Node *expr);
31+
bool expr_matches_operand(Node *operand, Node *expr);
3232

3333
/*
3434
* Misc.
@@ -68,6 +68,4 @@ RangeVar ** qualified_relnames_to_rangevars(char **relnames, size_t nrelnames);
6868
AttrNumber *get_pathman_attributes_map(const PartRelationInfo *prel,
6969
Relation child);
7070

71-
List *get_part_expression_columns(const PartRelationInfo *prel);
72-
7371
#endif /* PATHMAN_UTILS_H */

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ validate_hash_constraint(const Expr *expr,
10371037
hash_arg = (Node *) linitial(type_hash_proc_expr->args);
10381038

10391039
/* Check arg of TYPE_HASH_PROC() */
1040-
if (!match_expr_to_operand(prel->expr, hash_arg))
1040+
if (!expr_matches_operand(prel->expr, hash_arg))
10411041
return false;
10421042

10431043
/* Check that PARTITIONS_COUNT is equal to total amount of partitions */

src/nodes_common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ check_clause_for_expression(Node *node, struct check_clause_context *ctx)
335335
Node *left = linitial(expr->args),
336336
*right = lsecond(expr->args);
337337

338-
if (match_expr_to_operand(left, ctx->prel_expr))
338+
if (expr_matches_operand(left, ctx->prel_expr))
339339
ctx->count += 1;
340340

341-
if (match_expr_to_operand(right, ctx->prel_expr))
341+
if (expr_matches_operand(right, ctx->prel_expr))
342342
ctx->count += 1;
343343

344344
return false;

src/partition_creation.c

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ static void postprocess_child_table_and_atts(Oid parent_relid, Oid partition_rel
8484
static Oid text_to_regprocedure(text *proname_args);
8585

8686
static Constraint *make_constraint_common(char *name, Node *raw_expr);
87-
static Node *get_constraint_expression(Oid parent_relid,
88-
Oid *expr_type, List **columns);
8987
static Value make_string_value_struct(char *str);
9088
static Value make_int_value_struct(int int_val);
9189

90+
static Node *get_partitioning_expression(Oid parent_relid,
91+
Oid *expr_type,
92+
List **columns);
93+
9294
/*
9395
* ---------------------------------------
9496
* Public interface (partition creation)
@@ -123,7 +125,7 @@ create_single_range_partition_internal(Oid parent_relid,
123125
}
124126

125127
/* check pathman config and fill variables */
126-
expr = get_constraint_expression(parent_relid, NULL, &trigger_columns);
128+
expr = get_partitioning_expression(parent_relid, NULL, &trigger_columns);
127129

128130
/* Create a partition & get 'partitioning expression' */
129131
partition_relid = create_single_partition_internal(parent_relid,
@@ -163,7 +165,7 @@ create_single_hash_partition_internal(Oid parent_relid,
163165
char *tablespace)
164166
{
165167
Oid partition_relid,
166-
value_type;
168+
expr_type;
167169
Constraint *check_constr;
168170
Node *expr;
169171
init_callback_params callback_params;
@@ -187,14 +189,14 @@ create_single_hash_partition_internal(Oid parent_relid,
187189
tablespace);
188190

189191
/* check pathman config and fill variables */
190-
expr = get_constraint_expression(parent_relid, &value_type, &trigger_columns);
192+
expr = get_partitioning_expression(parent_relid, &expr_type, &trigger_columns);
191193

192194
/* Build check constraint for HASH partition */
193195
check_constr = build_hash_check_constraint(partition_relid,
194196
expr,
195197
part_idx,
196198
part_count,
197-
value_type);
199+
expr_type);
198200

199201
/* Cook args for init_callback */
200202
MakeInitCallbackHashParams(&callback_params,
@@ -1710,39 +1712,33 @@ validate_part_expression(Node *node, void *context)
17101712
return expression_tree_walker(node, validate_part_expression, context);
17111713
}
17121714

1713-
/* Wraps expression by SELECT query and returns parsed tree */
1715+
/* Wraps expression by SELECT query and returns parse tree */
17141716
Node *
1715-
get_raw_expression(Oid relid, const char *expr, char **query_string_out,
1716-
Node **parsetree)
1717+
parse_partitioning_expression(Oid relid,
1718+
const char *expression,
1719+
char **query_string_out,
1720+
Node **parsetree_out)
17171721
{
1718-
Node *result;
1719-
SelectStmt *select_stmt;
1720-
ResTarget *target;
1722+
SelectStmt *select_stmt;
1723+
List *parsetree_list;
17211724

1722-
char *fmt = "SELECT (%s) FROM ONLY %s.\"%s\"";
1723-
char *relname = get_rel_name(relid),
1724-
*namespace_name = get_namespace_name(get_rel_namespace(relid));
1725-
List *parsetree_list;
1726-
char *query_string = psprintf(fmt, expr, namespace_name, relname);
1725+
char *sql = "SELECT (%s) FROM ONLY %s.\"%s\"";
1726+
char *relname = get_rel_name(relid),
1727+
*nspname = get_namespace_name(get_rel_namespace(relid));
1728+
char *query_string = psprintf(sql, expression, nspname, relname);
17271729

17281730
parsetree_list = raw_parser(query_string);
17291731
Assert(list_length(parsetree_list) == 1);
17301732

1733+
select_stmt = (SelectStmt *) linitial(parsetree_list);
1734+
17311735
if (query_string_out)
1732-
{
17331736
*query_string_out = query_string;
1734-
}
17351737

1736-
select_stmt = (SelectStmt *) linitial(parsetree_list);
1738+
if (parsetree_out)
1739+
*parsetree_out = (Node *) select_stmt;
17371740

1738-
if (parsetree)
1739-
{
1740-
*parsetree = (Node *) select_stmt;
1741-
}
1742-
1743-
target = (ResTarget *) linitial(select_stmt->targetList);
1744-
result = (Node *) target->val;
1745-
return result;
1741+
return ((ResTarget *) linitial(select_stmt->targetList))->val;
17461742
}
17471743

17481744
/*
@@ -1751,7 +1747,7 @@ get_raw_expression(Oid relid, const char *expr, char **query_string_out,
17511747
*/
17521748
PartExpressionInfo *
17531749
get_part_expression_info(Oid relid, const char *expr_string,
1754-
bool check_hash_func, bool make_plan)
1750+
bool check_hash_func, bool make_plan)
17551751
{
17561752
Node *expr_node,
17571753
*parsetree;
@@ -1766,12 +1762,12 @@ get_part_expression_info(Oid relid, const char *expr_string,
17661762
expr_info = palloc(sizeof(PartExpressionInfo));
17671763

17681764
pathman_parse_context = AllocSetContextCreate(TopPathmanContext,
1769-
"pathman parse context",
1770-
ALLOCSET_DEFAULT_SIZES);
1765+
"pathman parse context",
1766+
ALLOCSET_DEFAULT_SIZES);
17711767

17721768
/* Keep raw expression */
1773-
expr_info->raw_expr = get_raw_expression(relid, expr_string,
1774-
&query_string, &parsetree);
1769+
expr_info->raw_expr = parse_partitioning_expression(relid, expr_string,
1770+
&query_string, &parsetree);
17751771

17761772
/* If expression is just column we check that is not null */
17771773
if (IsA(expr_info->raw_expr, ColumnRef))
@@ -1892,12 +1888,11 @@ extract_column_names(Node *node, struct extract_column_names_context *ctx)
18921888
return raw_expression_tree_walker(node, extract_column_names, ctx);
18931889
}
18941890

1895-
/*
1896-
* Returns raw partitioning expression, and if specified returns
1897-
* columns from expression and its type
1898-
*/
1891+
/* Returns raw partitioning expression + expr_type + columns */
18991892
static Node *
1900-
get_constraint_expression(Oid parent_relid, Oid *expr_type, List **columns)
1893+
get_partitioning_expression(Oid parent_relid,
1894+
Oid *expr_type, /* ret val #1 */
1895+
List **columns) /* ret val #2 */
19011896
{
19021897
/* Values extracted from PATHMAN_CONFIG */
19031898
Datum config_values[Natts_pathman_config];
@@ -1906,28 +1901,24 @@ get_constraint_expression(Oid parent_relid, Oid *expr_type, List **columns)
19061901
char *expr_string;
19071902

19081903
/* Check that table is registered in PATHMAN_CONFIG */
1909-
if (!pathman_config_contains_relation(parent_relid,
1910-
config_values, config_nulls, NULL, NULL))
1904+
if (!pathman_config_contains_relation(parent_relid, config_values,
1905+
config_nulls, NULL, NULL))
19111906
elog(ERROR, "table \"%s\" is not partitioned",
19121907
get_rel_name_or_relid(parent_relid));
19131908

1914-
/*
1915-
* We need expression type for hash functions. Range functions don't need
1916-
* this feature.
1917-
*/
1909+
/* We need expression type for hash functions */
19181910
if (expr_type)
19191911
*expr_type = DatumGetObjectId(config_values[Anum_pathman_config_atttype - 1]);
19201912

19211913
expr_string = TextDatumGetCString(config_values[Anum_pathman_config_expression - 1]);
1922-
expr = get_raw_expression(parent_relid, expr_string, NULL, NULL);
1914+
expr = parse_partitioning_expression(parent_relid, expr_string, NULL, NULL);
19231915
pfree(expr_string);
19241916

19251917
if (columns)
19261918
{
1927-
struct extract_column_names_context ctx;
1928-
ctx.columns = NIL;
1929-
extract_column_names(expr, &ctx);
1930-
*columns = ctx.columns;
1919+
struct extract_column_names_context context = { NIL };
1920+
extract_column_names(expr, &context);
1921+
*columns = context.columns;
19311922
}
19321923

19331924
return expr;

src/pg_pathman.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ handle_arrexpr(const ScalarArrayOpExpr *expr, WalkerContext *context)
868868

869869
Assert(exprnode != NULL);
870870

871-
if (!match_expr_to_operand(context->prel_expr, exprnode))
871+
if (!expr_matches_operand(context->prel_expr, exprnode))
872872
goto handle_arrexpr_return;
873873

874874
if (arraynode && IsA(arraynode, Const) &&
@@ -1146,14 +1146,14 @@ pull_var_param(const WalkerContext *ctx,
11461146
Node *left = linitial(expr->args),
11471147
*right = lsecond(expr->args);
11481148

1149-
if (match_expr_to_operand(left, ctx->prel_expr))
1149+
if (expr_matches_operand(left, ctx->prel_expr))
11501150
{
11511151
*var_ptr = left;
11521152
*param_ptr = right;
11531153
return true;
11541154
}
11551155

1156-
if (match_expr_to_operand(right, ctx->prel_expr))
1156+
if (expr_matches_operand(right, ctx->prel_expr))
11571157
{
11581158
*var_ptr = right;
11591159
*param_ptr = left;

src/pl_funcs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ create_update_triggers(PG_FUNCTION_ARGS)
13321332
trigname = build_update_trigger_name_internal(parent);
13331333

13341334
/* Create trigger for parent */
1335-
columns = get_part_expression_columns(prel);
1335+
columns = PrelExpressionColumnNames(prel);
13361336
create_single_update_trigger_internal(parent, trigname, columns);
13371337

13381338
/* Fetch children array */
@@ -1363,7 +1363,7 @@ create_single_update_trigger(PG_FUNCTION_ARGS)
13631363
trigname = build_update_trigger_name_internal(parent);
13641364

13651365
/* Generate list of columns used in expression */
1366-
columns = get_part_expression_columns(prel);
1366+
columns = PrelExpressionColumnNames(prel);
13671367
create_single_update_trigger_internal(child, trigname, columns);
13681368

13691369
PG_RETURN_VOID();

src/pl_range_funcs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ static void merge_range_partitions_internal(Oid parent,
6060
Oid *parts,
6161
uint32 nparts);
6262
static void modify_range_constraint(Oid child_relid,
63-
const char *attname,
64-
Oid atttype,
63+
const char *expression,
64+
Oid expression_type,
6565
const Bound *lower,
6666
const Bound *upper);
6767
static char *get_qualified_rel_name(Oid relid);
@@ -554,7 +554,7 @@ build_range_condition(PG_FUNCTION_ARGS)
554554
MakeBoundInf(PLUS_INFINITY) :
555555
MakeBound(PG_GETARG_DATUM(3));
556556

557-
expr = get_raw_expression(partition_relid, expression, NULL, NULL);
557+
expr = parse_partitioning_expression(partition_relid, expression, NULL, NULL);
558558
con = build_range_check_constraint(partition_relid,
559559
expr,
560560
&min, &max,
@@ -1016,8 +1016,8 @@ interval_is_trivial(Oid atttype, Datum interval, Oid interval_type)
10161016
*/
10171017
static void
10181018
modify_range_constraint(Oid child_relid,
1019-
const char *attname,
1020-
Oid atttype,
1019+
const char *expression,
1020+
Oid expression_type,
10211021
const Bound *lower,
10221022
const Bound *upper)
10231023
{
@@ -1029,14 +1029,14 @@ modify_range_constraint(Oid child_relid,
10291029
drop_check_constraint(child_relid);
10301030

10311031
/* Parse expression */
1032-
expr = get_raw_expression(child_relid, attname, NULL, NULL);
1032+
expr = parse_partitioning_expression(child_relid, expression, NULL, NULL);
10331033

10341034
/* Build a new one */
10351035
constraint = build_range_check_constraint(child_relid,
10361036
expr,
10371037
lower,
10381038
upper,
1039-
atttype);
1039+
expression_type);
10401040

10411041
/* Open the relation and add new check constraint */
10421042
partition_rel = heap_open(child_relid, AccessExclusiveLock);

0 commit comments

Comments
 (0)