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


##########
c/sedona-geos/src/lib.rs:
##########
@@ -26,6 +26,7 @@ mod st_buffer;
 mod st_centroid;
 mod st_convexhull;
 mod st_dwithin;
+mod st_geometryn;

Review Comment:
   You've very nicely implemented this using geo-traits/Wkb so it can live in 
`sedona-functions`!



##########
c/sedona-geos/src/st_geometryn.rs:
##########
@@ -0,0 +1,201 @@
+// 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 arrow_array::builder::BinaryBuilder;
+use datafusion_common::{cast::as_int64_array, DataFusionError, Result};
+use datafusion_expr::ColumnarValue;
+use geo_traits::{
+    GeometryCollectionTrait, GeometryTrait, MultiLineStringTrait, 
MultiPointTrait,
+    MultiPolygonTrait,
+};
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_functions::executor::WkbExecutor;
+use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES;
+use sedona_schema::{
+    datatypes::{SedonaType, WKB_GEOMETRY},
+    matchers::ArgMatcher,
+};
+use wkb::reader::Wkb;
+
+/// ST_GeometryN() implementation using geo-traits
+pub fn st_geometryn_impl() -> ScalarKernelRef {
+    Arc::new(STGeometryN {})
+}
+
+#[derive(Debug)]
+struct STGeometryN {}
+
+impl SedonaScalarKernel for STGeometryN {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_geometry(), ArgMatcher::is_integer()],
+            WKB_GEOMETRY,
+        );
+
+        matcher.match_args(args)
+    }
+
+    fn invoke_batch(
+        &self,
+        arg_types: &[SedonaType],
+        args: &[ColumnarValue],
+    ) -> Result<ColumnarValue> {
+        let executor = WkbExecutor::new(arg_types, args);
+        let mut builder = BinaryBuilder::with_capacity(
+            executor.num_iterations(),
+            WKB_MIN_PROBABLE_BYTES * executor.num_iterations(),
+        );
+
+        let integer_value = args[1]
+            .cast_to(&arrow_schema::DataType::Int64, None)?
+            .to_array(executor.num_iterations())?;
+        let index_array = as_int64_array(&integer_value)?;
+        let mut index_iter = index_array.iter();
+
+        executor.execute_wkb_void(|maybe_wkb| {
+            match (maybe_wkb, index_iter.next().unwrap()) {
+                (Some(wkb), Some(index)) => {
+                    if invoke_scalar(&wkb, (index - 1) as usize, &mut 
builder).is_err() {
+                        // Unsupported Geometry Type, Invalid index encountered
+                        builder.append_null();

Review Comment:
   Instead of using `is_err()` here, maybe `invoke_scalar()` could return 
something (`true/false`?) to indicate the out-of-bounds case so you can 
`append_null()` here. (If we swallow the error we might miss something else!)



##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -1064,6 +1064,79 @@ def test_st_geomfromwkb(eng, geom):
     eng.assert_query_result(f"SELECT ST_GeomFromWKB({wkb})", expected)
 
 
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+    ("geom", "index", "expected"),
+    [
+        (
+            "GEOMETRYCOLLECTION(POINT(1 1),MULTIPOLYGON(((0 2,1 1,0 0,0 
2)),((2 0,1 1,2 2,2 0))))",
+            1,
+            "POINT (1 1)",
+        ),

Review Comment:
   It's a bit hard to parse what each of these cases is trying to do. I think 
grouping them differently would be helpful:
   
   - Cases that result in null output (using compact geometries/n values)
   - Test that the n value is handled correctly (the same input geometry but 
multiple n values)
   - Test different geometry types (same n value, different collections)



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