mod contains;
mod distance;
mod intersects;
#[cfg(test)]
mod test_harness;
pub use contains::SpatialContainsPrune;
pub use distance::SpatialDistancePrune;
use geo::BoundingRect;
use geo::Rect as SpatialRect;
pub use intersects::SpatialIntersectsPrune;
use vortex_array::VortexSessionExecute;
use vortex_array::aggregate_fn::AggregateFnVTableExt;
use vortex_array::aggregate_fn::EmptyOptions;
use vortex_array::scalar::NumericOperator;
use vortex_array::scalar::Scalar;
use vortex_array::scalar_fn::builtin::get_item::GetItem;
use vortex_array::scalar_fn::builtin::literal::Literal;
use vortex_error::VortexResult;
use vortex_expr::BoundExpression;
use vortex_expr::bound::checked_add;
use vortex_expr::bound::get_item;
use vortex_expr::bound::gt;
use vortex_expr::bound::gt_eq;
use vortex_expr::bound::lit;
use vortex_expr::bound::lt;
use vortex_expr::bound::lt_eq;
use vortex_expr::bound::numeric;
use vortex_expr::stats::bound::stat;
use vortex_functions::expr::bound::case_when;
use vortex_functions::expr::bound::ext_storage;
use vortex_session::VortexSession;
use crate::aggregate_fn::GeometryAabb;
use crate::extension::is_native_geometry;
use crate::extension::single_geometry;
fn geometry_and_constant(
expr: &BoundExpression,
) -> VortexResult<Option<(&BoundExpression, &Scalar)>> {
let (lhs, rhs) = (expr.child(0), expr.child(1));
let (geom, constant) = if is_column(lhs) {
(lhs, rhs)
} else if is_column(rhs) {
(rhs, lhs)
} else {
return Ok(None);
};
if !is_native_geometry(geom.dtype()) {
return Ok(None);
}
Ok(constant.as_opt::<Literal>().map(|scalar| (geom, scalar)))
}
fn is_column(mut expr: &BoundExpression) -> bool {
while expr.is::<GetItem>() {
expr = expr.child(0);
}
expr.is_root()
}
fn query_aabb(
constant: &Scalar,
session: &VortexSession,
) -> VortexResult<Option<SpatialRect<f64>>> {
if constant.is_null() {
return Ok(None);
}
let mut exec = session.create_execution_ctx();
Ok(single_geometry(constant, &mut exec)?.bounding_rect())
}
fn aabb_stat(geom: &BoundExpression) -> BoundExpression {
ext_storage(stat(geom.clone(), GeometryAabb.bind(EmptyOptions)))
}
fn min_dist_sq(aabb: &BoundExpression, query: SpatialRect<f64>) -> BoundExpression {
let field = |name: &str| get_item(name, aabb.clone());
let gap = |q_lo: f64, q_hi: f64, lo: BoundExpression, hi: BoundExpression| {
maximum(
lit(0.0),
maximum(
numeric(NumericOperator::Sub, lit(q_lo), hi),
numeric(NumericOperator::Sub, lo, lit(q_hi)),
),
)
};
let dx = gap(query.min().x, query.max().x, field("xmin"), field("xmax"));
let dy = gap(query.min().y, query.max().y, field("ymin"), field("ymax"));
checked_add(square(dx), square(dy))
}
fn max_dist_sq(aabb: &BoundExpression, query: SpatialRect<f64>) -> BoundExpression {
let field = |name: &str| get_item(name, aabb.clone());
let span = |q_lo: f64, q_hi: f64, lo: BoundExpression, hi: BoundExpression| {
numeric(
NumericOperator::Sub,
maximum(lit(q_hi), hi),
minimum(lit(q_lo), lo),
)
};
let dx = span(query.min().x, query.max().x, field("xmin"), field("xmax"));
let dy = span(query.min().y, query.max().y, field("ymin"), field("ymax"));
checked_add(square(dx), square(dy))
}
fn square(e: BoundExpression) -> BoundExpression {
numeric(NumericOperator::Mul, e.clone(), e)
}
fn maximum(a: BoundExpression, b: BoundExpression) -> BoundExpression {
case_when(gt(a.clone(), b.clone()), a, b)
}
fn minimum(a: BoundExpression, b: BoundExpression) -> BoundExpression {
case_when(lt(a.clone(), b.clone()), a, b)
}