-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathpgduckdb_planner.cpp
More file actions
248 lines (200 loc) · 7.56 KB
/
pgduckdb_planner.cpp
File metadata and controls
248 lines (200 loc) · 7.56 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "pgduckdb/pgduckdb_planner.hpp"
#include "duckdb.hpp"
#include "pgduckdb/catalog/pgduckdb_transaction.hpp"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_planner.hpp"
extern "C" {
#include "postgres.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodes.h"
#include "nodes/params.h"
#include "optimizer/optimizer.h"
#include "optimizer/planner.h"
#include "optimizer/planmain.h"
#include "tcop/pquery.h"
#include "utils/syscache.h"
#include "utils/guc.h"
#include "parser/parse_relation.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "pgduckdb/pgduckdb_ruleutils.h"
#if PG_VERSION_NUM >= 180000
#include "executor/executor.h"
#endif
}
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_node.hpp"
#include "pgduckdb/vendor/pg_list.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
duckdb::unique_ptr<duckdb::PreparedStatement>
DuckdbPrepare(const Query *query, const char *explain_prefix) {
Query *copied_query = (Query *)copyObjectImpl(query);
const char *query_string = pgduckdb_get_querydef(copied_query);
if (explain_prefix) {
query_string = psprintf("%s %s", explain_prefix, query_string);
}
elog(DEBUG2, "(PGDuckDB/DuckdbPrepare) Preparing: %s", query_string);
auto con = pgduckdb::DuckDBManager::GetConnection();
return con->context->Prepare(query_string);
}
static Plan *
CreatePlan(Query *query, bool throw_error) {
int elevel = throw_error ? ERROR : WARNING;
/*
* Prepare the query, se we can get the returned types and column names.
*/
duckdb::unique_ptr<duckdb::PreparedStatement> prepared_query = DuckdbPrepare(query);
if (prepared_query->HasError()) {
elog(elevel, "(PGDuckDB/CreatePlan) Prepared query returned an error: %s", prepared_query->GetError().c_str());
return nullptr;
}
CustomScan *duckdb_node = makeNode(CustomScan);
auto &prepared_result_types = prepared_query->GetTypes();
elog(DEBUG2, "(PGDuckDB/CreatePlan) DuckDB Prepare returned %zu result column(s)", prepared_result_types.size());
for (size_t j = 0; j < prepared_result_types.size(); j++) {
elog(DEBUG2, "(PGDuckDB/CreatePlan) col[%zu] = %s name=%s", j, prepared_result_types[j].ToString().c_str(),
prepared_query->GetNames()[j].c_str());
}
for (size_t i = 0; i < prepared_result_types.size(); i++) {
Oid postgresColumnOid = pgduckdb::GetPostgresDuckDBType(prepared_result_types[i], throw_error);
if (!OidIsValid(postgresColumnOid)) {
return nullptr;
}
HeapTuple tp;
Form_pg_type typtup;
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(postgresColumnOid));
if (!HeapTupleIsValid(tp)) {
elog(elevel, "(PGDuckDB/CreatePlan) Cache lookup failed for type %u", postgresColumnOid);
return nullptr;
}
typtup = (Form_pg_type)GETSTRUCT(tp);
typtup->typtypmod = pgduckdb::GetPostgresDuckDBTypemod(prepared_result_types[i]);
/*
* We hardcode varno 1 here, because our final plan will only have a
* single RTE (this custom scan). In the past we put 0 here, and then
* filled it in later. If at some point we need multiple RTEs again, we
* might want to start doing that again.
*/
Var *var = makeVar(1, i + 1, postgresColumnOid, typtup->typtypmod, typtup->typcollation, 0);
TargetEntry *target_entry =
makeTargetEntry((Expr *)var, i + 1, (char *)pstrdup(prepared_query->GetNames()[i].c_str()), false);
/* Our custom scan node needs the custom_scan_tlist to be set */
duckdb_node->custom_scan_tlist = lappend(duckdb_node->custom_scan_tlist, copyObjectImpl(target_entry));
/* For the plan its targetlist we use INDEX_VAR as the varno, which
* means it references our custom_scan_tlist. */
var->varno = INDEX_VAR;
/* But we also need an actual target list, because Postgres expects it
* for things like materialization */
duckdb_node->scan.plan.targetlist = lappend(duckdb_node->scan.plan.targetlist, target_entry);
ReleaseSysCache(tp);
}
duckdb_node->custom_private = list_make1(query);
duckdb_node->methods = &duckdb_scan_scan_methods;
return (Plan *)duckdb_node;
}
/* Creates a matching RangeTblEntry for the given CustomScan node */
static RangeTblEntry *
DuckdbRangeTableEntry(CustomScan *custom_scan) {
List *column_names = NIL;
foreach_node(TargetEntry, target_entry, custom_scan->scan.plan.targetlist) {
column_names = lappend(column_names, makeString(target_entry->resname));
}
RangeTblEntry *rte = makeNode(RangeTblEntry);
/* We need to choose an RTE kind here. RTE_RELATION does not work due to
* various asserts that fail due to us not setting some of the fields on
* the entry. Instead of filling those fields in with dummy values we use
* RTE_NAMEDTUPLESTORE, for which no special fields exist. */
rte->rtekind = RTE_NAMEDTUPLESTORE;
rte->eref = makeAlias("duckdb_scan", column_names);
rte->inFromCl = true;
return rte;
}
static void
check_view_perms_recursive(Query *query) {
ListCell *lc;
if (query == NULL) {
return;
}
foreach (lc, query->rtable) {
RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc);
#if PG_VERSION_NUM < 160000
if (rte->relkind == RELKIND_VIEW) {
bool result = ExecCheckRTEPerms(rte);
if (!result) {
aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_VIEW, get_rel_name(rte->relid));
}
}
#else
if (rte->perminfoindex != 0 && rte->relkind == RELKIND_VIEW) {
RTEPermissionInfo *perminfo = getRTEPermissionInfo(query->rteperminfos, rte);
bool result = ExecCheckOneRelPerms(perminfo);
if (!result) {
aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_VIEW, get_rel_name(perminfo->relid));
}
}
#endif
if (rte->rtekind == RTE_SUBQUERY && rte->subquery) {
check_view_perms_recursive(rte->subquery);
}
}
if (query->cteList) {
ListCell *lc_cte;
foreach (lc_cte, query->cteList) {
CommonTableExpr *cte = (CommonTableExpr *)lfirst(lc_cte);
if (IsA(cte->ctequery, Query)) {
check_view_perms_recursive((Query *)cte->ctequery);
}
}
}
}
PlannedStmt *
DuckdbPlanNode(Query *parse, int cursor_options, bool throw_error) {
/* Properly check perms if there's a view or WITH statement */
check_view_perms_recursive(parse);
/* We need to check can we DuckDB create plan */
Plan *duckdb_plan = InvokeCPPFunc(CreatePlan, parse, throw_error);
CustomScan *custom_scan = castNode(CustomScan, duckdb_plan);
if (!duckdb_plan) {
return nullptr;
}
/*
* If creating a plan for a scrollable cursor add a Material node at the
* top because or CustomScan does not support backwards scanning.
*/
if (cursor_options & CURSOR_OPT_SCROLL) {
duckdb_plan = materialize_finished_plan(duckdb_plan);
}
RangeTblEntry *rte = DuckdbRangeTableEntry(custom_scan);
PlannedStmt *result = makeNode(PlannedStmt);
result->commandType = parse->commandType;
result->queryId = parse->queryId;
result->hasReturning = (parse->returningList != NIL);
result->hasModifyingCTE = parse->hasModifyingCTE;
result->canSetTag = parse->canSetTag;
result->transientPlan = false;
result->dependsOnRole = false;
result->parallelModeNeeded = false;
result->planTree = duckdb_plan;
result->rtable = list_make1(rte);
#if PG_VERSION_NUM >= 160000
result->permInfos = NULL;
#endif
result->resultRelations = NULL;
result->appendRelations = NULL;
result->subplans = NIL;
result->rewindPlanIDs = NULL;
result->rowMarks = NIL;
result->relationOids = NIL;
result->invalItems = NIL;
result->paramExecTypes = NIL;
/* utilityStmt should be null, but we might as well copy it */
result->utilityStmt = parse->utilityStmt;
result->stmt_location = parse->stmt_location;
result->stmt_len = parse->stmt_len;
return result;
}