-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove select with typed_simplify pass (#1929)
- Loading branch information
1 parent
e4a9061
commit 6cd94fc
Showing
6 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//! A collection of transformations that can be applied to a [`crate::ExprRef`]. | ||
pub mod partition; | ||
pub(crate) mod remove_select; | ||
pub mod simplify; | ||
pub mod simplify_typed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use itertools::Itertools; | ||
use vortex_dtype::DType; | ||
use vortex_error::{vortex_err, VortexResult}; | ||
|
||
use crate::traversal::{MutNodeVisitor, Node, TransformResult}; | ||
use crate::{get_item, pack, ExprRef, Select}; | ||
|
||
/// Select is a useful expression, however it can be defined in terms of get_item & pack, | ||
/// once the expression type is known, this simplifications pass removes the select expression. | ||
pub fn remove_select(e: ExprRef, scope_dt: DType) -> VortexResult<ExprRef> { | ||
let mut transform = RemoveSelectTransform::new(scope_dt); | ||
e.transform(&mut transform).map(|e| e.result) | ||
} | ||
|
||
struct RemoveSelectTransform { | ||
ident_dtype: DType, | ||
} | ||
|
||
impl RemoveSelectTransform { | ||
fn new(ident_dtype: DType) -> Self { | ||
Self { ident_dtype } | ||
} | ||
} | ||
|
||
impl MutNodeVisitor for RemoveSelectTransform { | ||
type NodeTy = ExprRef; | ||
|
||
fn visit_up(&mut self, node: ExprRef) -> VortexResult<TransformResult<Self::NodeTy>> { | ||
if let Some(select) = node.as_any().downcast_ref::<Select>() { | ||
let child = select.child(); | ||
let child_dtype = child.return_dtype(&self.ident_dtype)?; | ||
let child_dtype = child_dtype.as_struct().ok_or_else(|| { | ||
vortex_err!( | ||
"Select child must return a struct dtype, however it was a {}", | ||
child_dtype | ||
) | ||
})?; | ||
|
||
let names = select | ||
.fields() | ||
.as_include_names(child_dtype.names()) | ||
.map_err(|e| { | ||
vortex_err!( | ||
"Select fields must be a subset of child fields, however {}", | ||
e | ||
) | ||
})?; | ||
|
||
let pack_children = names | ||
.iter() | ||
.map(|name| get_item(name.clone(), child.clone())) | ||
.collect_vec(); | ||
|
||
Ok(TransformResult::yes(pack(names, pack_children))) | ||
} else { | ||
Ok(TransformResult::no(node)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use vortex_dtype::Nullability::NonNullable; | ||
use vortex_dtype::PType::I32; | ||
use vortex_dtype::{DType, StructDType}; | ||
|
||
use crate::transform::remove_select::remove_select; | ||
use crate::{ident, select, Pack}; | ||
|
||
#[test] | ||
fn test_remove_select() { | ||
let dtype = DType::Struct( | ||
StructDType::new( | ||
["a".into(), "b".into()].into(), | ||
vec![I32.into(), I32.into()], | ||
), | ||
NonNullable, | ||
); | ||
let e = select(["a".into(), "b".into()], ident()); | ||
let e = remove_select(e, dtype).unwrap(); | ||
|
||
assert!(e.as_any().downcast_ref::<Pack>().is_some()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use vortex_dtype::DType; | ||
use vortex_error::VortexResult; | ||
|
||
use crate::transform::remove_select::remove_select; | ||
use crate::transform::simplify::simplify; | ||
use crate::ExprRef; | ||
|
||
/// This pass simplifies an expression under the assumption that ident()/scope as a fixed DType. | ||
/// There is another pass `simplify` that simplifies an expression without any assumptions. | ||
/// This pass also applies simplify. | ||
pub fn simplify_typed(e: ExprRef, scope_dt: DType) -> VortexResult<ExprRef> { | ||
let e = simplify(e)?; | ||
remove_select(e, scope_dt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters