Skip to main content

vortex_array/scalar_fn/fns/
root.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt::Formatter;
5
6use vortex_error::VortexResult;
7use vortex_error::vortex_bail;
8use vortex_session::VortexSession;
9use vortex_session::registry::CachedId;
10
11use crate::ArrayRef;
12use crate::ExecutionCtx;
13use crate::dtype::DType;
14use crate::expr::expression::Expression;
15use crate::scalar_fn::Arity;
16use crate::scalar_fn::ChildName;
17use crate::scalar_fn::EmptyOptions;
18use crate::scalar_fn::ExecutionArgs;
19use crate::scalar_fn::ScalarFnId;
20use crate::scalar_fn::ScalarFnVTable;
21
22/// An expression that returns the full scope of the expression evaluation.
23// TODO(ngates): rename to "Scope"
24#[derive(Clone)]
25pub struct Root;
26
27impl ScalarFnVTable for Root {
28    type Options = EmptyOptions;
29
30    fn id(&self) -> ScalarFnId {
31        static ID: CachedId = CachedId::new("vortex.root");
32        *ID
33    }
34
35    fn serialize(&self, _instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
36        Ok(Some(vec![]))
37    }
38
39    fn deserialize(
40        &self,
41        _metadata: &[u8],
42        _session: &VortexSession,
43    ) -> VortexResult<Self::Options> {
44        Ok(EmptyOptions)
45    }
46
47    fn arity(&self, _options: &Self::Options) -> Arity {
48        Arity::Exact(0)
49    }
50
51    fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName {
52        unreachable!(
53            "Root expression does not have children, got index {}",
54            child_idx
55        )
56    }
57
58    fn fmt_sql(
59        &self,
60        _options: &Self::Options,
61        _expr: &Expression,
62        f: &mut Formatter<'_>,
63    ) -> std::fmt::Result {
64        write!(f, "$")
65    }
66
67    fn return_dtype(&self, _options: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
68        vortex_bail!("Root expression does not support return_dtype")
69    }
70
71    fn execute(
72        &self,
73        _data: &Self::Options,
74        _args: &dyn ExecutionArgs,
75        _ctx: &mut ExecutionCtx,
76    ) -> VortexResult<ArrayRef> {
77        vortex_bail!("Root expression is not executable")
78    }
79
80    fn is_null_sensitive(&self, _options: &Self::Options) -> bool {
81        false
82    }
83
84    fn is_fallible(&self, _options: &Self::Options) -> bool {
85        false
86    }
87}