vortex_spatial/prune/
mod.rs1mod contains;
13mod distance;
14mod intersects;
15#[cfg(test)]
16mod test_harness;
17
18pub use contains::SpatialContainsPrune;
19pub use distance::SpatialDistancePrune;
20use geo::BoundingRect;
21use geo::Rect as SpatialRect;
22pub use intersects::SpatialIntersectsPrune;
23use vortex_array::VortexSessionExecute;
24use vortex_array::aggregate_fn::AggregateFnVTableExt;
25use vortex_array::aggregate_fn::EmptyOptions;
26use vortex_array::scalar::NumericOperator;
27use vortex_array::scalar::Scalar;
28use vortex_array::scalar_fn::builtin::get_item::GetItem;
29use vortex_array::scalar_fn::builtin::literal::Literal;
30use vortex_error::VortexResult;
31use vortex_expr::BoundExpression;
32use vortex_expr::bound::checked_add;
33use vortex_expr::bound::get_item;
34use vortex_expr::bound::gt;
35use vortex_expr::bound::gt_eq;
36use vortex_expr::bound::lit;
37use vortex_expr::bound::lt;
38use vortex_expr::bound::lt_eq;
39use vortex_expr::bound::numeric;
40use vortex_expr::stats::bound::stat;
41use vortex_functions::expr::bound::case_when;
42use vortex_functions::expr::bound::ext_storage;
43use vortex_session::VortexSession;
44
45use crate::aggregate_fn::GeometryAabb;
46use crate::extension::is_native_geometry;
47use crate::extension::single_geometry;
48
49fn geometry_and_constant(
55 expr: &BoundExpression,
56) -> VortexResult<Option<(&BoundExpression, &Scalar)>> {
57 let (lhs, rhs) = (expr.child(0), expr.child(1));
59 let (geom, constant) = if is_column(lhs) {
60 (lhs, rhs)
61 } else if is_column(rhs) {
62 (rhs, lhs)
63 } else {
64 return Ok(None);
65 };
66
67 if !is_native_geometry(geom.dtype()) {
70 return Ok(None);
71 }
72
73 Ok(constant.as_opt::<Literal>().map(|scalar| (geom, scalar)))
74}
75
76fn is_column(mut expr: &BoundExpression) -> bool {
77 while expr.is::<GetItem>() {
78 expr = expr.child(0);
79 }
80 expr.is_root()
81}
82
83fn query_aabb(
89 constant: &Scalar,
90 session: &VortexSession,
91) -> VortexResult<Option<SpatialRect<f64>>> {
92 if constant.is_null() {
94 return Ok(None);
95 }
96 let mut exec = session.create_execution_ctx();
99 Ok(single_geometry(constant, &mut exec)?.bounding_rect())
100}
101
102fn aabb_stat(geom: &BoundExpression) -> BoundExpression {
107 ext_storage(stat(geom.clone(), GeometryAabb.bind(EmptyOptions)))
110}
111
112fn min_dist_sq(aabb: &BoundExpression, query: SpatialRect<f64>) -> BoundExpression {
117 let field = |name: &str| get_item(name, aabb.clone());
118 let gap = |q_lo: f64, q_hi: f64, lo: BoundExpression, hi: BoundExpression| {
122 maximum(
123 lit(0.0),
124 maximum(
125 numeric(NumericOperator::Sub, lit(q_lo), hi),
126 numeric(NumericOperator::Sub, lo, lit(q_hi)),
127 ),
128 )
129 };
130 let dx = gap(query.min().x, query.max().x, field("xmin"), field("xmax"));
131 let dy = gap(query.min().y, query.max().y, field("ymin"), field("ymax"));
132 checked_add(square(dx), square(dy))
133}
134
135fn max_dist_sq(aabb: &BoundExpression, query: SpatialRect<f64>) -> BoundExpression {
139 let field = |name: &str| get_item(name, aabb.clone());
140 let span = |q_lo: f64, q_hi: f64, lo: BoundExpression, hi: BoundExpression| {
144 numeric(
145 NumericOperator::Sub,
146 maximum(lit(q_hi), hi),
147 minimum(lit(q_lo), lo),
148 )
149 };
150 let dx = span(query.min().x, query.max().x, field("xmin"), field("xmax"));
151 let dy = span(query.min().y, query.max().y, field("ymin"), field("ymax"));
152 checked_add(square(dx), square(dy))
153}
154
155fn square(e: BoundExpression) -> BoundExpression {
157 numeric(NumericOperator::Mul, e.clone(), e)
158}
159
160fn maximum(a: BoundExpression, b: BoundExpression) -> BoundExpression {
162 case_when(gt(a.clone(), b.clone()), a, b)
163}
164
165fn minimum(a: BoundExpression, b: BoundExpression) -> BoundExpression {
167 case_when(lt(a.clone(), b.clone()), a, b)
168}