-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT]: dyn function registry (#2466)
this adds a new variant to `Expr`. `Expr::ScalarFunction` which is pretty similar to `FunctionExpr`, but uses dynamic dispatch and a registry instead of the enum variants. The registry is inspired by datafusion's function registry. when an expr is serialized, it just serializes the name and the inputs. for example `col("text").hash(seed=42)` ```js { "name": "hash", "inputs": [ `col('text')`, // serialized repr of this `lit(42)` // serialized repr of this ] } ``` then when deserializing, it just fetches the appropriate function from the registry. _(errorring if no matches found)_. Also just to make sure everything works, I refactored the `hash` function to use the new paradigm.
- Loading branch information
1 parent
ecebb82
commit cf9a09b
Showing
13 changed files
with
309 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::sync::Arc; | ||
|
||
use common_error::{DaftError, DaftResult}; | ||
use dashmap::DashMap; | ||
|
||
use super::{hash::HashFunction, ScalarUDF}; | ||
|
||
lazy_static::lazy_static! { | ||
pub static ref REGISTRY: Registry = Registry::new(); | ||
} | ||
|
||
pub struct Registry { | ||
functions: DashMap<&'static str, Arc<dyn ScalarUDF>>, | ||
} | ||
|
||
impl Registry { | ||
fn new() -> Self { | ||
let iter: Vec<Arc<dyn ScalarUDF>> = vec![Arc::new(HashFunction {})]; | ||
|
||
let functions = iter.into_iter().map(|f| (f.name(), f)).collect(); | ||
|
||
Self { functions } | ||
} | ||
pub fn register(&mut self, function: Arc<dyn ScalarUDF>) -> DaftResult<()> { | ||
if self.functions.contains_key(function.name()) { | ||
Err(DaftError::ValueError(format!( | ||
"function {} already exists", | ||
function.name() | ||
))) | ||
} else { | ||
self.functions.insert(function.name(), function); | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn get(&self, name: &str) -> Option<Arc<dyn ScalarUDF>> { | ||
self.functions.get(name).map(|f| f.value().clone()) | ||
} | ||
|
||
pub fn names(&self) -> Vec<&'static str> { | ||
self.functions.iter().map(|pair| pair.name()).collect() | ||
} | ||
} |
Oops, something went wrong.