fix: unterminated string literal on table update (#1573)

resolves #1429 
(python)

```python
-    return f"'{value}'"
+    return f'"{value}"'
```

---------

Co-authored-by: Will Jones <willjones127@gmail.com>
This commit is contained in:
Sayandip Dutta
2024-09-14 01:02:59 +05:30
committed by GitHub
parent 36d05ea641
commit 9b8472850e
2 changed files with 29 additions and 1 deletions

View File

@@ -219,6 +219,7 @@ def value_to_sql(value):
@value_to_sql.register(str)
def _(value: str):
value = value.replace("'", "''")
return f"'{value}'"

View File

@@ -15,7 +15,8 @@ import os
import pathlib
import pytest
from lancedb.util import get_uri_scheme, join_uri
import lancedb
from lancedb.util import get_uri_scheme, join_uri, value_to_sql
def test_normalize_uri():
@@ -84,3 +85,29 @@ def test_local_join_uri_windows():
assert joined == str(pathlib.Path(base) / "table.lance")
joined = join_uri(pathlib.Path(base), "table.lance")
assert joined == pathlib.Path(base) / "table.lance"
def test_value_to_sql_string(tmp_path):
# Make sure we can convert Python string literals to SQL strings, even if
# they contain characters meaningful in SQL, such as ' and \.
values = ["anthony's", 'a "test" string', "anthony's \"favorite color\" wasn't red"]
expected_values = [
"'anthony''s'",
"'a \"test\" string'",
"'anthony''s \"favorite color\" wasn''t red'",
]
for value, expected in zip(values, expected_values):
assert value_to_sql(value) == expected
# Also test we can roundtrip those strings through update.
# This validates the query parser understands the strings we
# are creating.
db = lancedb.connect(tmp_path)
table = db.create_table(
"test",
[{"search": value, "replace": "something"} for value in values],
)
for value in values:
table.update(where=f"search = {value_to_sql(value)}", values={"replace": value})
assert table.to_pandas().query("search == @value")["replace"].item() == value