paleolimbot commented on code in PR #812:
URL: https://github.com/apache/sedona-db/pull/812#discussion_r3190043698


##########
rust/sedona-raster-gdal/src/lib.rs:
##########
@@ -31,6 +31,8 @@ mod gdal_common;
 // Temporary until https://github.com/apache/sedona-db/issues/804 is resolved.
 #[allow(dead_code)]
 mod gdal_dataset_provider;
+#[allow(dead_code)]
+mod source_uri;

Review Comment:
   ```suggestion
   #[cfg(test)]
   mod source_uri;
   ```
   
   I very much dislike merging allow dead code because it is very easy to 
accumulate it (at one point I removed hundreds of lines from 
sedona-spatial-join that ended up being unused and unnoticed). LLMs love adding 
it because it makes clippy errors go away. DataFusion no longer allows it (you 
must use `expect(dead_code)` if you absolutely must).



##########
rust/sedona-raster-gdal/src/source_uri.rs:
##########
@@ -0,0 +1,237 @@
+// 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;
+
+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.
+///
+/// On success the returned URI is always borrowed from the input; this
+/// function never allocates on the happy path.
+pub(crate) fn parse_outdb_source(uri: &str) -> Result<(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=") {
+            return match band_str.parse::<u32>() {
+                Ok(band) if band >= 1 => Ok((Cow::Borrowed(prefix), band)),
+                _ => exec_err!(
+                    "Invalid band index in outdb URI fragment 
'#band={band_str}': expected a positive integer in 1..=u32::MAX"
+                ),
+            };
+        }
+    }
+    Ok((Cow::Borrowed(uri), 1))
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    fn parse_ok(uri: &str) -> (String, u32) {
+        let (s, n) = parse_outdb_source(uri).expect("expected Ok");
+        (s.into_owned(), n)
+    }

Review Comment:
   These will give more informative backtraces in the failures if you just call 
`unwrap()` in the test



##########
rust/sedona-raster-gdal/src/source_uri.rs:
##########
@@ -0,0 +1,237 @@
+// 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;
+
+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.
+///
+/// On success the returned URI is always borrowed from the input; this
+/// function never allocates on the happy path.
+pub(crate) fn parse_outdb_source(uri: &str) -> Result<(Cow<'_, str>, u32)> {

Review Comment:
   Unless you think there will be a performance issue here, it's probably 
easier to just return a String so that the caller doesn't have to deal with 
lifetimes.



-- 
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