-
Notifications
You must be signed in to change notification settings - Fork 200
Fix OOM on large spooled result sets #590
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
Draft
gopinathnelluri
wants to merge
1
commit into
trinodb:master
Choose a base branch
from
gopinathnelluri:OOM-Issue
base: master
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.
Draft
Changes from all commits
Commits
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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
| from trino.client import TrinoQuery, TrinoRequest, ClientSession, TrinoResult | ||
| from trino.client import SegmentIterator | ||
|
|
||
| class TestTrinoQueryLazy(unittest.TestCase): | ||
| def setUp(self): | ||
| self.mock_request = MagicMock(spec=TrinoRequest) | ||
| self.client_session = ClientSession("user") | ||
| self.mock_request.client_session = self.client_session | ||
|
|
||
| def test_fetch_returns_iterator_for_spooled_segments(self): | ||
| # Mock the initial POST response | ||
| post_response = MagicMock() | ||
| post_response.id = "query_1" | ||
| post_response.stats = {} | ||
| post_response.info_uri = "info" | ||
| post_response.next_uri = "next_1" | ||
| post_response.rows = [] # No rows initially | ||
|
|
||
| self.mock_request.process.return_value = post_response | ||
| self.mock_request.post.return_value = MagicMock() | ||
|
|
||
| query = TrinoQuery(self.mock_request, "SELECT 1") | ||
|
|
||
| # Execute should return empty result initially but try to fetch | ||
| # We need to mock fetch behavior too since execute calls it if rows are empty | ||
|
|
||
| # Mock the GET response for fetch() | ||
| get_response_status = MagicMock() | ||
| get_response_status.next_uri = None # Finished | ||
| get_response_status.stats = {} | ||
| # Status rows as dict indicates spooling protocol | ||
| get_response_status.rows = { | ||
| "encoding": "json", | ||
| "segments": [ | ||
| {"type": "spooled", "uri": "u1", "ackUri": "a1", "metadata": {"segmentSize": "10", "uncompressedSize": "10"}} | ||
| ], | ||
| "metadata": {} | ||
| } | ||
|
|
||
| # When execute calls fetch(), it calls request.get -> process -> returns get_response_status | ||
| self.mock_request.process.side_effect = [post_response, get_response_status] | ||
| self.mock_request.get.return_value = MagicMock() | ||
|
|
||
| # Mock _to_segments to return a list of decodable segments | ||
| # We can just verify that fetch returns a SegmentIterator | ||
| # But _to_segments is internal. | ||
|
|
||
| # We need to patch SegmentIterator or check the return type | ||
|
|
||
| result = query.execute() | ||
|
|
||
| # Verify result.rows is a SegmentIterator, NOT a list | ||
| self.assertIsInstance(result.rows, SegmentIterator) | ||
| self.assertNotIsInstance(result.rows, list) | ||
|
|
||
| def test_fetch_returns_list_for_normal_segments(self): | ||
| # Mock the initial POST response | ||
| post_response = MagicMock() | ||
| post_response.id = "query_1" | ||
| post_response.stats = {} | ||
| post_response.info_uri = "info" | ||
| post_response.next_uri = "next_1" | ||
| post_response.rows = [] | ||
|
|
||
| # Mock the GET response for fetch() | ||
| get_response_status = MagicMock() | ||
| get_response_status.next_uri = None | ||
| get_response_status.stats = {} | ||
| get_response_status.rows = [[1], [2]] # Normal list rows | ||
|
|
||
| self.mock_request.process.side_effect = [post_response, get_response_status] | ||
|
|
||
| query = TrinoQuery(self.mock_request, "SELECT 1") | ||
| result = query.execute() | ||
|
|
||
| # Verify result.rows is a list (appended) | ||
| self.assertIsInstance(result.rows, list) | ||
| self.assertEqual(result.rows, [[1], [2]]) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -904,9 +904,32 @@ def execute(self, additional_http_headers=None) -> TrinoResult: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rows = self._row_mapper.map(status.rows) if self._row_mapper else status.rows | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._result = TrinoResult(self, rows) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Execute should block until at least one row is received or query is finished or cancelled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while not self.finished and not self.cancelled and len(self._result.rows) == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._result.rows += self.fetch() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Execute should block until at least one row is received or query is finished or cancelled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| For Standard Execution, rows is a list, we can check len. the first response usually contains no rows (just stats), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| so we need to continue fetching until we get some rows or query is finished or cancelled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| For Spooled Execution, rows start as empty list and eventually fetch returns the rows as iterator, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| we can't check len of an iterator easily without peeking. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| So, if we get rows as non empty list or iterator, we stop blocking and return it to the caller to consume it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while not self.finished and not self.cancelled: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(self._result.rows, list) and len(self._result.rows) == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new_rows = self.fetch() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(new_rows, list): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._result.rows += new_rows | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # It's an iterator (spooled segments), replace rows with it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._result.rows = new_rows | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # We have an iterator now, so we can return result to user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # We have data (list with items or an iterator), so return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+920
to
+931
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This part could be made a bit more readable:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _update_state(self, status): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -920,7 +943,7 @@ def _update_state(self, status): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if status.columns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._columns = status.columns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def fetch(self) -> List[Union[List[Any]], Any]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def fetch(self) -> Union[List[Union[List[Any], Any]], Iterator[List[Any]]]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Continue fetching data for the current query_id""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response = self._request.get(self._request.next_uri) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -941,7 +964,8 @@ def fetch(self) -> List[Union[List[Any]], Any]: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| spooled = self._to_segments(rows) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._fetch_mode == "segments": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return spooled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return list(SegmentIterator(spooled, self._row_mapper)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Return iterator directly, do NOT materialize with list() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return SegmentIterator(spooled, self._row_mapper) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(status.rows, list): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._row_mapper.map(rows) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
This docstring is too verbose, it should be a short comment as it was previously, for example: