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;
9
10use crate::ArrayRef;
11use crate::dtype::DType;
12use crate::dtype::FieldPath;
13use crate::expr::StatsCatalog;
14use crate::expr::expression::Expression;
15use crate::expr::stats::Stat;
16use crate::scalar_fn::Arity;
17use crate::scalar_fn::ChildName;
18use crate::scalar_fn::EmptyOptions;
19use crate::scalar_fn::ExecutionArgs;
20use crate::scalar_fn::ScalarFnId;
21use crate::scalar_fn::ScalarFnVTable;
22
23/// An expression that returns the full scope of the expression evaluation.
24// TODO(ngates): rename to "Scope"
25#[derive(Clone)]
26pub struct Root;
27
28impl ScalarFnVTable for Root {
29    type Options = EmptyOptions;
30
31    fn id(&self) -> ScalarFnId {
32        ScalarFnId::from("vortex.root")
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(&self, _data: &Self::Options, _args: ExecutionArgs) -> VortexResult<ArrayRef> {
72        vortex_bail!("Root expression is not executable")
73    }
74
75    fn stat_expression(
76        &self,
77        _options: &Self::Options,
78        _expr: &Expression,
79        stat: Stat,
80        catalog: &dyn StatsCatalog,
81    ) -> Option<Expression> {
82        catalog.stats_ref(&FieldPath::root(), stat)
83    }
84
85    fn is_null_sensitive(&self, _options: &Self::Options) -> bool {
86        false
87    }
88
89    fn is_fallible(&self, _options: &Self::Options) -> bool {
90        false
91    }
92}