Skip to content
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

Merged
merged 3 commits into from
Oct 13, 2023

Conversation

rostan-t
Copy link
Contributor

The method Cursor.fetchall has the following docstring :

Fetch all (remaining) rows of a query result, returning them as a
sequence of sequences (e.g. a list of tuples). Note that the cursor's
arraysize attribute can affect the performance of this operation.
However, the method's implementation is simply :

return list(self)

which creates a list by calling self.fetchone, i.e. self._results.pop(0), once for each element of the list self._results.

This is a performance issue and can be done more straightforwardly.

@navina
Copy link

navina commented May 24, 2023

@RostanTabet : Iiuc, you are proposing to return a copy of the internal list object (_results) instead of letting the python's in-built list to construct a list instance by iterating through the objects. Is that right?

I am not that familiar with python internals. Does results = self._results create a copy of the list and assign to the lhs results variable ? Or is it just returning a reference to the list _results ?


results = self._results
self._results = None
return results

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

  1. this change the value of self._results to None, will affect other methods which need this result
  2. the returned result is changed, previously, we change self to a list, now it's just part of self (i.e., self._results).

Can we try to fix those?

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?

Copy link
Contributor Author

@rostan-t rostan-t May 24, 2023

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 self._results to [] instead of None1. 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 fetchone.

Footnotes

  1. Edit: this is now fixed

Copy link

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?

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 :

def test_fetches_all_results(self):

It needs more ?

Copy link

@zhtaoxiang zhtaoxiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xiangfu0
Copy link
Contributor

xiangfu0 commented Sep 5, 2023

Can you please rebase and we can merge this.

@xiangfu0 xiangfu0 merged commit 802357e into python-pinot-dbapi:master Oct 13, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants