-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy patherased.rs
More file actions
231 lines (202 loc) · 7.29 KB
/
erased.rs
File metadata and controls
231 lines (202 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Type-erased scalar function ([`ScalarFnRef`]).
use std::any::type_name;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::sync::Arc;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_err;
use vortex_utils::debug_with::DebugWith;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::dtype::DType;
use crate::expr::Expression;
use crate::expr::StatsCatalog;
use crate::expr::stats::Stat;
use crate::scalar_fn::EmptyOptions;
use crate::scalar_fn::ExecutionArgs;
use crate::scalar_fn::ReduceCtx;
use crate::scalar_fn::ReduceNode;
use crate::scalar_fn::ReduceNodeRef;
use crate::scalar_fn::ScalarFnId;
use crate::scalar_fn::ScalarFnVTable;
use crate::scalar_fn::ScalarFnVTableExt;
use crate::scalar_fn::SimplifyCtx;
use crate::scalar_fn::fns::is_not_null::IsNotNull;
use crate::scalar_fn::options::ScalarFnOptions;
use crate::scalar_fn::signature::ScalarFnSignature;
use crate::scalar_fn::typed::DynScalarFn;
use crate::scalar_fn::typed::ScalarFn;
/// A type-erased scalar function, pairing a vtable with bound options behind a trait object.
///
/// This stores a [`ScalarFnVTable`] and its options behind an `Arc<dyn DynScalarFn>`, allowing
/// heterogeneous storage inside [`Expression`] and [`crate::arrays::ScalarFnArray`].
///
/// Use [`super::ScalarFn::new()`] to construct, and [`super::ScalarFn::erased()`] to obtain a
/// [`ScalarFnRef`].
#[derive(Clone)]
pub struct ScalarFnRef(pub(super) Arc<dyn DynScalarFn>);
impl ScalarFnRef {
/// Returns the ID of this scalar function.
pub fn id(&self) -> ScalarFnId {
self.0.id()
}
/// Returns whether the scalar function is of the given vtable type.
pub fn is<V: ScalarFnVTable>(&self) -> bool {
self.0.as_any().is::<ScalarFn<V>>()
}
/// Returns the typed options for this scalar function if it matches the given vtable type.
pub fn as_opt<V: ScalarFnVTable>(&self) -> Option<&V::Options> {
self.0
.as_any()
.downcast_ref::<ScalarFn<V>>()
.map(|sf| sf.options())
}
/// Returns the typed options for this scalar function if it matches the given vtable type.
///
/// # Panics
///
/// Panics if the vtable type does not match.
pub fn as_<V: ScalarFnVTable>(&self) -> &V::Options {
self.as_opt::<V>()
.vortex_expect("Expression options type mismatch")
}
/// Downcast to the concrete [`ScalarFn`].
///
/// Returns `Err(self)` if the downcast fails.
pub fn try_downcast<V: ScalarFnVTable>(self) -> Result<Arc<ScalarFn<V>>, ScalarFnRef> {
if self.0.as_any().is::<ScalarFn<V>>() {
let ptr = Arc::into_raw(self.0) as *const ScalarFn<V>;
Ok(unsafe { Arc::from_raw(ptr) })
} else {
Err(self)
}
}
/// Downcast to the concrete [`ScalarFn`].
///
/// # Panics
///
/// Panics if the downcast fails.
pub fn downcast<V: ScalarFnVTable>(self) -> Arc<ScalarFn<V>> {
self.try_downcast::<V>()
.map_err(|this| {
vortex_err!(
"Failed to downcast ScalarFnRef {} to {}",
this.0.id(),
type_name::<V>(),
)
})
.vortex_expect("Failed to downcast ScalarFnRef")
}
/// Try to downcast into a typed [`ScalarFn`].
pub fn downcast_ref<V: ScalarFnVTable>(&self) -> Option<&ScalarFn<V>> {
self.0.as_any().downcast_ref::<ScalarFn<V>>()
}
/// The type-erased options for this scalar function.
pub fn options(&self) -> ScalarFnOptions<'_> {
ScalarFnOptions { inner: &*self.0 }
}
/// Signature information for this scalar function.
pub fn signature(&self) -> ScalarFnSignature<'_> {
ScalarFnSignature { inner: &*self.0 }
}
/// Compute the return [`DType`] of this expression given the input argument types.
pub fn return_dtype(&self, arg_types: &[DType]) -> VortexResult<DType> {
self.0.return_dtype(arg_types)
}
/// Coerce the argument types for this scalar function.
pub fn coerce_args(&self, arg_types: &[DType]) -> VortexResult<Vec<DType>> {
self.0.coerce_args(arg_types)
}
/// Transforms the expression into one representing the validity of this expression.
pub fn validity(&self, expr: &Expression) -> VortexResult<Expression> {
Ok(self.0.validity(expr)?.unwrap_or_else(|| {
// TODO(ngates): make validity a mandatory method on VTable to avoid this fallback.
IsNotNull.new_expr(EmptyOptions, [expr.clone()])
}))
}
/// Execute the expression given the input arguments.
pub fn execute(
&self,
args: &dyn ExecutionArgs,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
self.0.execute(args, ctx)
}
/// Perform abstract reduction on this scalar function node.
pub fn reduce(
&self,
node: &dyn ReduceNode,
ctx: &dyn ReduceCtx,
) -> VortexResult<Option<ReduceNodeRef>> {
self.0.reduce(node, ctx)
}
// ------------------------------------------------------------------
// Expression-taking methods — used by expr/ module via pub(crate)
// ------------------------------------------------------------------
/// Format this expression in SQL-style format.
pub(crate) fn fmt_sql(&self, expr: &Expression, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt_sql(expr, f)
}
/// Simplify the expression using type information.
pub(crate) fn simplify(
&self,
expr: &Expression,
ctx: &dyn SimplifyCtx,
) -> VortexResult<Option<Expression>> {
self.0.simplify(expr, ctx)
}
/// Simplify the expression without type information.
pub(crate) fn simplify_untyped(&self, expr: &Expression) -> VortexResult<Option<Expression>> {
self.0.simplify_untyped(expr)
}
/// Compute stat falsification expression.
pub(crate) fn stat_falsification(
&self,
expr: &Expression,
catalog: &dyn StatsCatalog,
) -> Option<Expression> {
self.0.stat_falsification(expr, catalog)
}
/// Compute stat expression.
pub(crate) fn stat_expression(
&self,
expr: &Expression,
stat: Stat,
catalog: &dyn StatsCatalog,
) -> Option<Expression> {
self.0.stat_expression(expr, stat, catalog)
}
}
impl Debug for ScalarFnRef {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ScalarFnRef")
.field("vtable", &self.0.id())
.field("options", &DebugWith(|fmt| self.0.options_debug(fmt)))
.finish()
}
}
impl Display for ScalarFnRef {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}(", self.0.id())?;
self.0.options_display(f)?;
write!(f, ")")
}
}
impl PartialEq for ScalarFnRef {
fn eq(&self, other: &Self) -> bool {
self.0.id() == other.0.id() && self.0.options_eq(other.0.options_any())
}
}
impl Eq for ScalarFnRef {}
impl Hash for ScalarFnRef {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.id().hash(state);
self.0.options_hash(state);
}
}