Skip to content

Commit 1251007

Browse files
committed
Guard the decoded length on the mixed constant path
A batch-constant argument makes the whole tuple decline the all-varying read path, so the row loop indexes each ArgColumn directly. That arm validated nothing, and a short decoded column reached an out-of-bounds row read rather than the named error. Add ElementTuple::decoded_lens_match, which exempts an operand collapsed to its one constant row and checks every argument beside it. The all-varying arm keeps its own check on the borrowed slice view, since that is what discharges the per-row bounds check for the hot loop. Also stop reporting the expected row count as the actual one: row_count_matches returns a bool, so the message can only name what was required. Signed-off-by: Connor Tsui <[email protected]>
1 parent dd19841 commit 1251007

3 files changed

Lines changed: 132 additions & 9 deletions

File tree

‎vortex-array/src/scalar_fn/row/element/tuple.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ impl<T: InputElement> ArgColumn<T> {
8282
}
8383
}
8484

85+
/// Whether this argument addresses exactly `row_count` rows.
86+
///
87+
/// A constant operand was collapsed to its one distinct row and is read at index 0 forever, so
88+
/// it addresses any row count and is exempt.
89+
fn addresses_rows(&self, row_count: usize) -> bool {
90+
match &self.0 {
91+
ArgColumnKind::Varying(column) => T::varying_len(&T::varying(column)) == row_count,
92+
ArgColumnKind::Constant(_) => true,
93+
}
94+
}
95+
8596
/// The single decoded element of a constant operand, or `None` for a real column.
8697
///
8798
/// `Some` exactly when [`decode`](Self::decode) collapsed the operand to its one distinct row,
@@ -188,6 +199,13 @@ pub trait ElementTuple: 'static {
188199
/// Whether every varying column contains exactly `row_count` rows.
189200
fn varying_len_matches(columns: &Self::VaryingColumns<'_>, row_count: usize) -> bool;
190201

202+
/// Whether every argument that varies within the batch contains exactly `row_count` rows.
203+
///
204+
/// The same guarantee as [`varying_len_matches`](Self::varying_len_matches), for the mixed case
205+
/// [`varying`](Self::varying) declines: a batch-constant argument is exempt because it was
206+
/// collapsed to one row, while every argument beside it still has to address the whole batch.
207+
fn decoded_lens_match(columns: &Self::Columns, row_count: usize) -> bool;
208+
191209
/// Read one row from columns already known to vary within the batch.
192210
fn get_varying<'a>(columns: &Self::VaryingColumns<'a>, index: usize) -> Self::Elems<'a>;
193211

@@ -237,6 +255,10 @@ impl ElementTuple for () {
237255
true
238256
}
239257

258+
fn decoded_lens_match(_columns: &Self::Columns, _row_count: usize) -> bool {
259+
true
260+
}
261+
240262
fn get_varying<'a>(_columns: &Self::VaryingColumns<'a>, _index: usize) -> Self::Elems<'a> {}
241263

242264
fn constants(_columns: &Self::Columns) -> Self::ConstElems<'_> {}
@@ -303,6 +325,10 @@ macro_rules! element_tuple {
303325
$($t::varying_len(&columns.$idx) == row_count &&)+ true
304326
}
305327

328+
fn decoded_lens_match(columns: &Self::Columns, row_count: usize) -> bool {
329+
$(columns.$idx.addresses_rows(row_count) &&)+ true
330+
}
331+
306332
fn get_varying<'a>(
307333
columns: &Self::VaryingColumns<'a>,
308334
index: usize,

‎vortex-array/src/scalar_fn/row/execute.rs‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ pub(super) fn execute_row_sink_prepared<A: ElementTuple, P, S: OutputSink, R: Si
108108
let mut rows = sink.rows();
109109
vortex_ensure!(
110110
S::row_count_matches(&rows, row_count),
111-
"output sink row count must match the execution row count, got {row_count}",
111+
"the output sink does not address exactly {row_count} rows",
112112
);
113113

114114
if let Some(varying) = A::varying(&columns) {
115115
vortex_ensure!(
116116
A::varying_len_matches(&varying, row_count),
117-
"decoded row input length must match the execution row count, got {row_count}",
117+
"a decoded row input does not address exactly {row_count} rows",
118118
);
119119

120120
for index in 0..row_count {
@@ -126,6 +126,11 @@ pub(super) fn execute_row_sink_prepared<A: ElementTuple, P, S: OutputSink, R: Si
126126
.accumulate(&mut deferred_error)?;
127127
}
128128
} else {
129+
vortex_ensure!(
130+
A::decoded_lens_match(&columns, row_count),
131+
"a decoded row input does not address exactly {row_count} rows",
132+
);
133+
129134
for index in 0..row_count {
130135
apply(&state, A::get(&columns, index), S::row(&mut rows, index))
131136
.accumulate(&mut deferred_error)?;
@@ -165,16 +170,18 @@ pub(super) fn execute_row_sink_branch<A: ElementTuple, P, S: OutputSink, R: Sink
165170
let mut rows = sink.rows();
166171
vortex_ensure!(
167172
S::row_count_matches(&rows, row_count),
168-
"output sink row count must match the execution row count, got {row_count}",
173+
"the output sink does not address exactly {row_count} rows",
169174
);
170175

171176
let varying = A::varying(&columns);
172-
if let Some(varying) = &varying {
173-
vortex_ensure!(
174-
A::varying_len_matches(varying, row_count),
175-
"decoded row input length must match the execution row count, got {row_count}",
176-
);
177-
}
177+
let lens_match = match &varying {
178+
Some(varying) => A::varying_len_matches(varying, row_count),
179+
None => A::decoded_lens_match(&columns, row_count),
180+
};
181+
vortex_ensure!(
182+
lens_match,
183+
"a decoded row input does not address exactly {row_count} rows",
184+
);
178185

179186
let mut error = None;
180187
valid.for_each_set_index(|index| {

‎vortex-array/src/scalar_fn/row/tests/constant_operands.rs‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,96 @@ impl RowFn for AddCounted {
8484
}
8585
}
8686

87+
/// An element whose decode drops the last row, standing in for a buggy element implementation.
88+
struct ShortDecodeI64;
89+
90+
impl InputElement for ShortDecodeI64 {
91+
type Column = Buffer<i64>;
92+
type Varying<'a> = <i64 as InputElement>::Varying<'a>;
93+
type Elem<'a> = i64;
94+
95+
const DENSE_SAFE: bool = true;
96+
const DECODE_FALLIBLE: bool = false;
97+
98+
fn validate(dtype: &DType) -> VortexResult<()> {
99+
<i64 as InputElement>::validate(dtype)
100+
}
101+
102+
fn decode(array: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self::Column> {
103+
let column = <i64 as InputElement>::decode(array, ctx)?;
104+
Ok(column.slice(0..column.len().saturating_sub(1)))
105+
}
106+
107+
fn get(column: &Self::Column, index: usize) -> i64 {
108+
<i64 as InputElement>::get(column, index)
109+
}
110+
111+
fn varying(column: &Self::Column) -> Self::Varying<'_> {
112+
<i64 as InputElement>::varying(column)
113+
}
114+
115+
fn varying_len(column: &Self::Varying<'_>) -> usize {
116+
<i64 as InputElement>::varying_len(column)
117+
}
118+
119+
fn get_varying<'a>(column: &Self::Varying<'a>, index: usize) -> i64
120+
where
121+
Self: 'a,
122+
{
123+
<i64 as InputElement>::get_varying(column, index)
124+
}
125+
}
126+
127+
/// Pairs a short-decoding argument with an ordinary one, so a batch-constant second operand takes
128+
/// the mixed constant-and-varying read path rather than the all-varying one.
129+
#[derive(Clone)]
130+
struct AddShort;
131+
132+
impl RowFn for AddShort {
133+
type Options = EmptyOptions;
134+
type ArgsWitness = (ShortDecodeI64, i64);
135+
136+
fn id(&self) -> ScalarFnId {
137+
static ID: CachedId = CachedId::new("vortex.test.add_short");
138+
*ID
139+
}
140+
141+
fn arg_name(&self, idx: usize) -> ChildName {
142+
ChildName::from(["lhs", "rhs"][idx])
143+
}
144+
145+
fn dispatch<V: RowVisitor>(
146+
&self,
147+
_options: &Self::Options,
148+
_args: &[DType],
149+
visitor: V,
150+
) -> VortexResult<V::Out> {
151+
visitor.visit_prepared_into::<(ShortDecodeI64, i64), ElementSink<i64>, _, _>(
152+
|_| (),
153+
|&(), (lhs, rhs), output| output.write(lhs + rhs),
154+
)
155+
}
156+
}
157+
158+
/// A constant operand makes the whole tuple decline the all-varying read path, so the row loop
159+
/// indexes each [`ArgColumn`](crate::scalar_fn::ArgColumn) directly. The decoded length still has to
160+
/// be checked there, or a short column reaches an out-of-bounds row read.
161+
#[test]
162+
fn a_short_decode_beside_a_constant_operand_is_rejected() {
163+
let mut ctx = array_session().create_execution_ctx();
164+
let column = PrimitiveArray::from_iter(0..64i64).into_array();
165+
let constant = ConstantArray::new(Scalar::from(10i64), 64).into_array();
166+
167+
let error = apply(AddShort, [column, constant], &mut ctx).unwrap_err();
168+
169+
assert!(
170+
error
171+
.to_string()
172+
.contains("does not address exactly 64 rows"),
173+
"{error}"
174+
);
175+
}
176+
87177
#[test]
88178
fn a_constant_operand_is_decoded_once() -> VortexResult<()> {
89179
let mut ctx = array_session().create_execution_ctx();

0 commit comments

Comments
 (0)