Skip to content

Apache DataFusion SQL Query Engine

Make `RowValues::compare`'s current-offset assumption self-checking (#25643)

Nathan7bda0cdb28f3
diff --git a/datafusion/physical-plan/src/sorts/cursor.rs b/datafusion/physical-plan/src/sorts/cursor.rs
index f12e0daab5..d17d178a98 100644
--- a/datafusion/physical-plan/src/sorts/cursor.rs
+++ b/datafusion/physical-plan/src/sorts/cursor.rs
@@ -192,6 +192,18 @@ pub struct RowValues {
/// Cached byte length for the current row.
current_len: usize,
+ /// The offset the cache was last refreshed for. Used by
+ /// [`CursorValues::compare`] to assert callers only compare at the
+ /// current offset (the narrowed contract this impl relies on).
+ ///
+ /// Gated on `debug_assertions` deliberately, not for tidiness: a plain
+ /// `debug_assert_eq!` would compile away on its own, but the field
+ /// itself would still widen `Cursor<RowValues>` — the struct the loser
+ /// tree shuffles on every comparison — by eight bytes in release. The
+ /// cfg keeps the release layout byte-identical.
+ #[cfg(debug_assertions)]
+ current_offset: usize,
+
/// Tracks for the memory used by in the `Rows` of this
/// cursor. Freed on drop
_reservation: MemoryReservation,
@@ -216,6 +228,10 @@ impl RowValues {
);
let len = rows.num_rows();
assert!(len > 0);
+ // Seed the cache for row 0. `current_ptr`, `current_len`, and (in
+ // debug builds) `current_offset` must always describe the same row;
+ // `Cursor::new` starts at offset 0 without calling `set_offset`, so
+ // it depends on this constructor having seeded exactly row 0.
// Extract raw ptr + length while the temporary `Row` is still alive.
// The pointer is into `rows`'s Arc buffer heap and stays valid.
let (current_ptr, current_len) = {
@@ -228,6 +244,8 @@ impl RowValues {
len,
current_ptr,
current_len,
+ #[cfg(debug_assertions)]
+ current_offset: 0,
_reservation: reservation,
}
}
@@ -241,6 +259,11 @@ impl RowValues {
}
}
+/// Note: this implementation deliberately narrows the [`CursorValues`]
+/// contract for [`CursorValues::compare`]: it only supports comparing the
+/// *current* offsets of `l` and `r` (as [`Cursor::cmp`] does), reading the
+/// cached slices instead of the passed indices. Debug builds assert this;
+/// callers needing arbitrary-index comparisons must not use `RowValues`.
impl CursorValues for RowValues {
// Reuse arrow-row's own owned-row type: `Row::owned()` copies just that
// row's bytes out of the shared `Rows` buffer, with no `RowConverter`
@@ -267,9 +290,27 @@ impl CursorValues for RowValues {
}
fn compare(l: &Self, l_idx: usize, r: &Self, r_idx: usize) -> Ordering {
- // Merge callers always compare at current offsets; the cache is up
- // to date. (Debug-only: verify the invariant.)
- debug_assert!(l_idx < l.len && r_idx < r.len);
+ // Narrowed contract (see impl docs): merge callers always compare at
+ // the current offsets, so the cached slices are the requested rows.
+ // Debug builds verify the indices match the offsets the cache was
+ // refreshed for; zero cost in release.
+ //
+ // A finished cursor (`offset == len`) has `current_offset == len - 1`
+ // because `advance` skips the out-of-bounds `set_offset`; the assert
+ // would fire on it, intentionally — a finished cursor's stale cache
+ // must never be read, and `Cursor::advance` upholds that one layer up
+ // (the previous bounds-only assert rejected the same state).
+ #[cfg(debug_assertions)]
+ {
+ debug_assert_eq!(
+ l_idx, l.current_offset,
+ "RowValues::compare only supports the current offset"
+ );
+ debug_assert_eq!(
+ r_idx, r.current_offset,
+ "RowValues::compare only supports the current offset"
+ );
+ }
let _ = (l_idx, r_idx);
l.current_slice().cmp(r.current_slice())
}
@@ -287,6 +328,10 @@ impl CursorValues for RowValues {
};
self.current_ptr = ptr;
self.current_len = len;
+ #[cfg(debug_assertions)]
+ {
+ self.current_offset = offset;
+ }
}
fn get_value(&self, idx: usize) -> OwnedRow {
@@ -756,6 +801,18 @@ mod tests {
assert!(!cursor.is_finished());
}
+ /// Debug builds must reject callers passing indices other than the current
+ /// offset — the narrowed contract `RowValues::compare` relies on.
+ #[test]
+ #[cfg(debug_assertions)]
+ #[should_panic(expected = "only supports the current offset")]
+ fn test_row_values_compare_rejects_non_current_offset() {
+ let a = new_row_values(&["a", "b"]);
+ let b = new_row_values(&["a", "b"]);
+ // Both cursors are at offset 0; index 1 is in bounds but stale.
+ RowValues::compare(&a.values, 1, &b.values, 1);
+ }
+
#[test]
fn test_primitive_nulls_first() {
let options = SortOptions {