Skip to content

Commit 03e4e11

Browse files
cpsievertclaude
andcommitted
refactor(pkg-py): remove unused duckdb_result_to_nw function
The function is no longer used since DataFrameSource now uses duckdb_result_to_polars and duckdb_result_to_pandas directly. Updated tests to use duckdb_result_to_polars. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 6db7fb7 commit 03e4e11

File tree

2 files changed

+14
-50
lines changed

2 files changed

+14
-50
lines changed

pkg-py/src/querychat/_df_compat.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,6 @@ def read_sql_pyarrow(query: TextClause, conn: Connection):
5757
return pl.read_database(query, connection=conn).to_arrow()
5858

5959

60-
def duckdb_result_to_nw(
61-
result: duckdb.DuckDBPyRelation | duckdb.DuckDBPyConnection,
62-
) -> nw.DataFrame:
63-
# Check for polars first without consuming the result
64-
try:
65-
import polars # noqa: F401, ICN001 # pyright: ignore[reportMissingImports]
66-
67-
return nw.from_native(result.pl())
68-
except ImportError:
69-
pass
70-
except Exception: # noqa: S110
71-
# Other polars errors (e.g., missing pyarrow) - fall through to pandas
72-
pass
73-
74-
try:
75-
import pandas # noqa: F401, ICN001 # pyright: ignore[reportMissingImports]
76-
77-
return nw.from_native(result.df())
78-
except ImportError:
79-
pass
80-
81-
raise ImportError(f"DataFrameSource requires 'polars' or 'pandas'. {_INSTALL_MSG}")
82-
83-
8460
def duckdb_result_to_polars(
8561
result: duckdb.DuckDBPyRelation | duckdb.DuckDBPyConnection,
8662
):

pkg-py/tests/test_df_compat.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import duckdb
88
import narwhals.stable.v1 as nw
99
import pytest
10-
from querychat._df_compat import duckdb_result_to_nw, read_csv
10+
from querychat._df_compat import duckdb_result_to_polars, read_csv
1111

1212
# Check if polars and pyarrow are available (both needed for DuckDB + polars)
1313
try:
@@ -60,8 +60,11 @@ def test_read_csv_data_integrity(self, gzip_csv_file):
6060
assert names == ["Alice", "Bob", "Charlie"]
6161

6262

63-
class TestDuckdbResultToNw:
64-
"""Tests for the duckdb_result_to_nw function."""
63+
@pytest.mark.skipif(
64+
not HAS_POLARS_WITH_PYARROW, reason="polars or pyarrow not installed"
65+
)
66+
class TestDuckdbResultToPolars:
67+
"""Tests for the duckdb_result_to_polars function."""
6568

6669
@pytest.fixture
6770
def duckdb_conn(self):
@@ -73,16 +76,16 @@ def duckdb_conn(self):
7376
yield conn
7477
conn.close()
7578

76-
def test_duckdb_result_returns_narwhals_dataframe(self, duckdb_conn):
77-
"""Test that duckdb_result_to_nw returns a narwhals DataFrame."""
79+
def test_duckdb_result_returns_polars_dataframe(self, duckdb_conn):
80+
"""Test that duckdb_result_to_polars returns a polars DataFrame."""
7881
result = duckdb_conn.execute("SELECT * FROM test")
79-
df = duckdb_result_to_nw(result)
80-
assert isinstance(df, nw.DataFrame)
82+
df = duckdb_result_to_polars(result)
83+
assert isinstance(df, pl.DataFrame)
8184

8285
def test_duckdb_result_has_correct_data(self, duckdb_conn):
83-
"""Test that duckdb_result_to_nw preserves data correctly."""
86+
"""Test that duckdb_result_to_polars preserves data correctly."""
8487
result = duckdb_conn.execute("SELECT * FROM test ORDER BY id")
85-
df = duckdb_result_to_nw(result)
88+
df = duckdb_result_to_polars(result)
8689

8790
assert df.shape == (2, 3)
8891
assert list(df.columns) == ["id", "name", "value"]
@@ -92,9 +95,9 @@ def test_duckdb_result_has_correct_data(self, duckdb_conn):
9295
def test_duckdb_result_empty_query(self, duckdb_conn):
9396
"""Test handling of empty query results."""
9497
result = duckdb_conn.execute("SELECT * FROM test WHERE id > 100")
95-
df = duckdb_result_to_nw(result)
98+
df = duckdb_result_to_polars(result)
9699

97-
assert isinstance(df, nw.DataFrame)
100+
assert isinstance(df, pl.DataFrame)
98101
assert df.shape == (0, 3)
99102

100103

@@ -117,18 +120,3 @@ def test_read_csv_uses_polars_when_available(self):
117120
assert isinstance(native, pl.DataFrame)
118121
finally:
119122
Path(temp_path).unlink()
120-
121-
def test_duckdb_result_uses_polars_when_available(self):
122-
"""Test that duckdb_result_to_nw uses polars when available."""
123-
conn = duckdb.connect(":memory:")
124-
conn.execute("CREATE TABLE t (x INTEGER)")
125-
conn.execute("INSERT INTO t VALUES (1)")
126-
127-
result = conn.execute("SELECT * FROM t")
128-
df = duckdb_result_to_nw(result)
129-
130-
# The native frame should be polars when polars is available
131-
native = df.to_native()
132-
assert isinstance(native, pl.DataFrame)
133-
134-
conn.close()

0 commit comments

Comments
 (0)