diff --git a/python/python/lancedb/table.py b/python/python/lancedb/table.py index 8715f8487..273c8085a 100644 --- a/python/python/lancedb/table.py +++ b/python/python/lancedb/table.py @@ -803,9 +803,20 @@ def _cast_to_target_schema( ) reordered_schema = pa.schema(fields, metadata=target_schema.metadata) + json_string_views = [ + (index, field) + for index, (source, field) in enumerate(zip(reader.schema, reordered_schema)) + if pa.types.is_string_view(source.type) + and pa.types.is_string(field.type) + and _field_extension_name(field) == "arrow.json" + ] + def gen(): for batch in reader: batch = _coerce_blob_write_columns(batch, reordered_schema) + for index, field in json_string_views: + column = _cast_json_string_view(batch.column(index)) + batch = batch.set_column(index, field, column) # Table but not RecordBatch has cast. cast_batches = ( pa.Table.from_batches([batch]).cast(reordered_schema).to_batches() @@ -1229,6 +1240,14 @@ def _arrow_json_storage_type(input_type: pa.DataType) -> Optional[pa.DataType]: return None +def _cast_json_string_view(column: pa.Array) -> pa.Array: + try: + return column.cast(pa.string()) + except pa.ArrowNotImplementedError: + # PyArrow 16 exposes StringView without a StringView-to-Utf8 cast. + return pa.array(column.to_pylist(), type=pa.string()) + + def _align_field(field: pa.Field, target_field: pa.Field) -> pa.Field: if is_blob_v2_field(target_field): source_type = field.type diff --git a/python/python/tests/test_merge_insert_lsm.py b/python/python/tests/test_merge_insert_lsm.py index ce98933b8..b8c7d0cd5 100644 --- a/python/python/tests/test_merge_insert_lsm.py +++ b/python/python/tests/test_merge_insert_lsm.py @@ -110,6 +110,7 @@ def json_rte_table(tmp_path): [ pytest.param(pa.string(), id="utf8"), pytest.param(pa.large_string(), id="large-utf8"), + pytest.param(pa.string_view(), id="utf8-view"), pytest.param( pa.json_() if hasattr(pa, "json_") else None, marks=pytest.mark.skipif( diff --git a/python/python/tests/test_table.py b/python/python/tests/test_table.py index 1eb519420..593417b07 100644 --- a/python/python/tests/test_table.py +++ b/python/python/tests/test_table.py @@ -5087,6 +5087,45 @@ def test_json_write_rejects_unmarked_binary(tmp_db: DBConnection, operation): assert tmp_db.open_table("t").search().to_list() == [{"id": 1, "j": '{"k":1}'}] +@pytest.mark.parametrize("operation", ["sanitized_append", "merge_insert"]) +def test_json_write_with_legacy_string_view(tmp_db: DBConnection, operation): + schema = pa.schema( + [ + pa.field("id", pa.int64()), + pa.field("j", pa.string(), metadata={"ARROW:extension:name": "arrow.json"}), + ] + ) + table = tmp_db.create_table("t", schema=schema) + if operation == "merge_insert": + table.add([{"id": 1, "j": '{"k": 0}'}]) + + source = pa.table( + { + "id": [1, 2, 3], + "j": pa.array(['{"k": 9}', '{ "k": 2 }', None], type=pa.string_view()), + } + ).to_reader(max_chunksize=1) + + if operation == "merge_insert": + ( + table.merge_insert("id") + .when_matched_update_all() + .when_not_matched_insert_all() + .execute(source) + ) + else: + table.add(source, on_bad_vectors="fill") + + reopened = tmp_db.open_table("t") + assert reopened.search().to_arrow().sort_by("id").to_pylist() == [ + {"id": 1, "j": '{"k":9}'}, + {"id": 2, "j": '{"k":2}'}, + {"id": 3, "j": None}, + ] + matches = reopened.search().where("json_extract(j, '$.k') IN ('9', '2')").to_list() + assert sorted(row["id"] for row in matches) == [1, 2] + + @pytest.mark.skipif(not hasattr(pa, "json_"), reason="requires PyArrow JSON type") @pytest.mark.asyncio async def test_merge_insert_encodes_json(mem_db_async: AsyncConnection):