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

add an option to deferred fetch result in Cursor.execute() #400

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dungdm93
Copy link
Member

@dungdm93 dungdm93 commented Aug 8, 2023

After introduce in #220, all query are now until at least one row is received or query is finished or cancelled.
In some cases such as Superset, users would like to be able to cancel the query but the execution still going on.

See also: apache/superset#24913
Fix: apache/superset#24858

@cla-bot cla-bot bot added the cla-signed label Aug 8, 2023
@dungdm93 dungdm93 changed the title Deferred fetch add an option to deferred fetch result in Cursor.execute() Aug 8, 2023
@dungdm93 dungdm93 changed the title add an option to deferred fetch result in Cursor.execute() add an option to deferred fetch result in Cursor.execute() Aug 8, 2023
Copy link

@giftig giftig left a comment

Choose a reason for hiding this comment

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

One small suggestion

trino/client.py Outdated Show resolved Hide resolved
@giftig
Copy link

giftig commented Aug 8, 2023

This probably needs unit test coverage as well.

@dungdm93
Copy link
Member Author

Test added

Copy link

@giftig giftig left a comment

Choose a reason for hiding this comment

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

A couple of comments on the test

Validates that the `TrinoQuery.execute` function deferred_fetch and non-block execution
"""

class MockResponse(mock.Mock):
Copy link

@giftig giftig Aug 16, 2023

Choose a reason for hiding this comment

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

Looks like this is copied and pasted from another function, so it would be nice to extract it out and stay DRY.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's depends on sample_get_response_data local variable.
Still have a way to extract it out, but I wonder is it generic enough for other tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

My suggestion is keep it as is. if there are more tests use it, we could extract it later, in other PR.

Copy link

@giftig giftig Aug 31, 2023

Choose a reason for hiding this comment

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

That's something which could just be passed into the constructor though, right? There's no reason for it to refer to local scope. It's a whole class which has been copied and pasted and defined in a local function, I think now is the right time to refactor it and stay DRY personally.

tests/unit/test_client.py Outdated Show resolved Hide resolved
tests/unit/test_client.py Show resolved Hide resolved
for row in self._rows:
self._rownumber += 1
logger.debug("row %s", row)
yield row

self._rows = next_rows
self._rows = self._query.fetch() if not self._query.finished else []
Copy link
Contributor

Choose a reason for hiding this comment

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

Through this change the above comment is not valid anymore as the next_uri is only acknowledged after exposing all rows from self._rows through dbapi. The result is the same but now the query will be hanging until the dbapi method fetch_one or fetch_many has processed all rows in the buffer.

Also in the future the fetch could be made async, done in a separate thread, which allows for continuously looping through rows and fetching the next resultset.

@dungdm93 dungdm93 force-pushed the deferred_fetch branch 2 times, most recently from 3af2b28 to 9e2ae77 Compare September 15, 2023 03:31

@pytest.fixture(scope="session")

@pytest.fixture
Copy link
Member Author

Choose a reason for hiding this comment

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

Change fixture scope to function to fix issue where it's modified between test cases.

@dungdm93
Copy link
Member Author

Hi @mdesmet, @hashhar, could you help me review this PR.

@@ -14,8 +14,10 @@

import pytest

from tests.unit.oauth_test_utils import SERVER_ADDRESS
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we put SERVER_ADDRESS in a more central place if we want to use it in multiple places. here the usage is not related to oauth.

@mdesmet
Copy link
Contributor

mdesmet commented Sep 28, 2023

@dungdm93 : Can you handle the open comments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Trino queries cannot be stopped in SQL Lab
3 participants