james-willis commented on code in PR #812:
URL: https://github.com/apache/sedona-db/pull/812#discussion_r3184490020


##########
rust/sedona-raster-gdal/src/source_uri.rs:
##########
@@ -0,0 +1,228 @@
+// 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 std::borrow::Cow;
+
+/// 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 positive `u32`: strips the fragment
+///   and returns `(<uri>, N)`.
+/// - GDAL-native subdataset URIs (e.g. `HDF5:"x.h5":/var`,
+///   `NETCDF:"x.nc":var`, `GTIFF_DIR:1:multi.tif`): pass through verbatim
+///   with default band index 1.
+/// - Plain URIs without the fragment convention: pass through verbatim with
+///   default band index 1.
+/// - Fragments other than `band=N`, or `band=` values that fail to parse as
+///   a positive `u32` (non-numeric, zero, negative, overflowing `u32`): pass
+///   through verbatim with default band index 1. The fragment is left in
+///   place so the underlying loader can decide what to do with it.
+///
+/// The returned URI is always borrowed from the input; this function never
+/// allocates.
+pub(crate) fn parse_outdb_source(uri: &str) -> (Cow<'_, str>, 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=") {
+            if let Ok(band) = band_str.parse::<u32>() {
+                if band >= 1 {
+                    return (Cow::Borrowed(prefix), band);
+                }
+            }
+        }
+    }
+    (Cow::Borrowed(uri), 1)
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    fn parse(uri: &str) -> (String, u32) {
+        let (s, n) = parse_outdb_source(uri);
+        (s.into_owned(), n)
+    }
+
+    #[test]
+    fn no_fragment_defaults_to_band_one() {
+        assert_eq!(
+            parse("s3://bucket/file.tif"),
+            ("s3://bucket/file.tif".to_string(), 1),
+        );
+    }
+
+    #[test]
+    fn band_fragment_extracts_index() {
+        assert_eq!(
+            parse("s3://bucket/file.tif#band=42"),
+            ("s3://bucket/file.tif".to_string(), 42),
+        );
+    }
+
+    #[test]
+    fn band_one_fragment_round_trips() {
+        assert_eq!(
+            parse("s3://bucket/file.tif#band=1"),
+            ("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(&uri), ("s3://bucket/file.tif".to_string(), max));
+    }
+
+    #[test]
+    fn band_zero_passes_through() {
+        // band=0 is not a valid 1-based index; we leave the URI untouched
+        // so the loader can surface a clearer error.
+        let uri = "s3://bucket/file.tif#band=0";
+        assert_eq!(parse(uri), (uri.to_string(), 1));
+    }

Review Comment:
   may be better to throw some like InvalidBand Error? caller can catch if need 
be.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to