vortex_array/expr/transform/
immediate_access.rs1use vortex_dtype::{FieldName, StructFields};
5use vortex_error::VortexExpect;
6use vortex_utils::aliases::hash_set::HashSet;
7
8use crate::expr::Expression;
9use crate::expr::exprs::get_item::GetItem;
10use crate::expr::exprs::root::Root;
11use crate::expr::exprs::select::Select;
12use crate::expr::transform::annotations::{AnnotationFn, Annotations, descendent_annotations};
13
14pub type FieldAccesses<'a> = Annotations<'a, FieldName>;
15
16pub fn annotate_scope_access(scope: &StructFields) -> impl AnnotationFn<Annotation = FieldName> {
18 move |expr: &Expression| {
19 assert!(
20 !expr.is::<Select>(),
21 "cannot analyse select, simplify the expression"
22 );
23
24 if let Some(get_item) = expr.as_opt::<GetItem>() {
25 if get_item.child(0).is::<Root>() {
26 return vec![get_item.data().clone()];
27 }
28 } else if expr.is::<Root>() {
29 return scope.names().iter().cloned().collect();
30 }
31
32 vec![]
33 }
34}
35
36pub fn immediate_scope_accesses<'a>(
44 expr: &'a Expression,
45 scope: &'a StructFields,
46) -> FieldAccesses<'a> {
47 descendent_annotations(expr, annotate_scope_access(scope))
48}
49
50pub fn immediate_scope_access<'a>(
52 expr: &'a Expression,
53 scope: &'a StructFields,
54) -> HashSet<FieldName> {
55 immediate_scope_accesses(expr, scope)
56 .get(expr)
57 .vortex_expect("Expression missing from scope accesses, this is a internal bug")
58 .clone()
59}