From 0dbdfd8e4dab4b8047e7fd407dd4ac414c699171 Mon Sep 17 00:00:00 2001 From: discord9 Date: Wed, 25 Nov 2026 23:27:31 +0800 Subject: [PATCH] fix(mito2): fail a row no piece selects when the pieces disagree The last silent replay in the parquet read: a row no candidate slice selects was bounded by the least advanced cursor. Exact while the pieces agree, a duplicate once they do not. Resolve the span lazily on the first unselected row and fail with SEQUENCE_RANGE_UNSUPPORTED when it is not a single value; rows at or below the substituted cut keep their treatment, and an agreeing set takes the same path as before. Signed-off-by: discord9 --- src/mito2/src/engine/scan_test.rs | 99 +++++++++++++++++++++++++ src/mito2/src/memtable/time_series.rs | 3 +- src/mito2/src/sst/parquet/file_range.rs | 63 ++++++++++++++-- 3 files changed, 157 insertions(+), 8 deletions(-) diff --git a/src/mito2/src/engine/scan_test.rs b/src/mito2/src/engine/scan_test.rs index f3f6d389e3e..cb1d8635431 100644 --- a/src/mito2/src/engine/scan_test.rs +++ b/src/mito2/src/engine/scan_test.rs @@ -7834,6 +7834,105 @@ async fn test_read_row_sequence_fails_closed_when_pieces_disagree_and_piece_is_u ); } +/// The third shape the per-row rule cannot answer: a row of the file that **no** candidate +/// expression selects, i.e. a row outside the value domain of every piece of the creator. +/// +/// Such a row has no piece to attribute it to, so only the least advanced candidate is left. +/// That floor is exact while every candidate carries the same cursor value — it then *is* what +/// the row's own piece states — and says nothing once they differ: it delivers the rows a more +/// advanced piece already consumed, so the read fails instead of replaying them. +#[tokio::test] +async fn test_read_row_sequence_fails_closed_when_no_piece_selects_a_row() { + let mut env = TestEnv::with_prefix("test_read_row_sequence_fails_closed_unselected_row").await; + let engine = env.create_engine(MitoConfig::default()).await; + let source = RegionId::new(1, 2); + let target = RegionId::new(1, 1); + + // One file of the creator: four rows of the series `k0 = "1"`, inside the domain of the + // left piece (`k0 < "5"`), and one row of `k0 = "9"`, outside the domain of both candidates. + let request = CreateRequestBuilder::new() + .insert_option("append_mode", "true") + .insert_option("preserve_row_sequence", "true") + .build(); + let schema = test_util::rows_schema(&request); + engine + .handle_request(source, RegionRequest::Create(request)) + .await + .unwrap(); + let mut rows = test_util::build_rows_for_key("1", 0, 4, 0); + rows.extend(test_util::build_rows_for_key("9", 4, 5, 0)); + test_util::put_rows(&engine, source, Rows { schema, rows }).await; + test_util::flush_region(&engine, source, None).await; + let source_file = manifest_files(&engine, source) + .await + .into_values() + .next() + .expect("flushed file"); + create_foreign_target(&engine, target).await; + adopt_foreign_file(&engine, target, source_file, 6, true, None); + + // The candidates carry 5 and 2. The row at 5 (`k0 = "9"`) belongs to no piece, and the + // least advanced cursor (2) does not stand for the pieces: it would deliver the rows 3 and + // 4 the left piece already consumed up to 5. + let mut stream = engine + .scan_batch( + target, + creator_cursor_scan( + source, + vec![ + partition_str_slice_cursor("0", "5", 5), + partition_str_slice_cursor("5", "9", 2), + ], + 0, + 10, + ), + true, + ) + .await + .unwrap(); + let err = match stream.try_next().await { + Err(err) => err, + Ok(_) => panic!("a row no candidate selects with disagreeing cursors must fail the read"), + }; + let message = error_chain(&err); + assert!( + message.contains("exact sequence range unavailable"), + "the shared exact-range contract prefix must reach the caller: {message}" + ); + assert!( + message.contains("cannot resolve a per-piece cursor"), + "the failure must say what could not be resolved: {message}" + ); + assert!( + message.contains("match no candidate creator cursor slice") && message.contains("(2..=5)"), + "the failure must name the unselected row and the cursors' own span: {message}" + ); + + // One cursor value for every candidate: the least advanced cursor is then every piece's + // cursor, so the row outside both domains keeps today's treatment and nothing is dropped. + let mut rows = scan_flat_row_sequences( + &engine, + target, + creator_cursor_scan( + source, + vec![ + partition_str_slice_cursor("0", "5", 2), + partition_str_slice_cursor("5", "9", 2), + ], + 0, + 10, + ), + ) + .await; + rows.sort_unstable(); + assert_eq!( + vec![(2000, 3), (3000, 4), (4000, 5)], + rows, + "with one cursor value the row no candidate selects is bounded by it exactly, so the \ + rows above 2 are delivered and the rows at or below it are not" + ); +} + /// The creator's cursor and the holder's snapshot are two different sequence domains, so a /// foreign file is filtered by the creator's cursor alone — never by the holder's `(C, H]`, /// which is what bounds the rows of every *local* file of the same scan. diff --git a/src/mito2/src/memtable/time_series.rs b/src/mito2/src/memtable/time_series.rs index 4affcc474de..3a9d4934aaa 100644 --- a/src/mito2/src/memtable/time_series.rs +++ b/src/mito2/src/memtable/time_series.rs @@ -2146,7 +2146,8 @@ mod tests { .find_map(|batch| batch.err()) .expect("a row no candidate explains fails the read while the cursors disagree"); assert!( - err.to_string().contains("cannot be resolved while the pieces"), + err.to_string() + .contains("cannot be resolved while the pieces"), "the failure names the unresolved piece: {err}" ); } diff --git a/src/mito2/src/sst/parquet/file_range.rs b/src/mito2/src/sst/parquet/file_range.rs index 69e78461c6f..23a34350222 100644 --- a/src/mito2/src/sst/parquet/file_range.rs +++ b/src/mito2/src/sst/parquet/file_range.rs @@ -43,7 +43,8 @@ use tokio::sync::OnceCell; use crate::cache::CacheStrategy; use crate::error::{ ComputeArrowSnafu, DecodeStatsSnafu, EvalPartitionFilterSnafu, InvalidRecordBatchSnafu, - NewRecordBatchSnafu, RecordBatchSnafu, Result, StatsNotPresentSnafu, UnexpectedSnafu, + NewRecordBatchSnafu, RecordBatchSnafu, Result, SequenceRangeUnsupportedSnafu, + StatsNotPresentSnafu, UnexpectedSnafu, }; use crate::read::compat::FlatCompatBatch; use crate::read::last_row::FlatRowGroupLastRowCachedReader; @@ -608,8 +609,11 @@ pub(crate) struct PartitionFilterContext { /// Cursor applied to a row of the creator's domain that no slice expression selects. /// `None` when [`Self::creator_slices`] is empty. /// - /// A row no slice explains is attributed to the least advanced candidate: re-reading a - /// consumed row is recoverable, dropping a live one is not. + /// A row no slice explains is bounded by the least advanced candidate. That bound is the + /// row's own piece's answer only while every candidate carries it: when the candidates + /// disagree, the read fails with + /// [`SequenceRangeUnsupported`](crate::error::Error::SequenceRangeUnsupported) instead of + /// delivering the rows a more advanced piece has already consumed. pub(crate) fallback_cursor: Option, /// The file's own substituted cut /// (`FileMeta::substituted_up_to`), when it states one. @@ -634,6 +638,21 @@ pub(crate) struct CreatorSliceFilter { pub(crate) cursor: SequenceNumber, } +/// The least and the greatest cursor value of a candidate slice set. +/// +/// Equal bounds mean every candidate carries the same cursor value — the only case in which +/// [`PartitionFilterContext::fallback_cursor`] bounds a row no candidate selects exactly. +fn creator_cursor_span(slices: &[CreatorSliceFilter]) -> (SequenceNumber, SequenceNumber) { + let mut span: Option<(SequenceNumber, SequenceNumber)> = None; + for slice in slices { + span = Some(match span { + None => (slice.cursor, slice.cursor), + Some((min, max)) => (min.min(slice.cursor), max.max(slice.cursor)), + }); + } + span.unwrap_or_default() +} + /// Common fields for a range to read and filter batches. pub(crate) struct RangeBase { /// Filters pushed down. @@ -981,9 +1000,13 @@ impl RangeBase { /// into narrower ones whose cursors advanced differently, and free of lost rows when /// the file's own expression is narrower than the rows it holds. /// - /// A row no slice expression selects is attributed to the least advanced candidate - /// ([`PartitionFilterContext::fallback_cursor`]) and reported, because dropping it - /// would lose live data while re-reading it only repeats a consumed row. + /// A row no slice expression selects has no piece to attribute it to, so only the least + /// advanced candidate ([`PartitionFilterContext::fallback_cursor`]) can bound it. That + /// bound is exact while every candidate carries the same cursor value — it then *is* the + /// row's own piece's answer — and states nothing about the row once the candidates differ: + /// it delivers the rows a more advanced piece has already consumed, which is a duplicate, + /// not a replay, so the read fails with + /// [`SequenceRangeUnsupported`](crate::error::Error::SequenceRangeUnsupported) instead. fn evaluate_creator_slices( &self, input: &RecordBatch, @@ -1027,7 +1050,13 @@ impl RangeBase { let mut kept = BooleanBufferBuilder::new(num_rows); let mut unmatched = 0; + // The candidates' own span, resolved on the first row no candidate selects: while + // every candidate carries the same value the fallback below is that row's answer and + // the span is never asked for. + let mut cursor_span: Option<(SequenceNumber, SequenceNumber)> = None; for row in 0..num_rows { + // A row at or below the substituted cut is re-read on purpose by the substitution + // rule, so no cursor decision — and no disagreement — applies to it. if substituted_mask .as_ref() .is_some_and(|mask| mask.value(row)) @@ -1039,6 +1068,26 @@ impl RangeBase { Some(index) => not_consumed[index].value(row), None => { unmatched += 1; + let (min, max) = + *cursor_span.get_or_insert_with(|| creator_cursor_span(slices)); + if min != max { + let region_id = self.read_format.metadata().region_id; + return SequenceRangeUnsupportedSnafu { + region_id, + min_seq: min, + max_seq: max, + reason: format!( + "the physical-sequence read of a file of region {} cannot \ + resolve a per-piece cursor: {} of {} rows of the file match no \ + candidate creator cursor slice, while the candidate slices of \ + that region disagree on the last consumed sequence ({}..={}), \ + so bounding those rows by the least advanced cursor {} would \ + deliver the rows a more advanced piece has already consumed", + region_id, unmatched, num_rows, min, max, min + ), + } + .fail(); + } fallback_mask.value(row) } }; @@ -1046,7 +1095,7 @@ impl RangeBase { } if unmatched > 0 { debug!( - "{} of {} rows of an adopted file match no creator cursor slice; bounding them by the least advanced cursor {}", + "{} of {} rows of an adopted file match no creator cursor slice; bounding them by the least advanced cursor {}, which every candidate carries", unmatched, num_rows, fallback_cursor ); }