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

duckdb frame scanning fix #1040

Merged
merged 8 commits into from
Nov 7, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 0.10.16dev

* [Fix] Updates docs for querying data frames when using DuckDB SQLAlchemy connections
* [Fix] Support for scanning data frames when using native DuckDB connections due to changes in DuckDB's API

## 0.10.15 (2024-11-05)

*Drops compatibility with Python 3.8*
Expand Down
8 changes: 8 additions & 0 deletions doc/integrations/duckdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ df = pd.DataFrame({"x": range(100)})
%sql engine
```

```{important}
If you're using DuckDB 1.1.0 or higher, you must run this before querying a data frame

~~~sql
%sql SET python_scan_all_frames=true
~~~
```

```{code-cell} ipython3
%%sql
SELECT *
Expand Down
13 changes: 11 additions & 2 deletions src/sql/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,9 @@ class DBAPIConnection(AbstractConnection):

def __init__(self, connection, alias=None, config=None):
# detect if the engine is a native duckdb connection
_is_duckdb_native = _check_if_duckdb_dbapi_connection(connection)
self._is_duckdb_native = _check_if_duckdb_dbapi_connection(connection)

self._dialect = "duckdb" if _is_duckdb_native else None
self._dialect = "duckdb" if self._is_duckdb_native else None
self._driver = None

# TODO: implement the dialect blacklist and add unit tests
Expand Down Expand Up @@ -1038,6 +1038,15 @@ def raw_execute(self, query, parameters=None, with_=None):
query = self._resolve_cte(query, with_)

cur = self._connection.cursor()

# NOTE: this is a workaround for duckdb 1.1.0 and higher so we keep the
# existing behavior of being able to query data frames
if self._is_duckdb_native:
try:
cur.execute("SET python_scan_all_frames=true")
except Exception:
pass

cur.execute(query)

if self._requires_manual_commit:
Expand Down
15 changes: 15 additions & 0 deletions src/tests/integration/test_duckDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,18 @@ def test_commits_all_statements(ip, sql, request):
out = ip.run_cell(sql)
assert out.error_in_exec is None
assert out.result.dict() == {"x": (1, 2)}


@pytest.mark.parametrize(
"ip",
[
"ip_with_duckdb_native_empty",
"ip_with_duckdb_sqlalchemy_empty",
],
)
def test_can_query_existing_df(ip, request):
ip = request.getfixturevalue(ip)
df = pd.DataFrame({"city": ["NYC"]}) # noqa
ip.run_cell("%sql SET python_scan_all_frames=true")
out = ip.run_cell("%sql SELECT * FROM df;")
assert out.result.dict() == {"city": ("NYC",)}
8 changes: 8 additions & 0 deletions src/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ def mock_dbapi_raw_execute(monkeypatch, conn_dbapi_duckdb):
def test_raw_execute_doesnt_transpile_sql_query(fixture_name, request):
mock_execute, conn = request.getfixturevalue(fixture_name)

# to prevent the "SET python_scan_all_frames=true" call, since we don't want to
# test that here
conn._is_duckdb_native = False

conn.raw_execute("CREATE TABLE foo (bar INT)")
conn.raw_execute("INSERT INTO foo VALUES (42), (43)")
conn.raw_execute("SELECT * FROM foo LIMIT 1")
Expand Down Expand Up @@ -949,6 +953,10 @@ def mock_dbapi_execute(monkeypatch):
def test_execute_transpiles_sql_query(fixture_name, request):
mock_execute, conn = request.getfixturevalue(fixture_name)

# to prevent the "SET python_scan_all_frames=true" call, since we don't want to
# test that here
conn._is_duckdb_native = False

conn.execute("CREATE TABLE foo (bar INT)")
conn.execute("INSERT INTO foo VALUES (42), (43)")
conn.execute("SELECT * FROM foo LIMIT 1")
Expand Down
Loading