vector worker: maintain a key at once when a write left NULL rows

A write that raced a key's add and stopped before fixing its rows leaves
NULL in the key's column, so queries through the key miss those rows.
They land in fragments the index does not cover yet, which maintenance
derives again, but only once the unindexed rows reached the threshold.
Detection now also proposes maintenance when those fragments hold a NULL
row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-19 23:11:11 -08:00
1 parent ddf3c2ef3a
commit 8d07288e41
1 file changed
+46 -5
@@ -73,11 +73,17 @@ pub async fn pending(
Some(State::Off) => Action::Remove,
Some(State::Ready) => {
let segments = metadata_index::segments(dataset, &key.column).await?;
let unindexed: u64 = metadata_index::unindexed(dataset, &segments)
.iter()
.map(|(_, rows)| rows)
.sum();
if unindexed == 0 || unindexed < min_unindexed_rows {
let pending = metadata_index::unindexed(dataset, &segments);
let unindexed: u64 = pending.iter().map(|(_, rows)| rows).sum();
if unindexed == 0 {
continue;
}
// Rows a write left NULL (one that raced the key's add and
// stopped before fixing them) wait for no threshold: queries
// through the key miss them until they are derived again.
if unindexed < min_unindexed_rows
&& !has_nulls(dataset, &key.column, &pending).await?
{
continue;
}
Action::Maintain
@@ -89,6 +95,27 @@ pub async fn pending(
Ok(out)
}
/// Whether `fragments` hold a row whose `column` is NULL.
async fn has_nulls(
dataset: &lance::Dataset,
column: &str,
fragments: &[(u32, u64)],
) -> Result<bool> {
let ids: Vec<u64> = fragments.iter().map(|(id, _)| *id as u64).collect();
let mut scanner = dataset.scan();
scanner
.filter(&format!("`{}` IS NULL", column.replace('`', "``")))?
.with_fragments(
dataset
.fragments()
.iter()
.filter(|f| ids.contains(&f.id))
.cloned()
.collect(),
);
Ok(scanner.count_rows().await? > 0)
}
/// Carries out `action` on `key`, as the table now records it; an action the
/// table no longer asks for is skipped. Returns what was done.
pub async fn run(
@@ -496,6 +523,20 @@ mod tests {
.await
.unwrap();
assert!(pending(&dataset, 0).await.unwrap().is_empty());
// A raced write that left NULL rows is maintained at once.
let stale = dataset.clone();
let mut dataset =
crate::metadata_index::tests::lifecycle::upsert_with_null_columns(stale, 4_500..4_520)
.await;
assert_eq!(
pending(&dataset, 1_000_000).await.unwrap(),
[("author".to_string(), Action::Maintain)]
);
run(&mut dataset, "author", Action::Maintain, 1_000, 1_000_000)
.await
.unwrap();
assert!(pending(&dataset, 1_000_000).await.unwrap().is_empty());
let noop = run(&mut dataset, "author", Action::Remove, 1_000, 0)
.await
.unwrap();