From c5a03a605932fb3b8aa285cb05f1586756ea42d4 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sat, 20 Apr 2024 16:36:36 +0400 Subject: [PATCH] switch implementation of sind to use ScalarUDFImpl --- src/postgres/math_udfs.rs | 60 ++++++++++++++++++++++++++++++--------- src/postgres/mod.rs | 17 ++--------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/postgres/math_udfs.rs b/src/postgres/math_udfs.rs index 92f7974..b29f407 100644 --- a/src/postgres/math_udfs.rs +++ b/src/postgres/math_udfs.rs @@ -100,22 +100,56 @@ pub fn asind(args: &[ArrayRef]) -> Result { } /// Sine, argument in degrees. -pub fn sind(args: &[ArrayRef]) -> Result { - let values = datafusion::common::cast::as_float64_array(&args[0])?; - let mut float64array_builder = Float64Array::builder(args[0].len()); +#[derive(Debug)] +pub struct Sind { + signature: Signature, +} - values.iter().try_for_each(|value| { - if let Some(value) = value { - let result = value.to_radians().sin(); - float64array_builder.append_value(result); - Ok::<(), DataFusionError>(()) - } else { - float64array_builder.append_null(); - Ok::<(), DataFusionError>(()) +impl Sind { + pub fn new() -> Self { + Self { + signature: Signature::uniform(1, vec![Float64], Volatility::Immutable), } - })?; + } +} - Ok(Arc::new(float64array_builder.finish()) as ArrayRef) +impl ScalarUDFImpl for Sind { + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn name(&self) -> &str { + "sind" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result { + Ok(Float64) + } + + fn invoke(&self, args: &[ColumnarValue]) -> Result { + 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 { + let result = value.to_radians().sin(); + float64array_builder.append_value(result); + Ok::<(), DataFusionError>(()) + } else { + float64array_builder.append_null(); + Ok::<(), DataFusionError>(()) + } + })?; + + Ok(ColumnarValue::Array( + Arc::new(float64array_builder.finish()) as ArrayRef, + )) + } } /// Inverse tangent, result in degrees. diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index bd227a1..8869ee0 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -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, sind, Atand, Ceiling, Div, Erf, Erfc, RandomNormal, Tand, + acosd, asind, cosd, cotd, 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, @@ -31,7 +31,7 @@ fn register_math_udfs(ctx: &SessionContext) -> Result<()> { register_cosd(ctx); register_cotd(ctx); register_asind(ctx); - register_sind(ctx); + ctx.register_udf(ScalarUDF::from(Sind::new())); ctx.register_udf(ScalarUDF::from(Atand::new())); ctx.register_udf(ScalarUDF::from(Tand::new())); ctx.register_udf(ScalarUDF::from(Ceiling::new())); @@ -68,19 +68,6 @@ fn register_asind(ctx: &SessionContext) { ctx.register_udf(asind_udf); } -fn register_sind(ctx: &SessionContext) { - let sind_udf = make_scalar_function(sind); - let return_type: ReturnTypeFunction = Arc::new(move |_| Ok(Arc::new(Float64))); - let sind_udf = ScalarUDF::new( - "sind", - &Signature::uniform(1, vec![Float64], Volatility::Immutable), - &return_type, - &sind_udf, - ); - - ctx.register_udf(sind_udf); -} - fn register_cosd(ctx: &SessionContext) { let cosd_udf = make_scalar_function(cosd); let return_type: ReturnTypeFunction = Arc::new(move |_| Ok(Arc::new(Float64)));