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

Queries that Return Zero Rows Raise Exception #217

Closed
cancan101 opened this issue Mar 28, 2022 · 2 comments · Fixed by #222
Closed

Queries that Return Zero Rows Raise Exception #217

cancan101 opened this issue Mar 28, 2022 · 2 comments · Fixed by #222

Comments

@cancan101
Copy link
Collaborator

e.g. if you run:

from sqlalchemy.engine import create_engine

engine = create_engine("shillelagh://")
connection = engine.connect()

query = """SELECT country, SUM(cnt)
FROM "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=0"
WHERE cnt > 10
GROUP BY country"""
for row in connection.execute(query):
    print(row)

you get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/engine/result.py", line 964, in __iter__
    return self._iter_impl()
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/engine/result.py", line 637, in _iter_impl
    return self._iterator_getter(self)
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 1180, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/engine/result.py", line 361, in _iterator_getter
    make_row = self._row_getter
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 1180, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/engine/result.py", line 320, in _row_getter
    keymap = metadata._keymap
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/engine/cursor.py", line 1208, in _keymap
    self._we_dont_return_rows()
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/engine/cursor.py", line 1189, in _we_dont_return_rows
    util.raise_(
  File "/Users/alex/.pyenv/versions/my-app/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically
@cancan101
Copy link
Collaborator Author

cancan101 commented Mar 31, 2022

I think this issue is caused by None being returned for the description:

def _get_description(self) -> Description:
"""
Return the cursor description.
We only return name and type, since that's what we get from APSW.
"""
try:
description = self._cursor.getdescription()
except apsw.ExecutionCompleteError:
return None

which then causes this error in sqla:
https://github.com/sqlalchemy/sqlalchemy/blob/3f00555a984e17eaa55fe37235baf6f75500c5ba/lib/sqlalchemy/engine/cursor.py#L1198

rather, I think an empty list should be returned.

According to this, a set should always be returned for a select: sqlalchemy/sqlalchemy#5433 (comment)

@cancan101
Copy link
Collaborator Author

There is some more clarification in apsw on this: rogerbinns/apsw#160

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

Successfully merging a pull request may close this issue.

2 participants