Skip to main content

vortex_array/expr/exprs/
root.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt::Formatter;
5
6use vortex_dtype::DType;
7use vortex_dtype::FieldPath;
8use vortex_error::VortexExpect;
9use vortex_error::VortexResult;
10use vortex_error::vortex_bail;
11use vortex_session::VortexSession;
12
13use crate::ArrayRef;
14use crate::expr::Arity;
15use crate::expr::ChildName;
16use crate::expr::EmptyOptions;
17use crate::expr::ExecutionArgs;
18use crate::expr::ExprId;
19use crate::expr::StatsCatalog;
20use crate::expr::VTable;
21use crate::expr::VTableExt;
22use crate::expr::expression::Expression;
23use crate::expr::stats::Stat;
24
25/// An expression that returns the full scope of the expression evaluation.
26// TODO(ngates): rename to "Scope"
27pub struct Root;
28
29impl VTable for Root {
30    type Options = EmptyOptions;
31
32    fn id(&self) -> ExprId {
33        ExprId::from("vortex.root")
34    }
35
36    fn serialize(&self, _instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
37        Ok(Some(vec![]))
38    }
39
40    fn deserialize(
41        &self,
42        _metadata: &[u8],
43        _session: &VortexSession,
44    ) -> VortexResult<Self::Options> {
45        Ok(EmptyOptions)
46    }
47
48    fn arity(&self, _options: &Self::Options) -> Arity {
49        Arity::Exact(0)
50    }
51
52    fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName {
53        unreachable!(
54            "Root expression does not have children, got index {}",
55            child_idx
56        )
57    }
58
59    fn fmt_sql(
60        &self,
61        _options: &Self::Options,
62        _expr: &Expression,
63        f: &mut Formatter<'_>,
64    ) -> std::fmt::Result {
65        write!(f, "$")
66    }
67
68    fn return_dtype(&self, _options: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
69        vortex_bail!("Root expression does not support return_dtype")
70    }
71
72    fn execute(&self, _data: &Self::Options, _args: ExecutionArgs) -> VortexResult<ArrayRef> {
73        vortex_bail!("Root expression is not executable")
74    }
75
76    fn stat_expression(
77        &self,
78        _options: &Self::Options,
79        _expr: &Expression,
80        stat: Stat,
81        catalog: &dyn StatsCatalog,
82    ) -> Option<Expression> {
83        catalog.stats_ref(&FieldPath::root(), stat)
84    }
85
86    fn is_null_sensitive(&self, _options: &Self::Options) -> bool {
87        false
88    }
89
90    fn is_fallible(&self, _options: &Self::Options) -> bool {
91        false
92    }
93}
94
95/// Creates an expression that references the root scope.
96///
97/// Returns the entire input array as passed to the expression evaluator.
98/// This is commonly used as the starting point for field access and other operations.
99pub fn root() -> Expression {
100    Root.try_new_expr(EmptyOptions, vec![])
101        .vortex_expect("Failed to create Root expression")
102}
103
104/// Return whether the expression is a root expression.
105pub fn is_root(expr: &Expression) -> bool {
106    expr.is::<Root>()
107}