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


##########
rust/sedona-raster-functions/src/rs_band_accessors.rs:
##########
@@ -0,0 +1,413 @@
+// 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 std::sync::Arc;
+use std::vec;
+
+use crate::executor::RasterExecutor;
+use arrow_array::builder::{Float64Builder, StringBuilder};
+use arrow_array::{cast::AsArray, types::Int32Type};
+use arrow_schema::DataType;
+use datafusion_common::error::Result;
+use datafusion_expr::{ColumnarValue, Volatility};
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_raster::traits::RasterRef;
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+
+// ===========================================================================
+// RS_BandPixelType
+// ===========================================================================
+
+/// RS_BandPixelType() scalar UDF implementation
+///
+/// Returns the pixel data type of the specified band as a string.
+/// Accepts an optional band_index parameter (1-based, default is 1).
+pub fn rs_bandpixeltype_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "rs_bandpixeltype",
+        vec![
+            Arc::new(RsBandPixelType {}),
+            Arc::new(RsBandPixelTypeWithBand {}),
+        ],
+        Volatility::Immutable,
+    )
+}
+
+#[derive(Debug)]
+struct RsBandPixelType {}
+
+impl SedonaScalarKernel for RsBandPixelType {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_raster()],
+            SedonaType::Arrow(DataType::Utf8),
+        );
+        matcher.match_args(args)
+    }
+
+    fn invoke_batch(
+        &self,
+        arg_types: &[SedonaType],
+        args: &[ColumnarValue],
+    ) -> Result<ColumnarValue> {
+        let executor = RasterExecutor::new(arg_types, args);
+        let mut builder =
+            StringBuilder::with_capacity(executor.num_iterations(), 
executor.num_iterations() * 20);
+
+        executor
+            .execute_raster_void(|_i, raster_opt| get_pixel_type(raster_opt, 
1, &mut builder))?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+#[derive(Debug)]
+struct RsBandPixelTypeWithBand {}
+
+impl SedonaScalarKernel for RsBandPixelTypeWithBand {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_raster(), ArgMatcher::is_integer()],
+            SedonaType::Arrow(DataType::Utf8),
+        );
+        matcher.match_args(args)
+    }
+
+    fn invoke_batch(
+        &self,
+        arg_types: &[SedonaType],
+        args: &[ColumnarValue],
+    ) -> Result<ColumnarValue> {
+        let executor = RasterExecutor::new(arg_types, args);
+        let band_index_array = 
args[1].clone().into_array(executor.num_iterations())?;
+        let band_index_array = band_index_array.as_primitive::<Int32Type>();

Review Comment:
   Probably better to use DataFusion's downcasting here (`as_int32_array()`, 
checks the downcast and returns an internal error in debug mode).



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