use geo::Distance;
use geo::Euclidean;
use vortex_array::ArrayRef;
use vortex_array::arrays::ScalarFnArray;
use vortex_array::dtype::DType;
use vortex_array::scalar_fn::EmptyOptions;
use vortex_array::scalar_fn::ScalarFnId;
use vortex_array::scalar_fn::TypedScalarFnInstance;
use vortex_array::scalar_fn::unstable::row::RowFn;
use vortex_array::scalar_fn::unstable::row::RowVisitor;
use vortex_error::VortexResult;
use vortex_session::VortexSession;
use vortex_session::registry::CachedId;
use crate::scalar_fn::row::GeometryRow;
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct SpatialDistance;
impl SpatialDistance {
pub fn try_new(a: ArrayRef, b: ArrayRef) -> VortexResult<ScalarFnArray> {
ScalarFnArray::try_new(
TypedScalarFnInstance::new(SpatialDistance, EmptyOptions).erased(),
vec![a, b],
)
}
}
impl RowFn for SpatialDistance {
type Options = EmptyOptions;
const ARG_NAMES: &'static [&'static str] = &["a", "b"];
const INFALLIBLE: bool = true;
fn id(&self) -> ScalarFnId {
static ID: CachedId = CachedId::new("vortex.st.distance");
*ID
}
fn serialize(&self, _options: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(vec![]))
}
fn deserialize(
&self,
_metadata: &[u8],
_session: &VortexSession,
) -> VortexResult<Self::Options> {
Ok(EmptyOptions)
}
fn dispatch<V: RowVisitor>(
&self,
_options: &Self::Options,
_args: &[DType],
visitor: V,
) -> VortexResult<V::VisitResult> {
visitor.visit::<(GeometryRow, GeometryRow), f64>(|(a, b)| Euclidean.distance(a, b))
}
}
#[cfg(test)]
mod tests {
use std::sync::LazyLock;
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_array::arrays::PrimitiveArray;
use vortex_array::assert_arrays_eq;
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_array::validity::Validity;
use vortex_error::VortexResult;
use vortex_session::VortexSession;
use super::SpatialDistance;
use crate::test_harness::nullable_multipolygon_column;
use crate::test_harness::nullable_point_column;
use crate::test_harness::point_column;
use crate::test_harness::polygon_column;
static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
fn point_constant(
x: f64,
y: f64,
len: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let single = point_column(vec![x], vec![y])?.execute_scalar(0, ctx)?;
Ok(ConstantArray::new(single, len).into_array())
}
#[test]
fn distance_over_points() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = point_column(vec![0.0, 3.0, 0.0, 3.0], vec![0.0, 0.0, 4.0, 4.0])?;
let b = point_constant(0.0, 0.0, 4, &mut ctx)?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
assert_arrays_eq!(
distance,
PrimitiveArray::from_iter([0.0, 3.0, 4.0, 5.0]),
&mut ctx
);
Ok(())
}
#[test]
fn distance_between_columns() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = point_column(vec![0.0, 1.0], vec![0.0, 1.0])?;
let b = point_column(vec![3.0, 1.0], vec![4.0, 1.0])?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
assert_arrays_eq!(distance, PrimitiveArray::from_iter([5.0, 0.0]), &mut ctx);
Ok(())
}
#[test]
fn distance_to_constant_polygon_is_exact() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let ring = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (0.0, 4.0), (0.0, 0.0)];
let single = polygon_column(vec![vec![ring]])?.execute_scalar(0, &mut ctx)?;
let square = ConstantArray::new(single, 2).into_array();
let points = point_column(vec![7.0, 2.0], vec![2.0, 2.0])?;
let distance = SpatialDistance::try_new(points, square)?.into_array();
assert_arrays_eq!(distance, PrimitiveArray::from_iter([3.0, 0.0]), &mut ctx);
Ok(())
}
#[test]
fn distance_with_constant_first_operand() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = point_constant(0.0, 0.0, 4, &mut ctx)?;
let b = point_column(vec![0.0, 3.0, 0.0, 3.0], vec![0.0, 0.0, 4.0, 4.0])?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
assert_arrays_eq!(
distance,
PrimitiveArray::from_iter([0.0, 3.0, 4.0, 5.0]),
&mut ctx
);
Ok(())
}
#[test]
fn distance_between_two_constants() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = point_constant(0.0, 0.0, 3, &mut ctx)?;
let b = point_constant(3.0, 4.0, 3, &mut ctx)?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
assert_arrays_eq!(
distance,
PrimitiveArray::from_iter([5.0, 5.0, 5.0]),
&mut ctx
);
Ok(())
}
#[test]
fn output_nullability_mirrors_operands() -> VortexResult<()> {
let dtype = point_column(vec![0.0], vec![0.0])?.dtype().clone();
let non_nullable =
SpatialDistance.return_dtype(&EmptyOptions, &[dtype.clone(), dtype.clone()])?;
assert_eq!(
non_nullable,
DType::Primitive(PType::F64, Nullability::NonNullable)
);
let nullable =
SpatialDistance.return_dtype(&EmptyOptions, &[dtype.as_nullable(), dtype])?;
assert_eq!(
nullable,
DType::Primitive(PType::F64, Nullability::Nullable)
);
Ok(())
}
#[test]
fn distance_propagates_null_rows() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = nullable_point_column(vec![Some((0.0, 0.0)), None, Some((3.0, 4.0))])?;
let b = point_constant(0.0, 0.0, 3, &mut ctx)?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
let expected = PrimitiveArray::new(
vec![0.0f64, 0.0, 5.0],
Validity::from_iter([true, false, true]),
)
.into_array();
assert_arrays_eq!(distance, expected, &mut ctx);
Ok(())
}
#[test]
fn distance_propagates_nulls_for_multipolygon() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let near = vec![vec![vec![
(0.0, 0.0),
(4.0, 0.0),
(4.0, 4.0),
(0.0, 4.0),
(0.0, 0.0),
]]];
let far = vec![vec![vec![
(10.0, 6.0),
(14.0, 6.0),
(14.0, 10.0),
(10.0, 10.0),
(10.0, 6.0),
]]];
let a = nullable_multipolygon_column(vec![Some(near), None, Some(far)])?;
let b = point_constant(7.0, 2.0, 3, &mut ctx)?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
let expected = PrimitiveArray::new(
vec![3.0f64, 0.0, 5.0],
Validity::from_iter([true, false, true]),
)
.into_array();
assert_arrays_eq!(distance, expected, &mut ctx);
Ok(())
}
#[test]
fn distance_propagates_column_pair_nulls() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = nullable_point_column(vec![Some((0.0, 0.0)), None, Some((0.0, 0.0))])?;
let b = nullable_point_column(vec![Some((3.0, 4.0)), Some((1.0, 1.0)), None])?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
let expected = PrimitiveArray::new(
vec![5.0f64, 0.0, 0.0],
Validity::from_iter([true, false, false]),
)
.into_array();
assert_arrays_eq!(distance, expected, &mut ctx);
Ok(())
}
#[test]
fn distance_constant_null_is_all_null() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let point_dtype = point_column(vec![0.0], vec![0.0])?.dtype().as_nullable();
let null_const = ConstantArray::new(Scalar::null(point_dtype), 3).into_array();
let b = point_column(vec![0.0, 3.0, 0.0], vec![0.0, 0.0, 4.0])?;
let distance = SpatialDistance::try_new(null_const, b)?.into_array();
let expected = PrimitiveArray::new(vec![0.0f64; 3], Validity::AllInvalid).into_array();
assert_arrays_eq!(distance, expected, &mut ctx);
Ok(())
}
#[test]
fn distance_all_null_column_is_all_null() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = nullable_point_column(vec![None, None])?;
let b = point_constant(0.0, 0.0, 2, &mut ctx)?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
let expected = PrimitiveArray::new(vec![0.0f64; 2], Validity::AllInvalid).into_array();
assert_arrays_eq!(distance, expected, &mut ctx);
Ok(())
}
#[test]
fn distance_column_pair_all_null() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = nullable_point_column(vec![Some((0.0, 0.0)), None])?;
let b = nullable_point_column(vec![None, Some((1.0, 1.0))])?;
let distance = SpatialDistance::try_new(a, b)?.into_array();
let expected = PrimitiveArray::new(vec![0.0f64; 2], Validity::AllInvalid).into_array();
assert_arrays_eq!(distance, expected, &mut ctx);
Ok(())
}
#[test]
fn distance_empty_non_nullable_keeps_dtype() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let a = point_column(vec![], vec![])?;
let b = point_column(vec![], vec![])?;
let result = SpatialDistance::try_new(a, b)?
.into_array()
.execute::<Canonical>(&mut ctx)?
.into_array();
assert!(!result.dtype().is_nullable());
assert_eq!(result.len(), 0);
Ok(())
}
#[test]
fn non_geometry_operand_is_rejected() -> VortexResult<()> {
let spatial_dtype = point_column(vec![0.0], vec![0.0])?.dtype().clone();
let numeric = DType::Primitive(PType::I32, Nullability::NonNullable);
let result = SpatialDistance.return_dtype(&EmptyOptions, &[spatial_dtype, numeric]);
assert!(result.is_err());
Ok(())
}
}