-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: deprecate usage of cursor.execute
statements in favor of the in class implementation of execute
.
#60748
Draft
gmcrocetti
wants to merge
9
commits into
pandas-dev:main
Choose a base branch
from
gmcrocetti:refactor-io-sql-execute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+41
−20
Draft
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
804fb3e
refactor: deprecate usage of `cursor.execute` statements in favor of …
gmcrocetti b13eca7
Merge branch 'main' into refactor-io-sql-execute
gmcrocetti ac87d2c
Merge branch 'main' into refactor-io-sql-execute
gmcrocetti 87156b1
chore: using cursor from transaction
gmcrocetti 182cfff
Merge branch 'main' into refactor-io-sql-execute
gmcrocetti 9020ee2
Merge branch 'main' into refactor-io-sql-execute
gmcrocetti 6b3883f
Merge branch 'main' into refactor-io-sql-execute
gmcrocetti b2f97cb
Merge branch 'main' into refactor-io-sql-execute
gmcrocetti 196305d
Merge branch 'main' into refactor-io-sql-execute
gmcrocetti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the need for
.close()
here? I am hoping we can avoid anything that implicitly changes the transaction stateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't say there's a need for that, but it is usually a good practice to close any cursor that is opened. We can remove to maintain the status quo - no problem on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh OK I misread this as closing the transaction. Does whatever
pd_sql.execute
return not follow the context manager protocol? We should be preferringwith
statements whenever a context like that needs to be managedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, no worries. I'm super thankful for all the help and attentive comments.
Yeah, it does implement the context manager protocol. This was a personal choice and has no technical reason:
Reads better to me than
What you think ? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm always in favor of using the context manager over calling close manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Let me try to illustrate that with an example from the codebase.
We can say that the ADBCDatabase implementation is the same as of SQLiteDatabase
pandas/pandas/io/sql.py
Lines 2110 to 2128 in c0c778b
Alright, so now take this line as example:
If we leave it like that ☝️ then an exception is raised because because the cursor was left open:
I'm no specialist here but believe that despite all differences between drivers the DBAPI is respected and if that is the case then the SQLite cursor should also be closed. The SQLite cursor will anyways get closed when
__del__
is called so we might be safer if we maintain the status quo in case we're uncertain about side-effects ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mroeschke , @WillAyd , just a heads up regarding
self.pd_sql.execute(stmt).close()
vswith self.pd_sql.execute(stmt)
. The later is not feasible because the SQLite Cursor object does not implement the context manager protocol.Options are calling
.close()
or usingclosing
from contextlib.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this example. So previously we were explicitly opening and closing a cursor but with the switch to using
self.execute
we are re-using the cursor attached to the class instance and not controlling its lifecycle.So how is that lifecycle being managed? Seems like there is just a gap / inconsistency that is making this all more complicated than it should be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @WillAyd ,
I don't think we have a lifecycle management problem but nonetheless the implementation has changed as complicated is not the goal here. Thanks for raising it o/
The implementation is back to its original version with a small change in the name of things.
cur
(instead ofconn
) is used to represent a cursor since it is whatrun_transaction
returns forSQLiteDatabase
's implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @WillAyd , it's been a while...
So just passing by to know if there's something else to do here or if it is ok to open for reviews
thanks in advance