Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions c/sedona-geos/benches/geos-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,34 @@ fn criterion_benchmark(c: &mut Criterion) {
"st_within",
ArrayScalar(Polygon(10), Polygon(500)),
);
benchmark::scalar(
c,
&f,
"geos",
"st_crosses",
ArrayScalar(Polygon(10), Polygon(10)),
);
benchmark::scalar(
c,
&f,
"geos",
"st_crosses",
ArrayScalar(Polygon(10), Polygon(500)),
);
benchmark::scalar(
c,
&f,
"geos",
"st_overlaps",
ArrayScalar(Polygon(10), Polygon(10)),
);
benchmark::scalar(
c,
&f,
"geos",
"st_overlaps",
ArrayScalar(Polygon(10), Polygon(500)),
);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
89 changes: 87 additions & 2 deletions c/sedona-geos/src/binary_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use std::sync::Arc;

use crate::geos::{
BinaryPredicate, Contains, CoveredBy, Covers, Disjoint, Equals, GeosPredicate, Intersects,
Touches, Within,
BinaryPredicate, Contains, CoveredBy, Covers, Crosses, Disjoint, Equals, GeosPredicate,
Intersects, Overlaps, Touches, Within,
};
use arrow_array::builder::BooleanBuilder;
use arrow_schema::DataType;
Expand Down Expand Up @@ -61,6 +61,14 @@ pub fn st_within_impl() -> ScalarKernelRef {
Arc::new(GeosPredicate::<Within>::default())
}

pub fn st_crosses_impl() -> ScalarKernelRef {
Arc::new(GeosPredicate::<Crosses>::default())
}

pub fn st_overlaps_impl() -> ScalarKernelRef {
Arc::new(GeosPredicate::<Overlaps>::default())
}

impl<Op: BinaryPredicate> SedonaScalarKernel for GeosPredicate<Op> {
fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
let matcher: ArgMatcher = ArgMatcher::new(
Expand Down Expand Up @@ -377,4 +385,81 @@ mod tests {
let expected: ArrayRef = arrow_array!(Boolean, [Some(true), Some(false), None]);
assert_array_equal(&tester.invoke_array_array(arg1, arg2).unwrap(), &expected);
}

#[rstest]
fn crosses_udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) {
use datafusion_common::ScalarValue;
let udf = SedonaScalarUDF::from_kernel("st_crosses", st_crosses_impl());
let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone(), sedona_type]);
tester.assert_return_type(DataType::Boolean);

let result = tester
.invoke_scalar_scalar("LINESTRING (0 0, 1 1)", "LINESTRING (0 1, 1 0)")
.unwrap();
tester.assert_scalar_result_equals(result, true);

let result = tester
.invoke_scalar_scalar(ScalarValue::Null, ScalarValue::Null)
.unwrap();
assert!(result.is_null());

let arg1 = create_array(
&[
Some("LINESTRING (0 0, 1 1)"),
Some("LINESTRING (0 0, 1 0)"),
None,
],
&WKB_GEOMETRY,
);
let arg2 = create_array(
&[
Some("LINESTRING (0 1, 1 0)"),
Some("POLYGON ((2 2, 2 3, 3 3, 3 2, 2 2))"),
Some("LINESTRING (0 0, 1 1)"),
],
&WKB_GEOMETRY,
);
let expected: ArrayRef = arrow_array!(Boolean, [Some(true), Some(false), None]);
assert_array_equal(&tester.invoke_array_array(arg1, arg2).unwrap(), &expected);
}

#[rstest]
fn overlaps_udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) {
use datafusion_common::ScalarValue;
let udf = SedonaScalarUDF::from_kernel("st_overlaps", st_overlaps_impl());
let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone(), sedona_type]);
tester.assert_return_type(DataType::Boolean);

let result = tester
.invoke_scalar_scalar(
"POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))",
"POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
)
.unwrap();
tester.assert_scalar_result_equals(result, true);

let result = tester
.invoke_scalar_scalar(ScalarValue::Null, ScalarValue::Null)
.unwrap();
assert!(result.is_null());

let arg1 = create_array(
&[
Some("POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))"),
Some("LINESTRING (0 0, 2 0)"),
None,
],
&WKB_GEOMETRY,
);
let arg2 = create_array(
&[
Some("POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))"),
Some("LINESTRING (1 0, 3 0)"),
Some("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"),
],
&WKB_GEOMETRY,
);
let expected: ArrayRef = arrow_array!(Boolean, [Some(true), Some(true), None]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test would be more useful if one of the results were false

assert_array_equal(&tester.invoke_array_array(arg1, arg2).unwrap(), &expected);
}
}
18 changes: 18 additions & 0 deletions c/sedona-geos/src/geos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,21 @@ impl BinaryPredicate for Touches {
lhs.touches(rhs)
}
}

/// Check if the geometries crosses
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
/// Check if the geometries crosses
/// Check if the geometries cross

#[derive(Debug, Default)]
pub struct Crosses {}
impl BinaryPredicate for Crosses {
fn evaluate(lhs: &Geometry, rhs: &Geometry) -> GResult<bool> {
lhs.crosses(rhs)
}
}

/// Check if the geometries overlaps
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
/// Check if the geometries overlaps
/// Check if the geometries overlap

#[derive(Debug, Default)]
pub struct Overlaps {}
impl BinaryPredicate for Overlaps {
fn evaluate(lhs: &Geometry, rhs: &Geometry) -> GResult<bool> {
lhs.overlaps(rhs)
}
}
6 changes: 4 additions & 2 deletions c/sedona-geos/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use crate::{
};

use crate::binary_predicates::{
st_contains_impl, st_covered_by_impl, st_covers_impl, st_disjoint_impl, st_equals_impl,
st_intersects_impl, st_touches_impl, st_within_impl,
st_contains_impl, st_covered_by_impl, st_covers_impl, st_crosses_impl, st_disjoint_impl,
st_equals_impl, st_intersects_impl, st_overlaps_impl, st_touches_impl, st_within_impl,
};

use crate::overlay::{
Expand Down Expand Up @@ -54,5 +54,7 @@ pub fn scalar_kernels() -> Vec<(&'static str, ScalarKernelRef)> {
("st_touches", st_touches_impl()),
("st_union", st_union_impl()),
("st_within", st_within_impl()),
("st_crosses", st_crosses_impl()),
("st_overlaps", st_overlaps_impl()),
]
}