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


##########
rust/sedona-raster-functions/src/executor.rs:
##########
@@ -45,6 +220,29 @@ impl<'a, 'b> RasterExecutor<'a, 'b> {
         }
     }
 
+    /// Create a new [RasterExecutor] with an explicit number of iterations.
+    ///
+    /// This is useful when the executor is built from a subset of the original
+    /// arguments (e.g. only raster + geometry) but the overall UDF should 
still
+    /// iterate according to other array arguments.
+    #[allow(dead_code)]

Review Comment:
   ```suggestion
       #[cfg(test)]
   ```



##########
rust/sedona-raster-functions/src/executor.rs:
##########
@@ -35,6 +40,176 @@ pub struct RasterExecutor<'a, 'b> {
     num_iterations: usize,
 }
 
+// The accessor types below use enum-based dispatch to handle different Arrow
+// array representations (Binary vs BinaryView, etc.) rather than trait objects
+// like `Box<dyn Iterator>`. Both approaches involve dynamic dispatch, but the
+// enum variant is simpler and avoids an extra heap allocation. Since raster
+// operations are expensive relative to per-element dispatch overhead, the cost
+// of matching on each access is negligible in practice.
+#[derive(Clone)]
+enum ItemWkbAccessor {
+    Binary(BinaryArray),
+    BinaryView(BinaryViewArray),
+}
+
+impl ItemWkbAccessor {
+    #[inline]
+    fn get(&self, i: usize) -> Option<&[u8]> {
+        match self {
+            Self::Binary(arr) => {
+                if arr.is_null(i) {
+                    None
+                } else {
+                    Some(arr.value(i))
+                }
+            }
+            Self::BinaryView(arr) => {
+                if arr.is_null(i) {
+                    None
+                } else {
+                    Some(arr.value(i))
+                }
+            }
+        }
+    }
+}
+
+// Same enum-dispatch rationale as `ItemWkbAccessor` above: the per-element
+// match cost is dwarfed by the raster and CRS operations performed on each 
row.
+enum GeomWkbCrsAccessor {
+    WkbArray {
+        wkb: ItemWkbAccessor,
+        static_crs: Crs,
+    },
+    WkbScalar {
+        wkb: Option<Vec<u8>>,
+        static_crs: Crs,
+    },
+    ItemCrsArray {
+        struct_array: StructArray,
+        item: ItemWkbAccessor,
+        crs: StringViewArray,
+        item_static_crs: Crs,
+        resolved_crs: Crs,
+    },
+    ItemCrsScalar {
+        struct_array: StructArray,
+        item: ItemWkbAccessor,
+        crs: StringViewArray,
+        item_static_crs: Crs,
+        resolved_crs: Crs,
+    },
+    Null,
+}
+
+impl GeomWkbCrsAccessor {
+    #[inline]
+    fn get(&mut self, i: usize) -> Result<(Option<&[u8]>, CrsRef<'_>)> {
+        match self {
+            Self::Null => Ok((None, None)),
+            Self::WkbArray { wkb, static_crs } => {
+                let maybe_wkb = wkb.get(i);
+                if maybe_wkb.is_none() {
+                    return Ok((None, None));
+                }
+                Ok((maybe_wkb, static_crs.as_deref()))
+            }
+            Self::WkbScalar { wkb, static_crs } => {
+                if wkb.is_none() {
+                    return Ok((None, None));
+                }
+                let _ = i;
+                Ok((wkb.as_deref(), static_crs.as_deref()))
+            }
+            Self::ItemCrsArray {
+                struct_array,
+                item,
+                crs,
+                item_static_crs,
+                resolved_crs,
+            } => {
+                if struct_array.is_null(i) {
+                    return Ok((None, None));
+                }
+
+                let maybe_wkb = item.get(i);
+                if maybe_wkb.is_none() {
+                    return Ok((None, None));
+                }
+
+                let item_crs_str = if crs.is_null(i) {
+                    None
+                } else {
+                    Some(crs.value(i))
+                };
+                *resolved_crs = resolve_item_crs(item_crs_str, 
item_static_crs)?;
+                Ok((maybe_wkb, resolved_crs.as_deref()))
+            }
+            Self::ItemCrsScalar {
+                struct_array,
+                item,
+                crs,
+                item_static_crs,
+                resolved_crs,
+            } => {
+                if struct_array.is_null(0) {
+                    return Ok((None, None));
+                }
+
+                let maybe_wkb = item.get(0);
+                if maybe_wkb.is_none() {
+                    return Ok((None, None));
+                }
+
+                let item_crs_str = if crs.is_null(0) {
+                    None
+                } else {
+                    Some(crs.value(0))
+                };
+                *resolved_crs = resolve_item_crs(item_crs_str, 
item_static_crs)?;
+                let _ = i;
+                Ok((maybe_wkb, resolved_crs.as_deref()))
+            }
+        }
+    }
+}
+
+fn resolve_item_crs(item_crs_str: Option<&str>, static_crs: &Crs) -> 
Result<Crs> {
+    let item_crs = if let Some(s) = item_crs_str {
+        deserialize_crs(s)?
+    } else {
+        None
+    };
+
+    match (&item_crs, static_crs) {
+        (None, None) => Ok(None),
+        (Some(_), None) => Ok(item_crs),
+        (None, Some(_)) => Ok(static_crs.clone()),
+        (Some(_), Some(_)) => {
+            if item_crs == *static_crs {
+                Ok(item_crs)
+            } else {
+                exec_err!("CRS values not equal: {item_crs:?} vs 
{static_crs:?}")
+            }
+        }
+    }
+}
+
+fn crs_from_sedona_type(sedona_type: &SedonaType) -> Crs {
+    match sedona_type {
+        SedonaType::Wkb(_, crs) | SedonaType::WkbView(_, crs) => crs.clone(),
+        _ => None,
+    }
+}
+
+fn is_item_crs_type(sedona_type: &SedonaType) -> bool {
+    matches!(
+        sedona_type,
+        SedonaType::Arrow(DataType::Struct(fields))
+            if fields.len() == 2 && fields[0].name() == "item" && 
fields[1].name() == "crs"
+    )
+}

Review Comment:
   No need to do it here but these could both be methods on the `SedonaType`



##########
docs/reference/sql/st_applydefaultcrs.qmd:
##########
@@ -0,0 +1,43 @@
+---
+# 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.
+
+title: ST_ApplyDefaultCRS

Review Comment:
   I would give this an `SD_*` prefix (and then you don't have to document it). 
I am not sure we want documentation for this because it's an implementation 
detail (all actual users should just do `ST_SetSRID()` or `ST_SetCrs()`).
   
   Alternatively, just put the definition in the raster benchmarks file since 
that's the only place we need it!



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