vortex_array/scalar_fn/
erased.rs1use std::any::type_name;
7use std::fmt::Debug;
8use std::fmt::Display;
9use std::fmt::Formatter;
10use std::hash::Hash;
11use std::hash::Hasher;
12use std::sync::Arc;
13
14use vortex_error::VortexExpect;
15use vortex_error::VortexResult;
16use vortex_error::vortex_err;
17use vortex_utils::debug_with::DebugWith;
18
19use crate::ArrayRef;
20use crate::ExecutionCtx;
21use crate::dtype::DType;
22use crate::expr::BoundExpression;
23use crate::expr::display::ExprDisplay;
24use crate::scalar_fn::ArrayReduceNode;
25use crate::scalar_fn::ExecutionArgs;
26use crate::scalar_fn::ExpressionReduceNode;
27use crate::scalar_fn::ReduceNodeValidity;
28use crate::scalar_fn::ScalarFnExecutionResult;
29use crate::scalar_fn::ScalarFnId;
30use crate::scalar_fn::ScalarFnVTable;
31use crate::scalar_fn::options::ScalarFnOptions;
32use crate::scalar_fn::signature::ScalarFnSignature;
33use crate::scalar_fn::typed::DynScalarFn;
34use crate::scalar_fn::typed::TypedScalarFnInstance;
35
36#[derive(Clone)]
44pub struct ScalarFnRef(pub(super) Arc<dyn DynScalarFn>);
45
46impl ScalarFnRef {
47 pub fn id(&self) -> ScalarFnId {
49 self.0.id()
50 }
51
52 pub fn is<V: ScalarFnVTable>(&self) -> bool {
54 self.0.as_any().is::<TypedScalarFnInstance<V>>()
55 }
56
57 pub fn as_opt<V: ScalarFnVTable>(&self) -> Option<&V::Options> {
59 self.0
60 .as_any()
61 .downcast_ref::<TypedScalarFnInstance<V>>()
62 .map(|sf| sf.options())
63 }
64
65 pub fn as_<V: ScalarFnVTable>(&self) -> &V::Options {
71 self.as_opt::<V>()
72 .vortex_expect("Expression options type mismatch")
73 }
74
75 pub fn try_downcast<V: ScalarFnVTable>(
79 self,
80 ) -> Result<Arc<TypedScalarFnInstance<V>>, ScalarFnRef> {
81 if self.0.as_any().is::<TypedScalarFnInstance<V>>() {
82 let ptr = Arc::into_raw(self.0) as *const TypedScalarFnInstance<V>;
83 Ok(unsafe { Arc::from_raw(ptr) })
84 } else {
85 Err(self)
86 }
87 }
88
89 pub fn downcast<V: ScalarFnVTable>(self) -> Arc<TypedScalarFnInstance<V>> {
95 self.try_downcast::<V>()
96 .map_err(|this| {
97 vortex_err!(
98 "Failed to downcast ScalarFnRef {} to {}",
99 this.0.id(),
100 type_name::<V>(),
101 )
102 })
103 .vortex_expect("Failed to downcast ScalarFnRef")
104 }
105
106 pub fn downcast_ref<V: ScalarFnVTable>(&self) -> Option<&TypedScalarFnInstance<V>> {
108 self.0.as_any().downcast_ref::<TypedScalarFnInstance<V>>()
109 }
110
111 pub fn options(&self) -> ScalarFnOptions<'_> {
113 ScalarFnOptions { inner: &*self.0 }
114 }
115
116 pub fn signature(&self) -> ScalarFnSignature<'_> {
118 ScalarFnSignature { inner: &*self.0 }
119 }
120
121 pub fn return_dtype(&self, arg_types: &[DType]) -> VortexResult<DType> {
123 self.0.return_dtype(arg_types)
124 }
125
126 pub(crate) fn bind_options(self, arg_types: &[DType]) -> VortexResult<ScalarFnRef> {
129 Ok(self.0.bind_options(arg_types)?.unwrap_or(self))
130 }
131
132 pub(crate) fn validity_expression<'a>(
134 &self,
135 node: &ExpressionReduceNode<'a>,
136 ) -> VortexResult<ReduceNodeValidity<ExpressionReduceNode<'a>>> {
137 self.0.validity_expression(node)
138 }
139
140 pub(crate) fn validity_array<'a>(
142 &self,
143 node: &ArrayReduceNode<'a>,
144 ) -> VortexResult<ReduceNodeValidity<ArrayReduceNode<'a>>> {
145 self.0.validity_array(node)
146 }
147
148 pub fn execute(
150 &self,
151 args: &dyn ExecutionArgs,
152 ctx: &mut ExecutionCtx,
153 ) -> VortexResult<ArrayRef> {
154 self.0.execute(args, ctx)
155 }
156
157 pub fn execute_step(
161 &self,
162 args: &dyn ExecutionArgs,
163 ctx: &mut ExecutionCtx,
164 ) -> VortexResult<ScalarFnExecutionResult> {
165 self.0.execute_step(args, ctx)
166 }
167
168 pub fn reduce_expression<'a>(
170 &self,
171 node: &ExpressionReduceNode<'a>,
172 ) -> VortexResult<Option<ExpressionReduceNode<'a>>> {
173 self.0.reduce_expression(node)
174 }
175
176 pub fn reduce_array<'a>(
178 &self,
179 node: &ArrayReduceNode<'a>,
180 ) -> VortexResult<Option<ArrayReduceNode<'a>>> {
181 self.0.reduce_array(node)
182 }
183
184 pub(crate) fn fmt_sql(
190 &self,
191 expr: &dyn ExprDisplay,
192 f: &mut Formatter<'_>,
193 ) -> std::fmt::Result {
194 self.0.fmt_sql(expr, f)
195 }
196
197 pub(crate) fn simplify(&self, expr: &BoundExpression) -> VortexResult<Option<BoundExpression>> {
199 self.0.simplify(expr)
200 }
201}
202
203impl Debug for ScalarFnRef {
204 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
205 f.debug_struct("ScalarFnRef")
206 .field("vtable", &self.0.id())
207 .field("options", &DebugWith(|fmt| self.0.options_debug(fmt)))
208 .finish()
209 }
210}
211
212impl Display for ScalarFnRef {
213 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
214 write!(f, "{}(", self.0.id())?;
215 self.0.options_display(f)?;
216 write!(f, ")")
217 }
218}
219
220impl PartialEq for ScalarFnRef {
221 fn eq(&self, other: &Self) -> bool {
222 self.0.id() == other.0.id() && self.0.options_eq(other.0.options_any())
223 }
224}
225impl Eq for ScalarFnRef {}
226
227impl Hash for ScalarFnRef {
228 fn hash<H: Hasher>(&self, state: &mut H) {
229 self.0.id().hash(state);
230 self.0.options_hash(state);
231 }
232}