Skip to content

Commit 155abab

Browse files
authored
Fix: Return currect field names in SelectExpr::as_include (#4232)
Signed-off-by: Adam Gutglick <[email protected]>
1 parent a773a5d commit 155abab

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

vortex-expr/src/exprs/select.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ impl SelectExpr {
179179
&self.child
180180
}
181181

182+
/// Turn the select expression into an `include`, relative to a provided array of field names.
183+
///
184+
/// For example:
185+
/// ```rust
186+
/// # use vortex_expr::root;
187+
/// # use vortex_expr::select::{SelectExpr, SelectField};
188+
/// # use vortex_dtype::FieldNames;
189+
/// let field_names = FieldNames::from(["a", "b", "c"]);
190+
/// let include = SelectExpr::new(SelectField::Include(["a"].into()), root());
191+
/// let exclude = SelectExpr::new(SelectField::Exclude(["b", "c"].into()), root());
192+
/// assert_eq!(
193+
/// &include.as_include(&field_names).unwrap(),
194+
/// &exclude.as_include(&field_names).unwrap()
195+
/// );
196+
/// ```
182197
pub fn as_include(&self, field_names: &FieldNames) -> VortexResult<ExprRef> {
183198
Ok(Self::new(
184199
SelectField::Include(self.fields.as_include_names(field_names)?),
@@ -229,7 +244,7 @@ impl SelectField {
229244
SelectField::Include(fields) => Ok(fields.clone()),
230245
SelectField::Exclude(exc_fields) => Ok(field_names
231246
.iter()
232-
.filter(|f| exc_fields.iter().contains(f))
247+
.filter(|f| !exc_fields.iter().contains(f))
233248
.cloned()
234249
.collect()),
235250
}
@@ -259,9 +274,9 @@ mod tests {
259274
use vortex_array::arrays::StructArray;
260275
use vortex_array::{IntoArray, ToCanonical};
261276
use vortex_buffer::buffer;
262-
use vortex_dtype::{DType, FieldName, Nullability};
277+
use vortex_dtype::{DType, FieldName, FieldNames, Nullability};
263278

264-
use crate::{Scope, root, select, select_exclude, test_harness};
279+
use crate::{Scope, SelectExpr, SelectField, root, select, select_exclude, test_harness};
265280

266281
fn test_array() -> StructArray {
267282
StructArray::from_fields(&[
@@ -338,4 +353,15 @@ mod tests {
338353
)
339354
);
340355
}
356+
357+
#[test]
358+
fn test_as_include_names() {
359+
let field_names = FieldNames::from(["a", "b", "c"]);
360+
let include = SelectExpr::new(SelectField::Include(["a"].into()), root());
361+
let exclude = SelectExpr::new(SelectField::Exclude(["b", "c"].into()), root());
362+
assert_eq!(
363+
&include.as_include(&field_names).unwrap(),
364+
&exclude.as_include(&field_names).unwrap()
365+
);
366+
}
341367
}

0 commit comments

Comments
 (0)