-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add user defined functions: md5, sqrt and stddev
- Loading branch information
Showing
6 changed files
with
316 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use rusqlite::functions::{Context, Aggregate}; | ||
use rusqlite::Result; | ||
use statistical::population_standard_deviation; | ||
|
||
pub(crate) fn calculate_md5(ctx: &Context) -> Result<String> { | ||
assert_eq!(ctx.len(), 1, "called with unexpected number of arguments"); | ||
let str = ctx.get_raw(0).as_str()?; | ||
let hash = md5::compute(str); | ||
Ok(format!("{:x}", hash)) | ||
} | ||
|
||
pub(crate) fn calculate_sqrt(ctx: &Context) -> Result<f64> { | ||
assert_eq!(ctx.len(), 1, "called with unexpected number of arguments"); | ||
let arg = ctx.get_raw(0); | ||
if let Ok(f64) = arg.as_f64() { | ||
Ok(f64.sqrt()) | ||
} else { | ||
let i64 = arg.as_i64()?; | ||
Ok((i64 as f64).sqrt()) | ||
} | ||
} | ||
pub struct Stddev; | ||
|
||
impl Aggregate<Vec<f64>, Option<f64>> for Stddev { | ||
fn init(&self, _: &mut Context<'_>) -> Result<Vec<f64>> { | ||
Ok(vec!()) | ||
} | ||
|
||
fn step(&self, ctx: &mut Context<'_>, stdev: &mut Vec<f64>) -> Result<()> { | ||
let next = ctx.get::<f64>(0)?; | ||
stdev.push(next); | ||
Ok(()) | ||
} | ||
|
||
fn finalize(&self, _: &mut Context<'_>, numbers: Option<Vec<f64>>) -> Result<Option<f64>> { | ||
println!("{:?}", &numbers); | ||
let stddev = numbers.map(|n| population_standard_deviation(&n, None)); | ||
println!("{:?}", stddev); | ||
Ok(stddev) | ||
} | ||
} | ||
|
Oops, something went wrong.