paleolimbot commented on code in PR #268: URL: https://github.com/apache/sedona-db/pull/268#discussion_r2491567900
########## rust/sedona-raster-functions/src/executor.rs: ########## @@ -0,0 +1,188 @@ +// 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. + +use arrow_array::{Array, ArrayRef, StructArray}; +use datafusion_common::error::Result; +use datafusion_common::{DataFusionError, ScalarValue}; +use datafusion_expr::ColumnarValue; +use sedona_raster::array::{RasterRefImpl, RasterStructArray}; +use sedona_schema::datatypes::SedonaType; +use sedona_schema::datatypes::RASTER; + +/// Helper for writing raster kernel implementations +/// +/// The [RasterExecutor] provides a simplified interface for executing functions +/// on raster arrays, handling the common pattern of downcasting to StructArray, +/// creating raster iterators, and handling null values. +pub struct RasterExecutor<'a, 'b> { + pub arg_types: &'a [SedonaType], + pub args: &'b [ColumnarValue], + num_iterations: usize, +} + +impl<'a, 'b> RasterExecutor<'a, 'b> { + /// Create a new [RasterExecutor] + pub fn new(arg_types: &'a [SedonaType], args: &'b [ColumnarValue]) -> Self { + Self { + arg_types, + args, + num_iterations: Self::calc_num_iterations(args), + } + } + + /// Return the number of iterations that will be performed + pub fn num_iterations(&self) -> usize { + self.num_iterations + } + + /// Execute a function by iterating over rasters in the first argument + /// + /// This handles the common pattern of: + /// 1. Downcasting array to StructArray + /// 2. Creating raster iterator + /// 3. Iterating with null checks + /// 4. Calling the provided function with each raster + pub fn execute_raster_void<F>(&self, mut func: F) -> Result<()> + where + F: FnMut(usize, Option<RasterRefImpl<'_>>) -> Result<()>, + { + if self.arg_types[0] != RASTER { + return Err(DataFusionError::Internal( + "First argument must be a raster type".to_string(), + )); Review Comment: Great point...Peter did a great job converting uses of `internal_err!()` to `sedona_internal_err!()` but there are a lot of `Err(DataFusionError::Internal)`s around, too. ########## rust/sedona-raster-functions/src/executor.rs: ########## @@ -0,0 +1,188 @@ +// 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. + +use arrow_array::{Array, ArrayRef, StructArray}; +use datafusion_common::error::Result; +use datafusion_common::{DataFusionError, ScalarValue}; +use datafusion_expr::ColumnarValue; +use sedona_raster::array::{RasterRefImpl, RasterStructArray}; +use sedona_schema::datatypes::SedonaType; +use sedona_schema::datatypes::RASTER; + +/// Helper for writing raster kernel implementations +/// +/// The [RasterExecutor] provides a simplified interface for executing functions +/// on raster arrays, handling the common pattern of downcasting to StructArray, +/// creating raster iterators, and handling null values. +pub struct RasterExecutor<'a, 'b> { + pub arg_types: &'a [SedonaType], + pub args: &'b [ColumnarValue], + num_iterations: usize, +} + +impl<'a, 'b> RasterExecutor<'a, 'b> { Review Comment: All good points...we'll also need to handle iterating over the CRS (resolving once at the top level and then iterating or looping over items) of the wkb as well and this is probably a great place to handle that. A `RasterExecutor` is definitely less fishy than the other thing I suggested 🙂 -- 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]
