33#include " duckdb/main/relation.hpp"
44#include " duckdb/main/statement_iterator.hpp"
55#include " duckdb/main/valid_checker.hpp"
6- #include " duckdb/parser/expression/star_expression.hpp"
7- #include " duckdb/parser/query_node/select_node.hpp"
86#include " duckdb/parser/statement/select_statement.hpp"
9- #include " duckdb/parser/tableref/column_data_ref.hpp"
107#include " duckdb/transaction/meta_transaction.hpp"
118
129namespace duckdb {
@@ -16,20 +13,52 @@ shared_ptr<ConnectionState> ConnectionState::Get(ClientContext &context) {
1613 return context.registered_state ->GetOrCreate <ConnectionState>(" python_connection_state" );
1714}
1815
19- void ConnectionState::SetOpen (const shared_ptr<Supersedable> &result) {
20- lock_guard<mutex> guard (lock);
21- open = result;
16+ static constexpr const char *BUSY_ERROR =
17+ " connection is busy with another call; it refuses instead of waiting, so use a separate connection from cursor()" ;
18+ static constexpr const char *CALLBACK_ERROR =
19+ " code running inside a query cannot use that query's connection; use a separate connection from cursor()" ;
20+
21+ static thread_local CallbackScope *innermost_callback = nullptr ;
22+
23+ CallbackScope::CallbackScope (const ClientContext &context_p) : context(context_p), outer(innermost_callback) {
24+ innermost_callback = this ;
2225}
2326
24- void ConnectionState::SupersedeOpen () {
25- shared_ptr<Supersedable> result;
26- {
27- lock_guard<mutex> guard (lock);
28- result = open.lock ();
29- open.reset ();
27+ CallbackScope::~CallbackScope () {
28+ innermost_callback = outer;
29+ }
30+
31+ bool CallbackScope::Inside (const ClientContext &context) {
32+ for (auto scope = innermost_callback; scope; scope = scope->outer ) {
33+ if (&scope->context == &context) {
34+ return true ;
35+ }
36+ }
37+ return false ;
38+ }
39+
40+ ConnectionClaim::ConnectionClaim (ConnectionState &state_p, const ClientContext &context) : state(state_p) {
41+ if (CallbackScope::Inside (context)) {
42+ throw ResourceInUseException (CALLBACK_ERROR );
3043 }
31- if (result) {
32- result->Supersede ();
44+ if (state.busy .exchange (true )) {
45+ throw ResourceInUseException (BUSY_ERROR );
46+ }
47+ }
48+
49+ ConnectionClaim::ConnectionClaim (ClientContext &context) : ConnectionClaim(*ConnectionState::Get (context), context) {
50+ }
51+
52+ ConnectionClaim::~ConnectionClaim () {
53+ state.busy = false ;
54+ }
55+
56+ void ConnectionState::ThrowIfBusy (const ClientContext &context) const {
57+ if (CallbackScope::Inside (context)) {
58+ throw ResourceInUseException (CALLBACK_ERROR );
59+ }
60+ if (busy) {
61+ throw ResourceInUseException (BUSY_ERROR );
3362 }
3463}
3564
@@ -111,11 +140,29 @@ class PinGuard {
111140
112141} // namespace
113142
143+ ClaimingContextWrapper::ClaimingContextWrapper (shared_ptr<ClientContextWrapper> inner_p)
144+ : ClientContextWrapper(inner_p->TryGetContext ()), inner(std::move(inner_p)) {
145+ }
146+
147+ void ClaimingContextWrapper::TryBindRelation (Relation &relation, vector<ColumnDefinition> &columns) {
148+ auto context = GetContext ();
149+ ConnectionClaim claim (*context);
150+ inner->TryBindRelation (relation, columns);
151+ }
152+
153+ void ClaimingContextWrapper::Install (Relation &relation) {
154+ if (!relation.context || dynamic_cast <ClaimingContextWrapper *>(relation.context .get ())) {
155+ return ;
156+ }
157+ relation.context = make_shared_ptr<ClaimingContextWrapper>(relation.context );
158+ }
159+
114160Session::Session (shared_ptr<ClientContext> context_p)
115161 : context(std::move(context_p)), state(ConnectionState::Get(*context)) {
116162}
117163
118164vector<unique_ptr<SQLStatement>> Session::Parse (const string &sql) {
165+ ConnectionClaim claim (*state, *context);
119166 // The iterator also runs the preprocessor, without which a PRAGMA cannot be prepared
120167 auto iterator = context->IterateStatements (sql);
121168 vector<unique_ptr<SQLStatement>> statements;
@@ -128,7 +175,7 @@ vector<unique_ptr<SQLStatement>> Session::Parse(const string &sql) {
128175}
129176
130177Prepared Session::Prepare (unique_ptr<SQLStatement> statement) {
131- state-> SupersedeOpen ( );
178+ ConnectionClaim claim (*state, *context );
132179 Prepared prepared;
133180 {
134181 ResolvingGuard guard (*state);
@@ -141,63 +188,40 @@ Prepared Session::Prepare(unique_ptr<SQLStatement> statement) {
141188 return prepared;
142189}
143190
144- shared_ptr<Result> Session::Track (unique_ptr<QueryResult> handle, const Format &format, shared_ptr<Relation> relation) {
145- auto result = make_shared_ptr<Result>(std::move (handle), format, std::move (relation));
146- state->SetOpen (result);
147- return result;
148- }
149-
150191shared_ptr<Result> Session::Submit (unique_ptr<SQLStatement> statement, identifier_map_t <BoundParameterData> &values,
151192 const Format &format) {
152- state-> SupersedeOpen ( );
193+ ConnectionClaim claim (*state, *context );
153194 auto handle = context->Submit (std::move (statement), values, QueryParameters (format.EngineFormat ()));
154- return Track (std::move (handle), format, nullptr );
195+ return make_shared_ptr<Result> (std::move (handle), format, nullptr );
155196}
156197
157198shared_ptr<Result> Session::Submit (Prepared &prepared, identifier_map_t <BoundParameterData> &values,
158199 const Format &format) {
159- state-> SupersedeOpen ( );
200+ ConnectionClaim claim (*state, *context );
160201 unique_ptr<QueryResult> handle;
161202 {
162203 PinGuard guard (*state, prepared.tables );
163204 handle = prepared.statement ->Submit (values, QueryParameters (format.EngineFormat ()));
164205 }
165- return Track (std::move (handle), format, nullptr );
206+ return make_shared_ptr<Result> (std::move (handle), format, nullptr );
166207}
167208
168209shared_ptr<Result> Session::Submit (const shared_ptr<Relation> &relation, const Format &format) {
169- state-> SupersedeOpen ( );
210+ ConnectionClaim claim (*state, *context );
170211 QueryParameters parameters (format.EngineFormat ());
171212 if (!relation->IsReadOnly ()) {
172213 auto handle = context->Submit (relation, parameters);
173- return Track (std::move (handle), format, relation);
214+ return make_shared_ptr<Result> (std::move (handle), format, relation);
174215 }
175216 auto select = make_uniq<SelectStatement>();
176217 select->node = relation->GetQueryNode ();
177218 select->query = relation->GetQuery ();
178219 auto handle = context->Submit (std::move (select), parameters);
179- return Track (std::move (handle), format, relation);
180- }
181-
182- shared_ptr<Result> Session::Submit (unique_ptr<ColumnDataCollection> collection, const vector<Identifier> &names,
183- const Format &format) {
184- // The binder rejects duplicate column names; the caller keeps the originals
185- auto deduplicated_names = names;
186- QueryResult::DeduplicateColumns (deduplicated_names);
187- auto table_ref = make_uniq<ColumnDataRef>(std::move (collection), std::move (deduplicated_names));
188- // Binding asserts on an unset alias
189- table_ref->alias = " materialized" ;
190- auto select_node = make_uniq<SelectNode>();
191- select_node->select_list .push_back (make_uniq<StarExpression>());
192- select_node->from_table = std::move (table_ref);
193- auto select = make_uniq<SelectStatement>();
194- select->node = std::move (select_node);
195- identifier_map_t <BoundParameterData> no_values;
196- return Submit (std::move (select), no_values, format);
220+ return make_shared_ptr<Result>(std::move (handle), format, relation);
197221}
198222
199223void Session::Run (unique_ptr<SQLStatement> statement, const InterruptCheck &check) {
200- state-> SupersedeOpen ( );
224+ ConnectionClaim claim (*state, *context );
201225 auto handle = context->Submit (std::move (statement), QueryParameters ());
202226 Complete (*handle, check);
203227}
0 commit comments