-
Notifications
You must be signed in to change notification settings - Fork 43
feat(rust/sedona-raster-functions): add RS_NumBands function #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // 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, vec}; | ||
|
|
||
| use crate::executor::RasterExecutor; | ||
| use arrow_array::builder::UInt32Builder; | ||
| use arrow_schema::DataType; | ||
| use datafusion_common::error::Result; | ||
| use datafusion_expr::{ | ||
| scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, | ||
| }; | ||
| use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; | ||
| use sedona_raster::traits::RasterRef; | ||
| use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; | ||
|
|
||
| /// RS_NumBands() scalar UDF implementation | ||
| /// | ||
| /// Returns the number of bands in the raster | ||
| pub fn rs_numbands_udf() -> SedonaScalarUDF { | ||
| SedonaScalarUDF::new( | ||
| "rs_numbands", | ||
| vec![Arc::new(RsNumBands {})], | ||
| Volatility::Immutable, | ||
| Some(rs_numbands_doc()), | ||
| ) | ||
| } | ||
|
|
||
| fn rs_numbands_doc() -> Documentation { | ||
| Documentation::builder( | ||
| DOC_SECTION_OTHER, | ||
| "Returns the number of bands in the raster.".to_string(), | ||
| "RS_NumBands(raster: Raster)".to_string(), | ||
| ) | ||
| .with_argument("raster", "Raster: Input raster") | ||
| .with_sql_example("SELECT RS_NumBands(RS_Example())".to_string()) | ||
| .build() | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct RsNumBands {} | ||
|
|
||
| impl SedonaScalarKernel for RsNumBands { | ||
| fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { | ||
| let matcher = ArgMatcher::new( | ||
| vec![ArgMatcher::is_raster()], | ||
| SedonaType::Arrow(DataType::UInt32), | ||
| ); | ||
|
|
||
| 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 = UInt32Builder::with_capacity(executor.num_iterations()); | ||
|
|
||
| executor.execute_raster_void(|_i, raster_opt| { | ||
| match raster_opt { | ||
| None => builder.append_null(), | ||
| Some(raster) => { | ||
| let num_bands = raster.bands().len() as u32; | ||
| builder.append_value(num_bands); | ||
| } | ||
| } | ||
| Ok(()) | ||
| })?; | ||
|
|
||
| executor.finish(Arc::new(builder.finish())) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use arrow_array::UInt32Array; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_expr::ScalarUDF; | ||
| use sedona_schema::datatypes::RASTER; | ||
| use sedona_testing::compare::assert_array_equal; | ||
| use sedona_testing::rasters::generate_test_rasters; | ||
| use sedona_testing::testers::ScalarUdfTester; | ||
|
|
||
| #[test] | ||
| fn udf_metadata() { | ||
| let udf: ScalarUDF = rs_numbands_udf().into(); | ||
| assert_eq!(udf.name(), "rs_numbands"); | ||
| assert!(udf.documentation().is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn udf_numbands() { | ||
| let udf: ScalarUDF = rs_numbands_udf().into(); | ||
| let tester = ScalarUdfTester::new(udf, vec![RASTER]); | ||
|
|
||
| tester.assert_return_type(DataType::UInt32); | ||
|
|
||
| // Test with rasters - generate_test_rasters creates rasters with 1 band each | ||
| let rasters = generate_test_rasters(3, Some(1)).unwrap(); | ||
| let expected: Arc<dyn arrow_array::Array> = | ||
| Arc::new(UInt32Array::from(vec![Some(1), None, Some(1)])); | ||
|
|
||
| let result = tester.invoke_array(Arc::new(rasters)).unwrap(); | ||
| assert_array_equal(&result, &expected); | ||
|
|
||
| // Test with null scalar | ||
| let result = tester.invoke_scalar(ScalarValue::Null).unwrap(); | ||
| tester.assert_scalar_result_equals(result, ScalarValue::UInt32(None)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.