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::ArrayReduceNode;
25use crate::scalar_fn::EmptyOptions;
26use crate::scalar_fn::ExecutionArgs;
27use crate::scalar_fn::ExpressionReduceNode;
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 validity(&self, expr: &Expression) -> VortexResult<Expression> {
130 Ok(self.0.validity(expr)?.unwrap_or_else(|| {
131 IsNotNull.new_expr(EmptyOptions, [expr.clone()])
133 }))
134 }
135
136 pub fn execute(
138 &self,
139 args: &dyn ExecutionArgs,
140 ctx: &mut ExecutionCtx,
141 ) -> VortexResult<ArrayRef> {
142 self.0.execute(args, ctx)
143 }
144
145 pub fn reduce_expression<'a>(
147 &self,
148 node: &ExpressionReduceNode<'a>,
149 ) -> VortexResult<Option<ExpressionReduceNode<'a>>> {
150 self.0.reduce_expression(node)
151 }
152
153 pub fn reduce_array<'a>(
155 &self,
156 node: &ArrayReduceNode<'a>,
157 ) -> VortexResult<Option<ArrayReduceNode<'a>>> {
158 self.0.reduce_array(node)
159 }
160
161 pub(crate) fn fmt_sql(
167 &self,
168 expr: &dyn ExprDisplay,
169 f: &mut Formatter<'_>,
170 ) -> std::fmt::Result {
171 self.0.fmt_sql(expr, f)
172 }
173
174 pub(crate) fn simplify(
176 &self,
177 expr: &Expression,
178 ctx: &dyn SimplifyCtx,
179 ) -> VortexResult<Option<Expression>> {
180 self.0.simplify(expr, ctx)
181 }
182
183 pub(crate) fn simplify_untyped(&self, expr: &Expression) -> VortexResult<Option<Expression>> {
185 self.0.simplify_untyped(expr)
186 }
187}
188
189impl Debug for ScalarFnRef {
190 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
191 f.debug_struct("ScalarFnRef")
192 .field("vtable", &self.0.id())
193 .field("options", &DebugWith(|fmt| self.0.options_debug(fmt)))
194 .finish()
195 }
196}
197
198impl Display for ScalarFnRef {
199 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
200 write!(f, "{}(", self.0.id())?;
201 self.0.options_display(f)?;
202 write!(f, ")")
203 }
204}
205
206impl PartialEq for ScalarFnRef {
207 fn eq(&self, other: &Self) -> bool {
208 self.0.id() == other.0.id() && self.0.options_eq(other.0.options_any())
209 }
210}
211impl Eq for ScalarFnRef {}
212
213impl Hash for ScalarFnRef {
214 fn hash<H: Hasher>(&self, state: &mut H) {
215 self.0.id().hash(state);
216 self.0.options_hash(state);
217 }
218}