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::SessionExt;
8use vortex_session::SessionGuard;
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::ext_storage::ExtStorage;
18use crate::scalar_fn::fns::fill_null::FillNull;
19use crate::scalar_fn::fns::get_item::GetItem;
20use crate::scalar_fn::fns::is_not_null::IsNotNull;
21use crate::scalar_fn::fns::is_null::IsNull;
22use crate::scalar_fn::fns::like::Like;
23use crate::scalar_fn::fns::list_contains::ListContains;
24use crate::scalar_fn::fns::list_length::ListLength;
25use crate::scalar_fn::fns::list_sum::ListSum;
26use crate::scalar_fn::fns::literal::Literal;
27use crate::scalar_fn::fns::merge::Merge;
28use crate::scalar_fn::fns::not::Not;
29use crate::scalar_fn::fns::pack::Pack;
30use crate::scalar_fn::fns::root::Root;
31use crate::scalar_fn::fns::select::Select;
32use crate::scalar_fn::fns::stat::StatFn;
33use crate::scalar_fn::fns::variant_get::VariantGet;
34
35/// Registry of scalar function vtables.
36pub type ScalarFnRegistry = Registry<ScalarFnPluginRef>;
37
38/// Session state for scalar function vtables and rewrite rules.
39#[derive(Clone, Debug)]
40pub struct ScalarFnSession {
41    registry: ScalarFnRegistry,
42}
43
44impl ScalarFnSession {
45    pub fn registry(&self) -> &ScalarFnRegistry {
46        &self.registry
47    }
48
49    /// Register a scalar function vtable in the session, replacing any existing vtable with the same ID.
50    pub fn register<V: ScalarFnVTable>(&self, vtable: V) {
51        self.registry
52            .register(vtable.id(), Arc::new(vtable) as ScalarFnPluginRef);
53    }
54}
55
56impl Default for ScalarFnSession {
57    fn default() -> Self {
58        let this = Self {
59            registry: ScalarFnRegistry::default(),
60        };
61
62        // Register built-in expressions.
63        this.register(Between);
64        this.register(Binary);
65        this.register(Cast);
66        this.register(ExtStorage);
67        this.register(FillNull);
68        this.register(GetItem);
69        this.register(IsNotNull);
70        this.register(IsNull);
71        this.register(Like);
72        this.register(ListContains);
73        this.register(ListLength);
74        this.register(ListSum);
75        this.register(Literal);
76        this.register(Merge);
77        this.register(Not);
78        this.register(Pack);
79        this.register(Root);
80        this.register(Select);
81        this.register(StatFn);
82        this.register(VariantGet);
83
84        this
85    }
86}
87
88impl SessionVar for ScalarFnSession {
89    fn as_any(&self) -> &dyn Any {
90        self
91    }
92
93    fn as_any_mut(&mut self) -> &mut dyn Any {
94        self
95    }
96}
97
98/// Extension trait for accessing scalar function session data.
99pub trait ScalarFnSessionExt: SessionExt {
100    /// Returns the scalar function vtable registry.
101    fn scalar_fns(&self) -> SessionGuard<'_, ScalarFnSession> {
102        self.get::<ScalarFnSession>()
103    }
104}
105impl<S: SessionExt> ScalarFnSessionExt for S {}