fix(python): write enum members as their values in table.update (#5061)

`table.update(values=...)` fails for a member of an int- or float-valued
Enum. `value_to_sql` dispatches it to the `int` or `float` handler, and
`str()` returns the member name, so the SQL reads `priority =
Priority.HIGH` as a field access. A plain Enum member raises
`NotImplementedError`. #3232 stores a `(int, Enum)` field as int64 and
#4357 writes Enum fields as their values on `add`, so only `update`
disagrees.

```python
class Priority(int, Enum):
    HIGH = 2

table = db.create_table("t", data=[{"id": 1, "priority": 1}])
table.update(values={"priority": Priority.HIGH})
# lancedb 0.41.0: ValueError: Invalid input, Execution error: Cannot access field at argument 1: type Int64 is not Struct, Map, or Null
```

The int and float handlers now convert with `int()` and `float()` first,
and an `Enum` handler writes `value.value`. Str-valued members already
worked, because `str.replace` returns a plain `str`. No issue is open
for this.

```
$ cd python && pytest python/tests/test_util.py -k enum_members -q   # 2ab103d7 plus this commit, lancedb 0.42.0b15 extension
1 passed, 139 deselected, 1 warning in 0.36s
# util.py reverted, test kept
E       AssertionError: assert 'Priority.HIGH' == '2'
1 failed, 139 deselected, 1 warning in 0.29s
$ ruff format --check . && ruff check .   # ruff 0.9.9, as CI pins
191 files already formatted
All checks passed!
$ pyright   # 1.1.414
3 errors, 0 warnings, 0 informations   # the same 3 in __init__.py at 2ab103d7
$ pytest python/tests -m "not slow and not s3_test" -q
1 failed, 4144 passed, 198 skipped, 73 deselected   # 2ab103d7: 1 failed, 4143 passed
# the failure in both is test_public_function_values_are_in_api_reference, which reads docs/ and my tree exported python/ only
# not run: remote and s3 tests
```

@Xuanwo, if you'd rather `update` reuse the #4357 converter than grow an
Enum branch in `value_to_sql`, I'll move it there.

Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com>
This commit is contained in:
Christopher Krah
2026-11-20 09:53:49 -08:00
committed by GitHub
parent cfa94f0077
commit 7eb40118ac
2 changed files with 37 additions and 2 deletions
+27
View File
@@ -407,6 +407,33 @@ def test_value_to_sql_nan_and_infinity(tmp_path):
assert rows[4]["f64"] == 4.0 and rows[4]["f32"] == 4.0
def test_value_to_sql_enum_members(tmp_path):
# str() of an int- or float-valued Enum member is its name, which SQL reads
# as a field access, and a plain Enum member raised NotImplementedError.
import enum
class Priority(int, enum.Enum):
LOW = 1
HIGH = 2
class Weight(float, enum.Enum):
HALF = 0.5
class Color(enum.Enum):
RED = "red"
assert value_to_sql(Priority.HIGH) == "2"
assert value_to_sql(Weight.HALF) == "0.5"
assert value_to_sql(Color.RED) == "'red'"
db = lancedb.connect(tmp_path)
table = db.create_table(
"test", pa.table({"id": [1], "p": [1], "w": [1.0], "c": ["blue"]})
)
table.update(values={"p": Priority.HIGH, "w": Weight.HALF, "c": Color.RED})
assert table.to_arrow().to_pylist() == [{"id": 1, "p": 2, "w": 0.5, "c": "red"}]
def test_append_vector_columns():
registry = EmbeddingFunctionRegistry.get_instance()
registry.register("test")(MockTextEmbeddingFunction)