Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bfa56c9

Browse files
committed
introduce inline function PrelExpressionForRelid()
1 parent 68aea8d commit bfa56c9

10 files changed

+66
-83
lines changed

src/hooks.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,7 @@ pathman_join_pathlist_hook(PlannerInfo *root,
130130
}
131131

132132
/* Make copy of partitioning expression and fix Var's varno attributes */
133-
expr = inner_prel->expr;
134-
if (innerrel->relid != 1)
135-
{
136-
expr = copyObject(expr);
137-
ChangeVarNodes(expr, 1, innerrel->relid, 0);
138-
}
133+
expr = PrelExpressionForRelid(inner_prel, innerrel->relid);
139134

140135
paramsel = 1.0;
141136
foreach (lc, joinclauses)
@@ -196,9 +191,9 @@ pathman_join_pathlist_hook(PlannerInfo *root,
196191
innerrel->ppilist = saved_ppi_list;
197192

198193
/* Skip ppi->ppi_clauses don't reference partition attribute */
199-
if (!(ppi && get_partitioned_attr_clauses(ppi->ppi_clauses,
200-
inner_prel,
201-
innerrel->relid)))
194+
if (!(ppi && get_partitioning_clauses(ppi->ppi_clauses,
195+
inner_prel,
196+
innerrel->relid)))
202197
continue;
203198

204199
inner = create_runtimeappend_path(root, cur_inner_path, ppi, paramsel);
@@ -319,9 +314,7 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
319314
bool modify_append_nodes;
320315

321316
/* Make copy of partitioning expression and fix Var's varno attributes */
322-
expr = copyObject(prel->expr);
323-
if (rti != 1)
324-
ChangeVarNodes(expr, 1, rti, 0);
317+
expr = PrelExpressionForRelid(prel, rti);
325318

326319
if (prel->parttype == PT_RANGE)
327320
{

src/include/nodes_common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ clear_plan_states(CustomScanState *scan_state)
6565
}
6666
}
6767

68-
List * get_partitioned_attr_clauses(List *restrictinfo_list,
69-
const PartRelationInfo *prel,
70-
Index partitioned_rel);
68+
List * get_partitioning_clauses(List *restrictinfo_list,
69+
const PartRelationInfo *prel,
70+
Index partitioned_rel);
7171

7272
Oid * get_partition_oids(List *ranges, int *n, const PartRelationInfo *prel,
7373
bool include_parent);

src/include/relation_info.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "nodes/primnodes.h"
2222
#include "nodes/value.h"
2323
#include "port/atomics.h"
24+
#include "rewrite/rewriteManip.h"
2425
#include "storage/lock.h"
2526
#include "utils/datum.h"
2627
#include "utils/lsyscache.h"
@@ -207,7 +208,7 @@ typedef enum
207208

208209

209210
/*
210-
* PartRelationInfo field access macros.
211+
* PartRelationInfo field access macros & functions.
211212
*/
212213

213214
#define PrelParentRelid(prel) ( (prel)->key )
@@ -249,6 +250,21 @@ PrelExpressionColumnNames(const PartRelationInfo *prel)
249250
return columns;
250251
}
251252

253+
static inline Node *
254+
PrelExpressionForRelid(const PartRelationInfo *prel, Index rel_index)
255+
{
256+
Node *expr;
257+
258+
if (rel_index != PART_EXPR_VARNO)
259+
{
260+
expr = copyObject(prel->expr);
261+
ChangeVarNodes(expr, PART_EXPR_VARNO, rel_index, 0);
262+
}
263+
else expr = prel->expr;
264+
265+
return expr;
266+
}
267+
252268

253269
const PartRelationInfo *refresh_pathman_relation_info(Oid relid,
254270
Datum *values,

src/include/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
bool clause_contains_params(Node *clause);
2525
bool is_date_type_internal(Oid typid);
2626
bool check_security_policy_internal(Oid relid, Oid role);
27-
bool expr_matches_operand(Node *operand, Node *expr);
27+
bool match_expr_to_operand(Node *expr, Node *operand);
2828

2929
/*
3030
* Misc.

src/init.c

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

11111111
/* Check arg of TYPE_HASH_PROC() */
1112-
if (!expr_matches_operand(prel->expr, hash_arg))
1112+
if (!match_expr_to_operand(prel->expr, hash_arg))
11131113
return false;
11141114

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

src/nodes_common.c

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -315,65 +315,41 @@ unpack_runtimeappend_private(RuntimeAppendState *scan_state, CustomScan *cscan)
315315
scan_state->enable_parent = (bool) linitial_int(lthird(runtimeappend_private));
316316
}
317317

318-
struct check_clause_context
319-
{
320-
Node *prel_expr;
321-
int count;
322-
};
323318

324319
/* Check that one of arguments of OpExpr is expression */
325320
static bool
326-
check_clause_for_expression(Node *node, struct check_clause_context *ctx)
321+
clause_contains_prel_expr(Node *node, Node *prel_expr)
327322
{
328323
if (node == NULL)
329324
return false;
330325

331-
if (IsA(node, OpExpr))
332-
{
333-
OpExpr *expr = (OpExpr *) node;
334-
Node *left = linitial(expr->args),
335-
*right = lsecond(expr->args);
336-
337-
if (expr_matches_operand(left, ctx->prel_expr))
338-
ctx->count += 1;
339-
340-
if (expr_matches_operand(right, ctx->prel_expr))
341-
ctx->count += 1;
342-
343-
return false;
344-
}
326+
if (match_expr_to_operand(node, prel_expr))
327+
return true;
345328

346-
return expression_tree_walker(node, check_clause_for_expression, (void *) ctx);
329+
return expression_tree_walker(node, clause_contains_prel_expr, prel_expr);
347330
}
348331

349332
/*
350333
* Filter all available clauses and extract relevant ones.
351334
*/
352335
List *
353-
get_partitioned_attr_clauses(List *restrictinfo_list,
354-
const PartRelationInfo *prel,
355-
Index partitioned_rel)
336+
get_partitioning_clauses(List *restrictinfo_list,
337+
const PartRelationInfo *prel,
338+
Index partitioned_rel)
356339
{
357340
List *result = NIL;
358341
ListCell *l;
359342

360343
foreach(l, restrictinfo_list)
361344
{
362-
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
363-
struct check_clause_context ctx;
345+
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
346+
Node *prel_expr;
364347

365348
Assert(IsA(rinfo, RestrictInfo));
366349

367-
ctx.count = 0;
368-
ctx.prel_expr = prel->expr;
369-
if (partitioned_rel != 1)
370-
{
371-
ctx.prel_expr = copyObject(prel->expr);
372-
ChangeVarNodes(ctx.prel_expr, 1, partitioned_rel, 0);
373-
}
374-
check_clause_for_expression((Node *) rinfo->clause, &ctx);
350+
prel_expr = PrelExpressionForRelid(prel, partitioned_rel);
375351

376-
if (ctx.count == 1)
352+
if (clause_contains_prel_expr((Node *) rinfo->clause, prel_expr))
377353
result = lappend(result, rinfo->clause);
378354
}
379355
return result;
@@ -591,7 +567,7 @@ create_append_plan_common(PlannerInfo *root, RelOptInfo *rel,
591567
/* Since we're not scanning any real table directly */
592568
cscan->scan.scanrelid = 0;
593569

594-
cscan->custom_exprs = get_partitioned_attr_clauses(clauses, prel, rel->relid);
570+
cscan->custom_exprs = get_partitioning_clauses(clauses, prel, rel->relid);
595571
cscan->custom_plans = custom_plans;
596572
cscan->methods = scan_methods;
597573

src/partition_filter.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ select_partition_for_insert(Datum value, Oid value_type,
417417
ResultPartsStorage *parts_storage,
418418
EState *estate)
419419
{
420-
MemoryContext old_cxt;
420+
MemoryContext old_mcxt;
421421
ResultRelInfoHolder *rri_holder;
422422
Oid selected_partid = InvalidOid;
423423
Oid *parts;
@@ -439,9 +439,9 @@ select_partition_for_insert(Datum value, Oid value_type,
439439
else selected_partid = parts[0];
440440

441441
/* Replace parent table with a suitable partition */
442-
old_cxt = MemoryContextSwitchTo(estate->es_query_cxt);
442+
old_mcxt = MemoryContextSwitchTo(estate->es_query_cxt);
443443
rri_holder = scan_result_parts_storage(selected_partid, parts_storage);
444-
MemoryContextSwitchTo(old_cxt);
444+
MemoryContextSwitchTo(old_mcxt);
445445

446446
/* Could not find suitable partition */
447447
if (rri_holder == NULL)
@@ -531,12 +531,12 @@ partition_filter_create_scan_state(CustomScan *node)
531531
void
532532
partition_filter_begin(CustomScanState *node, EState *estate, int eflags)
533533
{
534-
Index varno = 1;
535-
Node *expr;
536-
MemoryContext old_cxt;
537-
PartitionFilterState *state = (PartitionFilterState *) node;
538-
const PartRelationInfo *prel;
539-
ListCell *lc;
534+
Index varno = 1;
535+
Node *expr;
536+
MemoryContext old_mcxt;
537+
PartitionFilterState *state = (PartitionFilterState *) node;
538+
const PartRelationInfo *prel;
539+
ListCell *lc;
540540

541541
/* It's convenient to store PlanState in 'custom_ps' */
542542
node->custom_ps = list_make1(ExecInitNode(state->subplan, estate, eflags));
@@ -562,9 +562,9 @@ partition_filter_begin(CustomScanState *node, EState *estate, int eflags)
562562
}
563563

564564
/* Prepare state for expression execution */
565-
old_cxt = MemoryContextSwitchTo(estate->es_query_cxt);
565+
old_mcxt = MemoryContextSwitchTo(estate->es_query_cxt);
566566
state->expr_state = ExecInitExpr((Expr *) expr, NULL);
567-
MemoryContextSwitchTo(old_cxt);
567+
MemoryContextSwitchTo(old_mcxt);
568568
}
569569

570570
/* Init ResultRelInfo cache */
@@ -595,13 +595,13 @@ partition_filter_exec(CustomScanState *node)
595595

596596
if (!TupIsNull(slot))
597597
{
598-
MemoryContext old_cxt;
599-
const PartRelationInfo *prel;
600-
ResultRelInfoHolder *rri_holder;
601-
bool isnull;
602-
Datum value;
603-
ExprDoneCond itemIsDone;
604-
TupleTableSlot *tmp_slot;
598+
MemoryContext old_mcxt;
599+
const PartRelationInfo *prel;
600+
ResultRelInfoHolder *rri_holder;
601+
bool isnull;
602+
Datum value;
603+
ExprDoneCond itemIsDone;
604+
TupleTableSlot *tmp_slot;
605605

606606
/* Fetch PartRelationInfo for this partitioned relation */
607607
prel = get_pathman_relation_info(state->partitioned_table);
@@ -616,7 +616,7 @@ partition_filter_exec(CustomScanState *node)
616616
}
617617

618618
/* Switch to per-tuple context */
619-
old_cxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
619+
old_mcxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
620620

621621
/* Execute expression */
622622
tmp_slot = econtext->ecxt_scantuple;
@@ -635,7 +635,7 @@ partition_filter_exec(CustomScanState *node)
635635
&state->result_parts, estate);
636636

637637
/* Switch back and clean up per-tuple context */
638-
MemoryContextSwitchTo(old_cxt);
638+
MemoryContextSwitchTo(old_mcxt);
639639
ResetExprContext(econtext);
640640

641641
/* Magic: replace parent's ResultRelInfo with ours */

src/pg_pathman.c

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

868868
Assert(exprnode != NULL);
869869

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

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

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

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

src/planner_tree_modification.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ static void
239239
handle_modification_query(Query *parse)
240240
{
241241
const PartRelationInfo *prel;
242+
Node *prel_expr;
242243
List *ranges;
243244
RangeTblEntry *rte;
244245
WrapperNode *wrap;
245246
Expr *expr;
246247
WalkerContext context;
247248
Index result_rel;
248-
Node *prel_expr;
249249

250250
/* Fetch index of result relation */
251251
result_rel = parse->resultRelation;
@@ -277,9 +277,7 @@ handle_modification_query(Query *parse)
277277
if (!expr) return;
278278

279279
/* Prepare partitioning expression */
280-
prel_expr = copyObject(prel->expr);
281-
if (result_rel != 1)
282-
ChangeVarNodes(prel_expr, 1, result_rel, 0);
280+
prel_expr = PrelExpressionForRelid(prel, result_rel);
283281

284282
/* Parse syntax tree and extract partition ranges */
285283
InitWalkerContext(&context, prel_expr, prel, NULL, false);

src/utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ check_security_policy_internal(Oid relid, Oid role)
108108

109109
/* Compare clause operand with expression */
110110
bool
111-
expr_matches_operand(Node *operand, Node *expr)
111+
match_expr_to_operand(Node *expr, Node *operand)
112112
{
113-
/* strip relabeling for both operand and expr */
113+
/* Strip relabeling for both operand and expr */
114114
if (operand && IsA(operand, RelabelType))
115115
operand = (Node *) ((RelabelType *) operand)->arg;
116116

0 commit comments

Comments
 (0)