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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pinotdb/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,13 @@ def fetchmany(self, size=None):
def fetchall(self):
"""
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.
sequence of sequences (e.g. a list of tuples).
"""
return list(self)


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 ?


@check_result
@check_closed
def fetchwithschema(self):
Expand Down
Loading