|
17 | 17 |
|
18 | 18 | namespace duckdb { |
19 | 19 |
|
| 20 | +struct TableRefResult { |
| 21 | + string schema; |
| 22 | + string table; |
| 23 | + string context; |
| 24 | +}; |
| 25 | + |
20 | 26 | struct ParseTablesState : public GlobalTableFunctionState { |
21 | 27 | idx_t row = 0; |
| 28 | + vector<TableRefResult> results; |
22 | 29 | }; |
23 | 30 |
|
24 | 31 | struct ParseTablesBindData : public TableFunctionData { |
25 | 32 | string sql; |
26 | 33 | }; |
27 | 34 |
|
28 | | -struct TableRefResult { |
29 | | - string schema; |
30 | | - string table; |
31 | | - string context; |
32 | | -}; |
33 | | - |
34 | 35 | // BIND function: runs during query planning to decide output schema |
35 | 36 | static unique_ptr<FunctionData> Bind(ClientContext &context, |
36 | 37 | TableFunctionBindInput &input, |
@@ -126,36 +127,29 @@ static void MyFunc(ClientContext &context, |
126 | 127 | auto &state = (ParseTablesState &)*data.global_state; |
127 | 128 | auto &bind_data = (ParseTablesBindData &)*data.bind_data; |
128 | 129 |
|
129 | | - static vector<TableRefResult> results; |
130 | | - static bool parsed = false; |
131 | | - |
132 | | - if (!parsed) { |
| 130 | + if (state.results.empty() && state.row == 0) { |
133 | 131 | try { |
134 | 132 | Parser parser; |
135 | 133 | parser.ParseQuery(bind_data.sql); |
136 | 134 |
|
137 | | - std::cout << "Parsed " << parser.statements.size() << " statements" << std::endl; |
138 | | - |
139 | | - |
140 | 135 | for (auto &stmt : parser.statements) { |
141 | 136 | if (stmt->type == StatementType::SELECT_STATEMENT) { |
142 | 137 | auto &select_stmt = (SelectStatement &)*stmt; |
143 | 138 | if (select_stmt.node) { |
144 | | - ExtractTablesFromQueryNode(*select_stmt.node, results); |
| 139 | + ExtractTablesFromQueryNode(*select_stmt.node, state.results); |
145 | 140 | } |
146 | 141 | } |
147 | 142 | } |
148 | | - parsed = true; |
149 | 143 | } catch (const std::exception &ex) { |
150 | 144 | throw InvalidInputException("Failed to parse SQL: %s", ex.what()); |
151 | 145 | } |
152 | 146 | } |
153 | 147 |
|
154 | | - if (state.row >= results.size()) { |
| 148 | + if (state.row >= state.results.size()) { |
155 | 149 | return; |
156 | 150 | } |
157 | 151 |
|
158 | | - auto &ref = results[state.row]; |
| 152 | + auto &ref = state.results[state.row]; |
159 | 153 | output.SetCardinality(1); |
160 | 154 | output.SetValue(0, 0, Value(ref.schema)); |
161 | 155 | output.SetValue(1, 0, Value(ref.table)); |
|
0 commit comments