Skip to content

Fix empty result sets from stream #558

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

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions tests/aio/query/test_query_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ async def test_basic_execute(self, session: QuerySession):
async def test_two_results(self, session: QuerySession):
await session.create()
res = []
counter = 0

async with await session.execute("select 1; select 2") as results:
async for result_set in results:
counter += 1
if len(result_set.rows) > 0:
res.append(list(result_set.rows[0].values()))

assert res == [[1], [2]]
assert counter == 2
15 changes: 15 additions & 0 deletions tests/aio/query/test_query_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ async def test_execute_as_context_manager(self, tx: QueryTxContext):
res = [result_set async for result_set in results]

assert len(res) == 1

@pytest.mark.asyncio
async def test_execute_two_results(self, tx: QueryTxContext):
await tx.begin()
counter = 0
res = []

async with await tx.execute("select 1; select 2") as results:
async for result_set in results:
counter += 1
if len(result_set.rows) > 0:
res.append(list(result_set.rows[0].values()))

assert res == [[1], [2]]
assert counter == 2
3 changes: 3 additions & 0 deletions tests/query/test_query_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ def test_basic_execute(self, session: QuerySession):
def test_two_results(self, session: QuerySession):
session.create()
res = []
counter = 0

with session.execute("select 1; select 2") as results:
for result_set in results:
counter += 1
if len(result_set.rows) > 0:
res.append(list(result_set.rows[0].values()))

assert res == [[1], [2]]
assert counter == 2

def test_thread_leaks(self, session: QuerySession):
session.create()
Expand Down
13 changes: 13 additions & 0 deletions tests/query/test_query_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,16 @@ def test_execute_as_context_manager(self, tx: QueryTxContext):
res = [result_set for result_set in results]

assert len(res) == 1

def test_execute_two_results(self, tx: QueryTxContext):
tx.begin()
counter = 0
res = []

with tx.execute("select 1; select 2") as results:
for result_set in results:
counter += 1
res.append(list(result_set.rows[0].values()))

assert res == [[1], [2]]
assert counter == 2
5 changes: 4 additions & 1 deletion ydb/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def __iter__(self):
return self

def _next(self):
return self.wrapper(next(self.it))
res = self.wrapper(next(self.it))
if res is not None:
return res
return self._next()

def next(self):
return self._next()
Expand Down
5 changes: 4 additions & 1 deletion ydb/aio/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def __aiter__(self):
return self

async def _next(self):
return self.wrapper(await self.it.__anext__())
res = self.wrapper(await self.it.__anext__())
if res is not None:
return res
return await self._next()

async def next(self):
return await self._next()
Expand Down
5 changes: 4 additions & 1 deletion ydb/query/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,7 @@ def wrap_execute_query_response(
elif tx and response_pb.tx_meta and not tx.tx_id:
tx._move_to_beginned(response_pb.tx_meta.id)

return convert.ResultSet.from_message(response_pb.result_set, settings)
if response_pb.HasField("result_set"):
return convert.ResultSet.from_message(response_pb.result_set, settings)

return None
Loading