Skip to content
Merged
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
2 changes: 2 additions & 0 deletions c/sedona-geos/benches/geos-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ fn criterion_benchmark(c: &mut Criterion) {
benchmark::scalar(c, &f, "geos", "st_centroid", Polygon(10));
benchmark::scalar(c, &f, "geos", "st_centroid", Polygon(500));

benchmark::scalar(c, &f, "geos", "st_convexhull", MultiPoint(10));

benchmark::scalar(
c,
&f,
Expand Down
1 change: 1 addition & 0 deletions c/sedona-geos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod register;
mod st_area;
mod st_buffer;
mod st_centroid;
mod st_convexhull;
mod st_dwithin;
mod st_length;
mod st_perimeter;
2 changes: 2 additions & 0 deletions c/sedona-geos/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.
use sedona_expr::scalar_udf::ScalarKernelRef;

use crate::st_convexhull::st_convex_hull_impl;
use crate::{
distance::st_distance_impl, st_area::st_area_impl, st_buffer::st_buffer_impl,
st_centroid::st_centroid_impl, st_dwithin::st_dwithin_impl, st_length::st_length_impl,
Expand All @@ -37,6 +38,7 @@ pub fn scalar_kernels() -> Vec<(&'static str, ScalarKernelRef)> {
("st_buffer", st_buffer_impl()),
("st_centroid", st_centroid_impl()),
("st_contains", st_contains_impl()),
("st_convexhull", st_convex_hull_impl()),
("st_coveredby", st_covered_by_impl()),
("st_covers", st_covers_impl()),
("st_difference", st_difference_impl()),
Expand Down
125 changes: 125 additions & 0 deletions c/sedona-geos/src/st_convexhull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::sync::Arc;

use arrow_array::builder::BinaryBuilder;
use datafusion_common::{error::Result, DataFusionError};
use datafusion_expr::ColumnarValue;
use geos::Geom;
use sedona_expr::scalar_udf::{ArgMatcher, ScalarKernelRef, SedonaScalarKernel};
use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES;
use sedona_schema::datatypes::{SedonaType, WKB_GEOMETRY};

use crate::executor::GeosExecutor;

/// ST_ConvexHull() implementation using the geos crate
pub fn st_convex_hull_impl() -> ScalarKernelRef {
Arc::new(STConvexHull {})
}

#[derive(Debug)]
struct STConvexHull {}

impl SedonaScalarKernel for STConvexHull {
fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY);

matcher.match_args(args)
}

fn invoke_batch(
&self,
arg_types: &[SedonaType],
args: &[ColumnarValue],
) -> Result<ColumnarValue> {
let executor = GeosExecutor::new(arg_types, args);
let mut builder = BinaryBuilder::with_capacity(
executor.num_iterations(),
WKB_MIN_PROBABLE_BYTES * executor.num_iterations(),
);
executor.execute_wkb_void(|maybe_wkb| {
match maybe_wkb {
Some(wkb) => {
invoke_scalar(&wkb, &mut builder)?;
builder.append_value([]);
}
_ => builder.append_null(),
}

Ok(())
})?;

executor.finish(Arc::new(builder.finish()))
}
}

fn invoke_scalar(geos_geom: &geos::Geometry, writer: &mut impl std::io::Write) -> Result<()> {
let geometry = geos_geom
.convex_hull()
.map_err(|e| DataFusionError::Execution(format!("Failed to calculate convex_hull: {e}")))?;

let wkb = geometry
.to_wkb()
.map_err(|e| DataFusionError::Execution(format!("Failed to convert to wkb: {e}")))?;

writer.write_all(wkb.as_ref())?;
Ok(())
}

#[cfg(test)]
mod tests {
use datafusion_common::ScalarValue;
use rstest::rstest;
use sedona_expr::scalar_udf::SedonaScalarUDF;
use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_VIEW_GEOMETRY};
use sedona_testing::compare::assert_array_equal;
use sedona_testing::create::create_array;
use sedona_testing::testers::ScalarUdfTester;

use super::*;

#[rstest]
fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) {
let udf = SedonaScalarUDF::from_kernel("st_convex_hull", st_convex_hull_impl());
let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type]);
tester.assert_return_type(WKB_GEOMETRY);

let result = tester
.invoke_scalar("MULTIPOINT ((0 0), (0 1), (1 1), (1 0))")
.unwrap();
tester.assert_scalar_result_equals(result, "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))");

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

let input_wkt = vec![
Some("MULTIPOINT ((0 0), (0 1), (1 1), (1 0))"),
Some("POINT EMPTY"),
None,
];

let expected = create_array(
&[
Some("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"),
Some("GEOMETRYCOLLECTION EMPTY"),
None,
],
&WKB_GEOMETRY,
);
assert_array_equal(&tester.invoke_wkb_array(input_wkt).unwrap(), &expected);
}
}
117 changes: 117 additions & 0 deletions python/sedonadb/tests/functions/test_aggregate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import pytest
from sedonadb.testing import PostGIS, SedonaDB


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_collect_points(eng):
eng = eng.create_or_skip()
eng.assert_query_result(
"""SELECT ST_Collect(ST_GeomFromText(geom)) FROM (
VALUES
('POINT (1 2)'),
('POINT (3 4)'),
(NULL)
) AS t(geom)""",
"MULTIPOINT (1 2, 3 4)",
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_collect_linestrings(eng):
eng = eng.create_or_skip()
eng.assert_query_result(
"""SELECT ST_Collect(ST_GeomFromText(geom)) FROM (
VALUES
('LINESTRING (1 2, 3 4)'),
('LINESTRING (5 6, 7 8)'),
(NULL)
) AS t(geom)""",
"MULTILINESTRING ((1 2, 3 4), (5 6, 7 8))",
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_collect_polygons(eng):
eng = eng.create_or_skip()
eng.assert_query_result(
"""SELECT ST_Collect(ST_GeomFromText(geom)) FROM (
VALUES
('POLYGON ((0 0, 1 0, 0 1, 0 0))'),
('POLYGON ((10 10, 11 10, 10 11, 10 10))'),
(NULL)
) AS t(geom)""",
"MULTIPOLYGON (((0 0, 1 0, 0 1, 0 0)), ((10 10, 11 10, 10 11, 10 10)))",
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_collect_mixed_types(eng):
eng = eng.create_or_skip()
eng.assert_query_result(
"""SELECT ST_Collect(ST_GeomFromText(geom)) FROM (
VALUES
('POINT (1 2)'),
('LINESTRING (3 4, 5 6)'),
(NULL)
) AS t(geom)""",
"GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (3 4, 5 6))",
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_collect_mixed_dimensions(eng):
eng = eng.create_or_skip()

with pytest.raises(Exception, match="mixed dimension geometries"):
eng.assert_query_result(
"""SELECT ST_Collect(ST_GeomFromText(geom)) FROM (
VALUES
('POINT (1 2)'),
('POINT Z (3 4 5)'),
(NULL)
) AS t(geom)""",
"MULTIPOINT (1 2, 3 4)",
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_collect_all_null(eng):
eng = eng.create_or_skip()
eng.assert_query_result(
"""SELECT ST_Collect(geom) FROM (
VALUES
(NULL),
(NULL),
(NULL)
) AS t(geom)""",
None,
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_collect_zero_input(eng):
eng = eng.create_or_skip()
eng.assert_query_result(
"""SELECT ST_Collect(ST_GeomFromText(geom)) AS empty FROM (
VALUES
('POINT (1 2)')
) AS t(geom) WHERE false""",
None,
)
34 changes: 34 additions & 0 deletions python/sedonadb/tests/functions/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,40 @@ def test_st_centroid(eng, geom, expected):
eng.assert_query_result(f"SELECT ST_Centroid({geom_or_null(geom)})", expected)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize(
("geom", "expected"),
[
(None, None),
("POINT (0 0)", "POINT (0 0)"),
("MULTIPOINT (0 0, 1 1)", "LINESTRING (0 0, 1 1)"),
("MULTIPOINT (0 0, 1 1, 1 0)", "POLYGON ((0 0, 1 1, 1 0, 0 0))"),
("MULTIPOINT (0 0, 1 1, 1 0, 0.5 0.25)", "POLYGON ((0 0, 1 1, 1 0, 0 0))"),
],
)
def test_st_convexhull(eng, geom, expected):
eng = eng.create_or_skip()
eng.assert_query_result(f"SELECT ST_ConvexHull({geom_or_null(geom)})", expected)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_makeline(eng):
eng = eng.create_or_skip()
eng.assert_query_result(
"SELECT ST_MakeLine(ST_Point(0, 1), ST_Point(2, 3))", "LINESTRING (0 1, 2 3)"
)

eng.assert_query_result(
"SELECT ST_MakeLine(ST_Point(0, 1), ST_GeomFromText('LINESTRING (0 1, 2 3)'))",
"LINESTRING (0 1, 2 3)",
)

eng.assert_query_result(
"SELECT ST_MakeLine(ST_Point(0, 1), ST_GeomFromText('LINESTRING (2 3, 4 5)'))",
"LINESTRING (0 1, 2 3, 4 5)",
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize(
("geom", "expected"),
Expand Down
29 changes: 20 additions & 9 deletions rust/sedona-functions/benches/native-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ fn criterion_benchmark(c: &mut Criterion) {
Transformed(LineString(10).into(), st_astext.clone()),
);

benchmark::scalar(c, &f, "native", "st_hasz", Point);
benchmark::scalar(c, &f, "native", "st_hasz", LineString(10));

benchmark::scalar(c, &f, "native", "st_hasm", Point);
benchmark::scalar(c, &f, "native", "st_hasm", LineString(10));

benchmark::scalar(c, &f, "native", "st_isempty", Point);
benchmark::scalar(c, &f, "native", "st_isempty", LineString(10));

benchmark::scalar(
c,
&f,
"native",
"st_makeline",
BenchmarkArgs::ArrayArray(Point, Point),
);

benchmark::scalar(
c,
&f,
Expand Down Expand Up @@ -103,15 +120,6 @@ fn criterion_benchmark(c: &mut Criterion) {
),
);

benchmark::scalar(c, &f, "native", "st_hasz", Point);
benchmark::scalar(c, &f, "native", "st_hasz", LineString(10));

benchmark::scalar(c, &f, "native", "st_hasm", Point);
benchmark::scalar(c, &f, "native", "st_hasm", LineString(10));

benchmark::scalar(c, &f, "native", "st_isempty", Point);
benchmark::scalar(c, &f, "native", "st_isempty", LineString(10));

benchmark::scalar(c, &f, "native", "st_x", Point);
benchmark::scalar(c, &f, "native", "st_y", Point);
benchmark::scalar(c, &f, "native", "st_z", Point);
Expand All @@ -132,6 +140,9 @@ fn criterion_benchmark(c: &mut Criterion) {

benchmark::aggregate(c, &f, "native", "st_analyze_aggr", Point);
benchmark::aggregate(c, &f, "native", "st_analyze_aggr", LineString(10));

benchmark::aggregate(c, &f, "native", "st_collect", Point);
benchmark::aggregate(c, &f, "native", "st_collect", LineString(10));
}

criterion_group!(benches, criterion_benchmark);
Expand Down
10 changes: 9 additions & 1 deletion rust/sedona-functions/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use std::iter::zip;

use arrow_array::ArrayRef;
use arrow_schema::DataType;
use datafusion_common::cast::{as_binary_array, as_binary_view_array};
use datafusion_common::error::Result;
use datafusion_common::{DataFusionError, ScalarValue};
Expand Down Expand Up @@ -314,7 +315,7 @@ impl IterGeo for ArrayRef {
&'a self,
sedona_type: &SedonaType,
num_iterations: usize,
func: F,
mut func: F,
) -> Result<()> {
if num_iterations != self.len() {
return sedona_internal_err!(
Expand All @@ -324,6 +325,13 @@ impl IterGeo for ArrayRef {
}

match sedona_type {
SedonaType::Arrow(DataType::Null) => {
for _ in 0..num_iterations {
func(None)?;
}

Ok(())
}
SedonaType::Wkb(_, _) => iter_wkb_binary(as_binary_array(self)?, func),
SedonaType::WkbView(_, _) => iter_wkb_binary(as_binary_view_array(self)?, func),
_ => {
Expand Down
Loading