Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
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 (2 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(false), None]);
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 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 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()),
]
}
83 changes: 83 additions & 0 deletions python/sedonadb/tests/functions/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,86 @@ def test_st_within_skipped(eng, geom1, geom2, expected):
f"SELECT ST_Within({geom_or_null(geom1)}, {geom_or_null(geom2)})",
expected,
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize(
("geom1", "geom2", "expected"),
[
(None, None, None),
("POINT (0 0)", None, None),
(None, "POINT (0 0)", None),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
(None, "POINT (0 0)", None),
(None, "POINT (0 0)", None),
("POINT (0 0)", "POINT EMPTY", False),

How about we add one more case (empty geometries) while we're here. I checked out the branch and tested these locally, and saw that they already pass.

("POINT (0 0)", "POINT (0 0)", False),
("POINT (0.5 0.5)", "LINESTRING (0 0, 1 1)", False),
("POINT (0 0)", "LINESTRING (0 0, 1 1)", False),
("POINT (0.5 0.5)", "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", False),
("POINT (0 0)", "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", False),
("LINESTRING (0 0, 1 1)", "LINESTRING (0 1, 1 0)", True),
("LINESTRING (0 0, 1 1)", "LINESTRING (1 1, 2 2)", False),
("LINESTRING (0 0, 2 2)", "LINESTRING (1 1, 3 3)", False),
("LINESTRING (-1 -1, 1 1)", "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", True),
("LINESTRING (-1 0, 0 0)", "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", False),
(
"LINESTRING (0.1 0.1, 0.5 0.5)",
"POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
False,
),
(
"POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))",
"POLYGON ((1 1, 3 1, 3 3, 1 3, 1 1))",
False,
),
],
)
def test_st_crosses(eng, geom1, geom2, expected):
eng = eng.create_or_skip()
eng.assert_query_result(
f"SELECT ST_Crosses({geom_or_null(geom1)}, {geom_or_null(geom2)})",
expected,
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize(
("geom1", "geom2", "expected"),
[
(None, None, None),
("POINT (0 0)", None, None),
(None, "POINT (0 0)", None),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
(None, "POINT (0 0)", None),
(None, "POINT (0 0)", None),
("POINT (0 0)", "POINT EMPTY", False),

("POINT (0 0)", "LINESTRING (0 0, 1 1)", False),
("LINESTRING (0 0, 2 2)", "POLYGON ((1 1, 3 1, 3 3, 1 3, 1 1))", False),
("MULTIPOINT ((0 0), (1 1))", "MULTIPOINT ((1 1), (2 2))", True),
("MULTIPOINT ((0 0), (1 1))", "MULTIPOINT ((0 0), (1 1))", False),
("POINT (0 0)", "POINT (0 0)", False),
("LINESTRING (0 0, 2 2)", "LINESTRING (1 1, 3 3)", True),
("LINESTRING (0 0, 1 1)", "LINESTRING (0 1, 1 0)", False),
("LINESTRING (0 0, 1 1)", "LINESTRING (1 1, 2 2)", False),
Comment on lines +412 to +415
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like that we have some good edge cases here like identical geometries ("POINT (0 0)", "POINT (0 0)") and "touching geometries" (i.e. "LINESTRING (0 0, 1 1)", "LINESTRING (1 1, 2 2)")

("LINESTRING (0 0, 1 1)", "LINESTRING (0 0, 1 1)", False),
(
"POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))",
"POLYGON ((1 1, 3 1, 3 3, 1 3, 1 1))",
True,
),
(
"POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
"POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))",
False,
),
(
"POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
"POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
False,
),
(
"POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0))",
"POLYGON ((1 1, 2 1, 2 2, 1 2, 1 1))",
False,
),
],
)
def test_st_overlaps(eng, geom1, geom2, expected):
eng = eng.create_or_skip()
eng.assert_query_result(
f"SELECT ST_Overlaps({geom_or_null(geom1)}, {geom_or_null(geom2)})",
expected,
)