vortex_expr/exprs/
root.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt::Display;
5
6use vortex_array::stats::Stat;
7use vortex_array::{ArrayRef, DeserializeMetadata, EmptyMetadata};
8use vortex_dtype::{DType, FieldPath};
9use vortex_error::{VortexResult, vortex_bail};
10
11use crate::{
12    AnalysisExpr, ExprEncodingRef, ExprId, ExprRef, IntoExpr, Scope, StatsCatalog, VTable, vtable,
13};
14
15vtable!(Root);
16
17/// An expression that returns the full scope of the expression evaluation.
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub struct RootExpr;
20
21pub struct RootExprEncoding;
22
23impl VTable for RootVTable {
24    type Expr = RootExpr;
25    type Encoding = RootExprEncoding;
26    type Metadata = EmptyMetadata;
27
28    fn id(_encoding: &Self::Encoding) -> ExprId {
29        ExprId::new_ref("root")
30    }
31
32    fn encoding(_expr: &Self::Expr) -> ExprEncodingRef {
33        ExprEncodingRef::new_ref(RootExprEncoding.as_ref())
34    }
35
36    fn metadata(_expr: &Self::Expr) -> Option<Self::Metadata> {
37        Some(EmptyMetadata)
38    }
39
40    fn children(_expr: &Self::Expr) -> Vec<&ExprRef> {
41        vec![]
42    }
43
44    fn with_children(expr: &Self::Expr, _children: Vec<ExprRef>) -> VortexResult<Self::Expr> {
45        Ok(expr.clone())
46    }
47
48    fn build(
49        _encoding: &Self::Encoding,
50        _metadata: &<Self::Metadata as DeserializeMetadata>::Output,
51        children: Vec<ExprRef>,
52    ) -> VortexResult<Self::Expr> {
53        if !children.is_empty() {
54            vortex_bail!(
55                "Root expression does not have children, got: {:?}",
56                children
57            );
58        }
59        Ok(RootExpr)
60    }
61
62    fn evaluate(_expr: &Self::Expr, scope: &Scope) -> VortexResult<ArrayRef> {
63        Ok(scope.root().clone())
64    }
65
66    fn return_dtype(_expr: &Self::Expr, scope: &DType) -> VortexResult<DType> {
67        Ok(scope.clone())
68    }
69}
70
71impl Display for RootExpr {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        write!(f, "$")
74    }
75}
76
77impl AnalysisExpr for RootExpr {
78    fn max(&self, catalog: &mut dyn StatsCatalog) -> Option<ExprRef> {
79        catalog.stats_ref(&self.field_path()?, Stat::Max)
80    }
81
82    fn min(&self, catalog: &mut dyn StatsCatalog) -> Option<ExprRef> {
83        catalog.stats_ref(&self.field_path()?, Stat::Min)
84    }
85
86    fn nan_count(&self, catalog: &mut dyn StatsCatalog) -> Option<ExprRef> {
87        catalog.stats_ref(&self.field_path()?, Stat::NaNCount)
88    }
89
90    fn field_path(&self) -> Option<FieldPath> {
91        Some(FieldPath::root())
92    }
93}
94
95/// Return a global pointer to the identity token.
96/// This is the name of the data found in a vortex array or file.
97pub fn root() -> ExprRef {
98    RootExpr.into_expr()
99}
100
101/// Return whether the expression is a root expression.
102pub fn is_root(expr: &ExprRef) -> bool {
103    expr.is::<RootVTable>()
104}