Skip to main content

vortex_array/scalar_fn/
session.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::any::Any;
5use std::sync::Arc;
6
7use vortex_session::Ref;
8use vortex_session::SessionExt;
9use vortex_session::SessionVar;
10use vortex_session::registry::Registry;
11
12use crate::scalar_fn::ScalarFnPluginRef;
13use crate::scalar_fn::ScalarFnVTable;
14use crate::scalar_fn::fns::between::Between;
15use crate::scalar_fn::fns::binary::Binary;
16use crate::scalar_fn::fns::cast::Cast;
17use crate::scalar_fn::fns::fill_null::FillNull;
18use crate::scalar_fn::fns::get_item::GetItem;
19use crate::scalar_fn::fns::is_not_null::IsNotNull;
20use crate::scalar_fn::fns::is_null::IsNull;
21use crate::scalar_fn::fns::like::Like;
22use crate::scalar_fn::fns::list_contains::ListContains;
23use crate::scalar_fn::fns::literal::Literal;
24use crate::scalar_fn::fns::merge::Merge;
25use crate::scalar_fn::fns::not::Not;
26use crate::scalar_fn::fns::pack::Pack;
27use crate::scalar_fn::fns::root::Root;
28use crate::scalar_fn::fns::select::Select;
29
30/// Registry of scalar function vtables.
31/// Registry of scalar function vtables.
32pub type ScalarFnRegistry = Registry<ScalarFnPluginRef>;
33
34/// Session state for scalar function vtables and rewrite rules.
35#[derive(Debug)]
36pub struct ScalarFnSession {
37    registry: ScalarFnRegistry,
38}
39
40impl ScalarFnSession {
41    pub fn registry(&self) -> &ScalarFnRegistry {
42        &self.registry
43    }
44
45    /// Register a scalar function vtable in the session, replacing any existing vtable with the same ID.
46    pub fn register<V: ScalarFnVTable>(&self, vtable: V) {
47        self.registry
48            .register(vtable.id(), Arc::new(vtable) as ScalarFnPluginRef);
49    }
50}
51
52impl Default for ScalarFnSession {
53    fn default() -> Self {
54        let this = Self {
55            registry: ScalarFnRegistry::default(),
56        };
57
58        // Register built-in expressions.
59        this.register(Between);
60        this.register(Binary);
61        this.register(Cast);
62        this.register(FillNull);
63        this.register(GetItem);
64        this.register(IsNotNull);
65        this.register(IsNull);
66        this.register(Like);
67        this.register(ListContains);
68        this.register(Literal);
69        this.register(Merge);
70        this.register(Not);
71        this.register(Pack);
72        this.register(Root);
73        this.register(Select);
74
75        this
76    }
77}
78
79impl SessionVar for ScalarFnSession {
80    fn as_any(&self) -> &dyn Any {
81        self
82    }
83
84    fn as_any_mut(&mut self) -> &mut dyn Any {
85        self
86    }
87}
88
89/// Extension trait for accessing scalar function session data.
90pub trait ScalarFnSessionExt: SessionExt {
91    /// Returns the scalar function vtable registry.
92    fn scalar_fns(&self) -> Ref<'_, ScalarFnSession> {
93        self.get::<ScalarFnSession>()
94    }
95}
96impl<S: SessionExt> ScalarFnSessionExt for S {}