Skip to content

Commit

Permalink
Raising driver errors in resultset (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
neelasha23 authored Nov 14, 2023
1 parent f6bcd18 commit 5d36832
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.10.4dev
* [Fix] Fix bug causing empty result on SQL with trailing semicolon and comment (#907)

* [Fix] Fixed bug that returns empty results when exception is raised from DB driver

## 0.10.3 (2023-11-06)

* [Feature] Allow user-level config using ~/.jupysql/config ([#880](https://github.com/ploomber/jupysql/issues/880))
Expand Down Expand Up @@ -462,4 +464,4 @@ Converted from an IPython Plugin to an Extension for 1.0 compatibility

*Release date: 21-Mar-2013*

* Initial release
* Initial release
13 changes: 12 additions & 1 deletion src/sql/run/resultset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from sql.run.table import CustomPrettyTable
from sql._current import _config_feedback_all

from sql.exceptions import RuntimeError


class ResultSet(ColumnGuesserMixin):
"""
Expand Down Expand Up @@ -420,7 +422,16 @@ def fetchmany(self, size):
# psycopg2 raises psycopg2.ProgrammingError error when running a script
# that doesn't return rows e.g, 'CREATE TABLE' but others don't
# (e.g., duckdb), so here we catch all
except Exception:
except Exception as e:
if not any(
substring in str(e)
for substring in [
"This result object does not return rows",
"no results to fetch",
]
):
# raise specific DB driver errors
raise RuntimeError(f"Error running the query: {str(e)}") from e
self.mark_fetching_as_done()
return

Expand Down
23 changes: 23 additions & 0 deletions src/tests/test_resultset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pandas as pd
import polars as pl
import sqlalchemy
from IPython.core.error import UsageError

from sql.connection import DBAPIConnection, SQLAlchemyConnection
from sql.run.resultset import ResultSet
Expand Down Expand Up @@ -757,3 +758,25 @@ def test_pivot_dataframe_conversion_results(ip, df_type, library, equal_func):
}
expected = getattr(library, "DataFrame")(expected_result)
assert getattr(result, equal_func)(expected)


@pytest.mark.parametrize(
"query, error",
[
(
"select age(TIMESTAMP '2020-12-25', TIMESTAMP '2020-12-26') age",
"Python int too large to convert to C int",
),
(
"select format('Works {:,}', 12) ok, format('Will not work {:,}', "
"12.0) not_ok",
"Thousand separators are not supported for floating point numbers",
),
],
ids=["negative_age", "thousand_separators"],
)
def test_db_driver_error_raised(ip_empty, query, error):
ip_empty.run_cell("%sql duckdb://")
with pytest.raises(UsageError) as e:
ip_empty.run_cell(f"%sql {query}").result
assert error in str(e.value)

0 comments on commit 5d36832

Please sign in to comment.