Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/korp-map

crates/korp-map-wire/tests/zcols_on_a_real_cut.rs

//! **The WKB→native conversion against the export's own native copies**, on a real cut.
//!
//! osm-katana writes `geometry_z6/z9/z12` native since `osm-katana-native-zcols`; before
//! that they were WKB and this crate turned them native ([`NativeBuilder::push_wkb`],
//! NULL for a copy of another type). Both exports of one WKB cut hold the same rows in
//! the same order, so the conversion of the WKB copies must equal the native copies
//! array for array.
//!
//! Runs with `KORP_MAP_WIRE_WKB_Z_CUT=<export with WKB copies>` and
//! `KORP_MAP_WIRE_NATIVE_Z_CUT=<export with native copies>` (release build advised).

use std::fs::File;
use std::path::{Path, PathBuf};

use arrow_array::Array;
use arrow_array::cast::AsArray;
use korp_map_wire::Kind;
use korp_map_wire::geoarrow::NativeBuilder;
use parquet::arrow::ProjectionMask;
use parquet::arrow::arrow_reader::{ParquetRecordBatchReader, ParquetRecordBatchReaderBuilder};

const BATCH: usize = 65_536;

fn reader(p: &Path, column: &str) -> ParquetRecordBatchReader {
    let b = ParquetRecordBatchReaderBuilder::try_new(File::open(p).unwrap()).unwrap();
    let i = b.schema().index_of(column).unwrap();
    let mask = ProjectionMask::roots(b.parquet_schema(), [i]);
    b.with_projection(mask)
        .with_batch_size(BATCH)
        .build()
        .unwrap()
}

#[test]
fn the_wkb_copies_converted_equal_the_exported_native_copies() {
    let (Ok(wkb), Ok(native)) = (
        std::env::var("KORP_MAP_WIRE_WKB_Z_CUT"),
        std::env::var("KORP_MAP_WIRE_NATIVE_Z_CUT"),
    ) else {
        eprintln!("set KORP_MAP_WIRE_WKB_Z_CUT and KORP_MAP_WIRE_NATIVE_Z_CUT to run");
        return;
    };
    let files = [
        ("ways.multilinestring.parquet", Kind::Lines),
        ("ways.multipolygon.parquet", Kind::Areas),
        ("relations.resolved.multipolygon.parquet", Kind::Areas),
    ];
    for (name, kind) in files {
        let (w, n) = (
            PathBuf::from(&wkb).join(name),
            PathBuf::from(&native).join(name),
        );
        for column in ["geometry_z6", "geometry_z9", "geometry_z12"] {
            let (mut rows, mut valid, mut nulled) = (0usize, 0usize, 0usize);
            let mut rn = reader(&n, column);
            for wb in reader(&w, column) {
                let wb = wb.unwrap();
                let nb = rn
                    .next()
                    .expect("the native file has as many rows")
                    .unwrap();
                let wc = wb.column(0).as_binary::<i32>();
                let mut b = NativeBuilder::new(kind);
                for v in wc.iter() {
                    match v {
                        Some(v) => nulled += usize::from(!b.push_wkb(v)),
                        None => b.push_null(),
                    }
                }
                let got = b.finish();
                let want = nb.column(0);
                assert_eq!(got.data_type(), want.data_type(), "{name} {column}");
                assert_eq!(got.len(), want.len(), "{name} {column} rows {rows}..");
                assert!(
                    got.as_ref() == want.as_ref(),
                    "{name} {column}: rows {rows}..{} differ",
                    rows + got.len()
                );
                rows += got.len();
                valid += got.len() - got.null_count();
            }
            assert!(
                rn.next().is_none(),
                "{name} {column}: the native file has more rows"
            );
            eprintln!(
                "{name} {column}: {rows} rows, {valid} copies equal, {nulled} of another type NULL"
            );
        }
    }
}