From aca37530f1c294eed4a88db50e51494b80a1adec Mon Sep 17 00:00:00 2001 From: SangGyu An Date: Thu, 7 Sep 2023 16:28:10 -0700 Subject: [PATCH 1/7] Update connection.py --- src/sql/connection/connection.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py index 7e4812f31..60a21947b 100644 --- a/src/sql/connection/connection.py +++ b/src/sql/connection/connection.py @@ -896,6 +896,10 @@ def _execute_with_error_handling(self, operation): else: raise + except Exception as e: + if "duckdb.InvalidInputException" in str(e) and "please ROLLBACK" in str(e): + rollback_needed = True + if rollback_needed: self._connection.rollback() out = operation() From 38f30c93eacb630c17bc1572a4febde7022c5b47 Mon Sep 17 00:00:00 2001 From: SangGyu An Date: Thu, 7 Sep 2023 16:35:26 -0700 Subject: [PATCH 2/7] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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) From 005f0c3c9b723deebff490f3ff34eff79f07ba4d Mon Sep 17 00:00:00 2001 From: SangGyu An Date: Thu, 7 Sep 2023 17:30:12 -0700 Subject: [PATCH 3/7] fix --- src/sql/connection/connection.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py index 60a21947b..5a04a045c 100644 --- a/src/sql/connection/connection.py +++ b/src/sql/connection/connection.py @@ -899,6 +899,8 @@ def _execute_with_error_handling(self, operation): except Exception as e: if "duckdb.InvalidInputException" in str(e) and "please ROLLBACK" in str(e): rollback_needed = True + else: + raise if rollback_needed: self._connection.rollback() From 6fa6c62273209b5360c4f18d8423e6b9992625f7 Mon Sep 17 00:00:00 2001 From: SangGyu An Date: Fri, 8 Sep 2023 08:46:18 -0700 Subject: [PATCH 4/7] test --- src/tests/test_magic.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 From d6cc2d669178e7e4dae0a0c498ae055475f823ef Mon Sep 17 00:00:00 2001 From: SangGyu An Date: Fri, 8 Sep 2023 09:10:06 -0700 Subject: [PATCH 5/7] fix --- src/sql/connection/connection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py index 5a04a045c..0b52f1fc7 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,7 +897,7 @@ def _execute_with_error_handling(self, operation): else: raise - except Exception as e: + except ProgrammingError as e: if "duckdb.InvalidInputException" in str(e) and "please ROLLBACK" in str(e): rollback_needed = True else: From 990bcd28441e169bb23fbdee2f70567bbe97f358 Mon Sep 17 00:00:00 2001 From: SangGyu An Date: Fri, 8 Sep 2023 09:12:20 -0700 Subject: [PATCH 6/7] fix --- src/sql/connection/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py index 0b52f1fc7..84d254ee3 100644 --- a/src/sql/connection/connection.py +++ b/src/sql/connection/connection.py @@ -14,7 +14,7 @@ StatementError, PendingRollbackError, InternalError, - ProgrammingError + ProgrammingError, ) from IPython.core.error import UsageError import sqlglot From f5da251134a8eab83460e5664d9cb869e05b87da Mon Sep 17 00:00:00 2001 From: SangGyu An Date: Fri, 8 Sep 2023 11:58:01 -0700 Subject: [PATCH 7/7] fix --- src/sql/connection/connection.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py index 84d254ee3..96bc4d463 100644 --- a/src/sql/connection/connection.py +++ b/src/sql/connection/connection.py @@ -898,6 +898,8 @@ def _execute_with_error_handling(self, operation): 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: