fix: address JSON write review feedback

This commit is contained in:
LH-kevin
2026-08-24 21:23:51 +08:00
parent 276cd3c685
commit 601aebe877
4 changed files with 79 additions and 1 deletions
+6 -1
View File
@@ -465,9 +465,14 @@ def _coerce_json_field(field: pa.Field, target_field: pa.Field) -> Optional[pa.F
or (hasattr(pa.types, "is_string_view") and pa.types.is_string_view(field.type))
)
if is_string_like or pa.types.is_null(field.type):
json_type = (
pa.json_(pa.large_string())
if pa.types.is_large_string(field.type)
else pa.json_()
)
return pa.field(
field.name,
pa.json_(),
json_type,
nullable=field.nullable,
metadata=field.metadata,
)
@@ -654,3 +654,57 @@ def test_lsm_read_fts_optimized_index_not_rejected(tmp_path):
.to_pylist()
)
assert ids == set(range(1, 11))
@pytest.mark.skipif(not hasattr(pa, "json_"), reason="requires PyArrow JSON type")
def test_lsm_merge_insert_encodes_string_json(tmp_path):
json_type = pa.json_()
schema = pa.schema(
[
pa.field("id", pa.int64(), nullable=False),
pa.field("j", json_type),
]
)
json_values = pa.ExtensionArray.from_storage(
json_type,
pa.array(['{"k": 1}'], type=json_type.storage_type),
)
initial = pa.Table.from_arrays(
[
pa.array([1], type=pa.int64()),
json_values,
],
schema=schema,
)
db = lancedb.connect(
tmp_path,
read_consistency_interval=timedelta(seconds=0),
)
table = db.create_table("json_lsm", initial)
table.set_unenforced_primary_key("id")
table.set_lsm_write_spec(LsmWriteSpec.unsharded())
source = pa.table(
{
"id": pa.array([1, 2], type=pa.int64()),
"j": pa.array(
['{"k": 9}', '{"k": 2}'],
type=pa.string(),
),
}
)
result = (
table.merge_insert("id")
.when_matched_update_all()
.when_not_matched_insert_all()
.execute(source)
)
assert result.num_rows == 2
rows = table.search().to_arrow().sort_by("id")
assert rows["id"].to_pylist() == [1, 2]
assert rows["j"].to_pylist() == ['{"k":9}', '{"k":2}']
+17
View File
@@ -4032,3 +4032,20 @@ async def test_refresh_column_async_job_async_table(tmp_path):
assert await job.wait() is None
assert await job.status() == "finished"
assert (await table.to_arrow())["tripled"].to_pylist() == [9]
@pytest.mark.skipif(not hasattr(pa, "json_"), reason="requires PyArrow JSON type")
def test_coerce_json_field_preserves_large_string_storage():
from lancedb.table import _coerce_json_field
source = pa.field("j", pa.large_string())
target = pa.field(
"j",
pa.large_binary(),
metadata={b"ARROW:extension:name": b"lance.json"},
)
coerced = _coerce_json_field(source, target)
assert coerced is not None
assert coerced.type.storage_type == pa.large_string()