From fe549e1f9766b113bc18928b03ee5ac1418d2dee Mon Sep 17 00:00:00 2001 From: Nemo Yu Date: Wed, 5 Aug 2026 14:56:45 -0400 Subject: [PATCH 1/2] feat(vortex-geo): add make-line and length scalar functions Signed-off-by: Nemo Yu --- .../src/arrays/interleave/execute/bool.rs | 42 +- .../src/arrays/interleave/execute/mod.rs | 41 +- .../arrays/interleave/execute/primitive.rs | 85 +++ vortex-array/src/arrays/interleave/mod.rs | 23 +- vortex-geo/src/extension/coordinate.rs | 15 + vortex-geo/src/extension/linestring.rs | 79 +++ vortex-geo/src/lib.rs | 4 + vortex-geo/src/scalar_fn/execute.rs | 13 +- vortex-geo/src/scalar_fn/execute/unary.rs | 45 +- vortex-geo/src/scalar_fn/length.rs | 226 ++++++++ vortex-geo/src/scalar_fn/make_line.rs | 540 ++++++++++++++++++ vortex-geo/src/scalar_fn/mod.rs | 2 + 12 files changed, 1059 insertions(+), 56 deletions(-) create mode 100644 vortex-array/src/arrays/interleave/execute/primitive.rs create mode 100644 vortex-geo/src/scalar_fn/length.rs create mode 100644 vortex-geo/src/scalar_fn/make_line.rs diff --git a/vortex-array/src/arrays/interleave/execute/bool.rs b/vortex-array/src/arrays/interleave/execute/bool.rs index fde5b161dfd..a051f55ec5d 100644 --- a/vortex-array/src/arrays/interleave/execute/bool.rs +++ b/vortex-array/src/arrays/interleave/execute/bool.rs @@ -7,10 +7,10 @@ use num_traits::AsPrimitive; use vortex_buffer::BitBuffer; use vortex_buffer::BitBufferMut; use vortex_error::VortexResult; -use vortex_error::vortex_ensure; use super::super::Interleave; use super::super::InterleaveArrayExt; +use super::validate_selectors; use crate::array::Array; use crate::arrays::Bool; use crate::arrays::BoolArray; @@ -71,46 +71,18 @@ fn gather, R: AsPrimitive>( branches: &[A], rows: &[R], ) -> VortexResult { - let len = validate_selectors(value_bits, branches, rows)?; + let len = validate_selectors( + value_bits.len(), + |branch| value_bits[branch].len(), + branches, + rows, + )?; // SAFETY: `validate_selectors` proved `branches.len() == rows.len() == len`, and for every // `i < len` that `branches[i] < value_bits.len()` and `rows[i] < value_bits[branches[i]].len()`. Ok(unsafe { gather_bits(len, value_bits, branches, rows) }) } -/// Validates the per-row selector bounds, returning the output length (`branches.len()`). -/// -/// On success, `rows.len() == branches.len() == len` and, for every `i < len`, -/// `branches[i] < value_bits.len()` and `rows[i] < value_bits[branches[i]].len()` — exactly the -/// preconditions of [`gather_bits`]. Errors (rather than panics) on any out-of-bounds selector. -fn validate_selectors, R: AsPrimitive>( - value_bits: &[BitBuffer], - branches: &[A], - rows: &[R], -) -> VortexResult { - // The two selectors are validated to equal length at construction, which is the output length. - let len = branches.len(); - vortex_ensure!( - rows.len() == len, - "interleave selectors differ in length: array_indices {len}, row_indices {}", - rows.len() - ); - - for i in 0..len { - let branch = branches[i].as_(); - vortex_ensure!( - branch < value_bits.len(), - "interleave array index out of bounds" - ); - vortex_ensure!( - rows[i].as_() < value_bits[branch].len(), - "interleave row index out of bounds" - ); - } - - Ok(len) -} - /// Gathers one bit per output from `bits[branches[i]]` at position `rows[i]`, packing 64 results per /// word with [`BitBufferMut::collect_bool`]. /// diff --git a/vortex-array/src/arrays/interleave/execute/mod.rs b/vortex-array/src/arrays/interleave/execute/mod.rs index 05dcd161f62..a8047968136 100644 --- a/vortex-array/src/arrays/interleave/execute/mod.rs +++ b/vortex-array/src/arrays/interleave/execute/mod.rs @@ -5,14 +5,16 @@ //! //! All values share a type (validated in [`Interleave::check`]), so the //! physical gather kernel is chosen from the first value. The selector types are an orthogonal -//! concern handled within each kernel. Only boolean values are implemented today (see the [`bool`] module). +//! concern handled within each kernel. //! //! [`Interleave::check`]: super::Interleave::check -//! [`bool`]: module@crate::arrays::interleave::execute::bool mod bool; +mod primitive; +use num_traits::AsPrimitive; use vortex_error::VortexResult; +use vortex_error::vortex_ensure; use vortex_error::vortex_panic; use super::Interleave; @@ -28,12 +30,43 @@ pub(super) fn execute( ) -> VortexResult { if array.value(0).dtype().is_boolean() { bool::execute(array, ctx) + } else if array.value(0).dtype().is_primitive() { + primitive::execute(array, ctx) } else { let value_dtype = array.value(0).dtype().clone(); vortex_panic!( - "interleave execution is only implemented for boolean values; value dtype {} is not \ - yet supported", + "interleave execution is not implemented for value dtype {}", value_dtype ) } } + +/// Validate selector lengths and bounds, returning the output length. +fn validate_selectors( + num_values: usize, + value_len: F, + branches: &[A], + rows: &[R], +) -> VortexResult +where + A: AsPrimitive, + R: AsPrimitive, + F: Fn(usize) -> usize, +{ + let len = branches.len(); + vortex_ensure!( + rows.len() == len, + "interleave selectors differ in length: array_indices {len}, row_indices {}", + rows.len() + ); + + for i in 0..len { + let branch = branches[i].as_(); + vortex_ensure!(branch < num_values, "interleave array index out of bounds"); + vortex_ensure!( + rows[i].as_() < value_len(branch), + "interleave row index out of bounds" + ); + } + Ok(len) +} diff --git a/vortex-array/src/arrays/interleave/execute/primitive.rs b/vortex-array/src/arrays/interleave/execute/primitive.rs new file mode 100644 index 00000000000..40e59b5847d --- /dev/null +++ b/vortex-array/src/arrays/interleave/execute/primitive.rs @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! Execution for primitive [`Interleave`] values. + +use num_traits::AsPrimitive; +use vortex_buffer::Buffer; +use vortex_buffer::BufferMut; +use vortex_error::VortexResult; + +use super::super::Interleave; +use super::super::InterleaveArrayExt; +use super::validate_selectors; +use crate::array::Array; +use crate::array::ArrayView; +use crate::arrays::Primitive; +use crate::arrays::PrimitiveArray; +use crate::arrays::primitive::PrimitiveArrayExt; +use crate::dtype::NativePType; +use crate::executor::ExecutionCtx; +use crate::executor::ExecutionResult; +use crate::match_each_native_ptype; +use crate::match_each_unsigned_integer_ptype; +use crate::require_child; + +pub(super) fn execute( + array: Array, + _ctx: &mut ExecutionCtx, +) -> VortexResult { + let num_values = array.num_values(); + let mut array = array; + array = require_child!(array, array.array_indices(), 0 => Primitive); + array = require_child!(array, array.row_indices(), 1 => Primitive); + for i in 0..num_values { + array = require_child!(array, array.value(i), i + 2 => Primitive); + } + + let validity = array.as_ref().validity()?; + let output = match_each_native_ptype!(array.value(0).as_::().ptype(), |T| { + let values = gather_values::(&array)?; + VortexResult::Ok(PrimitiveArray::new(values, validity)) + })?; + + Ok(ExecutionResult::done(output)) +} + +fn gather_values(array: &Array) -> VortexResult> { + let buffers = (0..array.num_values()) + .map(|i| array.value(i).as_::().to_buffer::()) + .collect::>(); + let branches = array.array_indices().as_::(); + let rows = array.row_indices().as_::(); + + match_each_unsigned_integer_ptype!(branches.ptype(), |A| { + gather_rows::(&buffers, branches.as_slice::(), rows) + }) +} + +fn gather_rows( + values: &[Buffer], + branches: &[A], + rows: ArrayView<'_, Primitive>, +) -> VortexResult> +where + T: NativePType, + A: AsPrimitive, +{ + match_each_unsigned_integer_ptype!(rows.ptype(), |R| { + gather(values, branches, rows.as_slice::()) + }) +} + +fn gather(values: &[Buffer], branches: &[A], rows: &[R]) -> VortexResult> +where + T: NativePType, + A: AsPrimitive, + R: AsPrimitive, +{ + let len = validate_selectors(values.len(), |branch| values[branch].len(), branches, rows)?; + let mut output = BufferMut::with_capacity(len); + for i in 0..len { + output.push(values[branches[i].as_()][rows[i].as_()]); + } + Ok(output.freeze()) +} diff --git a/vortex-array/src/arrays/interleave/mod.rs b/vortex-array/src/arrays/interleave/mod.rs index bff03ab055f..9b63b721c3f 100644 --- a/vortex-array/src/arrays/interleave/mod.rs +++ b/vortex-array/src/arrays/interleave/mod.rs @@ -719,17 +719,18 @@ mod tests { } #[test] - #[should_panic(expected = "only implemented for boolean values")] - fn non_boolean_value_execution_panics() { - // Execution dispatches on the value type: primitive values have no kernel yet. - let v0 = PrimitiveArray::from_iter([1u32]).into_array(); - let v1 = PrimitiveArray::from_iter([2u32]).into_array(); - let array_indices = PrimitiveArray::from_iter([0u32, 1]).into_array(); - let row_indices = PrimitiveArray::from_iter([0u32, 0]).into_array(); - let interleaved = InterleaveArray::try_new(vec![v0, v1], array_indices, row_indices) - .vortex_expect("primitive values should construct") - .into_array(); + fn executes_primitive_values() -> VortexResult<()> { + let v0 = PrimitiveArray::from_iter([1.0f64, 2.0]).into_array(); + let v1 = PrimitiveArray::from_option_iter([Some(10.0f64), None]).into_array(); + let array_indices = PrimitiveArray::from_iter([0u8, 1, 0, 1]).into_array(); + let row_indices = PrimitiveArray::from_iter([0u32, 0, 1, 1]).into_array(); + let interleaved = + InterleaveArray::try_new(vec![v0, v1], array_indices, row_indices)?.into_array(); + let expected = + PrimitiveArray::from_option_iter([Some(1.0f64), Some(10.0), Some(2.0), None]) + .into_array(); let mut ctx = array_session().create_execution_ctx(); - interleaved.execute::(&mut ctx).ok(); + assert_arrays_eq!(interleaved, expected, &mut ctx); + Ok(()) } } diff --git a/vortex-geo/src/extension/coordinate.rs b/vortex-geo/src/extension/coordinate.rs index dc3537cfb30..43599a705b7 100644 --- a/vortex-geo/src/extension/coordinate.rs +++ b/vortex-geo/src/extension/coordinate.rs @@ -72,6 +72,21 @@ impl Dimension { Dimension::Xyzm => &["x", "y", "z", "m"], } } + + /// Promote two coordinate dimensions to the smallest dimension that represents both. + /// + /// Missing `z`/`m` ordinates are materialized as zero when values are converted to this + /// dimension, matching DuckDB Spatial's `ST_MakeLine` promotion. + pub(crate) fn promote(self, other: Self) -> Self { + match (self, other) { + (Self::Xyzm, _) | (_, Self::Xyzm) | (Self::Xyz, Self::Xym) | (Self::Xym, Self::Xyz) => { + Self::Xyzm + } + (Self::Xyz, _) | (_, Self::Xyz) => Self::Xyz, + (Self::Xym, _) | (_, Self::Xym) => Self::Xym, + (Self::Xy, Self::Xy) => Self::Xy, + } + } } impl From for Dimension { diff --git a/vortex-geo/src/extension/linestring.rs b/vortex-geo/src/extension/linestring.rs index 274d20f28ba..b627bcafc83 100644 --- a/vortex-geo/src/extension/linestring.rs +++ b/vortex-geo/src/extension/linestring.rs @@ -22,14 +22,22 @@ use prost::Message; use vortex_array::ArrayRef; use vortex_array::ExecutionCtx; use vortex_array::IntoArray; +use vortex_array::arrays::ConstantArray; use vortex_array::arrays::ExtensionArray; +use vortex_array::arrays::InterleaveArray; +use vortex_array::arrays::ListArray; +use vortex_array::arrays::PrimitiveArray; +use vortex_array::arrays::StructArray; use vortex_array::arrays::extension::ExtensionArrayExt; +use vortex_array::arrays::struct_::StructArrayExt; use vortex_array::dtype::DType; +use vortex_array::dtype::FieldNames; use vortex_array::dtype::Nullability; use vortex_array::dtype::extension::ExtDType; use vortex_array::dtype::extension::ExtId; use vortex_array::dtype::extension::ExtVTable; use vortex_array::scalar::ScalarValue; +use vortex_array::validity::Validity; use vortex_arrow::ArrowExport; use vortex_arrow::ArrowExportVTable; use vortex_arrow::ArrowImport; @@ -38,10 +46,12 @@ use vortex_arrow::ArrowSession; use vortex_arrow::ArrowSessionExt; use vortex_arrow::FromArrowArray; use vortex_arrow::FromArrowType; +use vortex_buffer::Buffer; use vortex_error::VortexError; use vortex_error::VortexResult; use vortex_error::vortex_bail; use vortex_error::vortex_ensure; +use vortex_error::vortex_ensure_eq; use vortex_error::vortex_err; use vortex_session::registry::CachedId; use vortex_session::registry::Id; @@ -103,6 +113,75 @@ pub(crate) fn linestring_dimension(dtype: &DType) -> VortexResult { coordinate_dimension(coords) } +/// Build one native [`LineString`] per corresponding pair of point coordinate rows. +pub(crate) fn linestring_array_from_point_pairs( + ext_dtype: &ExtDType, + starts: &StructArray, + ends: &StructArray, + validity: Validity, +) -> VortexResult { + let len = starts.len(); + vortex_ensure_eq!( + len, + ends.len(), + "geo: line string point columns must have equal lengths" + ); + let vertex_count = len + .checked_mul(2) + .ok_or_else(|| vortex_err!("geo: two-vertex line string length overflow"))?; + let dimension = linestring_dimension(ext_dtype.storage_dtype())?; + let start_dimension = coordinate_dimension(starts.dtype())?; + let end_dimension = coordinate_dimension(ends.dtype())?; + + let array_indices = PrimitiveArray::from_iter((0..len).flat_map(|_| [0u8, 1])).into_array(); + let rows = (0..len) + .flat_map(|row| [row, row]) + .map(|row| { + u64::try_from(row).map_err(|_| vortex_err!("geo: line string row index overflow")) + }) + .collect::>>()?; + let row_indices = Buffer::from(rows).into_array(); + let ordinate = + |points: &StructArray, point_dimension: Dimension, name: &str| -> VortexResult { + if point_dimension.field_names().contains(&name) { + points.unmasked_field_by_name(name).cloned() + } else { + Ok(ConstantArray::new(0.0f64, len).into_array()) + } + }; + let ordinates = dimension + .field_names() + .iter() + .map(|name| { + Ok(InterleaveArray::try_new( + vec![ + ordinate(starts, start_dimension, name)?, + ordinate(ends, end_dimension, name)?, + ], + array_indices.clone(), + row_indices.clone(), + )? + .into_array()) + }) + .collect::>>()?; + let vertices = StructArray::try_new( + FieldNames::from(dimension.field_names()), + ordinates, + vertex_count, + Validity::NonNullable, + )? + .into_array(); + let offsets = (0..=len) + .map(|row| { + i32::try_from(row * 2) + .map_err(|_| vortex_err!("geo: two-vertex line string offset overflow")) + }) + .collect::>>()?; + let storage = + ListArray::try_new(vertices, Buffer::from(offsets).into_array(), validity)?.into_array(); + Ok(ExtensionArray::try_new(ext_dtype.clone().erased(), storage)?.into_array()) +} + static ARROW_LINESTRING: CachedId = CachedId::new(LineStringType::NAME); /// The `geoarrow.linestring` extension type for `dimension`, with separated (struct) coordinates diff --git a/vortex-geo/src/lib.rs b/vortex-geo/src/lib.rs index b72df92a3f5..88c54d3da1a 100644 --- a/vortex-geo/src/lib.rs +++ b/vortex-geo/src/lib.rs @@ -25,6 +25,8 @@ use crate::scalar_fn::contains::GeoContains; use crate::scalar_fn::distance::GeoDistance; use crate::scalar_fn::envelope::GeoEnvelope; use crate::scalar_fn::intersects::GeoIntersects; +use crate::scalar_fn::length::GeoLength; +use crate::scalar_fn::make_line::GeoMakeLine; pub mod aggregate_fn; pub mod extension; @@ -68,6 +70,8 @@ pub fn initialize(session: &VortexSession) { session.scalar_fns().register(GeoContains); session.scalar_fns().register(GeoDistance); session.scalar_fns().register(GeoIntersects); + session.scalar_fns().register(GeoLength); + session.scalar_fns().register(GeoMakeLine); // The axis-aligned bounding-box (AABB) aggregate; self-declares as a per-chunk zone stat for // geometry columns. diff --git a/vortex-geo/src/scalar_fn/execute.rs b/vortex-geo/src/scalar_fn/execute.rs index a509e48ebfd..74233aa0396 100644 --- a/vortex-geo/src/scalar_fn/execute.rs +++ b/vortex-geo/src/scalar_fn/execute.rs @@ -3,20 +3,23 @@ //! Shared execution for native geometry scalar functions. //! -//! [`dispatch_unary`] and the binary dispatcher handle constant/column operands and strict null +//! [`dispatch_unary`] and [`dispatch_binary`] handle constant/column operands and strict null //! propagation without prescribing how a kernel represents geometries or builds its output. -//! Native columnar kernels such as `ST_Envelope` use the unary dispatcher directly. +//! Native columnar kernels such as `ST_MakeLine` use these dispatchers directly. //! -//! [`execute_binary_geo_types`] adapts row-oriented algorithms from the `geo` ecosystem. It decodes -//! valid inputs into `geo_types::Geometry`; the final output is still a Vortex [`ArrayRef`], such -//! as an `f64` or boolean array. +//! [`execute_unary_geo_types`] and [`execute_binary_geo_types`] are convenience adapters for +//! row-oriented algorithms from the `geo` ecosystem. They decode valid inputs into +//! `geo_types::Geometry`; the final output is still a Vortex [`ArrayRef`], such as an `f64` or +//! boolean array. mod binary; mod geo_types; mod unary; +pub(crate) use binary::dispatch_binary; pub(crate) use binary::execute_binary_geo_types; pub(crate) use unary::dispatch_unary; +pub(crate) use unary::execute_unary_geo_types; use vortex_array::ArrayRef; use vortex_array::dtype::Nullability; use vortex_array::scalar::Scalar; diff --git a/vortex-geo/src/scalar_fn/execute/unary.rs b/vortex-geo/src/scalar_fn/execute/unary.rs index 8af7474491d..89d88390fa4 100644 --- a/vortex-geo/src/scalar_fn/execute/unary.rs +++ b/vortex-geo/src/scalar_fn/execute/unary.rs @@ -1,8 +1,9 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright the Vortex contributors -//! Unary operand dispatch for native geometry kernels. +//! Unary operand dispatch, plus an adapter for row-oriented `geo_types` kernels. +use geo_types::Geometry; use vortex_array::ArrayRef; use vortex_array::ExecutionCtx; use vortex_array::IntoArray; @@ -15,6 +16,9 @@ use vortex_mask::Mask; use super::Execution; use super::Operand; +use super::geo_types::GeoTypesOutput; +use super::geo_types::eval_column; +use crate::extension::single_geometry; /// Dispatch a unary strict geometry kernel over a constant or column. /// @@ -60,3 +64,42 @@ where ctx, ) } + +/// Run a unary row-oriented kernel whose input is decoded to `geo_types::Geometry`. +/// +/// The `geo_types` name describes the value passed to `compute`, not the output. `T` is converted +/// into a Vortex array before this function returns. A constant is decoded and computed once +/// before broadcast; a column is decoded only for its valid rows. +pub(crate) fn execute_unary_geo_types( + array: &ArrayRef, + compute: F, + ctx: &mut ExecutionCtx, +) -> VortexResult +where + T: GeoTypesOutput, + F: Fn(&Geometry) -> T, +{ + let nullability = array.dtype().nullability(); + dispatch_unary( + array, + T::dtype(nullability), + |execution, ctx| match execution.operands { + [Operand::Constant(scalar)] => { + let geometry = single_geometry(&scalar, ctx)?; + Ok(ConstantArray::new( + compute(&geometry).into_scalar(execution.nullability), + execution.len, + ) + .into_array()) + } + [Operand::Column(array)] => eval_column( + &array, + &execution.valid, + compute, + execution.nullability, + ctx, + ), + }, + ctx, + ) +} diff --git a/vortex-geo/src/scalar_fn/length.rs b/vortex-geo/src/scalar_fn/length.rs new file mode 100644 index 00000000000..4027524eb85 --- /dev/null +++ b/vortex-geo/src/scalar_fn/length.rs @@ -0,0 +1,226 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! `ST_Length`: planar (Euclidean) length of native line strings. + +use geo::Euclidean; +use geo::Length; +use geo_types::Geometry; +use vortex_array::ArrayRef; +use vortex_array::ExecutionCtx; +use vortex_array::arrays::ScalarFnArray; +use vortex_array::dtype::DType; +use vortex_array::dtype::PType; +use vortex_array::expr::Expression; +use vortex_array::expr::union_child_validities; +use vortex_array::scalar_fn::Arity; +use vortex_array::scalar_fn::ChildName; +use vortex_array::scalar_fn::EmptyOptions; +use vortex_array::scalar_fn::ExecutionArgs; +use vortex_array::scalar_fn::ScalarFnId; +use vortex_array::scalar_fn::ScalarFnVTable; +use vortex_array::scalar_fn::TypedScalarFnInstance; +use vortex_error::VortexResult; +use vortex_error::vortex_ensure; +use vortex_session::VortexSession; +use vortex_session::registry::CachedId; + +use crate::scalar_fn::execute::execute_unary_geo_types; + +/// Validate the native line-string operand accepted by `ST_Length`. +fn validate_length_operands(dtypes: &[DType]) -> VortexResult<()> { + vortex_ensure!( + dtypes.len() == 1, + "geo: length requires exactly one line string operand, got {}", + dtypes.len() + ); + vortex_ensure!( + dtypes[0] + .as_extension_opt() + .is_some_and(|extension| extension.is::()), + "geo: length operand {} is not a native line string", + dtypes[0] + ); + Ok(()) +} + +/// Planar (Euclidean) `ST_Length` (no geodesic correction) of native line strings. +#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] +pub struct GeoLength; + +impl GeoLength { + /// A lazy `ScalarFnArray` computing the per-row length of a line string operand. + pub fn try_new_array(array: ArrayRef) -> VortexResult { + ScalarFnArray::try_new( + TypedScalarFnInstance::new(GeoLength, EmptyOptions).erased(), + vec![array], + ) + } +} + +impl ScalarFnVTable for GeoLength { + type Options = EmptyOptions; + + fn id(&self) -> ScalarFnId { + static ID: CachedId = CachedId::new("vortex.geo.length"); + *ID + } + + fn serialize(&self, _: &Self::Options) -> VortexResult>> { + Ok(Some(vec![])) + } + + fn deserialize(&self, _: &[u8], _: &VortexSession) -> VortexResult { + Ok(EmptyOptions) + } + + fn arity(&self, _: &Self::Options) -> Arity { + Arity::Exact(1) + } + + fn child_name(&self, _: &Self::Options, child_idx: usize) -> ChildName { + match child_idx { + 0 => ChildName::from("geometry"), + _ => unreachable!("length has exactly one child"), + } + } + + fn return_dtype(&self, _: &Self::Options, dtypes: &[DType]) -> VortexResult { + validate_length_operands(dtypes)?; + Ok(DType::Primitive(PType::F64, dtypes[0].nullability())) + } + + fn execute( + &self, + _: &Self::Options, + args: &dyn ExecutionArgs, + ctx: &mut ExecutionCtx, + ) -> VortexResult { + let array = args.get(0)?; + execute_unary_geo_types( + &array, + |geometry| match geometry { + Geometry::LineString(line) => Euclidean.length(line), + _ => unreachable!("length input is validated as a line string"), + }, + ctx, + ) + } + + fn validity( + &self, + _: &Self::Options, + expression: &Expression, + ) -> VortexResult> { + union_child_validities(expression) + } + + fn is_strict(&self, _: &Self::Options) -> bool { + true + } + + fn is_fallible(&self, _: &Self::Options) -> bool { + false + } +} + +#[cfg(test)] +mod tests { + use vortex_array::ArrayRef; + use vortex_array::Canonical; + use vortex_array::Columnar; + use vortex_array::ExecutionCtx; + use vortex_array::IntoArray; + use vortex_array::VortexSessionExecute; + use vortex_array::arrays::ConstantArray; + use vortex_array::dtype::DType; + use vortex_array::dtype::Nullability; + use vortex_array::dtype::PType; + use vortex_array::scalar::Scalar; + use vortex_array::scalar_fn::EmptyOptions; + use vortex_array::scalar_fn::ScalarFnVTable; + use vortex_error::VortexResult; + use vortex_error::vortex_err; + + use super::GeoLength; + use crate::test_harness::linestring_column; + use crate::test_harness::point_column; + + fn line_constant( + line: Vec<(f64, f64)>, + len: usize, + ctx: &mut ExecutionCtx, + ) -> VortexResult { + let scalar = linestring_column(vec![line])?.execute_scalar(0, ctx)?; + Ok(ConstantArray::new(scalar, len).into_array()) + } + + #[test] + fn measures_each_linestring() -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let lines = linestring_column(vec![ + vec![(0.0, 0.0), (3.0, 4.0)], + vec![(0.0, 0.0), (3.0, 4.0), (3.0, 8.0)], + vec![], + ])?; + + let lengths = GeoLength::try_new_array(lines)? + .into_array() + .execute::(&mut ctx)? + .into_primitive(); + + assert_eq!(lengths.as_slice::(), &[5.0, 9.0, 0.0]); + Ok(()) + } + + #[test] + fn constant_is_computed_once_and_remains_constant() -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let lines = line_constant(vec![(0.0, 0.0), (3.0, 4.0)], 3, &mut ctx)?; + + let result = GeoLength::try_new_array(lines)? + .into_array() + .execute::(&mut ctx)?; + let Columnar::Constant(lengths) = result else { + return Err(vortex_err!("length of a constant should remain constant")); + }; + assert_eq!(lengths.len(), 3); + assert_eq!(f64::try_from(lengths.scalar())?, 5.0); + Ok(()) + } + + #[test] + fn null_constant_is_all_null() -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let dtype = linestring_column(vec![vec![]])?.dtype().as_nullable(); + let lines = ConstantArray::new(Scalar::null(dtype), 2).into_array(); + + let result = GeoLength::try_new_array(lines)? + .into_array() + .execute::(&mut ctx)?; + let Columnar::Constant(lengths) = result else { + return Err(vortex_err!( + "length of a null constant should remain constant" + )); + }; + assert_eq!(lengths.len(), 2); + assert!(lengths.scalar().is_null()); + Ok(()) + } + + #[test] + fn rejects_non_linestring_dtype() -> VortexResult<()> { + let point = point_column(vec![0.0], vec![0.0])?; + assert!( + GeoLength + .return_dtype(&EmptyOptions, std::slice::from_ref(point.dtype())) + .is_err() + ); + let primitive = DType::Primitive(PType::F64, Nullability::NonNullable); + assert!(GeoLength.return_dtype(&EmptyOptions, &[primitive]).is_err()); + Ok(()) + } +} diff --git a/vortex-geo/src/scalar_fn/make_line.rs b/vortex-geo/src/scalar_fn/make_line.rs new file mode 100644 index 00000000000..e48569c3d50 --- /dev/null +++ b/vortex-geo/src/scalar_fn/make_line.rs @@ -0,0 +1,540 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! `ST_MakeLine`: construct a native line string between two native points. + +use vortex_array::ArrayRef; +use vortex_array::ExecutionCtx; +use vortex_array::IntoArray; +use vortex_array::arrays::ConstantArray; +use vortex_array::arrays::ScalarFnArray; +use vortex_array::dtype::DType; +use vortex_array::dtype::Nullability; +use vortex_array::dtype::extension::ExtDType; +use vortex_array::expr::Expression; +use vortex_array::expr::union_child_validities; +use vortex_array::scalar_fn::Arity; +use vortex_array::scalar_fn::ChildName; +use vortex_array::scalar_fn::EmptyOptions; +use vortex_array::scalar_fn::ExecutionArgs; +use vortex_array::scalar_fn::ScalarFnId; +use vortex_array::scalar_fn::ScalarFnVTable; +use vortex_array::scalar_fn::TypedScalarFnInstance; +use vortex_array::validity::Validity; +use vortex_error::VortexResult; +use vortex_error::vortex_ensure; +use vortex_mask::Mask; +use vortex_session::VortexSession; +use vortex_session::registry::CachedId; + +use crate::extension::GeoMetadata; +use crate::extension::LineString; +use crate::extension::Point; +use crate::extension::coordinate::coordinate_dimension; +use crate::extension::flatten_coordinates; +use crate::extension::linestring_array_from_point_pairs; +use crate::extension::linestring_storage_dtype; +use crate::scalar_fn::execute::Execution; +use crate::scalar_fn::execute::Operand; +use crate::scalar_fn::execute::dispatch_binary; + +/// Validate the two point operands accepted by `ST_MakeLine`. +fn validate_make_line_operands(dtypes: &[DType]) -> VortexResult<()> { + vortex_ensure!( + dtypes.len() == 2, + "geo: make_line requires exactly two point operands, got {}", + dtypes.len() + ); + for dtype in dtypes { + vortex_ensure!( + dtype + .as_extension_opt() + .is_some_and(|extension| extension.is::()), + "geo: make_line operand {dtype} is not a native point" + ); + } + Ok(()) +} + +/// Resolve DuckDB's `ST_MakeLine` CRS propagation for two geometry operands. +fn make_line_metadata(left: &GeoMetadata, right: &GeoMetadata) -> VortexResult { + match (&left.crs, &right.crs) { + (Some(left_crs), Some(right_crs)) => { + vortex_ensure!( + left_crs == right_crs, + "geo: make_line operands have different coordinate reference systems: \ + {left_crs} and {right_crs}" + ); + Ok(left.clone()) + } + (Some(_), None) => Ok(left.clone()), + (None, Some(_)) => Ok(right.clone()), + (None, None) => Ok(GeoMetadata::default()), + } +} + +/// The native `LineString` dtype emitted by `ST_MakeLine`. +fn make_line_dtype(dtypes: &[DType]) -> VortexResult> { + validate_make_line_operands(dtypes)?; + let left = dtypes[0].as_extension(); + let right = dtypes[1].as_extension(); + let dimension = coordinate_dimension(left.storage_dtype())? + .promote(coordinate_dimension(right.storage_dtype())?); + let metadata = make_line_metadata(left.metadata::(), right.metadata::())?; + let nullability = Nullability::from(dtypes.iter().any(DType::is_nullable)); + ExtDType::try_new(metadata, linestring_storage_dtype(dimension, nullability)) +} + +/// Build a native line-string column from dispatched point operands. +fn build_make_lines( + operands: [Operand; 2], + len: usize, + valid: Mask, + output_dtype: &ExtDType, + ctx: &mut ExecutionCtx, +) -> VortexResult { + let [start, end] = operands.map(|operand| match operand { + Operand::Constant(point) => ConstantArray::new(point, len).into_array(), + Operand::Column(points) => points, + }); + let starts = flatten_coordinates(&start, ctx)?; + let ends = flatten_coordinates(&end, ctx)?; + linestring_array_from_point_pairs( + output_dtype, + &starts, + &ends, + Validity::from_mask(valid, output_dtype.storage_dtype().nullability()), + ) +} + +/// Execute `ST_MakeLine` after shared constant/column and null dispatch. +fn execute_make_line( + execution: Execution<2>, + output_dtype: &ExtDType, + ctx: &mut ExecutionCtx, +) -> VortexResult { + match execution.operands { + [Operand::Constant(start), Operand::Constant(end)] => { + let one = build_make_lines( + [Operand::Constant(start), Operand::Constant(end)], + 1, + Mask::new_true(1), + output_dtype, + ctx, + )?; + Ok(ConstantArray::new(one.execute_scalar(0, ctx)?, execution.len).into_array()) + } + operands => build_make_lines(operands, execution.len, execution.valid, output_dtype, ctx), + } +} + +/// Construct `LineString`s from paired native point operands. The output's vertices preserve all +/// coordinate ordinates (`x`, `y`, and any `z`/`m`) and appear in operand order. When the points +/// have different dimensions, it promotes to their union and fills absent `z`/`m` ordinates with +/// zero. +#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] +pub struct GeoMakeLine; + +impl GeoMakeLine { + /// A lazy `ScalarFnArray` constructing one two-vertex line string per pair of point operands. + pub fn try_new_array(a: ArrayRef, b: ArrayRef) -> VortexResult { + ScalarFnArray::try_new( + TypedScalarFnInstance::new(GeoMakeLine, EmptyOptions).erased(), + vec![a, b], + ) + } +} + +impl ScalarFnVTable for GeoMakeLine { + type Options = EmptyOptions; + + fn id(&self) -> ScalarFnId { + static ID: CachedId = CachedId::new("vortex.geo.make_line"); + *ID + } + + fn serialize(&self, _: &Self::Options) -> VortexResult>> { + Ok(Some(vec![])) + } + + fn deserialize(&self, _: &[u8], _: &VortexSession) -> VortexResult { + Ok(EmptyOptions) + } + + fn arity(&self, _: &Self::Options) -> Arity { + Arity::Exact(2) + } + + fn child_name(&self, _: &Self::Options, child_idx: usize) -> ChildName { + match child_idx { + 0 => ChildName::from("start"), + 1 => ChildName::from("end"), + _ => unreachable!("make_line has exactly two children"), + } + } + + fn return_dtype(&self, _: &Self::Options, dtypes: &[DType]) -> VortexResult { + Ok(DType::Extension(make_line_dtype(dtypes)?.erased())) + } + + fn execute( + &self, + _: &Self::Options, + args: &dyn ExecutionArgs, + ctx: &mut ExecutionCtx, + ) -> VortexResult { + let a = args.get(0)?; + let b = args.get(1)?; + let output_dtype = make_line_dtype(&[a.dtype().clone(), b.dtype().clone()])?; + dispatch_binary( + &a, + &b, + DType::Extension(output_dtype.clone().erased()), + |execution, ctx| execute_make_line(execution, &output_dtype, ctx), + ctx, + ) + } + + fn validity( + &self, + _: &Self::Options, + expression: &Expression, + ) -> VortexResult> { + union_child_validities(expression) + } + + fn is_strict(&self, _: &Self::Options) -> bool { + true + } + + fn is_fallible(&self, _: &Self::Options) -> bool { + false + } +} + +#[cfg(test)] +mod tests { + use geo_types::Coord; + use geo_types::Geometry; + use geo_types::LineString as GeoLineString; + use rstest::rstest; + use vortex_array::ArrayRef; + use vortex_array::Columnar; + use vortex_array::ExecutionCtx; + use vortex_array::IntoArray; + use vortex_array::VortexSessionExecute; + use vortex_array::arrays::ConstantArray; + use vortex_array::arrays::ExtensionArray; + use vortex_array::arrays::PrimitiveArray; + use vortex_array::arrays::StructArray; + use vortex_array::assert_arrays_eq; + use vortex_array::dtype::DType; + use vortex_array::dtype::extension::ExtDType; + use vortex_array::scalar::Scalar; + use vortex_array::scalar_fn::EmptyOptions; + use vortex_array::scalar_fn::ScalarFnVTable; + use vortex_array::validity::Validity; + use vortex_error::VortexResult; + use vortex_error::vortex_err; + + use super::GeoMakeLine; + use crate::extension::GeoMetadata; + use crate::extension::LineString; + use crate::extension::Point; + use crate::extension::coordinate::Dimension; + use crate::extension::coordinate::coordinate_dimension; + use crate::extension::coordinate::ordinates; + use crate::extension::flatten_coordinates; + use crate::extension::geometries; + use crate::scalar_fn::length::GeoLength; + use crate::test_harness::nullable_point_column; + use crate::test_harness::point_column; + + fn dimensional_point( + dimension: Dimension, + coordinate: [f64; 4], + crs: Option<&str>, + ) -> VortexResult { + let mut fields = vec![ + ("x", PrimitiveArray::from_iter([coordinate[0]]).into_array()), + ("y", PrimitiveArray::from_iter([coordinate[1]]).into_array()), + ]; + if matches!(dimension, Dimension::Xyz | Dimension::Xyzm) { + fields.push(("z", PrimitiveArray::from_iter([coordinate[2]]).into_array())); + } + if matches!(dimension, Dimension::Xym | Dimension::Xyzm) { + fields.push(("m", PrimitiveArray::from_iter([coordinate[3]]).into_array())); + } + let storage = StructArray::from_fields(&fields)?.into_array(); + let dtype = ExtDType::::try_new( + GeoMetadata { + crs: crs.map(str::to_owned), + }, + storage.dtype().clone(), + )?; + Ok(ExtensionArray::try_new(dtype.erased(), storage)?.into_array()) + } + + fn point_constant( + x: f64, + y: f64, + len: usize, + ctx: &mut ExecutionCtx, + ) -> VortexResult { + let scalar = point_column(vec![x], vec![y])?.execute_scalar(0, ctx)?; + Ok(ConstantArray::new(scalar, len).into_array()) + } + + #[test] + fn connects_paired_points_in_operand_order() -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let starts = point_column(vec![0.0, 3.0], vec![0.0, 4.0])?; + let ends = point_column(vec![3.0, 0.0], vec![4.0, 0.0])?; + + let lines = GeoMakeLine::try_new_array(starts, ends)?.into_array(); + assert!(lines.dtype().as_extension().is::()); + assert_eq!( + geometries(&lines, &mut ctx)?, + vec![ + Geometry::LineString(GeoLineString::new(vec![ + Coord { x: 0.0, y: 0.0 }, + Coord { x: 3.0, y: 4.0 }, + ])), + Geometry::LineString(GeoLineString::new(vec![ + Coord { x: 3.0, y: 4.0 }, + Coord { x: 0.0, y: 0.0 }, + ])), + ] + ); + Ok(()) + } + + #[test] + fn two_constants_are_built_once_and_remain_constant() -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let starts = point_constant(0.0, 0.0, 3, &mut ctx)?; + let ends = point_constant(3.0, 4.0, 3, &mut ctx)?; + + let result = GeoMakeLine::try_new_array(starts, ends)? + .into_array() + .execute::(&mut ctx)?; + let Columnar::Constant(lines) = result else { + return Err(vortex_err!( + "make_line of two constants should remain constant" + )); + }; + assert_eq!(lines.len(), 3); + assert_eq!( + geometries(&lines.into_array(), &mut ctx)?, + vec![ + Geometry::LineString(GeoLineString::new(vec![ + Coord { x: 0.0, y: 0.0 }, + Coord { x: 3.0, y: 4.0 }, + ])); + 3 + ] + ); + Ok(()) + } + + #[rstest] + #[case::constant_start(true)] + #[case::constant_end(false)] + fn constant_and_column_are_paired_by_row(#[case] constant_start: bool) -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let constant = point_constant(0.0, 0.0, 2, &mut ctx)?; + let column = point_column(vec![3.0, 6.0], vec![4.0, 8.0])?; + let (starts, ends) = if constant_start { + (constant, column) + } else { + (column, constant) + }; + + let lines = GeoMakeLine::try_new_array(starts, ends)?.into_array(); + let endpoints = [(3.0, 4.0), (6.0, 8.0)]; + let expected = endpoints + .into_iter() + .map(|(x, y)| { + let constant = Coord { x: 0.0, y: 0.0 }; + let column = Coord { x, y }; + Geometry::LineString(GeoLineString::new(if constant_start { + vec![constant, column] + } else { + vec![column, constant] + })) + }) + .collect::>(); + assert_eq!(geometries(&lines, &mut ctx)?, expected); + Ok(()) + } + + #[test] + fn null_constant_is_all_null() -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let point_dtype = point_column(vec![0.0], vec![0.0])?.dtype().as_nullable(); + let starts = ConstantArray::new(Scalar::null(point_dtype), 2).into_array(); + let ends = point_column(vec![3.0, 6.0], vec![4.0, 8.0])?; + + let result = GeoMakeLine::try_new_array(starts, ends)? + .into_array() + .execute::(&mut ctx)?; + let Columnar::Constant(lines) = result else { + return Err(vortex_err!( + "make_line with a null constant should remain constant" + )); + }; + assert_eq!(lines.len(), 2); + assert!(lines.scalar().is_null()); + assert!(lines.dtype().as_extension().is::()); + Ok(()) + } + + /// A null endpoint produces a null line, which in turn produces a null length when the two + /// scalar functions are composed. + #[test] + fn propagates_endpoint_nulls_through_length() -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let starts = nullable_point_column(vec![Some((0.0, 0.0)), None, Some((0.0, 0.0))])?; + let ends = nullable_point_column(vec![Some((3.0, 4.0)), Some((1.0, 1.0)), None])?; + + let lines = GeoMakeLine::try_new_array(starts, ends)?.into_array(); + let lengths = GeoLength::try_new_array(lines)?.into_array(); + let expected = PrimitiveArray::new( + vec![5.0f64, 0.0, 0.0], + Validity::from_iter([true, false, false]), + ) + .into_array(); + + assert_arrays_eq!(lengths, expected, &mut ctx); + Ok(()) + } + + #[rstest] + #[case::xy_xyz( + Dimension::Xy, + [1.0, 2.0, 0.0, 0.0], + Dimension::Xyz, + [3.0, 4.0, 5.0, 0.0], + Dimension::Xyz, + Some([0.0, 5.0]), + None + )] + #[case::xyz_xy( + Dimension::Xyz, + [1.0, 2.0, 5.0, 0.0], + Dimension::Xy, + [3.0, 4.0, 0.0, 0.0], + Dimension::Xyz, + Some([5.0, 0.0]), + None + )] + #[case::xym_xyz( + Dimension::Xym, + [1.0, 2.0, 0.0, 6.0], + Dimension::Xyz, + [3.0, 4.0, 5.0, 0.0], + Dimension::Xyzm, + Some([0.0, 5.0]), + Some([6.0, 0.0]) + )] + #[case::xyz_xym( + Dimension::Xyz, + [1.0, 2.0, 5.0, 0.0], + Dimension::Xym, + [3.0, 4.0, 0.0, 6.0], + Dimension::Xyzm, + Some([5.0, 0.0]), + Some([0.0, 6.0]) + )] + fn promotes_mixed_point_dimensions( + #[case] start_dimension: Dimension, + #[case] start: [f64; 4], + #[case] end_dimension: Dimension, + #[case] end: [f64; 4], + #[case] expected_dimension: Dimension, + #[case] expected_z: Option<[f64; 2]>, + #[case] expected_m: Option<[f64; 2]>, + ) -> VortexResult<()> { + let session = vortex_array::array_session(); + let mut ctx = session.create_execution_ctx(); + let start = dimensional_point(start_dimension, start, None)?; + let end = dimensional_point(end_dimension, end, None)?; + + let lines = GeoMakeLine::try_new_array(start, end)?.into_array(); + let vertices = flatten_coordinates(&lines, &mut ctx)?; + assert_eq!(coordinate_dimension(vertices.dtype())?, expected_dimension); + for (name, expected) in [("z", expected_z), ("m", expected_m)] { + if let Some(expected) = expected { + assert_eq!( + ordinates(&vertices, name, &mut ctx)? + .iter() + .copied() + .collect::>(), + expected + ); + } + } + Ok(()) + } + + #[rstest] + #[case::matching(Some("EPSG:4326"), Some("EPSG:4326"), Some("EPSG:4326"))] + #[case::left_only(Some("EPSG:4326"), None, Some("EPSG:4326"))] + #[case::right_only(None, Some("EPSG:3857"), Some("EPSG:3857"))] + #[case::unreferenced(None, None, None)] + fn propagates_compatible_crs( + #[case] left_crs: Option<&str>, + #[case] right_crs: Option<&str>, + #[case] expected: Option<&str>, + ) -> VortexResult<()> { + let left = dimensional_point(Dimension::Xy, [0.0; 4], left_crs)?; + let right = dimensional_point(Dimension::Xy, [1.0; 4], right_crs)?; + + let dtype = GeoMakeLine.return_dtype( + &EmptyOptions, + &[left.dtype().clone(), right.dtype().clone()], + )?; + assert_eq!( + dtype.as_extension().metadata::().crs.as_deref(), + expected + ); + Ok(()) + } + + #[test] + fn rejects_mismatched_crs() -> VortexResult<()> { + let left = dimensional_point(Dimension::Xy, [0.0; 4], Some("EPSG:4326"))?; + let right = dimensional_point(Dimension::Xy, [1.0; 4], Some("EPSG:3857"))?; + assert!( + GeoMakeLine + .return_dtype( + &EmptyOptions, + &[left.dtype().clone(), right.dtype().clone()] + ) + .is_err() + ); + Ok(()) + } + + #[test] + fn rejects_non_point_dtype() -> VortexResult<()> { + let points = point_column(vec![0.0], vec![0.0])?; + assert!( + GeoMakeLine + .return_dtype(&EmptyOptions, std::slice::from_ref(points.dtype())) + .is_err() + ); + let non_point = DType::Bool(vortex_array::dtype::Nullability::NonNullable); + assert!( + GeoMakeLine + .return_dtype(&EmptyOptions, &[points.dtype().clone(), non_point]) + .is_err() + ); + Ok(()) + } +} diff --git a/vortex-geo/src/scalar_fn/mod.rs b/vortex-geo/src/scalar_fn/mod.rs index e6770be4fff..5e25f0e3774 100644 --- a/vortex-geo/src/scalar_fn/mod.rs +++ b/vortex-geo/src/scalar_fn/mod.rs @@ -8,3 +8,5 @@ pub mod distance; pub mod envelope; mod execute; pub mod intersects; +pub mod length; +pub mod make_line; From 72a4ae1772da3e10a48c03a8f690a69df538d208 Mon Sep 17 00:00:00 2001 From: Nemo Yu Date: Wed, 5 Aug 2026 16:07:58 -0400 Subject: [PATCH 2/2] bench(vortex-geo): cover make-line and length Signed-off-by: Nemo Yu --- vortex-geo/Cargo.toml | 8 +++ vortex-geo/benches/length.rs | 100 ++++++++++++++++++++++++++ vortex-geo/benches/make_line.rs | 120 ++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 vortex-geo/benches/length.rs create mode 100644 vortex-geo/benches/make_line.rs diff --git a/vortex-geo/Cargo.toml b/vortex-geo/Cargo.toml index 6eddab411d9..1a1001b688a 100644 --- a/vortex-geo/Cargo.toml +++ b/vortex-geo/Cargo.toml @@ -58,5 +58,13 @@ harness = false name = "distance" harness = false +[[bench]] +name = "make_line" +harness = false + +[[bench]] +name = "length" +harness = false + [lints] workspace = true diff --git a/vortex-geo/benches/length.rs b/vortex-geo/benches/length.rs new file mode 100644 index 00000000000..5d267eba600 --- /dev/null +++ b/vortex-geo/benches/length.rs @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! Microbenchmarks for native `ST_Length` over LineStrings. +//! +//! The two-vertex case tracks ordinary route segments, while the longer-line case captures the +//! per-vertex traversal cost. The nullable case measures strict null propagation separately. +//! +//! Run with `cargo bench -p vortex-geo --bench length`. + +#![expect(clippy::unwrap_used)] + +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::length::GeoLength; +use vortex_geo::test_harness::geo_session; +use vortex_geo::test_harness::linestring_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 = LazyLock::new(geo_session); + +const ROWS: usize = 512; + +fn main() { + divan::main(); +} + +/// A deterministic vertex ordinate. +fn ordinate(i: usize) -> f64 { + (i.wrapping_mul(2_654_435_761) % 10_000) as f64 / 100.0 +} + +fn linestrings(vertices: usize) -> ArrayRef { + linestring_column( + (0..ROWS) + .map(|row| { + (0..vertices) + .map(|vertex| (ordinate(row + vertex), ordinate(row + vertex + 1))) + .collect() + }) + .collect(), + ) + .unwrap() +} + +fn lengths(lines: &ArrayRef, ctx: &mut ExecutionCtx) -> ArrayRef { + GeoLength::try_new_array(lines.clone()) + .unwrap() + .into_array() + .execute::(ctx) + .unwrap() + .into_array() +} + +#[divan::bench] +fn two_vertex_lines(bencher: Bencher) { + let lines = linestrings(2); + let mut ctx = SESSION.create_execution_ctx(); + bencher + .counter(ItemsCount::new(ROWS)) + .bench_local(|| lengths(&lines, &mut ctx)); +} + +#[divan::bench] +fn sixteen_vertex_lines(bencher: Bencher) { + let lines = linestrings(16); + let mut ctx = SESSION.create_execution_ctx(); + bencher + .counter(ItemsCount::new(ROWS)) + .bench_local(|| lengths(&lines, &mut ctx)); +} + +#[divan::bench] +fn nullable_two_vertex_lines(bencher: Bencher) { + let lines = MaskedArray::try_new( + linestrings(2), + Validity::from_iter((0..ROWS).map(|i| !i.is_multiple_of(8))), + ) + .unwrap() + .into_array(); + let mut ctx = SESSION.create_execution_ctx(); + bencher + .counter(ItemsCount::new(ROWS)) + .bench_local(|| lengths(&lines, &mut ctx)); +} diff --git a/vortex-geo/benches/make_line.rs b/vortex-geo/benches/make_line.rs new file mode 100644 index 00000000000..5f87b7e0f2c --- /dev/null +++ b/vortex-geo/benches/make_line.rs @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Copyright the Vortex contributors + +//! Microbenchmarks for native `ST_MakeLine`. +//! +//! The cases cover the normal paired-column operation, a broadcast point constant, and strict +//! null propagation. They execute the result to its canonical representation so the benchmark +//! includes construction of the two-vertex line storage. +//! +//! Run with `cargo bench -p vortex-geo --bench make_line`. + +#![expect(clippy::unwrap_used)] + +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::ConstantArray; +use vortex_geo::scalar_fn::make_line::GeoMakeLine; +use vortex_geo::test_harness::geo_session; +use vortex_geo::test_harness::nullable_point_column; +use vortex_geo::test_harness::point_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 = LazyLock::new(geo_session); + +const ROWS: usize = 512; + +fn main() { + divan::main(); +} + +/// Deterministic pseudo-random value in `[0, 1)`. +fn unit(i: usize) -> f64 { + ((i.wrapping_mul(2_654_435_761) >> 8) % 10_000) as f64 / 10_000.0 +} + +fn points(offset: usize) -> ArrayRef { + let xs = (0..ROWS) + .map(|i| 300.0 * unit(i + offset) - 150.0) + .collect(); + let ys = (0..ROWS) + .map(|i| 300.0 * unit(i + offset + 1) - 150.0) + .collect(); + point_column(xs, ys).unwrap() +} + +fn nullable_points(offset: usize, null_every: usize) -> ArrayRef { + nullable_point_column( + (0..ROWS) + .map(|i| { + (!i.is_multiple_of(null_every)).then(|| { + ( + 300.0 * unit(i + offset) - 150.0, + 300.0 * unit(i + offset + 1) - 150.0, + ) + }) + }) + .collect(), + ) + .unwrap() +} + +fn point_constant(ctx: &mut ExecutionCtx) -> ArrayRef { + let scalar = point_column(vec![0.0], vec![0.0]) + .unwrap() + .execute_scalar(0, ctx) + .unwrap(); + ConstantArray::new(scalar, ROWS).into_array() +} + +fn make_lines(starts: &ArrayRef, ends: &ArrayRef, ctx: &mut ExecutionCtx) -> ArrayRef { + GeoMakeLine::try_new_array(starts.clone(), ends.clone()) + .unwrap() + .into_array() + .execute::(ctx) + .unwrap() + .into_array() +} + +#[divan::bench] +fn column_x_column(bencher: Bencher) { + let starts = points(0); + let ends = points(97); + let mut ctx = SESSION.create_execution_ctx(); + bencher + .counter(ItemsCount::new(ROWS)) + .bench_local(|| make_lines(&starts, &ends, &mut ctx)); +} + +#[divan::bench] +fn column_x_constant(bencher: Bencher) { + let starts = points(0); + let mut ctx = SESSION.create_execution_ctx(); + let end = point_constant(&mut ctx); + bencher + .counter(ItemsCount::new(ROWS)) + .bench_local(|| make_lines(&starts, &end, &mut ctx)); +} + +#[divan::bench] +fn nullable_columns(bencher: Bencher) { + let starts = nullable_points(0, 8); + let ends = nullable_points(97, 11); + let mut ctx = SESSION.create_execution_ctx(); + bencher + .counter(ItemsCount::new(ROWS)) + .bench_local(|| make_lines(&starts, &ends, &mut ctx)); +}