Skip to content

Commit 3b309f6

Browse files
committed
light refactoring, move change_varnos() to utils.h
1 parent e526274 commit 3b309f6

File tree

3 files changed

+149
-143
lines changed

3 files changed

+149
-143
lines changed

src/pg_pathman.c

Lines changed: 1 addition & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,12 @@
4040
#include "catalog/pg_type.h"
4141
#include "foreign/fdwapi.h"
4242
#include "hooks.h"
43+
#include "utils.h"
4344
#include "runtimeappend.h"
4445
#include "runtime_merge_append.h"
4546

4647
PG_MODULE_MAGIC;
4748

48-
typedef struct
49-
{
50-
Oid old_varno;
51-
Oid new_varno;
52-
} change_varno_context;
53-
5449
bool inheritance_disabled;
5550
bool pg_pathman_enable;
5651
PathmanState *pmstate;
@@ -84,9 +79,6 @@ static void handle_binary_opexpr(WalkerContext *context, WrapperNode *result, co
8479
static WrapperNode *handle_opexpr(const OpExpr *expr, WalkerContext *context);
8580
static WrapperNode *handle_boolexpr(const BoolExpr *expr, WalkerContext *context);
8681
static WrapperNode *handle_arrexpr(const ScalarArrayOpExpr *expr, WalkerContext *context);
87-
static void change_varnos_in_restrinct_info(RestrictInfo *rinfo, change_varno_context *context);
88-
static void change_varnos(Node *node, Oid old_varno, Oid new_varno);
89-
static bool change_varno_walker(Node *node, change_varno_context *context);
9082
static RestrictInfo *rebuild_restrictinfo(Node *clause, RestrictInfo *old_rinfo);
9183

9284
/* copied from allpaths.h */
@@ -740,124 +732,6 @@ wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue)
740732
}
741733
}
742734

743-
/*
744-
* Changes varno attribute in all variables nested in the node
745-
*/
746-
static void
747-
change_varnos(Node *node, Oid old_varno, Oid new_varno)
748-
{
749-
change_varno_context context;
750-
context.old_varno = old_varno;
751-
context.new_varno = new_varno;
752-
753-
change_varno_walker(node, &context);
754-
}
755-
756-
static bool
757-
change_varno_walker(Node *node, change_varno_context *context)
758-
{
759-
ListCell *lc;
760-
Var *var;
761-
EquivalenceClass *ec;
762-
EquivalenceMember *em;
763-
764-
if (node == NULL)
765-
return false;
766-
767-
switch(node->type)
768-
{
769-
case T_Var:
770-
var = (Var *) node;
771-
if (var->varno == context->old_varno)
772-
{
773-
var->varno = context->new_varno;
774-
var->varnoold = context->new_varno;
775-
}
776-
return false;
777-
778-
case T_RestrictInfo:
779-
change_varnos_in_restrinct_info((RestrictInfo *) node, context);
780-
return false;
781-
782-
case T_PathKey:
783-
change_varno_walker((Node *) ((PathKey *) node)->pk_eclass, context);
784-
return false;
785-
786-
case T_EquivalenceClass:
787-
ec = (EquivalenceClass *) node;
788-
789-
foreach(lc, ec->ec_members)
790-
change_varno_walker((Node *) lfirst(lc), context);
791-
foreach(lc, ec->ec_derives)
792-
change_varno_walker((Node *) lfirst(lc), context);
793-
return false;
794-
795-
case T_EquivalenceMember:
796-
em = (EquivalenceMember *) node;
797-
change_varno_walker((Node *) em->em_expr, context);
798-
if (bms_is_member(context->old_varno, em->em_relids))
799-
{
800-
em->em_relids = bms_del_member(em->em_relids, context->old_varno);
801-
em->em_relids = bms_add_member(em->em_relids, context->new_varno);
802-
}
803-
return false;
804-
805-
case T_TargetEntry:
806-
change_varno_walker((Node *) ((TargetEntry *) node)->expr, context);
807-
return false;
808-
809-
case T_List:
810-
foreach(lc, (List *) node)
811-
change_varno_walker((Node *) lfirst(lc), context);
812-
return false;
813-
814-
default:
815-
break;
816-
}
817-
818-
/* Should not find an unplanned subquery */
819-
Assert(!IsA(node, Query));
820-
821-
return expression_tree_walker(node, change_varno_walker, (void *) context);
822-
}
823-
824-
static void
825-
change_varnos_in_restrinct_info(RestrictInfo *rinfo, change_varno_context *context)
826-
{
827-
ListCell *lc;
828-
829-
change_varno_walker((Node *) rinfo->clause, context);
830-
if (rinfo->left_em)
831-
change_varno_walker((Node *) rinfo->left_em->em_expr, context);
832-
833-
if (rinfo->right_em)
834-
change_varno_walker((Node *) rinfo->right_em->em_expr, context);
835-
836-
if (rinfo->orclause)
837-
foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
838-
{
839-
Node *node = (Node *) lfirst(lc);
840-
change_varno_walker(node, context);
841-
}
842-
843-
/* TODO: find some elegant way to do this */
844-
if (bms_is_member(context->old_varno, rinfo->clause_relids))
845-
{
846-
rinfo->clause_relids = bms_del_member(rinfo->clause_relids, context->old_varno);
847-
rinfo->clause_relids = bms_add_member(rinfo->clause_relids, context->new_varno);
848-
}
849-
if (bms_is_member(context->old_varno, rinfo->left_relids))
850-
{
851-
rinfo->left_relids = bms_del_member(rinfo->left_relids, context->old_varno);
852-
rinfo->left_relids = bms_add_member(rinfo->left_relids, context->new_varno);
853-
}
854-
if (bms_is_member(context->old_varno, rinfo->right_relids))
855-
{
856-
rinfo->right_relids = bms_del_member(rinfo->right_relids, context->old_varno);
857-
rinfo->right_relids = bms_add_member(rinfo->right_relids, context->new_varno);
858-
}
859-
}
860-
861735
/*
862736
* Recursive function to walk through conditions tree
863737
*/
@@ -1838,22 +1712,6 @@ get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
18381712

18391713
//---------------------------------------------------------------
18401714

1841-
/*
1842-
* Returns the same list in reversed order.
1843-
*/
1844-
static List *
1845-
list_reverse(List *l)
1846-
{
1847-
List *result = NIL;
1848-
ListCell *lc;
1849-
1850-
foreach (lc, l)
1851-
{
1852-
result = lcons(lfirst(lc), result);
1853-
}
1854-
return result;
1855-
}
1856-
18571715
/*
18581716
* generate_mergeappend_paths
18591717
* Generate MergeAppend paths for an append relation

src/utils.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@
1919

2020

2121
static bool clause_contains_params_walker(Node *node, void *context);
22+
static void change_varnos_in_restrinct_info(RestrictInfo *rinfo, change_varno_context *context);
23+
static bool change_varno_walker(Node *node, change_varno_context *context);
24+
25+
/*
26+
* Returns the same list in reversed order.
27+
*/
28+
List *
29+
list_reverse(List *l)
30+
{
31+
List *result = NIL;
32+
ListCell *lc;
33+
34+
foreach (lc, l)
35+
{
36+
result = lcons(lfirst(lc), result);
37+
}
38+
return result;
39+
}
2240

2341
bool
2442
clause_contains_params(Node *clause)
@@ -141,3 +159,121 @@ check_rinfo_for_partitioned_attr(List *rinfo, Index varno, AttrNumber varattno)
141159

142160
return false;
143161
}
162+
163+
/*
164+
* Changes varno attribute in all variables nested in the node
165+
*/
166+
void
167+
change_varnos(Node *node, Oid old_varno, Oid new_varno)
168+
{
169+
change_varno_context context;
170+
context.old_varno = old_varno;
171+
context.new_varno = new_varno;
172+
173+
change_varno_walker(node, &context);
174+
}
175+
176+
static bool
177+
change_varno_walker(Node *node, change_varno_context *context)
178+
{
179+
ListCell *lc;
180+
Var *var;
181+
EquivalenceClass *ec;
182+
EquivalenceMember *em;
183+
184+
if (node == NULL)
185+
return false;
186+
187+
switch(node->type)
188+
{
189+
case T_Var:
190+
var = (Var *) node;
191+
if (var->varno == context->old_varno)
192+
{
193+
var->varno = context->new_varno;
194+
var->varnoold = context->new_varno;
195+
}
196+
return false;
197+
198+
case T_RestrictInfo:
199+
change_varnos_in_restrinct_info((RestrictInfo *) node, context);
200+
return false;
201+
202+
case T_PathKey:
203+
change_varno_walker((Node *) ((PathKey *) node)->pk_eclass, context);
204+
return false;
205+
206+
case T_EquivalenceClass:
207+
ec = (EquivalenceClass *) node;
208+
209+
foreach(lc, ec->ec_members)
210+
change_varno_walker((Node *) lfirst(lc), context);
211+
foreach(lc, ec->ec_derives)
212+
change_varno_walker((Node *) lfirst(lc), context);
213+
return false;
214+
215+
case T_EquivalenceMember:
216+
em = (EquivalenceMember *) node;
217+
change_varno_walker((Node *) em->em_expr, context);
218+
if (bms_is_member(context->old_varno, em->em_relids))
219+
{
220+
em->em_relids = bms_del_member(em->em_relids, context->old_varno);
221+
em->em_relids = bms_add_member(em->em_relids, context->new_varno);
222+
}
223+
return false;
224+
225+
case T_TargetEntry:
226+
change_varno_walker((Node *) ((TargetEntry *) node)->expr, context);
227+
return false;
228+
229+
case T_List:
230+
foreach(lc, (List *) node)
231+
change_varno_walker((Node *) lfirst(lc), context);
232+
return false;
233+
234+
default:
235+
break;
236+
}
237+
238+
/* Should not find an unplanned subquery */
239+
Assert(!IsA(node, Query));
240+
241+
return expression_tree_walker(node, change_varno_walker, (void *) context);
242+
}
243+
244+
static void
245+
change_varnos_in_restrinct_info(RestrictInfo *rinfo, change_varno_context *context)
246+
{
247+
ListCell *lc;
248+
249+
change_varno_walker((Node *) rinfo->clause, context);
250+
if (rinfo->left_em)
251+
change_varno_walker((Node *) rinfo->left_em->em_expr, context);
252+
253+
if (rinfo->right_em)
254+
change_varno_walker((Node *) rinfo->right_em->em_expr, context);
255+
256+
if (rinfo->orclause)
257+
foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
258+
{
259+
Node *node = (Node *) lfirst(lc);
260+
change_varno_walker(node, context);
261+
}
262+
263+
/* TODO: find some elegant way to do this */
264+
if (bms_is_member(context->old_varno, rinfo->clause_relids))
265+
{
266+
rinfo->clause_relids = bms_del_member(rinfo->clause_relids, context->old_varno);
267+
rinfo->clause_relids = bms_add_member(rinfo->clause_relids, context->new_varno);
268+
}
269+
if (bms_is_member(context->old_varno, rinfo->left_relids))
270+
{
271+
rinfo->left_relids = bms_del_member(rinfo->left_relids, context->old_varno);
272+
rinfo->left_relids = bms_add_member(rinfo->left_relids, context->new_varno);
273+
}
274+
if (bms_is_member(context->old_varno, rinfo->right_relids))
275+
{
276+
rinfo->right_relids = bms_del_member(rinfo->right_relids, context->old_varno);
277+
rinfo->right_relids = bms_add_member(rinfo->right_relids, context->new_varno);
278+
}
279+
}

src/utils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
1515
#include "nodes/relation.h"
1616
#include "nodes/nodeFuncs.h"
1717

18+
1819
typedef struct
1920
{
2021
RelOptInfo *child;
2122
RelOptInfo *parent;
2223
int sublevels_up;
2324
} ReplaceVarsContext;
2425

26+
typedef struct
27+
{
28+
Oid old_varno;
29+
Oid new_varno;
30+
} change_varno_context;
31+
32+
33+
List * list_reverse(List *l);
34+
2535
bool clause_contains_params(Node *clause);
2636

2737
List * build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
@@ -31,4 +41,6 @@ bool check_rinfo_for_partitioned_attr(List *rinfo,
3141
Index varno,
3242
AttrNumber varattno);
3343

44+
void change_varnos(Node *node, Oid old_varno, Oid new_varno);
45+
3446
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy