Skip to content

Commit

Permalink
switch implementation of broadcast to use ScalarUDFImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
dadepo committed Apr 20, 2024
1 parent 89457c6 commit ebb1a77
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
23 changes: 3 additions & 20 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#![cfg(feature = "postgres")]
#![allow(deprecated)]

use std::sync::Arc;

use datafusion::arrow::datatypes::DataType::Utf8;
use datafusion::error::Result;
use datafusion::logical_expr::{ReturnTypeFunction, ScalarUDF, Signature, Volatility};
use datafusion::physical_expr::functions::make_scalar_function;
use datafusion::logical_expr::ScalarUDF;
use datafusion::prelude::SessionContext;

use crate::postgres::math_udfs::{
Acosd, Asind, Atand, Ceiling, Cosd, Cotd, Div, Erf, Erfc, RandomNormal, Sind, Tand,
};
use crate::postgres::network_udfs::{
broadcast, Family, Host, HostMask, InetMerge, InetSameFamily, MaskLen, Netmask, Network,
Broadcast, Family, Host, HostMask, InetMerge, InetSameFamily, MaskLen, Netmask, Network,
SetMaskLen,
};

Expand Down Expand Up @@ -43,7 +39,7 @@ fn register_math_udfs(ctx: &SessionContext) -> Result<()> {
}

fn register_network_udfs(ctx: &SessionContext) -> Result<()> {
register_broadcast(ctx);
ctx.register_udf(ScalarUDF::from(Broadcast::new()));
ctx.register_udf(ScalarUDF::from(Family::new()));
ctx.register_udf(ScalarUDF::from(Host::new()));
ctx.register_udf(ScalarUDF::from(HostMask::new()));
Expand All @@ -55,16 +51,3 @@ fn register_network_udfs(ctx: &SessionContext) -> Result<()> {
ctx.register_udf(ScalarUDF::from(SetMaskLen::new()));
Ok(())
}

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

ctx.register_udf(broadcast_udf);
}
70 changes: 53 additions & 17 deletions src/postgres/network_udfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,61 @@ use ipnet::{IpNet, Ipv4Net, Ipv6Net};

/// Gives the broadcast address for the network.
/// Returns NULL for columns with NULL values.
pub fn broadcast(args: &[ArrayRef]) -> Result<ArrayRef> {
let mut string_builder = StringBuilder::with_capacity(args[0].len(), u8::MAX as usize);
let ip_string = datafusion::common::cast::as_string_array(&args[0])?;
ip_string.iter().try_for_each(|ip_string| {
if let Some(ip_string) = ip_string {
let broadcast_address = IpNet::from_str(ip_string)
.map_err(|e| {
DataFusionError::Internal(format!("Parsing {ip_string} failed with error {e}"))
})?
.broadcast();
string_builder.append_value(broadcast_address.to_string());
Ok::<(), DataFusionError>(())
} else {
string_builder.append_null();
Ok::<(), DataFusionError>(())
#[derive(Debug)]
pub struct Broadcast {
signature: Signature,
}

impl Broadcast {
pub fn new() -> Self {
Self {
signature: Signature::uniform(1, vec![Utf8], Volatility::Immutable),
}
})?;
}
}

impl ScalarUDFImpl for Broadcast {
fn as_any(&self) -> &dyn std::any::Any {
self
}

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

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

Ok(Arc::new(string_builder.finish()) as ArrayRef)
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(Utf8)
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
let args = ColumnarValue::values_to_arrays(args)?;
let mut string_builder = StringBuilder::with_capacity(args[0].len(), u8::MAX as usize);
let ip_string = datafusion::common::cast::as_string_array(&args[0])?;
ip_string.iter().try_for_each(|ip_string| {
if let Some(ip_string) = ip_string {
let broadcast_address = IpNet::from_str(ip_string)
.map_err(|e| {
DataFusionError::Internal(format!(
"Parsing {ip_string} failed with error {e}"
))
})?
.broadcast();
string_builder.append_value(broadcast_address.to_string());
Ok::<(), DataFusionError>(())
} else {
string_builder.append_null();
Ok::<(), DataFusionError>(())
}
})?;

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

/// Returns the address's family: 4 for IPv4, 6 for IPv6.
Expand Down

0 comments on commit ebb1a77

Please sign in to comment.