forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udf.rs
155 lines (141 loc) · 5.58 KB
/
udf.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use crate::error::{DataFusionError, Result};
use crate::physical_plan::udaf::AggregateUDF;
use crate::physical_plan::udf::ScalarUDF;
use crate::plugin::plugin_manager::global_plugin_manager;
use crate::plugin::{Plugin, PluginEnum, PluginRegistrar};
use libloading::{Library, Symbol};
use std::any::Any;
use std::collections::HashMap;
use std::io;
use std::sync::Arc;
/// 定义udf插件,udf的定义方需要实现该trait
pub trait UDFPlugin: Plugin {
/// get a ScalarUDF by name
fn get_scalar_udf_by_name(&self, fun_name: &str) -> Result<ScalarUDF>;
/// return all udf names in the plugin
fn udf_names(&self) -> Result<Vec<String>>;
/// get a aggregate udf by name
fn get_aggregate_udf_by_name(&self, fun_name: &str) -> Result<AggregateUDF>;
/// return all udaf names
fn udaf_names(&self) -> Result<Vec<String>>;
}
/// UDFPluginManager
#[derive(Default, Clone)]
pub struct UDFPluginManager {
/// scalar udfs
pub scalar_udfs: HashMap<String, Arc<ScalarUDF>>,
/// aggregate udfs
pub aggregate_udfs: HashMap<String, Arc<AggregateUDF>>,
/// All libraries load from the plugin dir.
pub libraries: Vec<Arc<Library>>,
}
impl PluginRegistrar for UDFPluginManager {
unsafe fn load(&mut self, library: Arc<Library>) -> Result<()> {
type PluginRegister = unsafe fn() -> Box<dyn UDFPlugin>;
let register_fun: Symbol<PluginRegister> =
library.get(b"registrar_udf_plugin\0").map_err(|e| {
DataFusionError::IoError(io::Error::new(
io::ErrorKind::Other,
format!("not found fn registrar_udf_plugin in the library: {}", e),
))
})?;
let udf_plugin: Box<dyn UDFPlugin> = register_fun();
udf_plugin
.udf_names()
.unwrap()
.iter()
.try_for_each(|udf_name| {
println!("udf_name: {}", udf_name);
if self.scalar_udfs.contains_key(udf_name) {
println!("udf_name exists");
Err(DataFusionError::IoError(io::Error::new(
io::ErrorKind::Other,
format!("udf name: {} already exists", udf_name),
)))
} else {
let scalar_udf = udf_plugin.get_scalar_udf_by_name(udf_name)?;
self.scalar_udfs
.insert(udf_name.to_string(), Arc::new(scalar_udf));
Ok(())
}
})?;
udf_plugin
.udaf_names()
.unwrap()
.iter()
.try_for_each(|udaf_name| {
println!("udaf_name: {}", udaf_name);
if self.aggregate_udfs.contains_key(udaf_name) {
println!("udaf_name exists");
Err(DataFusionError::IoError(io::Error::new(
io::ErrorKind::Other,
format!("udaf name: {} already exists", udaf_name),
)))
} else {
let aggregate_udf =
udf_plugin.get_aggregate_udf_by_name(udaf_name)?;
self.aggregate_udfs
.insert(udaf_name.to_string(), Arc::new(aggregate_udf));
Ok(())
}
})?;
Ok(())
}
fn as_any(&self) -> &dyn Any {
self
}
}
/// Declare a udf plugin registrar callback
///
/// # Notes
///
/// This works by automatically generating an `extern "C"` function named `registrar_udf_plugin` with a
/// pre-defined signature and symbol name.
/// Therefore you will only be able to declare one plugin per library.
#[macro_export]
macro_rules! declare_udf_plugin {
($curr_plugin_type:ty, $constructor:path) => {
#[no_mangle]
pub extern "C" fn registrar_udf_plugin() -> Box<dyn $crate::plugin::udf::UDFPlugin> {
// make sure the constructor is the correct type.
let constructor: fn() -> $curr_plugin_type = $constructor;
let object = constructor();
Box::new(object)
}
$crate::declare_plugin!($crate::plugin::PluginEnum::UDF);
};
}
/// get a Option of Immutable UDFPluginManager
pub fn get_udf_plugin_manager(path: &str) -> Option<UDFPluginManager> {
let udf_plugin_manager_opt = {
let gpm = global_plugin_manager(path).lock().unwrap();
let plugin_registrar_opt = gpm.plugin_managers.get(&PluginEnum::UDF);
if let Some(plugin_registrar) = plugin_registrar_opt {
if let Some(udf_plugin_manager) =
plugin_registrar.as_any().downcast_ref::<UDFPluginManager>()
{
return Some(udf_plugin_manager.clone());
} else {
return None;
}
}
None
};
udf_plugin_manager_opt
}