Skip to content

Commit 75b99c1

Browse files
authored
Rename functions in SelectExpr to make API clearer (#4690)
Unfortunately a minor breaking change to the API, but the amount of `fields()` functions we have is just too much. --------- Signed-off-by: Adam Gutglick <[email protected]>
1 parent bbb5106 commit 75b99c1

File tree

6 files changed

+77
-64
lines changed

6 files changed

+77
-64
lines changed

Cargo.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vortex-dtype/src/struct_.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl Hash for FieldDTypeInner {
9191

9292
impl FieldDType {
9393
/// Returns the concrete DType, parsing it from the underlying buffer if necessary.
94+
#[inline]
9495
pub fn value(&self) -> VortexResult<DType> {
9596
self.inner.value()
9697
}

vortex-expr/src/exprs/select.rs

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::field::DisplayFieldNames;
1515
use crate::{AnalysisExpr, ExprEncodingRef, ExprId, ExprRef, IntoExpr, Scope, VTable, vtable};
1616

1717
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18-
pub enum SelectField {
18+
pub enum FieldSelection {
1919
Include(FieldNames),
2020
Exclude(FieldNames),
2121
}
@@ -25,13 +25,13 @@ vtable!(Select);
2525
#[derive(Debug, Clone, Hash, Eq)]
2626
#[allow(clippy::derived_hash_with_manual_eq)]
2727
pub struct SelectExpr {
28-
fields: SelectField,
28+
selection: FieldSelection,
2929
child: ExprRef,
3030
}
3131

3232
impl PartialEq for SelectExpr {
3333
fn eq(&self, other: &Self) -> bool {
34-
self.fields == other.fields && self.child.eq(&other.child)
34+
self.selection == other.selection && self.child.eq(&other.child)
3535
}
3636
}
3737

@@ -52,13 +52,13 @@ impl VTable for SelectVTable {
5252

5353
fn metadata(expr: &Self::Expr) -> Option<Self::Metadata> {
5454
let names = expr
55-
.fields()
56-
.fields()
55+
.selection()
56+
.field_names()
5757
.iter()
5858
.map(|f| f.to_string())
5959
.collect_vec();
6060

61-
let opts = if expr.fields().is_include() {
61+
let opts = if expr.selection().is_include() {
6262
Opts::Include(ProtoFieldNames { names })
6363
} else {
6464
Opts::Exclude(ProtoFieldNames { names })
@@ -73,7 +73,7 @@ impl VTable for SelectVTable {
7373

7474
fn with_children(expr: &Self::Expr, children: Vec<ExprRef>) -> VortexResult<Self::Expr> {
7575
Ok(SelectExpr {
76-
fields: expr.fields.clone(),
76+
selection: expr.selection.clone(),
7777
child: children[0].clone(),
7878
})
7979
}
@@ -89,10 +89,10 @@ impl VTable for SelectVTable {
8989

9090
let fields = match metadata.opts.as_ref() {
9191
Some(opts) => match opts {
92-
Opts::Include(field_names) => SelectField::Include(FieldNames::from_iter(
92+
Opts::Include(field_names) => FieldSelection::Include(FieldNames::from_iter(
9393
field_names.names.iter().map(|s| s.as_str()),
9494
)),
95-
Opts::Exclude(field_names) => SelectField::Exclude(FieldNames::from_iter(
95+
Opts::Exclude(field_names) => FieldSelection::Exclude(FieldNames::from_iter(
9696
field_names.names.iter().map(|s| s.as_str()),
9797
)),
9898
},
@@ -106,14 +106,17 @@ impl VTable for SelectVTable {
106106
.next()
107107
.vortex_expect("number of children validated to be one");
108108

109-
Ok(SelectExpr { fields, child })
109+
Ok(SelectExpr {
110+
selection: fields,
111+
child,
112+
})
110113
}
111114

112115
fn evaluate(expr: &Self::Expr, scope: &Scope) -> VortexResult<ArrayRef> {
113116
let batch = expr.child.unchecked_evaluate(scope)?.to_struct();
114-
Ok(match &expr.fields {
115-
SelectField::Include(f) => batch.project(f.as_ref()),
116-
SelectField::Exclude(names) => {
117+
Ok(match &expr.selection {
118+
FieldSelection::Include(f) => batch.project(f.as_ref()),
119+
FieldSelection::Exclude(names) => {
117120
let included_names = batch
118121
.names()
119122
.iter()
@@ -132,9 +135,9 @@ impl VTable for SelectVTable {
132135
.as_struct_fields_opt()
133136
.ok_or_else(|| vortex_err!("Select child not a struct dtype"))?;
134137

135-
let projected = match &expr.fields {
136-
SelectField::Include(fields) => child_struct_dtype.project(fields.as_ref())?,
137-
SelectField::Exclude(fields) => child_struct_dtype
138+
let projected = match &expr.selection {
139+
FieldSelection::Include(fields) => child_struct_dtype.project(fields.as_ref())?,
140+
FieldSelection::Exclude(fields) => child_struct_dtype
138141
.names()
139142
.iter()
140143
.cloned()
@@ -154,8 +157,8 @@ impl VTable for SelectVTable {
154157
/// # use vortex_expr::{select, root};
155158
/// let expr = select(["name", "age"], root());
156159
/// ```
157-
pub fn select(fields: impl Into<FieldNames>, child: ExprRef) -> ExprRef {
158-
SelectExpr::include_expr(fields.into(), child)
160+
pub fn select(field_names: impl Into<FieldNames>, child: ExprRef) -> ExprRef {
161+
SelectExpr::include_expr(field_names.into(), child)
159162
}
160163

161164
/// Creates an expression that excludes specific fields from an array.
@@ -171,24 +174,27 @@ pub fn select_exclude(fields: impl Into<FieldNames>, child: ExprRef) -> ExprRef
171174
}
172175

173176
impl SelectExpr {
174-
pub fn new(fields: SelectField, child: ExprRef) -> Self {
175-
Self { fields, child }
177+
pub fn new(fields: FieldSelection, child: ExprRef) -> Self {
178+
Self {
179+
selection: fields,
180+
child,
181+
}
176182
}
177183

178-
pub fn new_expr(fields: SelectField, child: ExprRef) -> ExprRef {
184+
pub fn new_expr(fields: FieldSelection, child: ExprRef) -> ExprRef {
179185
Self::new(fields, child).into_expr()
180186
}
181187

182188
pub fn include_expr(columns: FieldNames, child: ExprRef) -> ExprRef {
183-
Self::new(SelectField::Include(columns), child).into_expr()
189+
Self::new(FieldSelection::Include(columns), child).into_expr()
184190
}
185191

186192
pub fn exclude_expr(columns: FieldNames, child: ExprRef) -> ExprRef {
187-
Self::new(SelectField::Exclude(columns), child).into_expr()
193+
Self::new(FieldSelection::Exclude(columns), child).into_expr()
188194
}
189195

190-
pub fn fields(&self) -> &SelectField {
191-
&self.fields
196+
pub fn selection(&self) -> &FieldSelection {
197+
&self.selection
192198
}
193199

194200
pub fn child(&self) -> &ExprRef {
@@ -200,26 +206,26 @@ impl SelectExpr {
200206
/// For example:
201207
/// ```rust
202208
/// # use vortex_expr::root;
203-
/// # use vortex_expr::{SelectExpr, SelectField};
209+
/// # use vortex_expr::{FieldSelection, SelectExpr};
204210
/// # use vortex_dtype::FieldNames;
205211
/// let field_names = FieldNames::from(["a", "b", "c"]);
206-
/// let include = SelectExpr::new(SelectField::Include(["a"].into()), root());
207-
/// let exclude = SelectExpr::new(SelectField::Exclude(["b", "c"].into()), root());
212+
/// let include = SelectExpr::new(FieldSelection::Include(["a"].into()), root());
213+
/// let exclude = SelectExpr::new(FieldSelection::Exclude(["b", "c"].into()), root());
208214
/// assert_eq!(
209215
/// &include.as_include(&field_names).unwrap(),
210216
/// &exclude.as_include(&field_names).unwrap()
211217
/// );
212218
/// ```
213219
pub fn as_include(&self, field_names: &FieldNames) -> VortexResult<ExprRef> {
214220
Ok(Self::new(
215-
SelectField::Include(self.fields.as_include_names(field_names)?),
221+
FieldSelection::Include(self.selection.as_include_names(field_names)?),
216222
self.child.clone(),
217223
)
218224
.into_expr())
219225
}
220226
}
221227

222-
impl SelectField {
228+
impl FieldSelection {
223229
pub fn include(columns: FieldNames) -> Self {
224230
assert_eq!(columns.iter().unique().collect_vec().len(), columns.len());
225231
Self::Include(columns)
@@ -238,15 +244,15 @@ impl SelectField {
238244
matches!(self, Self::Exclude(_))
239245
}
240246

241-
pub fn fields(&self) -> &FieldNames {
242-
let (SelectField::Include(fields) | SelectField::Exclude(fields)) = self;
247+
pub fn field_names(&self) -> &FieldNames {
248+
let (FieldSelection::Include(fields) | FieldSelection::Exclude(fields)) = self;
243249

244250
fields
245251
}
246252

247253
pub fn as_include_names(&self, field_names: &FieldNames) -> VortexResult<FieldNames> {
248254
if self
249-
.fields()
255+
.field_names()
250256
.iter()
251257
.any(|f| !field_names.iter().contains(f))
252258
{
@@ -257,8 +263,8 @@ impl SelectField {
257263
);
258264
}
259265
match self {
260-
SelectField::Include(fields) => Ok(fields.clone()),
261-
SelectField::Exclude(exc_fields) => Ok(field_names
266+
FieldSelection::Include(fields) => Ok(fields.clone()),
267+
FieldSelection::Exclude(exc_fields) => Ok(field_names
262268
.iter()
263269
.filter(|f| !exc_fields.iter().contains(f))
264270
.cloned()
@@ -267,11 +273,11 @@ impl SelectField {
267273
}
268274
}
269275

270-
impl Display for SelectField {
276+
impl Display for FieldSelection {
271277
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
272278
match self {
273-
SelectField::Include(fields) => write!(f, "{{{}}}", DisplayFieldNames(fields)),
274-
SelectField::Exclude(fields) => write!(f, "~{{{}}}", DisplayFieldNames(fields)),
279+
FieldSelection::Include(fields) => write!(f, "{{{}}}", DisplayFieldNames(fields)),
280+
FieldSelection::Exclude(fields) => write!(f, "~{{{}}}", DisplayFieldNames(fields)),
275281
}
276282
}
277283
}
@@ -280,16 +286,21 @@ impl DisplayAs for SelectExpr {
280286
fn fmt_as(&self, df: DisplayFormat, f: &mut std::fmt::Formatter) -> std::fmt::Result {
281287
match df {
282288
DisplayFormat::Compact => {
283-
write!(f, "{}{}", self.child, self.fields)
289+
write!(f, "{}{}", self.child, self.selection)
284290
}
285291
DisplayFormat::Tree => {
286-
let field_type = if self.fields.is_include() {
292+
let field_type = if self.selection.is_include() {
287293
"include"
288294
} else {
289295
"exclude"
290296
};
291297

292-
write!(f, "Select({}): {}", field_type, self.fields().fields())
298+
write!(
299+
f,
300+
"Select({}): {}",
301+
field_type,
302+
self.selection().field_names()
303+
)
293304
}
294305
}
295306
}
@@ -310,7 +321,7 @@ mod tests {
310321
use vortex_buffer::buffer;
311322
use vortex_dtype::{DType, FieldName, FieldNames, Nullability};
312323

313-
use crate::{Scope, SelectExpr, SelectField, root, select, select_exclude, test_harness};
324+
use crate::{FieldSelection, Scope, SelectExpr, root, select, select_exclude, test_harness};
314325

315326
fn test_array() -> StructArray {
316327
StructArray::from_fields(&[
@@ -393,8 +404,8 @@ mod tests {
393404
#[test]
394405
fn test_as_include_names() {
395406
let field_names = FieldNames::from(["a", "b", "c"]);
396-
let include = SelectExpr::new(SelectField::Include(["a"].into()), root());
397-
let exclude = SelectExpr::new(SelectField::Exclude(["b", "c"].into()), root());
407+
let include = SelectExpr::new(FieldSelection::Include(["a"].into()), root());
408+
let exclude = SelectExpr::new(FieldSelection::Exclude(["b", "c"].into()), root());
398409
assert_eq!(
399410
&include.as_include(&field_names).unwrap(),
400411
&exclude.as_include(&field_names).unwrap()

vortex-expr/src/transform/remove_select.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ fn remove_select_transformer(node: ExprRef, ctx: &DType) -> VortexResult<Transfo
2929

3030
let expr = pack(
3131
select
32-
.fields()
32+
.selection()
3333
.as_include_names(child_dtype.names())
3434
.map_err(|e| {
3535
e.with_context(format!(
3636
"Select fields {:?} must be a subset of child fields {:?}",
37-
select.fields(),
37+
select.selection(),
3838
child_dtype.names()
3939
))
4040
})?

vortex-expr/src/traversal/references.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ impl NodeVisitor<'_> for ReferenceCollector {
3737
self.fields.insert(get_item.field().clone());
3838
}
3939
if let Some(sel) = node.as_opt::<SelectVTable>() {
40-
self.fields.extend(sel.fields().fields().iter().cloned());
40+
self.fields
41+
.extend(sel.selection().field_names().iter().cloned());
4142
}
4243
Ok(TraversalOrder::Continue)
4344
}

0 commit comments

Comments
 (0)