Skip to main content

vortex_spatial/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4#![doc = include_str!(concat!("../", env!("CARGO_PKG_README")))]
5
6use vortex_array::aggregate_fn::session::AggregateFnSessionExt;
7use vortex_array::dtype::session::DTypeSessionExt;
8use vortex_array::scalar_fn::session::ScalarFnSessionExt;
9use vortex_arrow::ArrowSessionExt;
10use vortex_edition::EditionSessionExt;
11use vortex_error::VortexExpect;
12use vortex_expr::stats::session::StatsSessionExt;
13use vortex_session::VortexSession;
14
15use crate::aggregate_fn::GeometryAabb;
16use crate::extension::LineString;
17use crate::extension::MultiLineString;
18use crate::extension::MultiPoint;
19use crate::extension::MultiPolygon;
20use crate::extension::Point;
21use crate::extension::Polygon;
22use crate::extension::Rect;
23use crate::extension::WellKnownBinary;
24use crate::prune::SpatialContainsPrune;
25use crate::prune::SpatialDistancePrune;
26use crate::prune::SpatialIntersectsPrune;
27use crate::scalar_fn::area::SpatialArea;
28use crate::scalar_fn::collect::SpatialCollect;
29use crate::scalar_fn::contains::SpatialContains;
30use crate::scalar_fn::convex_hull::SpatialConvexHull;
31use crate::scalar_fn::distance::SpatialDistance;
32use crate::scalar_fn::envelope::SpatialEnvelope;
33use crate::scalar_fn::intersects::SpatialIntersects;
34use crate::scalar_fn::length::SpatialLength;
35use crate::scalar_fn::make_line::SpatialMakeLine;
36
37pub mod aggregate_fn;
38pub mod editions;
39pub mod extension;
40pub mod prune;
41pub mod scalar_fn;
42#[cfg(any(test, feature = "_test-harness"))]
43pub mod test_harness;
44#[cfg(test)]
45mod tests;
46
47/// Set up a session with spatial extension types, Arrow interop, functions, and pruning.
48pub fn initialize(session: &VortexSession) {
49    let dtypes = session.dtypes();
50    let arrow = session.arrow();
51
52    dtypes.register(WellKnownBinary);
53    arrow.register_extension(WellKnownBinary);
54    dtypes.register(Point);
55    arrow.register_extension(Point);
56    dtypes.register(LineString);
57    arrow.register_extension(LineString);
58    dtypes.register(MultiPoint);
59    arrow.register_extension(MultiPoint);
60    dtypes.register(Polygon);
61    arrow.register_extension(Polygon);
62    dtypes.register(MultiLineString);
63    arrow.register_extension(MultiLineString);
64    dtypes.register(MultiPolygon);
65    arrow.register_extension(MultiPolygon);
66    dtypes.register(Rect);
67    arrow.register_extension(Rect);
68
69    // Register the geometry scalar functions.
70    session.scalar_fns().register(SpatialArea);
71    session.scalar_fns().register(SpatialCollect);
72    session.scalar_fns().register(SpatialConvexHull);
73    session.scalar_fns().register(SpatialEnvelope);
74    session.scalar_fns().register(SpatialContains);
75    session.scalar_fns().register(SpatialDistance);
76    session.scalar_fns().register(SpatialIntersects);
77    session.scalar_fns().register(SpatialMakeLine);
78    session.scalar_fns().register(SpatialLength);
79
80    // The axis-aligned bounding-box (AABB) aggregate; self-declares as a per-chunk zone stat for
81    // geometry columns.
82    session.aggregate_fns().register(GeometryAabb);
83
84    // Register the spatial pruning rules that use that AABB.
85    session.stats().register_rewrite(SpatialContainsPrune);
86    session.stats().register_rewrite(SpatialDistancePrune);
87    session.stats().register_rewrite(SpatialIntersectsPrune);
88
89    // Spatial members belong to their own edition family, enabled here so the writer may emit
90    // the AABB zone stat this session just registered. `initialize` is idempotent, so a
91    // repeated call must not re-declare the edition.
92    if session
93        .editions()
94        .find(&editions::SPATIAL_2026_08)
95        .is_none()
96    {
97        session
98            .editions()
99            .declare_family(&editions::FAMILY)
100            .vortex_expect("spatial edition family is valid");
101        session
102            .register_edition(&editions::DECLARATION)
103            .vortex_expect("spatial edition declaration is valid");
104    }
105    session
106        .enable_edition(editions::SPATIAL_2026_08)
107        .vortex_expect("spatial edition is registered");
108}