vortex_array/expr/transform/
immediate_access.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use 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
16/// An [`AnnotationFn`] for annotating scope accesses.
17pub 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
36/// For all subexpressions in an expression, find the fields that are accessed directly from the
37/// scope, but not any fields in those fields
38/// e.g. scope = {a: {b: .., c: ..}, d: ..}, expr = root().a.b + root().d accesses {a,d} (not b).
39///
40/// Note: This is a very naive, but simple analysis to find the fields that are accessed directly on an
41/// identity node. This is combined to provide an over-approximation of the fields that are accessed
42/// by an expression.
43pub 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
50/// This returns the immediate scope_access (as explained `immediate_scope_accesses`) for `expr`.
51pub 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}