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::Expression;
23use crate::expr::display::ExprDisplay;
24use crate::scalar_fn::EmptyOptions;
25use crate::scalar_fn::ExecutionArgs;
26use crate::scalar_fn::ReduceCtx;
27use crate::scalar_fn::ReduceNode;
28use crate::scalar_fn::ReduceNodeRef;
29use crate::scalar_fn::ScalarFnId;
30use crate::scalar_fn::ScalarFnVTable;
31use crate::scalar_fn::ScalarFnVTableExt;
32use crate::scalar_fn::SimplifyCtx;
33use crate::scalar_fn::fns::is_not_null::IsNotNull;
34use crate::scalar_fn::options::ScalarFnOptions;
35use crate::scalar_fn::signature::ScalarFnSignature;
36use crate::scalar_fn::typed::DynScalarFn;
37use crate::scalar_fn::typed::TypedScalarFnInstance;
38
39#[derive(Clone)]
47pub struct ScalarFnRef(pub(super) Arc<dyn DynScalarFn>);
48
49impl ScalarFnRef {
50 pub fn id(&self) -> ScalarFnId {
52 self.0.id()
53 }
54
55 pub fn is<V: ScalarFnVTable>(&self) -> bool {
57 self.0.as_any().is::<TypedScalarFnInstance<V>>()
58 }
59
60 pub fn as_opt<V: ScalarFnVTable>(&self) -> Option<&V::Options> {
62 self.0
63 .as_any()
64 .downcast_ref::<TypedScalarFnInstance<V>>()
65 .map(|sf| sf.options())
66 }
67
68 pub fn as_<V: ScalarFnVTable>(&self) -> &V::Options {
74 self.as_opt::<V>()
75 .vortex_expect("Expression options type mismatch")
76 }
77
78 pub fn try_downcast<V: ScalarFnVTable>(
82 self,
83 ) -> Result<Arc<TypedScalarFnInstance<V>>, ScalarFnRef> {
84 if self.0.as_any().is::<TypedScalarFnInstance<V>>() {
85 let ptr = Arc::into_raw(self.0) as *const TypedScalarFnInstance<V>;
86 Ok(unsafe { Arc::from_raw(ptr) })
87 } else {
88 Err(self)
89 }
90 }
91
92 pub fn downcast<V: ScalarFnVTable>(self) -> Arc<TypedScalarFnInstance<V>> {
98 self.try_downcast::<V>()
99 .map_err(|this| {
100 vortex_err!(
101 "Failed to downcast ScalarFnRef {} to {}",
102 this.0.id(),
103 type_name::<V>(),
104 )
105 })
106 .vortex_expect("Failed to downcast ScalarFnRef")
107 }
108
109 pub fn downcast_ref<V: ScalarFnVTable>(&self) -> Option<&TypedScalarFnInstance<V>> {
111 self.0.as_any().downcast_ref::<TypedScalarFnInstance<V>>()
112 }
113
114 pub fn options(&self) -> ScalarFnOptions<'_> {
116 ScalarFnOptions { inner: &*self.0 }
117 }
118
119 pub fn signature(&self) -> ScalarFnSignature<'_> {
121 ScalarFnSignature { inner: &*self.0 }
122 }
123
124 pub fn return_dtype(&self, arg_types: &[DType]) -> VortexResult<DType> {
126 self.0.return_dtype(arg_types)
127 }
128
129 pub fn coerce_args(&self, arg_types: &[DType]) -> VortexResult<Vec<DType>> {
131 self.0.coerce_args(arg_types)
132 }
133
134 pub fn validity(&self, expr: &Expression) -> VortexResult<Expression> {
136 Ok(self.0.validity(expr)?.unwrap_or_else(|| {
137 IsNotNull.new_expr(EmptyOptions, [expr.clone()])
139 }))
140 }
141
142 pub fn execute(
144 &self,
145 args: &dyn ExecutionArgs,
146 ctx: &mut ExecutionCtx,
147 ) -> VortexResult<ArrayRef> {
148 self.0.execute(args, ctx)
149 }
150
151 pub fn reduce(
153 &self,
154 node: &dyn ReduceNode,
155 ctx: &dyn ReduceCtx,
156 ) -> VortexResult<Option<ReduceNodeRef>> {
157 self.0.reduce(node, ctx)
158 }
159
160 pub(crate) fn fmt_sql(
166 &self,
167 expr: &dyn ExprDisplay,
168 f: &mut Formatter<'_>,
169 ) -> std::fmt::Result {
170 self.0.fmt_sql(expr, f)
171 }
172
173 pub(crate) fn simplify(
175 &self,
176 expr: &Expression,
177 ctx: &dyn SimplifyCtx,
178 ) -> VortexResult<Option<Expression>> {
179 self.0.simplify(expr, ctx)
180 }
181
182 pub(crate) fn simplify_untyped(&self, expr: &Expression) -> VortexResult<Option<Expression>> {
184 self.0.simplify_untyped(expr)
185 }
186}
187
188impl Debug for ScalarFnRef {
189 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
190 f.debug_struct("ScalarFnRef")
191 .field("vtable", &self.0.id())
192 .field("options", &DebugWith(|fmt| self.0.options_debug(fmt)))
193 .finish()
194 }
195}
196
197impl Display for ScalarFnRef {
198 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
199 write!(f, "{}(", self.0.id())?;
200 self.0.options_display(f)?;
201 write!(f, ")")
202 }
203}
204
205impl PartialEq for ScalarFnRef {
206 fn eq(&self, other: &Self) -> bool {
207 self.0.id() == other.0.id() && self.0.options_eq(other.0.options_any())
208 }
209}
210impl Eq for ScalarFnRef {}
211
212impl Hash for ScalarFnRef {
213 fn hash<H: Hasher>(&self, state: &mut H) {
214 self.0.id().hash(state);
215 self.0.options_hash(state);
216 }
217}