mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix: address JSON write review feedback
This commit is contained in:
@@ -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))
|
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):
|
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(
|
return pa.field(
|
||||||
field.name,
|
field.name,
|
||||||
pa.json_(),
|
json_type,
|
||||||
nullable=field.nullable,
|
nullable=field.nullable,
|
||||||
metadata=field.metadata,
|
metadata=field.metadata,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -654,3 +654,57 @@ def test_lsm_read_fts_optimized_index_not_rejected(tmp_path):
|
|||||||
.to_pylist()
|
.to_pylist()
|
||||||
)
|
)
|
||||||
assert ids == set(range(1, 11))
|
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}']
|
||||||
|
|||||||
@@ -4032,3 +4032,20 @@ async def test_refresh_column_async_job_async_table(tmp_path):
|
|||||||
assert await job.wait() is None
|
assert await job.wait() is None
|
||||||
assert await job.status() == "finished"
|
assert await job.status() == "finished"
|
||||||
assert (await table.to_arrow())["tripled"].to_pylist() == [9]
|
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()
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ use lance::dataset::mem_wal::{
|
|||||||
validate_maintained_indexes,
|
validate_maintained_indexes,
|
||||||
};
|
};
|
||||||
use lance::index::DatasetIndexExt;
|
use lance::index::DatasetIndexExt;
|
||||||
|
use lance_arrow::json::convert_json_columns;
|
||||||
use lance_core::datatypes::Schema as LanceSchema;
|
use lance_core::datatypes::Schema as LanceSchema;
|
||||||
use lance_index::mem_wal::{MemWalIndexDetails, ShardingField, ShardingSpec};
|
use lance_index::mem_wal::{MemWalIndexDetails, ShardingField, ShardingSpec};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
@@ -683,6 +684,7 @@ pub(crate) async fn execute_lsm_merge_insert(
|
|||||||
if batch.num_rows() == 0 {
|
if batch.num_rows() == 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
let batch = convert_json_columns(&batch).map_err(|e| Error::Arrow { source: e })?;
|
||||||
let batch = align_batch_schema(batch, &target_schema)?;
|
let batch = align_batch_schema(batch, &target_schema)?;
|
||||||
total_rows += batch.num_rows() as u64;
|
total_rows += batch.num_rows() as u64;
|
||||||
batches.push(batch);
|
batches.push(batch);
|
||||||
|
|||||||
Reference in New Issue
Block a user