Skip to content

Commit 1bbdb09

Browse files
committed
update
Signed-off-by: Joe Isaacs <[email protected]>
1 parent e4ed704 commit 1bbdb09

File tree

6 files changed

+28
-66
lines changed

6 files changed

+28
-66
lines changed

vortex-array/src/expr/transform/optimizer.rs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,33 @@ use crate::expr::session::ExprSession;
99
use crate::expr::transform::{simplify, simplify_typed};
1010

1111
/// A unified optimizer for expressions that can work with or without type information.
12-
///
13-
/// This provides a convenient API for optimizing expressions, automatically choosing between
14-
/// typed and untyped optimization based on whether a dtype is provided.
15-
///
16-
/// # Examples
17-
///
18-
/// ```rust
19-
/// # use vortex_array::expr::Expression;
20-
/// # use vortex_array::expr::session::ExprSession;
21-
/// # use vortex_array::expr::transform::ExprOptimizer;
22-
/// # use vortex_dtype::DType;
23-
/// # fn example(expr: Expression, dtype: DType) {
24-
/// let session = ExprSession::default();
25-
///
26-
/// // Untyped optimization
27-
/// let optimizer = ExprOptimizer::new(&session);
28-
/// let optimized = optimizer.optimize(expr.clone()).unwrap();
29-
///
30-
/// // Typed optimization
31-
/// let optimizer = ExprOptimizer::with_dtype(&session, dtype);
32-
/// let optimized = optimizer.optimize(expr).unwrap();
33-
/// # }
34-
/// ```
35-
pub struct ExprOptimizer<'a> {
36-
session: &'a ExprSession,
12+
pub struct ExprOptimizer {
13+
session: ExprSession,
3714
dtype: Option<DType>,
3815
}
3916

40-
impl<'a> ExprOptimizer<'a> {
17+
impl ExprOptimizer {
4118
/// Create a new untyped optimizer.
4219
///
4320
/// This optimizer will use untyped simplification rules only.
44-
pub fn new(session: &'a ExprSession) -> Self {
21+
pub fn new(session: ExprSession) -> Self {
4522
Self {
4623
session,
4724
dtype: None,
4825
}
4926
}
5027

51-
/// Create a new typed optimizer with the given dtype.
52-
///
53-
/// This optimizer will use both typed and untyped simplification rules,
54-
/// with access to dtype information.
55-
pub fn with_dtype(session: &'a ExprSession, dtype: DType) -> Self {
56-
Self {
57-
session,
58-
dtype: Some(dtype),
59-
}
60-
}
61-
6228
/// Optimize the given expression.
6329
///
6430
/// If this optimizer was created with a dtype, this will perform typed optimization.
6531
/// Otherwise, it will perform untyped optimization.
6632
pub fn optimize(&self, expr: Expression) -> VortexResult<Expression> {
67-
match &self.dtype {
68-
Some(dtype) => simplify_typed(expr, dtype, self.session),
69-
None => simplify(expr, self.session),
70-
}
71-
}
72-
73-
/// Get the dtype associated with this optimizer, if any.
74-
pub fn dtype(&self) -> Option<&DType> {
75-
self.dtype.as_ref()
33+
simplify(expr, &self.session)
7634
}
7735

78-
/// Get the session associated with this optimizer.
79-
pub fn session(&self) -> &ExprSession {
80-
self.session
36+
/// Apply optimize rules to the expression, with a known dtype. This will also apply rules
37+
/// in `optimize`.
38+
pub fn optimize_typed(&self, expr: Expression, dtype: &DType) -> VortexResult<Expression> {
39+
simplify_typed(expr, dtype, &self.session)
8140
}
8241
}

vortex-array/src/expr/transform/partition.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ use crate::expr::Expression;
1212
use crate::expr::exprs::get_item::get_item;
1313
use crate::expr::exprs::pack::pack;
1414
use crate::expr::exprs::root::root;
15-
use crate::expr::session::ExprSession;
15+
use crate::expr::transform::ExprOptimizer;
1616
use crate::expr::transform::annotations::{
1717
Annotation, AnnotationFn, Annotations, descendent_annotations,
1818
};
19-
use crate::expr::transform::simplify_typed::simplify_typed;
2019
use crate::expr::traversal::{NodeExt, NodeRewriter, Transformed, TraversalOrder};
2120

2221
/// Partition an expression into sub-expressions that are uniquely associated with an annotation.
@@ -34,7 +33,7 @@ pub fn partition<A: AnnotationFn>(
3433
expr: Expression,
3534
scope: &DType,
3635
annotate_fn: A,
37-
session: &ExprSession,
36+
optimizer: &ExprOptimizer,
3837
) -> VortexResult<PartitionedExpr<A::Annotation>>
3938
where
4039
A::Annotation: Display,
@@ -64,7 +63,7 @@ where
6463
Nullability::NonNullable,
6564
);
6665

67-
let expr = simplify_typed(expr.clone(), scope, session)?;
66+
let expr = optimizer.optimize_typed(expr.clone(), scope)?;
6867
let expr_dtype = expr.return_dtype(scope)?;
6968

7069
partitions.push(expr);
@@ -82,7 +81,7 @@ where
8281
);
8382

8483
Ok(PartitionedExpr {
85-
root: simplify_typed(root, &root_scope, session)?,
84+
root: optimizer.optimize_typed(root, &root_scope)?,
8685
partitions: partitions.into_boxed_slice(),
8786
partition_names,
8887
partition_dtypes: partition_dtypes.into_boxed_slice(),

vortex-array/src/expr/transform/simplify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::expr::traversal::{NodeExt, Transformed};
1313
///
1414
/// This applies only untyped rewrite rules registered in the default session.
1515
/// If the scope dtype is known, see `simplify_typed` for a simplifier which uses dtype.
16-
pub fn simplify(e: Expression, session: &ExprSession) -> VortexResult<Expression> {
16+
pub(crate) fn simplify(e: Expression, session: &ExprSession) -> VortexResult<Expression> {
1717
let ctx = RuleContext;
1818

1919
let e = apply_parent_rules(e, &ctx, session)?;

vortex-array/src/expr/transform/simplify_typed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::expr::traversal::{NodeExt, Transformed};
1414
///
1515
/// NOTE: After typed simplification, returned expressions is "bound" to the scope DType.
1616
/// Applying the returned expression to a different DType may produce wrong results.
17-
pub fn simplify_typed(
17+
pub(crate) fn simplify_typed(
1818
expr: Expression,
1919
dtype: &DType,
2020
session: &ExprSession,

vortex-layout/src/layouts/struct_/reader.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use vortex_array::arrays::StructArray;
1111
use vortex_array::expr::session::ExprSessionExt;
1212
use vortex_array::expr::transform::immediate_access::annotate_scope_access;
1313
use vortex_array::expr::transform::{
14-
PartitionedExpr, partition, replace, replace_root_fields, simplify_typed,
14+
ExprOptimizer, PartitionedExpr, partition, replace, replace_root_fields,
1515
};
1616
use vortex_array::expr::{ExactExpr, Expression, Merge, Pack, col, root};
1717
use vortex_array::vtable::ValidityHelper;
@@ -41,6 +41,8 @@ pub struct StructReader {
4141

4242
field_lookup: Option<HashMap<FieldName, usize>>,
4343
partitioned_expr_cache: DashMap<ExactExpr, Partitioned>,
44+
45+
optimizer: ExprOptimizer,
4446
}
4547

4648
impl StructReader {
@@ -95,6 +97,7 @@ impl StructReader {
9597
lazy_children,
9698
field_lookup,
9799
partitioned_expr_cache: Default::default(),
100+
optimizer: ExprOptimizer::new(*session.expressions()),
98101
})
99102
}
100103

@@ -141,7 +144,9 @@ impl StructReader {
141144
// First, we expand the root scope into the fields of the struct to ensure
142145
// that partitioning works correctly.
143146
let expr = replace(expr.clone(), &root(), self.expanded_root_expr.clone());
144-
let expr = simplify_typed(expr, self.dtype(), &self.session.expressions())
147+
let expr = self
148+
.optimizer
149+
.optimize_typed(expr, self.dtype())
145150
.vortex_expect("We should not fail to simplify expression over struct fields");
146151

147152
// Partition the expression into expressions that can be evaluated over individual fields

vortex-scan/src/scan_builder.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use futures::future::BoxFuture;
99
use itertools::Itertools;
1010
use vortex_array::ArrayRef;
1111
use vortex_array::expr::session::ExprSessionExt;
12+
use vortex_array::expr::transform::ExprOptimizer;
1213
use vortex_array::expr::transform::immediate_access::immediate_scope_access;
13-
use vortex_array::expr::transform::simplify_typed;
1414
use vortex_array::expr::{Expression, root};
1515
use vortex_array::iter::{ArrayIterator, ArrayIteratorAdapter};
1616
use vortex_array::stats::StatsSet;
@@ -216,15 +216,14 @@ impl<A: 'static + Send> ScanBuilder<A> {
216216
&self.session,
217217
));
218218

219+
let optimizer = ExprOptimizer::new(*self.session.expressions());
220+
219221
// Normalize and simplify the expressions.
220-
let projection = simplify_typed(
221-
self.projection,
222-
layout_reader.dtype(),
223-
&self.session.expressions(),
224-
)?;
222+
let projection = optimizer.optimize_typed(self.projection, layout_reader.as_ref())?;
223+
225224
let filter = self
226225
.filter
227-
.map(|f| simplify_typed(f, layout_reader.dtype(), &self.session.expressions()))
226+
.map(|f| optimizer.optimize_typed(f, layout_reader.dtype()))
228227
.transpose()?;
229228

230229
// Construct field masks and compute the row splits of the scan.

0 commit comments

Comments
 (0)