diff --git a/CHANGELOG.md b/CHANGELOG.md index b2186fbb4..84494e479 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [Doc] Add chDB integration tutorial * [Doc] Clarify the use of `pyproject.toml` and `connections.ini` in documentations (#850) * [Fix] Fix result not displayed when `SUMMARIZE` argument is used in duckdb with a sqlalchemy connection (#836) +* [Fix] Fix error when trying to access previously non-existing file (#840) ## 0.10.1 (2023-08-30) diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py index 7e4812f31..96bc4d463 100644 --- a/src/sql/connection/connection.py +++ b/src/sql/connection/connection.py @@ -14,6 +14,7 @@ StatementError, PendingRollbackError, InternalError, + ProgrammingError, ) from IPython.core.error import UsageError import sqlglot @@ -896,6 +897,14 @@ def _execute_with_error_handling(self, operation): else: raise + except ProgrammingError as e: + # error when accessing previously non-existing file with duckdb using + # sqlalchemy 2.x + if "duckdb.InvalidInputException" in str(e) and "please ROLLBACK" in str(e): + rollback_needed = True + else: + raise + if rollback_needed: self._connection.rollback() out = operation() diff --git a/src/tests/test_magic.py b/src/tests/test_magic.py index d027b343e..fb2062e77 100644 --- a/src/tests/test_magic.py +++ b/src/tests/test_magic.py @@ -1964,3 +1964,26 @@ def test_summarize_in_duckdb(ip_empty): SUMMARIZE table1""" ).result assert out.dict() == expected_result + + +def test_accessing_previously_nonexisting_file(ip_empty, tmp_empty, capsys): + ip_empty.run_cell("%sql duckdb://") + with pytest.raises(UsageError): + ip_empty.run_cell("%sql SELECT * FROM 'data.csv' LIMIT 3") + + Path("data.csv").write_text( + "name,age\nDan,33\nBob,19\nSheri,\nVin,33\nMick,\nJay,33\nSky,33" + ) + expected = ( + "+-------+------+\n" + "| name | age |\n" + "+-------+------+\n" + "| Dan | 33 |\n" + "| Bob | 19 |\n" + "| Sheri | None |\n" + "+-------+------+" + ) + + ip_empty.run_cell("%sql SELECT * FROM 'data.csv' LIMIT 3") + out, _ = capsys.readouterr() + assert expected in out