Skip to content

Commit

Permalink
switch implementation of network to use ScalarUDFImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
dadepo committed Apr 20, 2024
1 parent 9837e6b commit efe11be
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
19 changes: 3 additions & 16 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ 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, inet_merge, inet_same_family, masklen, netmask, network,
Masklen,
broadcast, family, host, hostmask, inet_merge, inet_same_family, masklen, netmask, Masklen,
Network,
};

mod math_udfs;
Expand Down Expand Up @@ -51,7 +51,7 @@ fn register_network_udfs(ctx: &SessionContext) -> Result<()> {
register_inet_merge(ctx);
register_masklen(ctx);
register_netmask(ctx);
register_network(ctx);
ctx.register_udf(ScalarUDF::from(Network::new()));
ctx.register_udf(ScalarUDF::from(Masklen::new()));
Ok(())
}
Expand Down Expand Up @@ -159,16 +159,3 @@ fn register_netmask(ctx: &SessionContext) {

ctx.register_udf(netmask_udf);
}

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

ctx.register_udf(network_udf);
}
70 changes: 53 additions & 17 deletions src/postgres/network_udfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,25 +301,61 @@ pub fn netmask(args: &[ArrayRef]) -> Result<ArrayRef> {

/// Extracts network part of address.
/// Returns NULL for columns with NULL values.
pub fn network(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 network = IpNet::from_str(ip_string)
.map_err(|e| {
DataFusionError::Internal(format!("Parsing {ip_string} failed with error {e}"))
})?
.network();
string_builder.append_value(network.to_string());
Ok::<(), DataFusionError>(())
} else {
string_builder.append_null();
Ok::<(), DataFusionError>(())
#[derive(Debug)]
pub struct Network {
signature: Signature,
}

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

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

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

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

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 network = IpNet::from_str(ip_string)
.map_err(|e| {
DataFusionError::Internal(format!(
"Parsing {ip_string} failed with error {e}"
))
})?
.network();
string_builder.append_value(network.to_string());
Ok::<(), DataFusionError>(())
} else {
string_builder.append_null();
Ok::<(), DataFusionError>(())
}
})?;

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

/// Sets the netmask length.
Expand Down

0 comments on commit efe11be

Please sign in to comment.