Skip to main content

vortex_expr/proto/
foreign.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt;
5use std::fmt::Display;
6use std::fmt::Formatter;
7
8use vortex_array::ArrayRef;
9use vortex_array::ExecutionCtx;
10use vortex_array::dtype::DType;
11use vortex_array::dtype::Nullability;
12use vortex_array::scalar_fn::Arity;
13use vortex_array::scalar_fn::ChildName;
14use vortex_array::scalar_fn::ExecutionArgs;
15use vortex_array::scalar_fn::ScalarFnId;
16use vortex_array::scalar_fn::ScalarFnRef;
17use vortex_array::scalar_fn::ScalarFnVTable;
18use vortex_array::scalar_fn::TypedScalarFnInstance;
19use vortex_array::scalar_fn::display::ExprDisplay;
20use vortex_error::VortexResult;
21use vortex_error::vortex_bail;
22use vortex_session::VortexSession;
23
24/// Options payload for a foreign scalar function.
25#[derive(Clone, Debug, PartialEq, Eq, Hash)]
26pub struct ForeignScalarFnOptions {
27    metadata: Vec<u8>,
28    arity: usize,
29}
30
31impl ForeignScalarFnOptions {
32    pub fn new(metadata: Vec<u8>, arity: usize) -> Self {
33        Self { metadata, arity }
34    }
35}
36
37impl Display for ForeignScalarFnOptions {
38    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39        write!(
40            f,
41            "foreign(arity={}, metadata={}B)",
42            self.arity,
43            self.metadata.len()
44        )
45    }
46}
47
48/// Scalar function placeholder used when deserializing an unknown scalar function ID.
49#[derive(Clone, Debug)]
50pub struct ForeignScalarFnVTable {
51    id: ScalarFnId,
52}
53
54impl ForeignScalarFnVTable {
55    pub fn new(id: ScalarFnId) -> Self {
56        Self { id }
57    }
58
59    pub fn make_scalar_fn(id: ScalarFnId, metadata: Vec<u8>, arity: usize) -> ScalarFnRef {
60        TypedScalarFnInstance::new(Self::new(id), ForeignScalarFnOptions::new(metadata, arity))
61            .erased()
62    }
63}
64
65impl ScalarFnVTable for ForeignScalarFnVTable {
66    type Options = ForeignScalarFnOptions;
67
68    fn id(&self) -> ScalarFnId {
69        self.id
70    }
71
72    fn serialize(&self, options: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
73        Ok(Some(options.metadata.clone()))
74    }
75
76    fn deserialize(
77        &self,
78        metadata: &[u8],
79        _session: &VortexSession,
80    ) -> VortexResult<Self::Options> {
81        Ok(ForeignScalarFnOptions::new(metadata.to_vec(), 0))
82    }
83
84    fn arity(&self, options: &Self::Options) -> Arity {
85        Arity::Exact(options.arity)
86    }
87
88    fn child_name(&self, _options: &Self::Options, child_idx: usize) -> ChildName {
89        ChildName::new_arc(format!("arg{child_idx}").into())
90    }
91
92    fn fmt_sql(
93        &self,
94        _options: &Self::Options,
95        expr: &dyn ExprDisplay,
96        f: &mut Formatter<'_>,
97    ) -> fmt::Result {
98        write!(f, "{}(", self.id)?;
99        for i in 0..expr.display_children_count() {
100            if i > 0 {
101                write!(f, ", ")?;
102            }
103            Display::fmt(expr.display_child(i), f)?;
104        }
105        write!(f, ")")
106    }
107
108    fn return_dtype(&self, _options: &Self::Options, _args: &[DType]) -> VortexResult<DType> {
109        Ok(DType::Variant(Nullability::Nullable))
110    }
111
112    fn execute(
113        &self,
114        _options: &Self::Options,
115        _args: &dyn ExecutionArgs,
116        _ctx: &mut ExecutionCtx,
117    ) -> VortexResult<ArrayRef> {
118        vortex_bail!("Cannot execute unknown scalar function '{}'", self.id);
119    }
120
121    fn is_strict(&self, _options: &Self::Options) -> bool {
122        false
123    }
124}