Skip to content

Commit

Permalink
switch implementation of asind to use ScalarUDFImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
dadepo committed Apr 20, 2024
1 parent c5a03a6 commit 3c0c71b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
70 changes: 52 additions & 18 deletions src/postgres/math_udfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,61 @@ pub fn cotd(args: &[ArrayRef]) -> Result<ArrayRef> {
}

/// Inverse sine, result in degrees.
pub fn asind(args: &[ArrayRef]) -> Result<ArrayRef> {
let values = datafusion::common::cast::as_float64_array(&args[0])?;
let mut float64array_builder = Float64Array::builder(args[0].len());
#[derive(Debug)]
pub struct Asind {
signature: Signature,
}

values.iter().try_for_each(|value| {
if let Some(value) = value {
if value > 1.0 {
return Err(DataFusionError::Internal(
"input is out of range".to_string(),
));
}
let result = value.asin().to_degrees();
float64array_builder.append_value(result);
Ok::<(), DataFusionError>(())
} else {
float64array_builder.append_null();
Ok::<(), DataFusionError>(())
impl Asind {
pub fn new() -> Self {
Self {
signature: Signature::uniform(1, vec![Float64], Volatility::Immutable),
}
})?;
}
}

Ok(Arc::new(float64array_builder.finish()) as ArrayRef)
impl ScalarUDFImpl for Asind {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn name(&self) -> &str {
"asind"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(Float64)
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
let args = ColumnarValue::values_to_arrays(args)?;
let values = datafusion::common::cast::as_float64_array(&args[0])?;
let mut float64array_builder = Float64Array::builder(args[0].len());

values.iter().try_for_each(|value| {
if let Some(value) = value {
if value > 1.0 {
return Err(DataFusionError::Internal(
"input is out of range".to_string(),
));
}
let result = value.asin().to_degrees();
float64array_builder.append_value(result);
Ok::<(), DataFusionError>(())
} else {
float64array_builder.append_null();
Ok::<(), DataFusionError>(())
}
})?;

Ok(ColumnarValue::Array(
Arc::new(float64array_builder.finish()) as ArrayRef,
))
}
}

/// Sine, argument in degrees.
Expand Down
17 changes: 2 additions & 15 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use datafusion::physical_expr::functions::make_scalar_function;
use datafusion::prelude::SessionContext;

use crate::postgres::math_udfs::{
acosd, asind, cosd, cotd, Atand, Ceiling, Div, Erf, Erfc, RandomNormal, Sind, Tand,
acosd, cosd, cotd, Asind, Atand, Ceiling, Div, Erf, Erfc, RandomNormal, Sind, Tand,
};
use crate::postgres::network_udfs::{
broadcast, family, host, hostmask, inet_merge, inet_same_family, masklen, netmask, network,
Expand All @@ -30,7 +30,7 @@ fn register_math_udfs(ctx: &SessionContext) -> Result<()> {
register_acosd(ctx);
register_cosd(ctx);
register_cotd(ctx);
register_asind(ctx);
ctx.register_udf(ScalarUDF::from(Asind::new()));
ctx.register_udf(ScalarUDF::from(Sind::new()));
ctx.register_udf(ScalarUDF::from(Atand::new()));
ctx.register_udf(ScalarUDF::from(Tand::new()));
Expand All @@ -55,19 +55,6 @@ fn register_acosd(ctx: &SessionContext) {
ctx.register_udf(acosd_udf);
}

fn register_asind(ctx: &SessionContext) {
let asind_udf = make_scalar_function(asind);
let return_type: ReturnTypeFunction = Arc::new(move |_| Ok(Arc::new(Float64)));
let asind_udf = ScalarUDF::new(
"asind",
&Signature::uniform(1, vec![Float64], Volatility::Immutable),
&return_type,
&asind_udf,
);

ctx.register_udf(asind_udf);
}

fn register_cosd(ctx: &SessionContext) {
let cosd_udf = make_scalar_function(cosd);
let return_type: ReturnTypeFunction = Arc::new(move |_| Ok(Arc::new(Float64)));
Expand Down

0 comments on commit 3c0c71b

Please sign in to comment.