vortex-spatial 0.95.0

Spatial data types and compute for Vortex
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Generated geometry families and metadata for Arrow transport checks.
//!
//! Each case samples one family so shrinking can reduce the geometry as well as its storage.

mod tests {
    use std::sync::Arc;

    use arrow_array::ArrayRef as ArrowArrayRef;
    use arrow_schema::Field;
    use geo_traits::to_geo::ToGeoGeometry;
    use geoarrow::datatypes::BoxType;
    use geoarrow::datatypes::Crs;
    use geoarrow::datatypes::LineStringType;
    use geoarrow::datatypes::Metadata;
    use geoarrow::datatypes::MultiLineStringType;
    use geoarrow::datatypes::MultiPointType;
    use geoarrow::datatypes::MultiPolygonType;
    use geoarrow::datatypes::PointType;
    use geoarrow::datatypes::PolygonType;
    use geoarrow::datatypes::WkbType;
    use hegel::TestCase;
    use hegel::generators as gs;
    use vortex_array::ArrayRef;
    use vortex_array::ExecutionCtx;
    use vortex_array::IntoArray;
    use vortex_array::VortexSessionExecute;
    use vortex_array::arrays::ExtensionArray;
    use vortex_array::arrays::ListArray;
    use vortex_array::arrays::PrimitiveArray;
    use vortex_array::arrays::StructArray;
    use vortex_array::builders::builder_with_capacity_in;
    use vortex_array::dtype::DType;
    use vortex_array::dtype::Nullability;
    use vortex_array::dtype::extension::ExtDType;
    use vortex_array::dtype::extension::ExtDTypeRef;
    use vortex_array::dtype::extension::ExtVTable;
    use vortex_array::scalar::Scalar;
    use vortex_array::validity::Validity;
    use vortex_arrow::test_harness::StorageCase;
    use vortex_arrow::test_harness::check_arrow;
    use vortex_arrow::test_harness::check_generated;
    use vortex_buffer::BufferAllocatorRef;
    use vortex_error::VortexExpect;
    use vortex_error::VortexResult;
    use vortex_session::VortexSession;

    use crate::extension::Dimension;
    use crate::extension::LineString;
    use crate::extension::MultiLineString;
    use crate::extension::MultiPoint;
    use crate::extension::MultiPolygon;
    use crate::extension::Point;
    use crate::extension::Polygon;
    use crate::extension::Rect;
    use crate::extension::SpatialMetadata;
    use crate::extension::WellKnownBinary;

    #[derive(Clone, Copy, Debug)]
    enum GeometryKind {
        Point,
        Rect,
        LineString,
        MultiPoint,
        Polygon,
        MultiLineString,
        MultiPolygon,
        Wkb,
    }

    impl GeometryKind {
        fn dtype(
            self,
            metadata: SpatialMetadata,
            dimension: Dimension,
            nullability: Nullability,
        ) -> VortexResult<ExtDTypeRef> {
            let metadata = if matches!(self, Self::Wkb) {
                metadata
            } else {
                metadata.with_dimension(dimension)
            };

            Ok(match self {
                Self::Point => ExtDType::try_with_vtable(Point, metadata, nullability)?.erased(),
                Self::Rect => ExtDType::try_with_vtable(Rect, metadata, nullability)?.erased(),
                Self::LineString => {
                    ExtDType::try_with_vtable(LineString, metadata, nullability)?.erased()
                }
                Self::MultiPoint => {
                    ExtDType::try_with_vtable(MultiPoint, metadata, nullability)?.erased()
                }
                Self::Polygon => {
                    ExtDType::try_with_vtable(Polygon, metadata, nullability)?.erased()
                }
                Self::MultiLineString => {
                    ExtDType::try_with_vtable(MultiLineString, metadata, nullability)?.erased()
                }
                Self::MultiPolygon => {
                    ExtDType::try_with_vtable(MultiPolygon, metadata, nullability)?.erased()
                }
                Self::Wkb => {
                    ExtDType::try_with_vtable(WellKnownBinary, metadata, nullability)?.erased()
                }
            })
        }
    }

    #[track_caller]
    fn check_spatial_round_trip(tc: &TestCase) -> VortexResult<()> {
        let session = VortexSession::empty();
        crate::initialize(&session);
        let mut ctx = session.create_execution_ctx();

        let len = tc.draw(gs::integers::<usize>().max_value(32));
        let nullability = tc.draw(gs::booleans()).into();
        let dimension = tc.draw(gs::sampled_from(&[
            Dimension::Xy,
            Dimension::Xyz,
            Dimension::Xym,
            Dimension::Xyzm,
        ]));
        let crs = tc.draw(gs::sampled_from(&[
            None,
            Some(""),
            Some("EPSG:4326"),
            Some("非ASCII"),
        ]));
        let metadata = SpatialMetadata::new(crs.map(Into::into));

        let kind = tc.draw(gs::sampled_from(&[
            GeometryKind::Point,
            GeometryKind::Rect,
            GeometryKind::LineString,
            GeometryKind::MultiPoint,
            GeometryKind::Polygon,
            GeometryKind::MultiLineString,
            GeometryKind::MultiPolygon,
            GeometryKind::Wkb,
        ]));
        let dtype = kind.dtype(metadata, dimension, nullability)?;
        let arrow_metadata = Arc::new(Metadata::new(
            crs.map(|s| Crs::from_unknown_crs_type(s.to_string()))
                .unwrap_or_default(),
            None,
        ));
        let dim = dimension.into();
        let field = match kind {
            GeometryKind::Point => {
                PointType::new(dim, arrow_metadata).to_field("value", nullability.is_nullable())
            }
            GeometryKind::Rect => {
                BoxType::new(dim, arrow_metadata).to_field("value", nullability.is_nullable())
            }
            GeometryKind::LineString => LineStringType::new(dim, arrow_metadata)
                .to_field("value", nullability.is_nullable()),
            GeometryKind::MultiPoint => MultiPointType::new(dim, arrow_metadata)
                .to_field("value", nullability.is_nullable()),
            GeometryKind::Polygon => {
                PolygonType::new(dim, arrow_metadata).to_field("value", nullability.is_nullable())
            }
            GeometryKind::MultiLineString => MultiLineStringType::new(dim, arrow_metadata)
                .to_field("value", nullability.is_nullable()),
            GeometryKind::MultiPolygon => MultiPolygonType::new(dim, arrow_metadata)
                .to_field("value", nullability.is_nullable()),
            GeometryKind::Wkb => {
                let mut field = Field::new(
                    "value",
                    arrow_schema::DataType::BinaryView,
                    nullability.is_nullable(),
                );
                field.try_with_extension_type(WkbType::new(arrow_metadata))?;
                field
            }
        };

        if matches!(kind, GeometryKind::Wkb) {
            let WkbCase {
                array: storage,
                bytes: source,
                ..
            } = draw_wkb_storage(tc, nullability, len)?;
            let array = ExtensionArray::try_new(dtype, storage)?.into_array();
            let bytes = source.iter().map(|row| row.as_deref());
            let alternate_layouts: [ArrowArrayRef; 2] = [
                Arc::new(arrow_array::BinaryArray::from_iter(bytes.clone())),
                Arc::new(arrow_array::LargeBinaryArray::from_iter(bytes.clone())),
            ];
            for expected in alternate_layouts {
                let target = field.clone().with_data_type(expected.data_type().clone());
                check_arrow(array.clone(), &target, expected, &mut ctx)?;
            }
            let expected = Arc::new(arrow_array::BinaryViewArray::from_iter(bytes));
            check_generated(tc, array, &field, expected, &mut ctx)
        } else {
            let source = (0..len)
                .map(|_| {
                    if nullability.is_nullable() && tc.draw(gs::booleans()) {
                        None
                    } else {
                        Some(draw_geometry(tc, dtype.storage_dtype(), kind))
                    }
                })
                .collect::<Vec<_>>();
            let storage =
                build_geometry(dtype.storage_dtype(), field.data_type(), &source, &mut ctx)?;
            let array = ExtensionArray::try_new(dtype, storage.array)?.into_array();
            check_generated(tc, array, &field, storage.arrow, &mut ctx)
        }
    }

    #[derive(Clone)]
    enum GeometryValue {
        Coordinates(Vec<f64>),
        List(Vec<Self>),
    }

    fn draw_geometry(tc: &TestCase, dtype: &DType, kind: GeometryKind) -> GeometryValue {
        match dtype {
            DType::Struct(fields, _) => {
                let mut values = (0..fields.names().len())
                    .map(|_| f64::from(tc.draw(gs::integers::<i32>())) / 8.0)
                    .collect::<Vec<_>>();
                if matches!(kind, GeometryKind::Rect) {
                    let axes = values.len() / 2;
                    for axis in 0..axes {
                        let (min, max) = (
                            values[axis].min(values[axes + axis]),
                            values[axis].max(values[axes + axis]),
                        );
                        values[axis] = min;
                        values[axes + axis] = max;
                    }
                }
                GeometryValue::Coordinates(values)
            }
            DType::List(child, _) => {
                let ring = matches!(kind, GeometryKind::Polygon | GeometryKind::MultiPolygon)
                    && matches!(child.as_ref(), DType::Struct(..));
                let len = if ring {
                    3
                } else {
                    tc.draw(gs::integers::<usize>().max_value(4))
                };
                let mut values = (0..len)
                    .map(|_| draw_geometry(tc, child, kind))
                    .collect::<Vec<_>>();
                if ring {
                    values.push(values[0].clone());
                }
                GeometryValue::List(values)
            }
            _ => unreachable!("native geometry storage contains lists and coordinate structs"),
        }
    }

    /// Builds both representations from source geometry values, using the independent Arrow field.
    /// Coordinate arities and list nesting must agree with the two storage dtypes.
    #[track_caller]
    fn build_geometry(
        dtype: &DType,
        arrow_dtype: &arrow_schema::DataType,
        source: &[Option<GeometryValue>],
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<StorageCase> {
        assert!(
            dtype.is_nullable() || source.iter().all(Option::is_some),
            "non-nullable geometry storage requires non-null source rows"
        );
        let validity = if dtype.is_nullable() {
            Validity::from_iter(source.iter().map(Option::is_some))
        } else {
            Validity::NonNullable
        };
        let nulls = dtype.is_nullable().then(|| {
            arrow_buffer::NullBuffer::from(source.iter().map(Option::is_some).collect::<Vec<_>>())
        });
        match dtype {
            DType::Struct(fields, _) => {
                for value in source.iter().flatten() {
                    let GeometryValue::Coordinates(values) = value else {
                        unreachable!("struct storage requires coordinate source rows")
                    };
                    assert_eq!(values.len(), fields.names().len());
                }

                let mut arrays = Vec::new();
                let mut arrow_arrays = Vec::new();
                let mut arrow_fields = Vec::new();
                for (index, name) in fields.names().iter().enumerate() {
                    let values = source
                        .iter()
                        .map(|v| match v {
                            Some(GeometryValue::Coordinates(v)) => v[index],
                            None => 0.0,
                            _ => unreachable!("struct storage requires coordinate source rows"),
                        })
                        .collect::<Vec<_>>();
                    arrays.push(PrimitiveArray::from_iter(values.clone()).into_array());
                    arrow_arrays
                        .push(Arc::new(arrow_array::Float64Array::from(values)) as ArrowArrayRef);
                    arrow_fields.push(Field::new(
                        name.as_ref(),
                        arrow_schema::DataType::Float64,
                        false,
                    ));
                }

                Ok(StorageCase {
                    array: StructArray::try_new(
                        fields.names().clone(),
                        arrays,
                        source.len(),
                        validity,
                    )?
                    .into_array(),
                    arrow: Arc::new(arrow_array::StructArray::try_new_with_length(
                        arrow_fields.into(),
                        arrow_arrays,
                        nulls,
                        source.len(),
                    )?),
                })
            }
            DType::List(child, _) => {
                let mut offsets = vec![0i32];
                let mut children = Vec::new();
                for value in source {
                    match value {
                        Some(GeometryValue::List(values)) => {
                            children.extend(values.iter().cloned().map(Some))
                        }
                        None => {}
                        Some(GeometryValue::Coordinates(_)) => {
                            unreachable!("list storage requires list source rows")
                        }
                    }
                    offsets.push(i32::try_from(children.len())?);
                }
                let arrow_schema::DataType::List(field) = arrow_dtype else {
                    unreachable!("GeoArrow list storage")
                };
                let child = build_geometry(child, field.data_type(), &children, ctx)?;
                let field = Arc::clone(field);
                Ok(StorageCase {
                    array: ListArray::try_new(
                        child.array,
                        PrimitiveArray::from_iter(offsets.clone()).into_array(),
                        validity,
                        ctx,
                    )?
                    .into_array(),
                    arrow: Arc::new(arrow_array::ListArray::try_new(
                        field,
                        arrow_buffer::OffsetBuffer::new(offsets.into()),
                        child.arrow,
                        nulls,
                    )?),
                })
            }
            _ => unreachable!("geometry storage has only list and struct levels"),
        }
    }

    struct WkbCase {
        array: ArrayRef,
        bytes: Vec<Option<Vec<u8>>>,
        geometries: Vec<Option<geo_types::Geometry>>,
    }

    fn draw_wkb_storage(
        tc: &TestCase,
        nullability: Nullability,
        len: usize,
    ) -> VortexResult<WkbCase> {
        let mut builder = builder_with_capacity_in(
            &DType::Binary(nullability),
            len,
            BufferAllocatorRef::static_ref(),
        );
        let mut bytes = Vec::with_capacity(len);
        let mut geometries = Vec::with_capacity(len);
        for _ in 0..len {
            if nullability.is_nullable() && tc.draw(gs::booleans()) {
                builder.append_nulls(1);
                bytes.push(None);
                geometries.push(None);
            } else {
                let x = f64::from(tc.draw(gs::integers::<i32>())) / 8.0;
                let y = f64::from(tc.draw(gs::integers::<i32>())) / 8.0;
                let geometry = if tc.draw(gs::booleans()) {
                    geo_types::Geometry::Point(geo_types::Point::new(x, y))
                } else {
                    geo_types::Geometry::Polygon(geo_types::Polygon::new(
                        geo_types::LineString::from(vec![
                            (x, y),
                            (x + 1.0, y),
                            (x + 1.0, y + 1.0),
                            (x, y),
                        ]),
                        vec![],
                    ))
                };
                let mut encoded = Vec::new();
                wkb::writer::write_geometry(
                    &mut encoded,
                    &geometry,
                    &wkb::writer::WriteOptions::default(),
                )
                .map_err(|e| vortex_error::vortex_err!("writing source WKB: {e}"))?;
                builder.append_scalar(&Scalar::binary(encoded.clone(), nullability))?;
                bytes.push(Some(encoded));
                geometries.push(Some(geometry));
            }
        }

        Ok(WkbCase {
            array: builder.finish(),
            bytes,
            geometries,
        })
    }

    #[track_caller]
    fn check_native_points(tc: &TestCase) -> VortexResult<()> {
        let session = VortexSession::empty();
        crate::initialize(&session);
        let mut ctx = session.create_execution_ctx();
        let len = tc.draw(gs::integers::<usize>().max_value(32));
        let nullable: Nullability = tc.draw(gs::booleans()).into();
        let dimension = tc.draw(gs::sampled_from(&[
            Dimension::Xy,
            Dimension::Xyz,
            Dimension::Xym,
            Dimension::Xyzm,
        ]));
        let dtype = ExtDType::try_with_vtable(
            Point,
            SpatialMetadata::default().with_dimension(dimension),
            nullable,
        )?;
        let source = (0..len)
            .map(|_| {
                if nullable.is_nullable() && tc.draw(gs::booleans()) {
                    None
                } else {
                    Some(draw_geometry(
                        tc,
                        dtype.storage_dtype(),
                        GeometryKind::Point,
                    ))
                }
            })
            .collect::<Vec<_>>();
        let field = PointType::new(dimension.into(), Default::default())
            .to_field("point", nullable.is_nullable());
        let storage = build_geometry(dtype.storage_dtype(), field.data_type(), &source, &mut ctx)?;
        for (i, source) in source.iter().enumerate() {
            let scalar = storage.array.execute_scalar(i, &mut ctx)?;
            let actual = scalar
                .value()
                .map(|v| Point::unpack_native(&dtype, v))
                .transpose()?;
            match (source, actual) {
                (None, None) => {}
                (Some(GeometryValue::Coordinates(source)), Some(actual)) => {
                    assert_eq!(actual.x, source[0]);
                    assert_eq!(actual.y, source[1]);
                    assert_eq!(
                        actual.z,
                        match dimension {
                            Dimension::Xyz | Dimension::Xyzm => Some(source[2]),
                            _ => None,
                        }
                    );
                    assert_eq!(
                        actual.m,
                        match dimension {
                            Dimension::Xym => Some(source[2]),
                            Dimension::Xyzm => Some(source[3]),
                            _ => None,
                        }
                    );
                }
                _ => panic!("native point nullability must match the source"),
            }
        }

        Ok(())
    }

    #[track_caller]
    fn check_native_wkb(tc: &TestCase) -> VortexResult<()> {
        let session = VortexSession::empty();
        crate::initialize(&session);
        let mut ctx = session.create_execution_ctx();

        let len = tc.draw(gs::integers::<usize>().max_value(32));
        let nullable: Nullability = tc.draw(gs::booleans()).into();
        let source = draw_wkb_storage(tc, nullable, len)?;
        let dtype =
            ExtDType::try_with_vtable(WellKnownBinary, SpatialMetadata::default(), nullable)?;
        for (i, expected) in source.geometries.iter().enumerate() {
            let scalar = source.array.execute_scalar(i, &mut ctx)?;
            let actual = scalar
                .value()
                .map(|v| WellKnownBinary::unpack_native(&dtype, v))
                .transpose()?;
            assert_eq!(actual.map(|g| g.to_geometry()), *expected);
        }

        Ok(())
    }

    /// The trailing null makes filtering leave unused coordinate children in valid Arrow lists.
    /// Reconstructing a GeoArrow polygon rejects that layout even though Arrow accepts it.
    #[track_caller]
    fn check_filtered_polygons(base: i32) -> VortexResult<()> {
        let session = VortexSession::empty();
        crate::initialize(&session);
        let mut ctx = session.create_execution_ctx();
        let dtype = ExtDType::try_with_vtable(
            Polygon,
            SpatialMetadata::default().with_dimension(Dimension::Xym),
            Nullability::Nullable,
        )?;
        let field =
            PolygonType::new(Dimension::Xym.into(), Default::default()).to_field("value", true);
        let mut source = (0..2)
            .map(|row| {
                let x = f64::from(base) + f64::from(row);
                Some(GeometryValue::List(vec![GeometryValue::List(vec![
                    GeometryValue::Coordinates(vec![x, 0.0, 0.0]),
                    GeometryValue::Coordinates(vec![x + 1.0, 0.0, 0.0]),
                    GeometryValue::Coordinates(vec![x, 1.0, 0.0]),
                    GeometryValue::Coordinates(vec![x, 0.0, 0.0]),
                ])]))
            })
            .collect::<Vec<_>>();
        source.push(None);
        let storage = build_geometry(dtype.storage_dtype(), field.data_type(), &source, &mut ctx)?;
        let array = ExtensionArray::try_new(dtype.erased(), storage.array)?.into_array();
        check_arrow(array, &field, storage.arrow, &mut ctx)
    }

    #[hegel::test(test_cases = 100, print_blob = true)]
    #[hegel::explicit_test_case(base = 0i32)]
    fn filtered_polygons(tc: TestCase) {
        let base = tc.draw(gs::integers::<i32>());
        check_filtered_polygons(base)
            .vortex_expect("filtered polygons must export with unused coordinate children");
    }

    #[hegel::test(test_cases = 100, print_blob = true)]
    fn native_points(tc: TestCase) {
        check_native_points(&tc)
            .vortex_expect("native point decoding must preserve source coordinates");
    }

    #[hegel::test(test_cases = 100, print_blob = true)]
    fn native_wkb(tc: TestCase) {
        check_native_wkb(&tc).vortex_expect("native WKB decoding must preserve source geometries");
    }

    #[hegel::test(test_cases = 100, print_blob = true)]
    fn spatial_arrow_transport(tc: TestCase) {
        check_spatial_round_trip(&tc)
            .vortex_expect("spatial Arrow transport must preserve dtype and stored values");
    }
}