From 8d07288e4128205a2fd018fea2f4981fc72f5908 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 19 Nov 2026 23:11:11 -0800 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK --- .../crates/lance/src/jobs/metadata_index.rs | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/seaweed-worker/crates/lance/src/jobs/metadata_index.rs b/seaweed-worker/crates/lance/src/jobs/metadata_index.rs index 0c565bb01b..c9792ee368 100644 --- a/seaweed-worker/crates/lance/src/jobs/metadata_index.rs +++ b/seaweed-worker/crates/lance/src/jobs/metadata_index.rs @@ -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 { + let ids: Vec = 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();