-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathpostgres_scan.cpp
More file actions
279 lines (239 loc) · 10.1 KB
/
postgres_scan.cpp
File metadata and controls
279 lines (239 loc) · 10.1 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "duckdb/planner/filter/optional_filter.hpp"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/scan/postgres_table_reader.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/pg/relations.hpp"
#include "pgduckdb/pgduckdb_process_lock.hpp"
#include "pgduckdb/logger.hpp"
#include <numeric> // std::accumulate
namespace pgduckdb {
//
// PostgresScanGlobalState
//
static duckdb::string
FilterJoin(duckdb::vector<duckdb::string> &filters, duckdb::string &&delimiter) {
return std::accumulate(filters.begin() + 1, filters.end(), filters[0],
[&delimiter](duckdb::string l, duckdb::string r) { return l + delimiter + r; });
}
int
PostgresScanGlobalState::ExtractQueryFilters(duckdb::TableFilter *filter, const char *column_name,
duckdb::string &query_filters, bool is_optional_filter_parent) {
switch (filter->filter_type) {
case duckdb::TableFilterType::CONSTANT_COMPARISON:
case duckdb::TableFilterType::IS_NULL:
case duckdb::TableFilterType::IS_NOT_NULL:
case duckdb::TableFilterType::IN_FILTER: {
query_filters += filter->ToString(column_name).c_str();
return 1;
}
case duckdb::TableFilterType::CONJUNCTION_OR:
case duckdb::TableFilterType::CONJUNCTION_AND: {
auto conjuction_filter = reinterpret_cast<duckdb::ConjunctionFilter *>(filter);
duckdb::vector<std::string> conjuction_child_filters;
for (idx_t i = 0; i < conjuction_filter->child_filters.size(); i++) {
std::string child_filter;
if (ExtractQueryFilters(conjuction_filter->child_filters[i].get(), column_name, child_filter, false)) {
conjuction_child_filters.emplace_back(child_filter);
}
}
duckdb::string conjuction_delimiter =
filter->filter_type == duckdb::TableFilterType::CONJUNCTION_OR ? " OR " : " AND ";
if (conjuction_child_filters.size()) {
query_filters += "(" + FilterJoin(conjuction_child_filters, std::move(conjuction_delimiter)) + ")";
}
return conjuction_child_filters.size();
}
case duckdb::TableFilterType::OPTIONAL_FILTER: {
auto optional_filter = reinterpret_cast<duckdb::OptionalFilter *>(filter);
return ExtractQueryFilters(optional_filter->child_filter.get(), column_name, query_filters, true);
}
/* DYNAMIC_FILTER is push down filter from topN execution. STRUCT_EXTRACT is
* only received if struct_extract function is used. Default will catch all
* filter that could be added in future in DuckDB.
*/
case duckdb::TableFilterType::DYNAMIC_FILTER:
case duckdb::TableFilterType::STRUCT_EXTRACT:
default: {
if (is_optional_filter_parent) {
pd_log(DEBUG1, "(DuckDB/ExtractQueryFilters) Unsupported optional filter: %s",
filter->ToString(column_name).c_str());
return 0;
}
throw duckdb::Exception(duckdb::ExceptionType::EXECUTOR,
"Invalid Filter Type: " + filter->ToString(column_name));
}
}
}
void
PostgresScanGlobalState::ConstructTableScanQuery(const duckdb::TableFunctionInitInput &input) {
/* SELECT COUNT(*) FROM */
if (input.column_ids.size() == 1 && input.column_ids[0] == UINT64_MAX) {
scan_query << "SELECT COUNT(*) FROM " << pgduckdb::GenerateQualifiedRelationName(rel);
count_tuples_only = true;
return;
}
/*
* We need to read columns from the Postgres tuple in column order, but for
* outputting them we care about the DuckDB order. A map automatically
* orders them based on key, which in this case is the Postgres column
* order
*/
duckdb::map<AttrNumber, duckdb::idx_t> pg_column_order;
duckdb::idx_t scan_index = 0;
for (const auto &pg_column : input.column_ids) {
/* Postgres AttrNumbers are 1-based */
pg_column_order[pg_column + 1] = scan_index++;
}
auto table_filters = input.filters.get();
std::vector<duckdb::pair<AttrNumber, duckdb::idx_t>> columns_to_scan;
std::vector<duckdb::TableFilter *> column_filters(input.column_ids.size(), 0);
for (auto const &[att_num, duckdb_scanned_index] : pg_column_order) {
columns_to_scan.emplace_back(att_num, duckdb_scanned_index);
if (!table_filters) {
continue;
}
auto column_filter_it = table_filters->filters.find(duckdb_scanned_index);
if (column_filter_it != table_filters->filters.end()) {
column_filters[duckdb_scanned_index] = column_filter_it->second.get();
}
}
/* We need to check do we consider projection_ids or column_ids list to be used
* for writing to output vector. Projection ids list will be used when
* columns that are used for query filtering are not used afterwards; otherwise
* column ids list will be used and all read tuple columns need to passed
* to upper layers of query execution.
*/
if (input.CanRemoveFilterColumns()) {
for (const auto &projection_id : input.projection_ids) {
output_columns.emplace_back(input.column_ids[projection_id] + 1);
}
} else {
for (const auto &column_id : input.column_ids) {
output_columns.emplace_back(column_id + 1);
}
}
scan_query << "SELECT ";
bool first = true;
for (auto const &attr_num : output_columns) {
if (!first) {
scan_query << ", ";
}
first = false;
auto attr = GetAttr(table_tuple_desc, attr_num - 1);
scan_query << pgduckdb::QuoteIdentifier(GetAttName(attr));
}
scan_query << " FROM " << GenerateQualifiedRelationName(rel);
duckdb::vector<duckdb::string> query_filters;
for (auto const &[attr_num, duckdb_scanned_index] : columns_to_scan) {
auto filter = column_filters[duckdb_scanned_index];
if (!filter) {
continue;
}
duckdb::string column_query_filters;
auto attr = GetAttr(table_tuple_desc, attr_num - 1);
auto col = pgduckdb::QuoteIdentifier(GetAttName(attr));
if (ExtractQueryFilters(filter, col, column_query_filters, false)) {
query_filters.emplace_back(column_query_filters);
};
}
if (query_filters.size()) {
scan_query << " WHERE ";
scan_query << FilterJoin(query_filters, " AND ");
}
}
PostgresScanGlobalState::PostgresScanGlobalState(Snapshot _snapshot, Relation _rel,
const duckdb::TableFunctionInitInput &input)
: snapshot(_snapshot), rel(_rel), table_tuple_desc(RelationGetDescr(rel)), count_tuples_only(false),
total_row_count(0) {
ConstructTableScanQuery(input);
table_reader_global_state =
duckdb::make_shared_ptr<PostgresTableReader>(scan_query.str().c_str(), count_tuples_only);
pd_log(DEBUG2, "(DuckDB/PostgresSeqScanGlobalState) Running %" PRIu64 " threads -- ", (uint64_t)MaxThreads());
}
PostgresScanGlobalState::~PostgresScanGlobalState() {
}
//
// PostgresScanLocalState
//
PostgresScanLocalState::PostgresScanLocalState(PostgresScanGlobalState *_global_state)
: global_state(_global_state), exhausted_scan(false) {
}
PostgresScanLocalState::~PostgresScanLocalState() {
}
//
// PostgresSeqScanFunctionData
//
PostgresScanFunctionData::PostgresScanFunctionData(Relation _rel, uint64_t _cardinality, Snapshot _snapshot)
: rel(_rel), cardinality(_cardinality), snapshot(_snapshot) {
}
PostgresScanFunctionData::~PostgresScanFunctionData() {
}
//
// PostgresScanFunction
//
PostgresScanTableFunction::PostgresScanTableFunction()
: TableFunction("postgres_scan", {}, PostgresScanFunction, nullptr, PostgresScanInitGlobal, PostgresScanInitLocal) {
named_parameters["cardinality"] = duckdb::LogicalType::UBIGINT;
named_parameters["relid"] = duckdb::LogicalType::UINTEGER;
named_parameters["snapshot"] = duckdb::LogicalType::POINTER;
projection_pushdown = true;
filter_pushdown = true;
filter_prune = true;
cardinality = PostgresScanCardinality;
to_string = ToString;
}
duckdb::InsertionOrderPreservingMap<duckdb::string>
PostgresScanTableFunction::ToString(duckdb::TableFunctionToStringInput &input) {
auto &bind_data = input.bind_data->Cast<PostgresScanFunctionData>();
duckdb::InsertionOrderPreservingMap<duckdb::string> result;
result["Table"] = GetRelationName(bind_data.rel);
return result;
}
duckdb::unique_ptr<duckdb::GlobalTableFunctionState>
PostgresScanTableFunction::PostgresScanInitGlobal(duckdb::ClientContext &, duckdb::TableFunctionInitInput &input) {
auto &bind_data = input.bind_data->CastNoConst<PostgresScanFunctionData>();
return duckdb::make_uniq<PostgresScanGlobalState>(bind_data.snapshot, bind_data.rel, input);
}
duckdb::unique_ptr<duckdb::LocalTableFunctionState>
PostgresScanTableFunction::PostgresScanInitLocal(duckdb::ExecutionContext &, duckdb::TableFunctionInitInput &,
duckdb::GlobalTableFunctionState *gstate) {
auto global_state = reinterpret_cast<PostgresScanGlobalState *>(gstate);
return duckdb::make_uniq<PostgresScanLocalState>(global_state);
}
void
SetOutputCardinality(duckdb::DataChunk &output, PostgresScanLocalState &local_state) {
idx_t output_cardinality =
local_state.output_vector_size <= STANDARD_VECTOR_SIZE ? local_state.output_vector_size : STANDARD_VECTOR_SIZE;
output.SetCardinality(output_cardinality);
local_state.output_vector_size -= output_cardinality;
}
void
PostgresScanTableFunction::PostgresScanFunction(duckdb::ClientContext &, duckdb::TableFunctionInput &data,
duckdb::DataChunk &output) {
auto &local_state = data.local_state->Cast<PostgresScanLocalState>();
/* We have exhausted table scan */
if (local_state.exhausted_scan) {
SetOutputCardinality(output, local_state);
return;
}
local_state.output_vector_size = 0;
std::lock_guard<std::mutex> lock(GlobalProcessLock::GetLock());
for (size_t i = 0; i < STANDARD_VECTOR_SIZE; i++) {
TupleTableSlot *slot = local_state.global_state->table_reader_global_state->GetNextTuple();
if (pgduckdb::TupleIsNull(slot)) {
local_state.global_state->table_reader_global_state->PostgresTableReaderCleanup();
local_state.exhausted_scan = true;
break;
}
SlotGetAllAttrs(slot);
InsertTupleIntoChunk(output, local_state, slot);
}
SetOutputCardinality(output, local_state);
}
duckdb::unique_ptr<duckdb::NodeStatistics>
PostgresScanTableFunction::PostgresScanCardinality(duckdb::ClientContext &, const duckdb::FunctionData *data) {
auto &bind_data = data->Cast<PostgresScanFunctionData>();
return duckdb::make_uniq<duckdb::NodeStatistics>(bind_data.cardinality, bind_data.cardinality);
}
} // namespace pgduckdb