Skip to content

Commit 5d8c3d1

Browse files
committed
first working implementation
1 parent ab4464b commit 5d8c3d1

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/parse_tables_extension.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717

1818
namespace duckdb {
1919

20+
struct TableRefResult {
21+
string schema;
22+
string table;
23+
string context;
24+
};
25+
2026
struct ParseTablesState : public GlobalTableFunctionState {
2127
idx_t row = 0;
28+
vector<TableRefResult> results;
2229
};
2330

2431
struct ParseTablesBindData : public TableFunctionData {
2532
string sql;
2633
};
2734

28-
struct TableRefResult {
29-
string schema;
30-
string table;
31-
string context;
32-
};
33-
3435
// BIND function: runs during query planning to decide output schema
3536
static unique_ptr<FunctionData> Bind(ClientContext &context,
3637
TableFunctionBindInput &input,
@@ -126,36 +127,29 @@ static void MyFunc(ClientContext &context,
126127
auto &state = (ParseTablesState &)*data.global_state;
127128
auto &bind_data = (ParseTablesBindData &)*data.bind_data;
128129

129-
static vector<TableRefResult> results;
130-
static bool parsed = false;
131-
132-
if (!parsed) {
130+
if (state.results.empty() && state.row == 0) {
133131
try {
134132
Parser parser;
135133
parser.ParseQuery(bind_data.sql);
136134

137-
std::cout << "Parsed " << parser.statements.size() << " statements" << std::endl;
138-
139-
140135
for (auto &stmt : parser.statements) {
141136
if (stmt->type == StatementType::SELECT_STATEMENT) {
142137
auto &select_stmt = (SelectStatement &)*stmt;
143138
if (select_stmt.node) {
144-
ExtractTablesFromQueryNode(*select_stmt.node, results);
139+
ExtractTablesFromQueryNode(*select_stmt.node, state.results);
145140
}
146141
}
147142
}
148-
parsed = true;
149143
} catch (const std::exception &ex) {
150144
throw InvalidInputException("Failed to parse SQL: %s", ex.what());
151145
}
152146
}
153147

154-
if (state.row >= results.size()) {
148+
if (state.row >= state.results.size()) {
155149
return;
156150
}
157151

158-
auto &ref = results[state.row];
152+
auto &ref = state.results[state.row];
159153
output.SetCardinality(1);
160154
output.SetValue(0, 0, Value(ref.schema));
161155
output.SetValue(1, 0, Value(ref.table));

0 commit comments

Comments
 (0)