Skip to content

Commit e898585

Browse files
committed
Simplfy code
1 parent dcfc5bc commit e898585

File tree

6 files changed

+76
-95
lines changed

6 files changed

+76
-95
lines changed

src/include/init.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ extern PathmanInitState pg_pathman_init_state;
5050
*/
5151
#define IsPathmanEnabled() ( pg_pathman_init_state.pg_pathman_enable )
5252

53-
/*
54-
* Enable or disable pg_pathman
55-
*/
56-
#define EnablePathman(b) ( pg_pathman_init_state.pg_pathman_enable = (b) )
57-
5853
/*
5954
* Check if pg_pathman is initialized & enabled.
6055
*/

src/include/partition_creation.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ Node * build_raw_hash_check_tree(const char *base_expr,
8080
void drop_check_constraint(Oid relid, AttrNumber attnum);
8181

8282
/* expression parsing functions */
83-
Node *get_expression_node(Oid relid, const char *expr, bool analyze,
84-
RTEMapItem **rte_map);
83+
Node *get_expression_node(Oid relid, const char *expr, bool analyze);
8584
Oid get_partition_expr_type(Oid relid, const char *expr);
8685

8786

src/include/relation_info.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ typedef struct
135135
RangeEntry *ranges; /* per-partition range entry or NULL */
136136

137137
Expr *expr; /* planned expression */
138-
RTEMapItem *expr_map; /* 'varno' map */
139138
PartType parttype; /* partitioning type (HASH | RANGE) */
140139
Oid atttype; /* expression type */
141140
int32 atttypmod; /* expression type modifier */
@@ -161,14 +160,14 @@ typedef struct
161160

162161
/*
163162
* CustomConst
164-
* Const with Var pointer
165-
* We can know that is CustomConst by checking `location`. It should be
166-
* equal -2
163+
* Modified Const that also stores 'varattno' attribute from some Var
164+
* We can check that is CustomConst by checking `location` attrubute.
165+
* It should be equal -2
167166
*/
168167
typedef struct
169168
{
170-
Const cns;
171-
Var *orig;
169+
Const cns;
170+
AttrNumber varattno;
172171
} CustomConst;
173172

174173
/*

src/partition_creation.c

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "utils/jsonb.h"
4444
#include "utils/snapmgr.h"
4545
#include "utils/lsyscache.h"
46+
#include "utils/memutils.h"
4647
#include "utils/syscache.h"
4748
#include "utils/typcache.h"
4849

@@ -1296,7 +1297,7 @@ build_raw_hash_check_tree(const char *base_expr,
12961297

12971298
Oid hash_proc;
12981299
TypeCacheEntry *tce;
1299-
Node *expr = get_expression_node(relid, base_expr, false, NULL);
1300+
Node *expr = get_expression_node(relid, base_expr, false);
13001301

13011302
tce = lookup_type_cache(value_type, TYPECACHE_HASH_PROC);
13021303
hash_proc = tce->hash_proc;
@@ -1702,26 +1703,40 @@ parse_expression(Oid relid, const char *expr, char **query_string_out)
17021703
return (Node *)(lfirst(list_head(parsetree_list)));
17031704
}
17041705

1706+
struct expr_mutator_context
1707+
{
1708+
Oid relid; /* partitioned table */
1709+
List *rtable; /* range table list from expression query */
1710+
};
1711+
17051712
/*
1706-
* To prevent calculation of Vars in expression, we change them with
1713+
* To prevent calculation of Vars in expression, we wrap them with
17071714
* CustomConst, and later before execution we fill it with actual value
17081715
*/
17091716
static Node *
1710-
expression_mutator(Node *node, void *context)
1717+
expression_mutator(Node *node, struct expr_mutator_context *context)
17111718
{
17121719
const TypeCacheEntry *typcache;
17131720

17141721
if (IsA(node, Var))
17151722
{
1716-
Node *new_node = newNode(sizeof(CustomConst), T_Const);
1717-
Const *new_const = (Const *)new_node;
1718-
((CustomConst *)new_node)->orig = (Var *)node;
1723+
Var *variable = (Var *) node;
1724+
Node *new_node = newNode(sizeof(CustomConst), T_Const);
1725+
Const *new_const = (Const *)new_node;
1726+
1727+
RangeTblEntry *entry = rt_fetch(variable->varno, context->rtable);
1728+
if (entry->relid != context->relid)
1729+
elog(ERROR, "Columns in the expression should "
1730+
"be only from partitioned relation");
1731+
1732+
/* we only need varattno from original Var, for now */
1733+
((CustomConst *)new_node)->varattno = ((Var *)node)->varattno;
17191734

17201735
new_const->consttype = ((Var *)node)->vartype;
17211736
new_const->consttypmod = ((Var *)node)->vartypmod;
17221737
new_const->constcollid = ((Var *)node)->varcollid;
17231738
new_const->constvalue = (Datum) 0;
1724-
new_const->constisnull = false;
1739+
new_const->constisnull = true;
17251740
new_const->location = -2;
17261741

17271742
typcache = lookup_type_cache(new_const->consttype, 0);
@@ -1730,22 +1745,25 @@ expression_mutator(Node *node, void *context)
17301745

17311746
return new_node;
17321747
}
1733-
return expression_tree_mutator(node, expression_mutator, NULL);
1748+
return expression_tree_mutator(node, expression_mutator, (void *) context);
17341749
}
17351750

17361751
/* By given relation id and expression returns node */
17371752
Node *
1738-
get_expression_node(Oid relid, const char *expr, bool analyze, RTEMapItem **rte_map)
1753+
get_expression_node(Oid relid, const char *expr, bool analyze)
17391754
{
1740-
List *querytree_list;
1741-
List *target_list;
1742-
char *query_string;
1743-
Node *parsetree = parse_expression(relid, expr, &query_string),
1744-
*result;
1745-
Query *query;
1746-
TargetEntry *target_entry;
1747-
PlannedStmt *plan;
1748-
1755+
List *querytree_list;
1756+
List *target_list;
1757+
char *query_string;
1758+
Node *parsetree,
1759+
*result;
1760+
Query *query;
1761+
TargetEntry *target_entry;
1762+
PlannedStmt *plan;
1763+
MemoryContext oldcontext;
1764+
struct expr_mutator_context context;
1765+
1766+
parsetree = parse_expression(relid, expr, &query_string),
17491767
target_list = ((SelectStmt *)parsetree)->targetList;
17501768

17511769
if (!analyze) {
@@ -1759,33 +1777,21 @@ get_expression_node(Oid relid, const char *expr, bool analyze, RTEMapItem **rte_
17591777
querytree_list = pg_analyze_and_rewrite(parsetree, query_string, NULL, 0);
17601778
query = (Query *)lfirst(list_head(querytree_list));
17611779
plan = pg_plan_query(query, 0, NULL);
1762-
1763-
if (rte_map != NULL)
1764-
{
1765-
int i = 0;
1766-
int len = list_length(plan->rtable);
1767-
ListCell *cell;
1768-
1769-
*rte_map = (RTEMapItem *)palloc0(sizeof(RTEMapItem) * (len + 1));
1770-
foreach(cell, plan->rtable)
1771-
{
1772-
RangeTblEntry *tbl = lfirst(cell);
1773-
/* only plain relation RTE */
1774-
Assert(tbl->relid > 0);
1775-
(*rte_map)[i].relid = tbl->relid;
1776-
(*rte_map)[i].res_idx = -1;
1777-
1778-
i++;
1779-
}
1780-
}
1781-
17821780
target_entry = lfirst(list_head(plan->planTree->targetlist));
17831781

17841782
/* Hooks can work now */
17851783
hooks_enabled = true;
17861784

17871785
result = (Node *)target_entry->expr;
1788-
result = expression_mutator(result, NULL);
1786+
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
1787+
1788+
/* We need relid and range table list for mutator */
1789+
context.relid = relid;
1790+
context.rtable = plan->rtable;
1791+
1792+
/* This will create new tree in TopMemoryContext */
1793+
result = expression_mutator(result, (void *) &context);
1794+
MemoryContextSwitchTo(oldcontext);
17891795
return result;
17901796
}
17911797

src/partition_filter.c

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -528,68 +528,45 @@ partition_filter_create_scan_state(CustomScan *node)
528528
return (Node *) state;
529529
}
530530

531-
static void
532-
adapt_rte_map(List *es_rangetable, RTEMapItem *rte_map)
533-
{
534-
int i = 0;
535-
ListCell *cell;
536-
537-
while (true)
538-
{
539-
int j = 1; /* rangetable entries are counting from 1 */
540-
bool found = false;
541-
542-
RTEMapItem *item = &rte_map[i++];
543-
if (item->relid == 0) /* end of array */
544-
break;
545-
546-
foreach(cell, es_rangetable)
547-
{
548-
RangeTblEntry *entry = lfirst(cell);
549-
if (entry->relid == item->relid) {
550-
item->res_idx = j;
551-
found = true;
552-
break;
553-
}
554-
555-
j++;
556-
}
557-
558-
if (!found)
559-
elog(ERROR, "Didn't found RTE entry for relid %d in expression",
560-
item->relid);
561-
}
562-
}
563-
564531
struct expr_walker_context
565532
{
566533
const PartRelationInfo *prel;
567534
TupleTableSlot *slot;
535+
bool clear;
568536
};
569537

538+
/* Fills CustomConst nodes with values from slot */
570539
static bool
571540
adapt_values (Node *node, struct expr_walker_context *context)
572541
{
573542
if (node == NULL)
574543
return false;
575544

545+
/* location == -2 means that it's our CustomConst node */
576546
if (IsA(node, Const) && ((Const *)node)->location == -2)
577547
{
578-
Var *variable;
579548
AttrNumber attnum;
580549
Const *cst;
581550
bool isNull;
582551

583552
cst = (Const *)node;
584-
variable = ((CustomConst *)node)->orig;
585553

586-
attnum = variable->varattno;
554+
attnum = ((CustomConst *)node)->varattno;
587555
Assert(attnum != InvalidAttrNumber);
588556

589-
Assert(context->slot->tts_tupleDescriptor->
590-
attrs[attnum - 1]->atttypid == cst->consttype);
591-
cst->constvalue = slot_getattr(context->slot, attnum, &isNull);
592-
cst->constisnull = isNull;
557+
if (context->clear)
558+
{
559+
cst->constvalue = (Datum) 0;
560+
cst->constisnull = true;
561+
}
562+
else
563+
{
564+
/* check that type is still same */
565+
Assert(context->slot->tts_tupleDescriptor->
566+
attrs[attnum - 1]->atttypid == cst->consttype);
567+
cst->constvalue = slot_getattr(context->slot, attnum, &isNull);
568+
cst->constisnull = isNull;
569+
}
593570
return false;
594571
}
595572

@@ -656,9 +633,9 @@ partition_filter_exec(CustomScanState *node)
656633
/* Prepare walker context */
657634
expr_walker_context.prel = prel; /* maybe slot will be enough */
658635
expr_walker_context.slot = slot;
636+
expr_walker_context.clear = true;
659637

660-
/* Fetch values from slot for expression */
661-
adapt_rte_map(estate->es_range_table, prel->expr_map);
638+
/* Clear values from slot for expression */
662639
adapt_values((Node *)prel->expr, (void *) &expr_walker_context);
663640

664641
/* Prepare state before execution */
@@ -667,6 +644,11 @@ partition_filter_exec(CustomScanState *node)
667644
/* Switch to per-tuple context */
668645
old_cxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
669646

647+
expr_walker_context.clear = false;
648+
649+
/* Fetch values from slot for expression */
650+
adapt_values((Node *)prel->expr, (void *) &expr_walker_context);
651+
670652
/* Execute expression */
671653
value = ExecEvalExpr(expr_state, econtext, &isnull, &itemIsDone);
672654

src/relation_info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ refresh_pathman_relation_info(Oid relid,
144144
* Save parsed expression to cache and use already saved expression type
145145
* from config
146146
*/
147-
prel->expr = (Expr *) get_expression_node(relid, expr, true, &prel->expr_map);
147+
prel->expr = (Expr *) get_expression_node(relid, expr, true);
148148
prel->atttype = expr_type;
149149

150150
tp = SearchSysCache1(TYPEOID, values[Anum_pathman_config_atttype - 1]);

0 commit comments

Comments
 (0)