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::dtype::FieldPath;
15use crate::expr::StatsCatalog;
16use crate::expr::expression::Expression;
17use crate::expr::stats::Stat;
18use crate::scalar_fn::Arity;
19use crate::scalar_fn::ChildName;
20use crate::scalar_fn::EmptyOptions;
21use crate::scalar_fn::ExecutionArgs;
22use crate::scalar_fn::ScalarFnId;
23use crate::scalar_fn::ScalarFnVTable;
24
25/// An expression that returns the full scope of the expression evaluation.
26// TODO(ngates): rename to "Scope"
27#[derive(Clone)]
28pub struct Root;
29
30impl ScalarFnVTable for Root {
31    type Options = EmptyOptions;
32
33    fn id(&self) -> ScalarFnId {
34        static ID: CachedId = CachedId::new("vortex.root");
35        *ID
36    }
37
38    fn serialize(&self, _instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
39        Ok(Some(vec![]))
40    }
41
42    fn deserialize(
43        &self,
44        _metadata: &[u8],
45        _session: &VortexSession,
46    ) -> VortexResult<Self::Options> {
47        Ok(EmptyOptions)
48    }
49
50    fn arity(&self, _options: &Self::Options) -> Arity {
51        Arity::Exact(0)
52    }
53
54    fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName {
55        unreachable!(
56            "Root expression does not have children, got index {}",
57            child_idx
58        )
59    }
60
61    fn fmt_sql(
62        &self,
63        _options: &Self::Options,
64        _expr: &Expression,
65        f: &mut Formatter<'_>,
66    ) -> std::fmt::Result {
67        write!(f, "$")
68    }
69
70    fn return_dtype(&self, _options: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
71        vortex_bail!("Root expression does not support return_dtype")
72    }
73
74    fn execute(
75        &self,
76        _data: &Self::Options,
77        _args: &dyn ExecutionArgs,
78        _ctx: &mut ExecutionCtx,
79    ) -> VortexResult<ArrayRef> {
80        vortex_bail!("Root expression is not executable")
81    }
82
83    fn stat_expression(
84        &self,
85        _options: &Self::Options,
86        _expr: &Expression,
87        stat: Stat,
88        catalog: &dyn StatsCatalog,
89    ) -> Option<Expression> {
90        catalog.stats_ref(&FieldPath::root(), stat)
91    }
92
93    fn is_null_sensitive(&self, _options: &Self::Options) -> bool {
94        false
95    }
96
97    fn is_fallible(&self, _options: &Self::Options) -> bool {
98        false
99    }
100}