Skip to content

Commit 0dd3514

Browse files
Fix select_dtypes for quoted column identifiers (#242)
Fixes duckdb/duckdb#20329 `DuckDBPyRelation.select_dtypes` failed for relations with column names that require quoting (e.g., names containing spaces). The projection builder did not correctly quote identifiers, leading to binder errors. This change ensures identifiers are quoted consistently and adds a regression test to cover the reported case.
2 parents 087dd1d + ff32a8a commit 0dd3514

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/duckdb_py/pyrelation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ unique_ptr<DuckDBPyRelation> DuckDBPyRelation::ProjectFromTypes(const py::object
153153
if (!projection.empty()) {
154154
projection += ", ";
155155
}
156-
projection += names[i];
156+
projection += KeywordHelper::WriteOptionallyQuoted(names[i]);
157157
}
158158
}
159159
if (projection.empty()) {

tests/fast/test_relation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,10 @@ def create_view(con, view_name: str) -> None:
688688

689689
res = con.sql("select * from vw").fetchall()
690690
assert res == expected
691+
692+
def test_relation_select_dtypes_quotes_identifiers_with_spaces(self, duckdb_cursor):
693+
df = pd.DataFrame({"na me": ["alice", "bob"], "x": [1, 2]})
694+
rel = duckdb_cursor.from_df(df)
695+
out = rel.select_dtypes([duckdb.sqltypes.VARCHAR]).fetchdf()
696+
assert list(out.columns) == ["na me"]
697+
assert out["na me"].tolist() == ["alice", "bob"]

0 commit comments

Comments
 (0)