mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
fix: allow BTree indexes on LargeUtf8 columns (#5089)
## What changed `supported_btree_data_type` in `rust/lancedb/src/utils/mod.rs` now accepts `LargeUtf8` next to `Utf8`. This is the only code change, one line. Python and TypeScript have no BTree type check of their own and call into this function, so the fix covers all three SDKs. `Index::Auto` also picks BTree for `LargeUtf8` columns now, locally and in the remote client. ## Why Fixes #5088 Lance builds BTree indexes on `LargeUtf8`, but this check in lancedb rejected them with `A BTree index cannot be created on the field ... which has data type LargeUtf8`. pandas 3 stores text columns as `large_string`, so `create_index(col, config=BTree())` and `create_scalar_index(col)` fail on tables created from a plain DataFrame. #2297 removed a similar extra check for `FixedSizeBinary`. `Binary`, `LargeBinary` and `Utf8View` may also be supported by Lance but are left out of this PR. ## How it was tested Run in a `rust:1.97.0-bookworm` container with `--profile ci --locked --features remote`, on `main` ate29ec0e0. The branch is now rebased ona6d7d02f, which only changes version numbers in `Cargo.toml`/`Cargo.lock`; the commits are unchanged. - New test `table::create_index::tests::test_create_btree_index_on_large_utf8` creates a BTree index (explicit and `Index::Auto`) on two `LargeUtf8` columns, checks the index type, and checks that `=`, `>` and `IS NULL` filters use `ScalarIndexQuery` and return the right row counts. - On `main` without the fix, the new test fails with `A BTree index cannot be created on the field ... which has data type LargeUtf8`. The existing `test_create_zonemap_index_on_wider_scalar_types` and `test_create_scalar_index` pass. - With the fix: the new test passes in 5 consecutive runs, and `create_index::`, `utils::` and `remote::table::` tests pass (559 passed). - With the one-line fix reverted, the new test fails again and the zonemap control passes. - `cargo fmt --all -- --check` and `cargo clippy --features remote -p lancedb --tests --examples -- -D warnings`: clean. - Python and TypeScript code is unchanged, so `ruff` and `pnpm` were not run. Written with help from Claude Code. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e9319c86bf
commit
0f648bbad4
@@ -1664,6 +1664,74 @@ mod tests {
|
||||
assert_eq!(null_count, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_btree_index_on_large_utf8() {
|
||||
let conn = connect("memory://").execute().await.unwrap();
|
||||
let schema = Arc::new(Schema::new(vec![
|
||||
Field::new("explicit", DataType::LargeUtf8, true),
|
||||
Field::new("auto", DataType::LargeUtf8, true),
|
||||
]));
|
||||
let values = || {
|
||||
Arc::new(LargeStringArray::from(vec![
|
||||
Some("alpha"),
|
||||
None,
|
||||
Some("omega"),
|
||||
Some("alpha"),
|
||||
])) as ArrayRef
|
||||
};
|
||||
let batch = RecordBatch::try_new(schema, vec![values(), values()]).unwrap();
|
||||
let table = conn
|
||||
.create_table("large_utf8_btree_table", batch)
|
||||
.execute()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
table
|
||||
.create_index(&["explicit"], Index::BTree(BTreeIndexBuilder::default()))
|
||||
.execute()
|
||||
.await
|
||||
.unwrap();
|
||||
// Auto picks BTree for string columns, so LargeUtf8 must get one too.
|
||||
table
|
||||
.create_index(&["auto"], Index::Auto)
|
||||
.execute()
|
||||
.await
|
||||
.unwrap();
|
||||
table
|
||||
.wait_for_index(&["explicit_idx", "auto_idx"], Duration::from_millis(10))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let index_configs = table.list_indices().await.unwrap();
|
||||
assert_eq!(index_configs.len(), 2);
|
||||
for index in index_configs {
|
||||
assert_eq!(index.index_type, crate::index::IndexType::BTree);
|
||||
}
|
||||
|
||||
for column in ["explicit", "auto"] {
|
||||
for (filter, expected_rows) in [
|
||||
(format!("{column} = 'alpha'"), 2),
|
||||
(format!("{column} > 'beta'"), 1),
|
||||
(format!("{column} IS NULL"), 1),
|
||||
] {
|
||||
let query = table.query().only_if(filter.as_str());
|
||||
let plan = query.explain_plan(false).await.unwrap();
|
||||
assert!(plan.contains("ScalarIndexQuery"), "{filter}: {plan}");
|
||||
let num_rows = query
|
||||
.execute()
|
||||
.await
|
||||
.unwrap()
|
||||
.try_collect::<Vec<_>>()
|
||||
.await
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|b| b.num_rows())
|
||||
.sum::<usize>();
|
||||
assert_eq!(num_rows, expected_rows, "{filter}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_builtin_scalar_index() {
|
||||
for (index, index_type, predicate, invalid_column) in [
|
||||
|
||||
@@ -539,6 +539,7 @@ pub fn supported_btree_data_type(dtype: &DataType) -> bool {
|
||||
dtype,
|
||||
DataType::Boolean
|
||||
| DataType::Utf8
|
||||
| DataType::LargeUtf8
|
||||
| DataType::Time32(_)
|
||||
| DataType::Time64(_)
|
||||
| DataType::Date32
|
||||
|
||||
Reference in New Issue
Block a user