You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ja...@apache.org on 2026/08/03 17:45:15 UTC
(sedona-db) branch main updated: refactor(rust/sedona-raster): validate outdb #band=N in split_outdb_band_fragment (#1105)
This is an automated email from the ASF dual-hosted git repository.
james-willis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/sedona-db.git
The following commit(s) were added to refs/heads/main by this push:
new 6c0c7b2db refactor(rust/sedona-raster): validate outdb #band=N in split_outdb_band_fragment (#1105)
6c0c7b2db is described below
commit 6c0c7b2db857f24e27794497ea1b83aad65dd766
Author: James Willis <ja...@users.noreply.github.com>
AuthorDate: Mon Aug 3 10:45:10 2026 -0700
refactor(rust/sedona-raster): validate outdb #band=N in split_outdb_band_fragment (#1105)
---
rust/sedona-raster-functions/src/rs_bandpath.rs | 2 +-
.../src/gdal_dataset_provider.rs | 6 +-
rust/sedona-raster-gdal/src/lib.rs | 1 -
rust/sedona-raster-gdal/src/raster_loader.rs | 5 +-
rust/sedona-raster-gdal/src/source_uri.rs | 223 ---------------------
rust/sedona-raster/src/traits.rs | 208 +++++++++++++++++--
6 files changed, 194 insertions(+), 251 deletions(-)
diff --git a/rust/sedona-raster-functions/src/rs_bandpath.rs b/rust/sedona-raster-functions/src/rs_bandpath.rs
index c06c609ff..6575a0292 100644
--- a/rust/sedona-raster-functions/src/rs_bandpath.rs
+++ b/rust/sedona-raster-functions/src/rs_bandpath.rs
@@ -134,7 +134,7 @@ fn get_band_path(
match band.outdb_uri() {
Some(uri) => {
let (base_url, _band_id) =
- sedona_raster::traits::split_outdb_band_fragment(uri);
+ sedona_raster::traits::split_outdb_band_fragment(uri)?;
builder.append_value(base_url)
}
None => builder.append_null(),
diff --git a/rust/sedona-raster-gdal/src/gdal_dataset_provider.rs b/rust/sedona-raster-gdal/src/gdal_dataset_provider.rs
index 87d93e631..5b954d041 100644
--- a/rust/sedona-raster-gdal/src/gdal_dataset_provider.rs
+++ b/rust/sedona-raster-gdal/src/gdal_dataset_provider.rs
@@ -305,7 +305,8 @@ impl GDALDatasetCache {
let uri = band.outdb_uri().ok_or_else(|| {
exec_datafusion_err!("Band {} is out-db but missing outdb_uri", i)
})?;
- let (url, source_band_num_u32) = split_outdb_band_fragment(uri);
+ let (url, source_band_num_u32) =
+ split_outdb_band_fragment(uri).map_err(|e| arrow_datafusion_err!(e))?;
let source_band_num: usize = source_band_num_u32
.try_into()
.map_err(|_| exec_datafusion_err!("Band {} out-db band_id is too large", i))?;
@@ -502,7 +503,8 @@ impl VrtKey {
// URI; in-db bands have no URI hint here.
let (outdb_url, outdb_band_id) = match band.outdb_uri() {
Some(uri) => {
- let (url, band_num) = split_outdb_band_fragment(uri);
+ let (url, band_num) =
+ split_outdb_band_fragment(uri).map_err(|e| arrow_datafusion_err!(e))?;
(Some(normalize_outdb_source_path(&url)), Some(band_num))
}
None => (None, None),
diff --git a/rust/sedona-raster-gdal/src/lib.rs b/rust/sedona-raster-gdal/src/lib.rs
index 08673ef71..102bbdfa9 100644
--- a/rust/sedona-raster-gdal/src/lib.rs
+++ b/rust/sedona-raster-gdal/src/lib.rs
@@ -45,7 +45,6 @@ mod rs_reproject_match;
mod rs_resample;
mod rs_tile;
mod rs_zonal_stats;
-mod source_uri;
mod utils;
// Re-export main dataset conversion functions
diff --git a/rust/sedona-raster-gdal/src/raster_loader.rs b/rust/sedona-raster-gdal/src/raster_loader.rs
index 4142cc6c4..92f4ef5c0 100644
--- a/rust/sedona-raster-gdal/src/raster_loader.rs
+++ b/rust/sedona-raster-gdal/src/raster_loader.rs
@@ -67,12 +67,11 @@ use async_trait::async_trait;
use datafusion_common::{DataFusionError, Result as DFResult};
use sedona_gdal::raster::rasterband::RasterBand;
use sedona_raster::raster_loader::{AsyncRasterLoader, RasterLoadRequest, RasterLoadResult};
-use sedona_raster::traits::is_spatial_dim_pair;
+use sedona_raster::traits::{is_spatial_dim_pair, split_outdb_band_fragment};
use sedona_schema::raster::BandDataType;
use crate::gdal_common::{convert_gdal_err, gdal_to_band_data_type, with_gdal};
use crate::gdal_dataset_provider::thread_local_cache;
-use crate::source_uri::parse_outdb_source;
/// Diagnostic name for the GDAL raster loader (reported via
/// [`AsyncRasterLoader::name`]). GDAL is a catch-all loader — it doesn't key
@@ -250,7 +249,7 @@ impl GdalLoader {
let mut buffers = Vec::new();
for req in reqs {
// `#band=N` fragment, with N defaulting to 1 if absent.
- let (path, band_num) = parse_outdb_source(&req.uri)?;
+ let (path, band_num) = split_outdb_band_fragment(&req.uri)?;
let cache = thread_local_cache()?;
let dataset = cache.get_or_create_outdb_source(gdal, &path, None)?;
let band = dataset
diff --git a/rust/sedona-raster-gdal/src/source_uri.rs b/rust/sedona-raster-gdal/src/source_uri.rs
deleted file mode 100644
index b611f352c..000000000
--- a/rust/sedona-raster-gdal/src/source_uri.rs
+++ /dev/null
@@ -1,223 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-//! GDAL-format-driver-internal parser for out-db raster source URIs.
-//!
-//! When a band's `outdb_format` dispatches to the GDAL driver, the loader
-//! uses this helper to extract a 1-based source band index from `outdb_uri`
-//! via the SedonaDB convention `<uri>#band=N`. The convention is private to
-//! the GDAL driver — the schema and format-agnostic surfaces (e.g.
-//! `RS_BandPath`) treat `outdb_uri` as opaque. Other format drivers handle
-//! their own URIs however they like.
-
-use datafusion_common::{error::Result, exec_err};
-
-/// Parse a SedonaDB out-db source URI into the GDAL-side URI and 1-based
-/// source band index.
-///
-/// Behaviour:
-///
-/// - `<uri>#band=N` where `N` parses as a `u32` in `1..=u32::MAX`: strips
-/// the fragment and returns `(<uri>, N)`.
-/// - `<uri>#band=...` with a value that is not a positive `u32` (zero,
-/// negative, non-numeric, empty, or overflowing `u32`): returns an
-/// `Execution` error. The user explicitly asked for a band; we refuse to
-/// silently substitute a default.
-/// - GDAL-native subdataset URIs (e.g. `HDF5:"x.h5":/var`,
-/// `NETCDF:"x.nc":var`, `GTIFF_DIR:1:multi.tif`) and any URI whose
-/// fragment is not `band=...`: pass through verbatim with default band
-/// index 1.
-/// - Plain URIs without any fragment: pass through verbatim with default
-/// band index 1.
-pub(crate) fn parse_outdb_source(uri: &str) -> Result<(String, u32)> {
- // rsplit lets a trailing `#band=N` win over any earlier `#anchor` in the
- // URI — useful for users who append the SedonaDB convention to a URI
- // that already carries a fragment.
- if let Some((prefix, fragment)) = uri.rsplit_once('#') {
- if let Some(band_str) = fragment.strip_prefix("band=") {
- return match band_str.parse::<u32>() {
- Ok(band) if band >= 1 => Ok((prefix.to_string(), band)),
- _ => exec_err!(
- "Invalid band index in outdb URI fragment '#band={band_str}': expected a positive integer in 1..=u32::MAX"
- ),
- };
- }
- }
- Ok((uri.to_string(), 1))
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn no_fragment_defaults_to_band_one() {
- assert_eq!(
- parse_outdb_source("s3://bucket/file.tif").unwrap(),
- ("s3://bucket/file.tif".to_string(), 1),
- );
- }
-
- #[test]
- fn band_fragment_extracts_index() {
- assert_eq!(
- parse_outdb_source("s3://bucket/file.tif#band=42").unwrap(),
- ("s3://bucket/file.tif".to_string(), 42),
- );
- }
-
- #[test]
- fn band_one_fragment_round_trips() {
- assert_eq!(
- parse_outdb_source("s3://bucket/file.tif#band=1").unwrap(),
- ("s3://bucket/file.tif".to_string(), 1),
- );
- }
-
- #[test]
- fn band_max_u32_accepted() {
- let max = u32::MAX;
- let uri = format!("s3://bucket/file.tif#band={max}");
- assert_eq!(
- parse_outdb_source(&uri).unwrap(),
- ("s3://bucket/file.tif".to_string(), max),
- );
- }
-
- #[test]
- fn band_zero_errors() {
- let msg = parse_outdb_source("s3://bucket/file.tif#band=0")
- .unwrap_err()
- .to_string();
- assert!(msg.contains("band=0"), "msg was: {msg}");
- assert!(msg.contains("positive integer"), "msg was: {msg}");
- }
-
- #[test]
- fn negative_band_errors() {
- let msg = parse_outdb_source("s3://bucket/file.tif#band=-2")
- .unwrap_err()
- .to_string();
- assert!(msg.contains("band=-2"), "msg was: {msg}");
- }
-
- #[test]
- fn band_overflow_errors() {
- // 4294967296 = u32::MAX + 1
- let msg = parse_outdb_source("s3://bucket/file.tif#band=4294967296")
- .unwrap_err()
- .to_string();
- assert!(msg.contains("band=4294967296"), "msg was: {msg}");
- }
-
- #[test]
- fn non_numeric_band_errors() {
- let msg = parse_outdb_source("s3://bucket/file.tif#band=abc")
- .unwrap_err()
- .to_string();
- assert!(msg.contains("band=abc"), "msg was: {msg}");
- }
-
- #[test]
- fn empty_band_value_errors() {
- let msg = parse_outdb_source("s3://bucket/file.tif#band=")
- .unwrap_err()
- .to_string();
- assert!(msg.contains("band="), "msg was: {msg}");
- }
-
- #[test]
- fn non_band_fragment_passes_through() {
- let uri = "s3://bucket/file.tif#section";
- assert_eq!(parse_outdb_source(uri).unwrap(), (uri.to_string(), 1));
- }
-
- #[test]
- fn empty_fragment_passes_through() {
- let uri = "s3://bucket/file.tif#";
- assert_eq!(parse_outdb_source(uri).unwrap(), (uri.to_string(), 1));
- }
-
- #[test]
- fn url_query_string_preserved_with_band_fragment() {
- assert_eq!(
- parse_outdb_source("https://example.com/r.tif?token=abc#band=3").unwrap(),
- ("https://example.com/r.tif?token=abc".to_string(), 3),
- );
- }
-
- #[test]
- fn url_query_string_with_non_band_fragment_passes_through() {
- let uri = "https://example.com/r.tif?token=abc#anchor";
- assert_eq!(parse_outdb_source(uri).unwrap(), (uri.to_string(), 1));
- }
-
- #[test]
- fn local_path_with_band_fragment() {
- assert_eq!(
- parse_outdb_source("/tmp/file.tif#band=5").unwrap(),
- ("/tmp/file.tif".to_string(), 5),
- );
- }
-
- #[test]
- fn local_path_without_fragment() {
- assert_eq!(
- parse_outdb_source("/tmp/file.tif").unwrap(),
- ("/tmp/file.tif".to_string(), 1),
- );
- }
-
- #[test]
- fn gdal_subdataset_hdf5_passthrough() {
- let uri = r#"HDF5:"/path/x.h5"://temperature"#;
- assert_eq!(parse_outdb_source(uri).unwrap(), (uri.to_string(), 1));
- }
-
- #[test]
- fn gdal_subdataset_netcdf_passthrough() {
- let uri = r#"NETCDF:"/path/file.nc":variable"#;
- assert_eq!(parse_outdb_source(uri).unwrap(), (uri.to_string(), 1));
- }
-
- #[test]
- fn gdal_subdataset_gtiff_dir_passthrough() {
- let uri = "GTIFF_DIR:1:/path/multi.tif";
- assert_eq!(parse_outdb_source(uri).unwrap(), (uri.to_string(), 1));
- }
-
- #[test]
- fn gdal_subdataset_with_band_fragment_extracts_band() {
- let uri = r#"HDF5:"/path/x.h5":/var#band=3"#;
- let (gdal_uri, band) = parse_outdb_source(uri).unwrap();
- assert_eq!(gdal_uri, r#"HDF5:"/path/x.h5":/var"#);
- assert_eq!(band, 3);
- }
-
- #[test]
- fn trailing_band_wins_over_earlier_anchor() {
- assert_eq!(
- parse_outdb_source("https://example.com/r.tif#anchor#band=7").unwrap(),
- ("https://example.com/r.tif#anchor".to_string(), 7),
- );
- }
-
- #[test]
- fn empty_uri() {
- assert_eq!(parse_outdb_source("").unwrap(), (String::new(), 1));
- }
-}
diff --git a/rust/sedona-raster/src/traits.rs b/rust/sedona-raster/src/traits.rs
index 9f4beafaf..550c272f1 100644
--- a/rust/sedona-raster/src/traits.rs
+++ b/rust/sedona-raster/src/traits.rs
@@ -121,23 +121,36 @@ impl<'a> NdBuffer<'a> {
}
}
-/// Parse the SedonaDB `#band=N` fragment out of an out-DB URI.
-/// Returns `(base_url, band_id)`; band_id defaults to 1 if the fragment is
-/// absent or malformed (e.g. a non-positive-integer band value), in which
-/// case the whole string is treated as the base URL. This lenient parsing
-/// is the shared convention consumers use to recover the source path and
+/// Parse the SedonaDB `#band=N` fragment out of an out-DB URI into the base
+/// URL and 1-based source band index.
+///
+/// Behaviour:
+///
+/// - `<uri>#band=N` where `N` parses as a `u32` in `1..=u32::MAX`: strips the
+/// fragment and returns `(<uri>, N)`. `rsplit_once` lets a trailing
+/// `#band=N` win over any earlier `#anchor` already in the URI.
+/// - `<uri>#band=...` with a value that is not a positive `u32` (zero,
+/// negative, non-numeric, empty, or overflowing `u32`): returns an error.
+/// The user explicitly asked for a band; we refuse to silently substitute a
+/// default.
+/// - Any URI whose fragment is not `band=...` (including GDAL-native
+/// subdataset URIs like `HDF5:"x.h5":/var`) and plain URIs without a
+/// fragment: pass through verbatim with default band index 1.
+///
+/// This is the shared convention consumers use to recover the source path and
/// band from a band's `outdb_uri()`.
-pub fn split_outdb_band_fragment(uri: &str) -> (String, u32) {
- if let Some(hash_pos) = uri.rfind('#') {
- let (base, fragment) = uri.split_at(hash_pos);
- let fragment = &fragment[1..]; // skip the '#'
- if let Some(rest) = fragment.strip_prefix("band=") {
- if let Ok(n) = rest.parse::<u32>() {
- return (base.to_string(), n);
- }
+pub fn split_outdb_band_fragment(uri: &str) -> Result<(String, u32), ArrowError> {
+ if let Some((prefix, fragment)) = uri.rsplit_once('#') {
+ if let Some(band_str) = fragment.strip_prefix("band=") {
+ return match band_str.parse::<u32>() {
+ Ok(band) if band >= 1 => Ok((prefix.to_string(), band)),
+ _ => Err(ArrowError::InvalidArgumentError(format!(
+ "Invalid band index in outdb URI fragment '#band={band_str}': expected a positive integer in 1..=u32::MAX"
+ ))),
+ };
}
}
- (uri.to_string(), 1)
+ Ok((uri.to_string(), 1))
}
/// Iteration view over a raster's bands. Returned by `RasterRef::bands()`.
@@ -826,24 +839,26 @@ mod tests {
#[test]
fn test_split_outdb_band_fragment_with_band() {
- let (base, n) = split_outdb_band_fragment("s3://bucket/file.tif#band=42");
+ let (base, n) = split_outdb_band_fragment("s3://bucket/file.tif#band=42").unwrap();
assert_eq!(base, "s3://bucket/file.tif");
assert_eq!(n, 42);
}
#[test]
fn test_split_outdb_band_fragment_without_band_defaults_to_1() {
- let (base, n) = split_outdb_band_fragment("s3://bucket/file.tif");
+ let (base, n) = split_outdb_band_fragment("s3://bucket/file.tif").unwrap();
assert_eq!(base, "s3://bucket/file.tif");
assert_eq!(n, 1);
}
#[test]
- fn test_split_outdb_band_fragment_malformed_fragment_defaults_to_1() {
- // `#band=abc` is malformed; treat the whole string as the base URL.
- let (base, n) = split_outdb_band_fragment("s3://bucket/file.tif#band=abc");
- assert_eq!(base, "s3://bucket/file.tif#band=abc");
- assert_eq!(n, 1);
+ fn test_split_outdb_band_fragment_malformed_fragment_errors() {
+ // `#band=abc` explicitly requests a band but is not a positive integer;
+ // reject it rather than silently substituting the default band 1.
+ let msg = split_outdb_band_fragment("s3://bucket/file.tif#band=abc")
+ .unwrap_err()
+ .to_string();
+ assert!(msg.contains("band=abc"), "msg was: {msg}");
}
fn ve(source_axis: i64, start: i64, step: i64, steps: i64) -> ViewEntry {
@@ -1030,4 +1045,155 @@ mod tests {
// Zero stride (broadcast) is not packed.
assert!(!ndbuf(&[2, 3], &[0, 1], 0).is_contiguous());
}
+
+ #[test]
+ fn split_outdb_band_one_fragment_round_trips() {
+ assert_eq!(
+ split_outdb_band_fragment("s3://bucket/file.tif#band=1").unwrap(),
+ ("s3://bucket/file.tif".to_string(), 1),
+ );
+ }
+
+ #[test]
+ fn split_outdb_band_max_u32_accepted() {
+ let max = u32::MAX;
+ let uri = format!("s3://bucket/file.tif#band={max}");
+ assert_eq!(
+ split_outdb_band_fragment(&uri).unwrap(),
+ ("s3://bucket/file.tif".to_string(), max),
+ );
+ }
+
+ #[test]
+ fn split_outdb_band_zero_errors() {
+ let msg = split_outdb_band_fragment("s3://bucket/file.tif#band=0")
+ .unwrap_err()
+ .to_string();
+ assert!(msg.contains("band=0"), "msg was: {msg}");
+ assert!(msg.contains("positive integer"), "msg was: {msg}");
+ }
+
+ #[test]
+ fn split_outdb_negative_band_errors() {
+ let msg = split_outdb_band_fragment("s3://bucket/file.tif#band=-2")
+ .unwrap_err()
+ .to_string();
+ assert!(msg.contains("band=-2"), "msg was: {msg}");
+ }
+
+ #[test]
+ fn split_outdb_band_overflow_errors() {
+ // 4294967296 = u32::MAX + 1
+ let msg = split_outdb_band_fragment("s3://bucket/file.tif#band=4294967296")
+ .unwrap_err()
+ .to_string();
+ assert!(msg.contains("band=4294967296"), "msg was: {msg}");
+ }
+
+ #[test]
+ fn split_outdb_empty_band_value_errors() {
+ let msg = split_outdb_band_fragment("s3://bucket/file.tif#band=")
+ .unwrap_err()
+ .to_string();
+ assert!(msg.contains("band="), "msg was: {msg}");
+ }
+
+ #[test]
+ fn split_outdb_non_band_fragment_passes_through() {
+ let uri = "s3://bucket/file.tif#section";
+ assert_eq!(
+ split_outdb_band_fragment(uri).unwrap(),
+ (uri.to_string(), 1)
+ );
+ }
+
+ #[test]
+ fn split_outdb_empty_fragment_passes_through() {
+ let uri = "s3://bucket/file.tif#";
+ assert_eq!(
+ split_outdb_band_fragment(uri).unwrap(),
+ (uri.to_string(), 1)
+ );
+ }
+
+ #[test]
+ fn split_outdb_url_query_string_preserved_with_band_fragment() {
+ assert_eq!(
+ split_outdb_band_fragment("https://example.com/r.tif?token=abc#band=3").unwrap(),
+ ("https://example.com/r.tif?token=abc".to_string(), 3),
+ );
+ }
+
+ #[test]
+ fn split_outdb_url_query_string_with_non_band_fragment_passes_through() {
+ let uri = "https://example.com/r.tif?token=abc#anchor";
+ assert_eq!(
+ split_outdb_band_fragment(uri).unwrap(),
+ (uri.to_string(), 1)
+ );
+ }
+
+ #[test]
+ fn split_outdb_local_path_with_band_fragment() {
+ assert_eq!(
+ split_outdb_band_fragment("/tmp/file.tif#band=5").unwrap(),
+ ("/tmp/file.tif".to_string(), 5),
+ );
+ }
+
+ #[test]
+ fn split_outdb_local_path_without_fragment() {
+ assert_eq!(
+ split_outdb_band_fragment("/tmp/file.tif").unwrap(),
+ ("/tmp/file.tif".to_string(), 1),
+ );
+ }
+
+ #[test]
+ fn split_outdb_gdal_subdataset_hdf5_passthrough() {
+ let uri = r#"HDF5:"/path/x.h5"://temperature"#;
+ assert_eq!(
+ split_outdb_band_fragment(uri).unwrap(),
+ (uri.to_string(), 1)
+ );
+ }
+
+ #[test]
+ fn split_outdb_gdal_subdataset_netcdf_passthrough() {
+ let uri = r#"NETCDF:"/path/file.nc":variable"#;
+ assert_eq!(
+ split_outdb_band_fragment(uri).unwrap(),
+ (uri.to_string(), 1)
+ );
+ }
+
+ #[test]
+ fn split_outdb_gdal_subdataset_gtiff_dir_passthrough() {
+ let uri = "GTIFF_DIR:1:/path/multi.tif";
+ assert_eq!(
+ split_outdb_band_fragment(uri).unwrap(),
+ (uri.to_string(), 1)
+ );
+ }
+
+ #[test]
+ fn split_outdb_gdal_subdataset_with_band_fragment_extracts_band() {
+ let uri = r#"HDF5:"/path/x.h5":/var#band=3"#;
+ let (gdal_uri, band) = split_outdb_band_fragment(uri).unwrap();
+ assert_eq!(gdal_uri, r#"HDF5:"/path/x.h5":/var"#);
+ assert_eq!(band, 3);
+ }
+
+ #[test]
+ fn split_outdb_trailing_band_wins_over_earlier_anchor() {
+ assert_eq!(
+ split_outdb_band_fragment("https://example.com/r.tif#anchor#band=7").unwrap(),
+ ("https://example.com/r.tif#anchor".to_string(), 7),
+ );
+ }
+
+ #[test]
+ fn split_outdb_empty_uri() {
+ assert_eq!(split_outdb_band_fragment("").unwrap(), (String::new(), 1));
+ }
}