Kontinuation commented on code in PR #615:
URL: https://github.com/apache/sedona-db/pull/615#discussion_r2906277708
##########
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:
We can do this in a separate PR and replace other ad-hoc item-crs schema
checks with `SedonaType::is_item_crs_type`.
--
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]