mirror of
https://github.com/lancedb/lancedb.git
synced 2026-05-14 02:20:40 +00:00
feat(python): support dict to SQL struct conversion in table.update() (#3089)
## Summary
- Add `@value_to_sql.register(dict)` handler that converts Python dicts
to DataFusion's `named_struct()` SQL syntax
- Enables updating struct-typed columns via `table.update(values={"col":
{"field_a": 1, "field_b": "hello"}})`
- Recursively handles nested structs, lists, nulls, and all existing
scalar types
Closes #1363
## Details
The `named_struct` function was introduced in DataFusion 38 and is now
available (LanceDB uses DataFusion 52.1). The implementation follows the
existing `singledispatch` pattern in `util.py`.
**Example conversion:**
```python
value_to_sql({"field_a": 1, "field_b": "hello"})
# => "named_struct('field_a', 1, 'field_b', 'hello')"
```
## Test plan
- [x] Unit tests for flat struct, nested struct, list inside struct,
mixed types, null values, and empty dict
- [ ] CI integration tests with actual table.update() on struct columns
🔗 [DataFusion named_struct
docs](https://datafusion.apache.org/user-guide/sql/scalar_functions.html#named-struct)
This commit is contained in:
@@ -121,6 +121,32 @@ def test_value_to_sql_string(tmp_path):
|
||||
assert table.to_pandas().query("search == @value")["replace"].item() == value
|
||||
|
||||
|
||||
def test_value_to_sql_dict():
|
||||
# Simple flat struct
|
||||
assert value_to_sql({"a": 1, "b": "hello"}) == "named_struct('a', 1, 'b', 'hello')"
|
||||
|
||||
# Nested struct
|
||||
assert (
|
||||
value_to_sql({"outer": {"inner": 1}})
|
||||
== "named_struct('outer', named_struct('inner', 1))"
|
||||
)
|
||||
|
||||
# List inside struct
|
||||
assert value_to_sql({"a": [1, 2]}) == "named_struct('a', [1, 2])"
|
||||
|
||||
# Mixed types
|
||||
assert (
|
||||
value_to_sql({"name": "test", "count": 42, "rate": 3.14, "active": True})
|
||||
== "named_struct('name', 'test', 'count', 42, 'rate', 3.14, 'active', TRUE)"
|
||||
)
|
||||
|
||||
# Null value inside struct
|
||||
assert value_to_sql({"a": None}) == "named_struct('a', NULL)"
|
||||
|
||||
# Empty dict
|
||||
assert value_to_sql({}) == "named_struct()"
|
||||
|
||||
|
||||
def test_append_vector_columns():
|
||||
registry = EmbeddingFunctionRegistry.get_instance()
|
||||
registry.register("test")(MockTextEmbeddingFunction)
|
||||
|
||||
Reference in New Issue
Block a user