-
Notifications
You must be signed in to change notification settings - Fork 33
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
Optimize Cursor.fetchall #62
Optimize Cursor.fetchall #62
Conversation
@RostanTabet : Iiuc, you are proposing to return a copy of the internal list object ( I am not that familiar with python internals. Does |
|
||
results = self._results | ||
self._results = None | ||
return results |
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.
IIUC, this change may not be safe
- this change the value of
self._results
to None, will affect other methods which need this result - the returned result is changed, previously, we change
self
to a list, now it's just part ofself
(i.e., self._results).
Can we try to fix those?
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.
To keep backward compatibility, maybe we can add a new method fetchresults?
and keep the existing one unchanged?
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 taking the time to review this pull request.
It is actually an expected behaviour that a call to fetchall
affects subsequent calls to other methods. In fact, this is already the case since returning list(self)
will call __iter__
, which will itself call fetchone
until the list is emptied.
To sum up, after a call to fetchall
, fetchone
has been called n
times with n
the size of the resulting list and self._results
is empty.
There is actually a mistake in my code, I should have set 1. Appart from this, the bahavior is strictly the same, except from the fact that the result is directly returned as is instead of resulting from multiple calls to self._results
to []
instead of None
fetchone
.
Footnotes
-
Edit: this is now fixed ↩
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.
@RostanTabet it might be useful to add a simple unit test to show that this is not an incompatible change in the function's behavior. Can you please add a unit test?
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.
The method is already tested in :
pinot-dbapi/tests/unit/test_db.py
Line 618 in ef93a90
def test_fetches_all_results(self): |
It needs more ?
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.
LGTM
Can you please rebase and we can merge this. |
The method
Cursor.fetchall
has the following docstring :which creates a list by calling
self.fetchone
, i.e.self._results.pop(0)
, once for each element of the listself._results
.This is a performance issue and can be done more straightforwardly.