use vortex_dtype::{FieldName, StructFields};
use vortex_error::VortexExpect;
use vortex_utils::aliases::hash_set::HashSet;
use crate::transform::annotations::{AnnotationFn, Annotations, descendent_annotations};
use crate::{ExprRef, GetItemVTable, SelectVTable, is_root};
pub type FieldAccesses<'a> = Annotations<'a, FieldName>;
pub fn annotate_scope_access(scope: &StructFields) -> impl AnnotationFn<Annotation = FieldName> {
move |expr: &ExprRef| {
assert!(
!expr.is::<SelectVTable>(),
"cannot analyse select, simplify the expression"
);
if let Some(get_item) = expr.as_opt::<GetItemVTable>() {
if is_root(get_item.child()) {
return vec![get_item.field().clone()];
}
} else if is_root(expr) {
return scope.names().iter().cloned().collect();
}
vec![]
}
}
pub fn immediate_scope_accesses<'a>(
expr: &'a ExprRef,
scope: &'a StructFields,
) -> FieldAccesses<'a> {
descendent_annotations(expr, annotate_scope_access(scope))
}
pub fn immediate_scope_access<'a>(
expr: &'a ExprRef,
scope: &'a StructFields,
) -> HashSet<FieldName> {
immediate_scope_accesses(expr, scope)
.get(expr)
.vortex_expect("Expression missing from scope accesses, this is a internal bug")
.clone()
}