forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_utils.c
203 lines (188 loc) · 5.33 KB
/
path_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
*******************************************************************************
*
* EXTRACTING PATH INFORMATION UTILITIES
*
*******************************************************************************
*
* Copyright (c) 2016-2021, Postgres Professional
*
* IDENTIFICATION
* aqo/path_utils.c
*
*/
#include "aqo.h"
#include "optimizer/optimizer.h"
/*
* Returns list of marginal selectivities using as an arguments for each clause
* (root, clause, 0, jointype, NULL).
* That is not quite correct for parameterized baserel and foreign key join
* cases, but nevertheless that is bearable.
*/
List *
get_selectivities(PlannerInfo *root,
List *clauses,
int varRelids,
JoinType jointype,
SpecialJoinInfo *sjinfo)
{
List *res = NIL;
ListCell *l;
double *elem;
foreach(l, clauses)
{
elem = palloc(sizeof(*elem));
*elem = clause_selectivity(root, lfirst(l), varRelids,
jointype, sjinfo);
res = lappend(res, elem);
}
return res;
}
/*
* Transforms given relids from path optimization stage format to list of
* an absolute (independent on query optimization context) relids.
*/
List *
get_list_of_relids(PlannerInfo *root, Relids relids)
{
int i;
RangeTblEntry *entry;
List *l = NIL;
if (relids == NULL)
return NIL;
i = -1;
while ((i = bms_next_member(relids, i)) >= 0)
{
entry = planner_rt_fetch(i, root);
l = lappend_int(l, entry->relid);
}
return l;
}
/*
* For given path returns the list of all clauses used in it.
* Also returns selectivities for the clauses throw the selectivities variable.
* Both clauses and selectivities returned lists are copies and therefore
* may be modified without corruption of the input data.
*/
List *
get_path_clauses(Path *path, PlannerInfo *root, List **selectivities)
{
List *inner;
List *inner_sel = NIL;
List *outer;
List *outer_sel = NIL;
List *cur = NIL;
List *cur_sel = NIL;
Assert(selectivities != NULL);
*selectivities = NIL;
if (path == NULL)
return NIL;
switch (path->type)
{
case T_NestPath:
case T_MergePath:
case T_HashPath:
cur = ((JoinPath *) path)->joinrestrictinfo;
/* Not quite correct to avoid sjinfo, but we believe in caching */
cur_sel = get_selectivities(root, cur, 0,
((JoinPath *) path)->jointype,
NULL);
outer = get_path_clauses(((JoinPath *) path)->outerjoinpath, root,
&outer_sel);
inner = get_path_clauses(((JoinPath *) path)->innerjoinpath, root,
&inner_sel);
*selectivities = list_concat(cur_sel,
list_concat(outer_sel, inner_sel));
return list_concat(list_copy(cur), list_concat(outer, inner));
break;
case T_UniquePath:
return get_path_clauses(((UniquePath *) path)->subpath, root,
selectivities);
break;
case T_GatherPath:
case T_GatherMergePath:
return get_path_clauses(((GatherPath *) path)->subpath, root,
selectivities);
break;
case T_MaterialPath:
return get_path_clauses(((MaterialPath *) path)->subpath, root,
selectivities);
break;
case T_ProjectionPath:
return get_path_clauses(((ProjectionPath *) path)->subpath, root,
selectivities);
break;
case T_SortPath:
return get_path_clauses(((SortPath *) path)->subpath, root,
selectivities);
break;
case T_GroupPath:
return get_path_clauses(((GroupPath *) path)->subpath, root,
selectivities);
break;
case T_UpperUniquePath:
return get_path_clauses(((UpperUniquePath *) path)->subpath, root,
selectivities);
break;
case T_AggPath:
return get_path_clauses(((AggPath *) path)->subpath, root,
selectivities);
break;
case T_GroupingSetsPath:
return get_path_clauses(((GroupingSetsPath *) path)->subpath, root,
selectivities);
break;
case T_WindowAggPath:
return get_path_clauses(((WindowAggPath *) path)->subpath, root,
selectivities);
break;
case T_SetOpPath:
return get_path_clauses(((SetOpPath *) path)->subpath, root,
selectivities);
break;
case T_LockRowsPath:
return get_path_clauses(((LockRowsPath *) path)->subpath, root,
selectivities);
break;
case T_LimitPath:
return get_path_clauses(((LimitPath *) path)->subpath, root,
selectivities);
break;
case T_SubqueryScanPath:
return get_path_clauses(((SubqueryScanPath *) path)->subpath, root,
selectivities);
break;
case T_AppendPath:
{
ListCell *lc;
foreach (lc, ((AppendPath *) path)->subpaths)
{
Path *subpath = lfirst(lc);
cur = list_concat(cur, list_copy(
get_path_clauses(subpath, root, selectivities)));
cur_sel = list_concat(cur_sel, *selectivities);
}
cur = list_concat(cur, list_copy(path->parent->baserestrictinfo));
*selectivities = list_concat(cur_sel,
get_selectivities(root,
path->parent->baserestrictinfo,
0, JOIN_INNER, NULL));
return cur;
}
break;
case T_ForeignPath:
/* The same as in the default case */
default:
cur = list_concat(list_copy(path->parent->baserestrictinfo),
path->param_info ?
list_copy(path->param_info->ppi_clauses) : NIL);
if (path->param_info)
cur_sel = get_selectivities(root, cur, path->parent->relid,
JOIN_INNER, NULL);
else
cur_sel = get_selectivities(root, cur, 0, JOIN_INNER, NULL);
*selectivities = cur_sel;
return cur;
break;
}
}