Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datafusion: GEOS-provided boolean ops #948

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions rust/geoarrow/src/array/geometry/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,30 @@ impl TryFrom<GeometryArray> for MixedGeometryArray {
}
}

impl From<RectArray> for GeometryArray {
fn from(value: RectArray) -> Self {
PolygonArray::from(value).into()
}
}

impl From<Arc<dyn NativeArray>> for GeometryArray {
fn from(value: Arc<dyn NativeArray>) -> Self {
use NativeType::*;

match value.data_type() {
Point(_, _) => value.as_ref().as_point().clone().into(),
LineString(_, _) => value.as_ref().as_line_string().clone().into(),
Polygon(_, _) => value.as_ref().as_polygon().clone().into(),
MultiPoint(_, _) => value.as_ref().as_multi_point().clone().into(),
MultiLineString(_, _) => value.as_ref().as_multi_line_string().clone().into(),
MultiPolygon(_, _) => value.as_ref().as_multi_polygon().clone().into(),
Geometry(_) => value.as_ref().as_geometry().clone(),
GeometryCollection(_, _) => value.as_ref().as_geometry_collection().clone().into(),
Rect(_) => value.as_ref().as_rect().clone().into(),
}
}
}

/// Default to an empty array
impl Default for GeometryArray {
fn default() -> Self {
Expand Down
2 changes: 2 additions & 0 deletions rust/geodatafusion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ description = "Rust implementation of GeoArrow"
categories = ["science::geo"]
rust-version = "1.82"

[features]
geos = ["geoarrow/geos"]

[dependencies]
datafusion = { git = "https://github.com/apache/datafusion", rev = "03e39da62e403e064d21b57e9d6c200464c03749" }
Expand Down
20 changes: 20 additions & 0 deletions rust/geodatafusion/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ pub(crate) fn any_single_geometry_type_input() -> Signature {
)
}

pub(crate) fn any_two_geometry_type_input() -> Signature {
// TODO: not sure if this is correct. We want the types to vary and each one can be a different
// type.
Signature::uniform(
2,
vec![
POINT2D_TYPE.into(),
POINT3D_TYPE.into(),
BOX2D_TYPE.into(),
BOX3D_TYPE.into(),
GEOMETRY_TYPE.into(),
],
Volatility::Immutable,
)
}

/// This will not cast a PointArray to a GeometryArray
pub(crate) fn parse_to_native_array(array: ArrayRef) -> GeoDataFusionResult<Arc<dyn NativeArray>> {
let data_type = array.data_type();
Expand All @@ -50,3 +66,7 @@ pub(crate) fn parse_to_native_array(array: ArrayRef) -> GeoDataFusionResult<Arc<
Err(DataFusionError::Execution(format!("Unexpected input data type: {}", data_type)).into())
}
}

pub(crate) fn parse_to_geometry_array(array: ArrayRef) -> GeoDataFusionResult<GeometryArray> {
Ok(parse_to_native_array(array)?.into())
}
2 changes: 2 additions & 0 deletions rust/geodatafusion/src/udf/geos/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
//! User-defined functions that wrap the [geos] crate.

mod spatial_relationships;
87 changes: 87 additions & 0 deletions rust/geodatafusion/src/udf/geos/spatial_relationships/contains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::any::Any;
use std::sync::{Arc, OnceLock};

use arrow_schema::DataType;
use datafusion::logical_expr::scalar_doc_sections::DOC_SECTION_OTHER;
use datafusion::logical_expr::{ColumnarValue, Documentation, ScalarUDFImpl, Signature};
use geoarrow::algorithm::geos::{BooleanOps, BooleanOpsScalar};
use geoarrow::trait_::ArrayAccessor;
use geoarrow::ArrayBase;

use crate::data_types::{
any_single_geometry_type_input, any_two_geometry_type_input, parse_to_geometry_array,
parse_to_native_array, GEOMETRY_TYPE,
};
use crate::error::GeoDataFusionResult;

#[derive(Debug)]
pub(super) struct Contains {
signature: Signature,
}

impl Contains {
pub fn new() -> Self {
Self {
signature: any_two_geometry_type_input(),
}
}
}

static CONTAINS_DOC: OnceLock<Documentation> = OnceLock::new();

impl ScalarUDFImpl for Contains {
fn as_any(&self) -> &dyn Any {
self
}

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

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

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

fn invoke(&self, args: &[ColumnarValue]) -> datafusion::error::Result<ColumnarValue> {
Ok(contains_impl(args)?)
}

fn documentation(&self) -> Option<&Documentation> {
Some(CONTAINS_DOC.get_or_init(|| {
Documentation::builder(
DOC_SECTION_OTHER,
"Returns TRUE if geometry A contains geometry B.",
"ST_Contains(geometry)",
)
.with_argument("geomA", "geometry")
.with_argument("geomB", "geometry")
.build()
}))
}
}

fn contains_impl(args: &[ColumnarValue]) -> GeoDataFusionResult<ColumnarValue> {
let left = ColumnarValue::values_to_arrays(&args[0..1])?
.into_iter()
.next()
.unwrap();

let left = parse_to_geometry_array(left)?;

let out = match &args[1] {
ColumnarValue::Array(arr) => {
let right = parse_to_geometry_array(arr.clone())?;
BooleanOps::contains(&left, &right)?
}
ColumnarValue::Scalar(scalar) => {
let right = parse_to_geometry_array(scalar.to_array()?)?;
let right = right.value(0);
BooleanOpsScalar::contains(&left, &right)?
}
};
Ok(ColumnarValue::Array(Arc::new(out)))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod contains;
1 change: 1 addition & 0 deletions rust/geodatafusion/src/udf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[cfg(feature = "geos")]
pub mod geos;
pub mod native;
Loading