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