forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.rs
317 lines (270 loc) · 9.08 KB
/
functions.rs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
//! Functions.
use std::{
hash::{Hash, Hasher},
sync::Arc,
};
use arrow::datatypes::DataType;
use common_types::{column::ColumnBlock, datum::DatumKind};
use datafusion::{
error::DataFusionError,
logical_expr::{
AccumulatorFactoryFunction, ReturnTypeFunction, ScalarFunctionImplementation,
Signature as DfSignature, StateTypeFunction, TypeSignature as DfTypeSignature, Volatility,
},
physical_plan::ColumnarValue as DfColumnarValue,
scalar::ScalarValue as DfScalarValue,
};
use generic_error::GenericError;
use macros::define_result;
use smallvec::SmallVec;
use snafu::{ResultExt, Snafu};
use crate::aggregate::{Accumulator, ToDfAccumulator};
// Most functions have no more than 5 args.
const FUNC_ARG_NUM: usize = 5;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum Error {
#[snafu(display("Failed to convert array to ColumnarValue, err:{}", source))]
InvalidArray { source: common_types::column::Error },
#[snafu(display("Invalid function arguments, err:{}", source))]
InvalidArguments { source: GenericError },
#[snafu(display("Failed to execute function, err:{}", source))]
CallFunction { source: GenericError },
}
define_result!(Error);
/// A dynamically typed, nullable single value.
// TODO(yingwen): Can we use Datum?
#[derive(Debug)]
pub struct ScalarValue(DfScalarValue);
impl ScalarValue {
pub(crate) fn into_df_scalar_value(self) -> DfScalarValue {
self.0
}
fn from_df_scalar_value(df_scalar: &DfScalarValue) -> Self {
Self(df_scalar.clone())
}
pub fn as_str(&self) -> Option<&str> {
match &self.0 {
DfScalarValue::Utf8(value_opt) => value_opt.as_ref().map(|v| v.as_str()),
_ => None,
}
}
}
impl From<String> for ScalarValue {
fn from(value: String) -> Self {
Self(DfScalarValue::Utf8(Some(value)))
}
}
impl From<u64> for ScalarValue {
fn from(value: u64) -> Self {
Self(value.into())
}
}
pub struct ScalarValueRef<'a>(&'a DfScalarValue);
impl<'a> ScalarValueRef<'a> {
pub fn as_str(&self) -> Option<&str> {
match self.0 {
DfScalarValue::Utf8(value_opt) | DfScalarValue::LargeUtf8(value_opt) => {
value_opt.as_ref().map(|v| v.as_str())
}
_ => None,
}
}
}
impl<'a> From<&'a DfScalarValue> for ScalarValueRef<'a> {
fn from(value: &DfScalarValue) -> ScalarValueRef {
ScalarValueRef(value)
}
}
impl<'a> Hash for ScalarValueRef<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}
/// Represent a value of function result.
#[derive(Debug)]
pub enum ColumnarValue {
/// Array of values.
Array(ColumnBlock),
/// A single value.
Scalar(ScalarValue),
}
impl ColumnarValue {
fn into_df_columnar_value(self) -> DfColumnarValue {
match self {
ColumnarValue::Array(v) => DfColumnarValue::Array(v.to_arrow_array_ref()),
ColumnarValue::Scalar(v) => DfColumnarValue::Scalar(v.into_df_scalar_value()),
}
}
fn try_from_df_columnar_value(df_value: &DfColumnarValue) -> Result<Self> {
let columnar_value = match df_value {
DfColumnarValue::Array(array) => {
let column_block =
ColumnBlock::try_cast_arrow_array_ref(array).context(InvalidArray)?;
ColumnarValue::Array(column_block)
}
DfColumnarValue::Scalar(v) => {
ColumnarValue::Scalar(ScalarValue::from_df_scalar_value(v))
}
};
Ok(columnar_value)
}
}
/// A function's TypeSignature.
#[derive(Debug)]
pub enum TypeSignature {
/// exact number of arguments of an exact type
Exact(Vec<DatumKind>),
/// fixed number of arguments of an arbitrary but equal type out of a list
/// of valid types
// A function of one argument of double is `Uniform(1, vec![DatumKind::Double])`
// A function of one argument of double or uint64 is `Uniform(1, vec![DatumKind::Double,
// DatumKind::UInt64])`
Uniform(usize, Vec<DatumKind>),
/// One of a list of signatures
OneOf(Vec<TypeSignature>),
}
impl TypeSignature {
pub(crate) fn to_datafusion_signature(&self) -> DfSignature {
DfSignature::new(self.to_datafusion_type_signature(), Volatility::Immutable)
}
fn to_datafusion_type_signature(&self) -> DfTypeSignature {
match self {
TypeSignature::Exact(kinds) => {
let data_types = kinds.iter().map(|v| DataType::from(*v)).collect();
DfTypeSignature::Exact(data_types)
}
TypeSignature::Uniform(num, kinds) => {
let data_types = kinds.iter().map(|v| DataType::from(*v)).collect();
DfTypeSignature::Uniform(*num, data_types)
}
TypeSignature::OneOf(sigs) => {
let df_sigs = sigs
.iter()
.map(|v| v.to_datafusion_type_signature())
.collect();
DfTypeSignature::OneOf(df_sigs)
}
}
}
}
/// A scalar function's return type.
#[derive(Debug)]
pub struct ReturnType {
kind: DatumKind,
}
impl ReturnType {
pub(crate) fn to_datafusion_return_type(&self) -> ReturnTypeFunction {
let data_type = Arc::new(DataType::from(self.kind));
Arc::new(move |_| Ok(data_type.clone()))
}
}
pub struct ScalarFunction {
signature: TypeSignature,
return_type: ReturnType,
df_scalar_fn: ScalarFunctionImplementation,
}
impl ScalarFunction {
pub fn make_by_fn<F>(signature: TypeSignature, return_type: DatumKind, func: F) -> Self
where
F: Fn(&[ColumnarValue]) -> Result<ColumnarValue> + Send + Sync + 'static,
{
let return_type = ReturnType { kind: return_type };
// Adapter to map func to Fn(&[DfColumnarValue]) -> Result<DfColumnarValue>
let df_adapter = move |df_args: &[DfColumnarValue]| {
// Convert df_args from DfColumnarValue to ColumnarValue.
let mut values: SmallVec<[ColumnarValue; FUNC_ARG_NUM]> =
SmallVec::with_capacity(df_args.len());
for df_arg in df_args {
let value = ColumnarValue::try_from_df_columnar_value(df_arg).map_err(|e| {
DataFusionError::Internal(format!(
"Failed to convert datafusion columnar value, err:{e}"
))
})?;
values.push(value);
}
// Execute our function.
let result_value = func(&values).map_err(|e| {
DataFusionError::Execution(format!("Failed to execute function, err:{e}"))
})?;
// Convert the result value to DfColumnarValue.
Ok(result_value.into_df_columnar_value())
};
let df_scalar_fn = Arc::new(df_adapter);
Self {
signature,
return_type,
df_scalar_fn,
}
}
#[inline]
pub fn signature(&self) -> &TypeSignature {
&self.signature
}
#[inline]
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
#[inline]
pub(crate) fn to_datafusion_function(&self) -> ScalarFunctionImplementation {
self.df_scalar_fn.clone()
}
}
pub struct AggregateFunction {
type_signature: TypeSignature,
return_type: ReturnType,
df_accumulator: AccumulatorFactoryFunction,
state_type: Vec<DatumKind>,
}
impl AggregateFunction {
pub fn make_by_fn<F, A>(
type_signature: TypeSignature,
return_type: DatumKind,
state_type: Vec<DatumKind>,
accumulator_fn: F,
) -> Self
where
F: Fn(&DataType) -> Result<A> + Send + Sync + 'static,
A: Accumulator + 'static,
{
// Create accumulator.
let df_adapter = move |data_type: &DataType| {
let accumulator = accumulator_fn(data_type).map_err(|e| {
DataFusionError::Execution(format!("Failed to create accumulator, err:{e}"))
})?;
let accumulator = Box::new(ToDfAccumulator::new(accumulator));
Ok(accumulator as _)
};
let df_accumulator = Arc::new(df_adapter);
// Create return type.
let return_type = ReturnType { kind: return_type };
Self {
type_signature,
return_type,
df_accumulator,
state_type,
}
}
#[inline]
pub fn signature(&self) -> &TypeSignature {
&self.type_signature
}
#[inline]
pub fn return_type(&self) -> &ReturnType {
&self.return_type
}
#[inline]
pub(crate) fn to_datafusion_accumulator(&self) -> AccumulatorFactoryFunction {
self.df_accumulator.clone()
}
pub(crate) fn to_datafusion_state_type(&self) -> StateTypeFunction {
let data_types = Arc::new(
self.state_type
.iter()
.map(|kind| DataType::from(*kind))
.collect::<Vec<_>>(),
);
Arc::new(move |_| Ok(data_types.clone()))
}
}