vortex_array/expr/
session.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_session::registry::Registry;
5use vortex_session::{Ref, SessionExt};
6
7use crate::expr::ExprVTable;
8use crate::expr::exprs::between::Between;
9use crate::expr::exprs::binary::Binary;
10use crate::expr::exprs::cast::Cast;
11use crate::expr::exprs::get_item::GetItem;
12use crate::expr::exprs::is_null::IsNull;
13use crate::expr::exprs::like::Like;
14use crate::expr::exprs::list_contains::ListContains;
15use crate::expr::exprs::literal::Literal;
16use crate::expr::exprs::merge::Merge;
17use crate::expr::exprs::not::Not;
18use crate::expr::exprs::pack::Pack;
19use crate::expr::exprs::root::Root;
20use crate::expr::exprs::select::Select;
21
22/// Registry of expression vtables.
23pub type ExprRegistry = Registry<ExprVTable>;
24
25/// Session state for expression vtables.
26#[derive(Debug)]
27pub struct ExprSession {
28    registry: ExprRegistry,
29}
30
31impl ExprSession {
32    pub fn registry(&self) -> &ExprRegistry {
33        &self.registry
34    }
35
36    /// Register an expression vtable in the session, replacing any existing vtable with the same ID.
37    pub fn register(&self, expr: ExprVTable) {
38        self.registry.register(expr)
39    }
40
41    /// Register expression vtables in the session, replacing any existing vtables with the same IDs.
42    pub fn register_many(&self, exprs: impl IntoIterator<Item = ExprVTable>) {
43        self.registry.register_many(exprs);
44    }
45}
46
47impl Default for ExprSession {
48    fn default() -> Self {
49        let expressions = ExprRegistry::default();
50
51        // Register built-in expressions here if needed.
52        expressions.register_many([
53            ExprVTable::from_static(&Between),
54            ExprVTable::from_static(&Binary),
55            ExprVTable::from_static(&Cast),
56            ExprVTable::from_static(&GetItem),
57            ExprVTable::from_static(&IsNull),
58            ExprVTable::from_static(&Like),
59            ExprVTable::from_static(&ListContains),
60            ExprVTable::from_static(&Literal),
61            ExprVTable::from_static(&Merge),
62            ExprVTable::from_static(&Not),
63            ExprVTable::from_static(&Pack),
64            ExprVTable::from_static(&Root),
65            ExprVTable::from_static(&Select),
66        ]);
67
68        Self {
69            registry: expressions,
70        }
71    }
72}
73
74/// Extension trait for accessing expression session data.
75pub trait ExprSessionExt: SessionExt {
76    /// Returns the expression vtable registry.
77    fn expressions(&self) -> Ref<'_, ExprSession> {
78        self.get::<ExprSession>()
79    }
80}
81impl<S: SessionExt> ExprSessionExt for S {}