Skip to content

Commit

Permalink
fix(duckdb): avoid double escaping backslashes for bind parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and jcrist committed Jul 15, 2023
1 parent e369333 commit 8436f57
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ def _format_kwargs(kwargs: Mapping[str, Any]):
bindparams, pieces = [], []
for name, value in kwargs.items():
bindparam = sa.bindparam(name, value)
if not isinstance(
bindparam.type, sa.sql.sqltypes.NullType
): # the parameter type is not null
if isinstance(paramtype := bindparam.type, sa.String):
# special case strings to avoid double escaping backslashes
pieces.append(f"{name} = '{value!s}'")
elif not isinstance(paramtype, sa.types.NullType):
bindparams.append(bindparam)
pieces.append(f"{name} = :{name}")
else: # fallback to string strategy
Expand Down
8 changes: 8 additions & 0 deletions ibis/backends/duckdb/tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,11 @@ def test_register_recordbatchreader_warns(con):
t = con.read_in_memory(reader, table_name=t.get_name())
res = t.execute()
tm.assert_frame_equal(res, sol)


def test_csv_with_slash_n_null(con, tmp_path):
data_path = tmp_path / "data.csv"
data_path.write_text("a\n1\n3\n\\N\n")
t = con.read_csv(data_path, nullstr="\\N")
col = t.a.execute()
assert pd.isna(col.iat[-1])

0 comments on commit 8436f57

Please sign in to comment.