Skip to content
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
4 changes: 4 additions & 0 deletions vortex-geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,9 @@ harness = false
name = "collect"
harness = false

[[bench]]
name = "convex_hull"
harness = false

[lints]
workspace = true
96 changes: 96 additions & 0 deletions vortex-geo/benches/convex_hull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Microbenchmarks for native `ST_ConvexHull` over `MultiPoint` rows.
//!
//! The cases separate ordinary small hulls, larger point sets, and strict null propagation. They
//! execute the result to its canonical polygon representation.
//!
//! Run with `cargo bench -p vortex-geo --bench convex_hull`.

#![expect(clippy::unwrap_used)]

use std::f64::consts::TAU;
use std::sync::LazyLock;

use divan::Bencher;
use divan::counter::ItemsCount;
use mimalloc::MiMalloc;
use vortex_array::ArrayRef;
use vortex_array::Canonical;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::arrays::MaskedArray;
use vortex_array::validity::Validity;
use vortex_geo::scalar_fn::convex_hull::GeoConvexHull;
use vortex_geo::test_harness::geo_session;
use vortex_geo::test_harness::multipoint_column;
use vortex_session::VortexSession;

// Scalar function execution allocates its output inside the timed region, so use the vendored
// allocator instead of measuring glibc differences between CodSpeed runner images.
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

static SESSION: LazyLock<VortexSession> = LazyLock::new(geo_session);

const ROWS: usize = 512;

fn main() {
divan::main();
}

fn multipoints(points_per_row: usize) -> ArrayRef {
multipoint_column(
(0..ROWS)
.map(|row| {
(0..points_per_row)
.map(|point| {
let angle = TAU * point as f64 / points_per_row as f64;
let radius = 10.0 + ((row + point) % 7) as f64;
(radius * angle.cos(), radius * angle.sin())
})
.collect()
})
.collect(),
)
.unwrap()
}

fn hulls(input: &ArrayRef, ctx: &mut ExecutionCtx) -> ArrayRef {
GeoConvexHull::try_new_array(input.clone())
.unwrap()
.into_array()
.execute::<Canonical>(ctx)
.unwrap()
.into_array()
}

fn bench_hulls(bencher: Bencher, input: ArrayRef) {
let mut ctx = SESSION.create_execution_ctx();
bencher
.counter(ItemsCount::new(ROWS))
.bench_local(|| hulls(&input, &mut ctx));
}

#[divan::bench]
fn eight_points(bencher: Bencher) {
bench_hulls(bencher, multipoints(8));
}

#[divan::bench]
fn sixty_four_points(bencher: Bencher) {
bench_hulls(bencher, multipoints(64));
}

#[divan::bench]
fn nullable_eight_points(bencher: Bencher) {
let input = MaskedArray::try_new(
multipoints(8),
Validity::from_iter((0..ROWS).map(|row| !row.is_multiple_of(8))),
)
.unwrap()
.into_array();
bench_hulls(bencher, input);
}
22 changes: 22 additions & 0 deletions vortex-geo/src/extension/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use arrow_schema::Field;
use arrow_schema::extension::ExtensionType;
use geo_traits::to_geo::ToGeoGeometry;
use geo_types::Geometry;
use geoarrow::array::GeoArrowArray;
use geoarrow::array::GeoArrowArrayAccessor;
use geoarrow::array::IntoArrow;
use geoarrow::array::PolygonArray;
use geoarrow::array::PolygonBuilder;
use geoarrow::datatypes::CoordType;
use geoarrow::datatypes::PolygonType;
use prost::Message;
Expand All @@ -24,6 +26,7 @@ use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::ExtensionArray;
use vortex_array::arrays::extension::ExtensionArrayExt;
use vortex_array::builtins::ArrayBuiltins;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::extension::ExtDType;
Expand Down Expand Up @@ -114,6 +117,25 @@ fn polygon_type(geo_metadata: &GeoMetadata, dimension: Dimension) -> PolygonType
PolygonType::new(dimension.into(), geoarrow_metadata(geo_metadata))
}

/// Build a native 2-D [`Polygon`] array from row-oriented `geo_types` polygons.
pub(crate) fn build_polygon_array(
polygons: &[Option<geo_types::Polygon<f64>>],
metadata: GeoMetadata,
nullability: Nullability,
) -> VortexResult<ArrayRef> {
let polygons =
PolygonBuilder::from_nullable_polygons(polygons, polygon_type(&metadata, Dimension::Xy))
.finish();
let storage_dtype = polygon_storage_dtype(Dimension::Xy, nullability);
let storage = ArrayRef::from_arrow(
polygons.to_array_ref().as_ref(),
nullability == Nullability::Nullable,
)?
.cast(storage_dtype.clone())?;
let ext_dtype = ExtDType::<Polygon>::try_new(metadata, storage_dtype)?;
Ok(ExtensionArray::try_new(ext_dtype.erased(), storage)?.into_array())
}

/// Decode `Polygon` storage (`List<List<coordinate>>`) to `geo_types` polygons, for the geo scalar
/// functions. CRS does not affect planar geometry ops, so default metadata is used.
pub(crate) fn polygon_geometries(
Expand Down
2 changes: 2 additions & 0 deletions vortex-geo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::prune::GeoIntersectsPrune;
use crate::scalar_fn::area::GeoArea;
use crate::scalar_fn::collect::GeoCollect;
use crate::scalar_fn::contains::GeoContains;
use crate::scalar_fn::convex_hull::GeoConvexHull;
use crate::scalar_fn::distance::GeoDistance;
use crate::scalar_fn::envelope::GeoEnvelope;
use crate::scalar_fn::intersects::GeoIntersects;
Expand Down Expand Up @@ -70,6 +71,7 @@ pub fn initialize(session: &VortexSession) {
// Register the geometry scalar functions.
session.scalar_fns().register(GeoArea);
session.scalar_fns().register(GeoCollect);
session.scalar_fns().register(GeoConvexHull);
session.scalar_fns().register(GeoEnvelope);
session.scalar_fns().register(GeoContains);
session.scalar_fns().register(GeoDistance);
Expand Down
Loading
Loading