You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "LakshmiSowmya04 (via GitHub)" <gi...@apache.org> on 2025/11/29 15:35:09 UTC

[PR] added st_numpoints file [sedona-db]

LakshmiSowmya04 opened a new pull request, #386:
URL: https://github.com/apache/sedona-db/pull/386

   (no comment)


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(c/sedona-geos): Implement ST_NumPoints [sedona-db]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 merged PR #386:
URL: https://github.com/apache/sedona-db/pull/386


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] added st_numpoints file [sedona-db]

Posted by "LakshmiSowmya04 (via GitHub)" <gi...@apache.org>.
LakshmiSowmya04 commented on PR #386:
URL: https://github.com/apache/sedona-db/pull/386#issuecomment-3591778077

   @petern48 I tried to add circularstring as it is mentioned in [document](https://postgis.net/docs/ST_NumPoints.html).But i got a build fialure saying "Your test fails because CIRCULARSTRING is not supported." Is there anyway I can add this or we can skip this ?


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] added st_numpoints file [sedona-db]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on PR #386:
URL: https://github.com/apache/sedona-db/pull/386#issuecomment-3591819805

   > But i got a build fialure saying "Your test fails because CIRCULARSTRING is not supported." Is there anyway I can add this or we can skip this ?
   
   Oh yeah, Sedona doesn't support `CIRCULARSTRING`. It's equivalent to a LineString. The same can be said about LinearRing. There's no need to test it.


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] added st_numpoints file [sedona-db]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on code in PR #386:
URL: https://github.com/apache/sedona-db/pull/386#discussion_r2573106458


##########
c/sedona-geos/src/st_numpoints.rs:
##########
@@ -0,0 +1,142 @@
+// 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 crate::executor::GeosExecutor;
+use arrow_array::builder::Int32Builder;
+use arrow_schema::DataType;
+use datafusion_common::{error::Result, DataFusionError};
+use datafusion_expr::ColumnarValue;
+use geos::{Geom, Geometry, GeometryTypes};
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+
+pub fn st_num_points_impl() -> ScalarKernelRef {
+    Arc::new(STNumPoints {})
+}
+
+#[derive(Debug)]
+struct STNumPoints {}
+
+impl SedonaScalarKernel for STNumPoints {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_geometry()],
+            SedonaType::Arrow(DataType::Int32),
+        );
+
+        matcher.match_args(args)
+    }
+
+    fn invoke_batch(
+        &self,
+        arg_types: &[SedonaType],
+        args: &[ColumnarValue],
+    ) -> Result<ColumnarValue> {
+        let executor = GeosExecutor::new(arg_types, args);
+        let mut builder = Int32Builder::with_capacity(executor.num_iterations());
+        executor.execute_wkb_void(|maybe_geom| {
+            match maybe_geom {
+                None => builder.append_null(),
+                Some(geom) => {
+                    let res = invoke_scalar(&geom)?;
+                    match res {
+                        Some(n) => builder.append_value(n),
+                        None => builder.append_null(),
+                    }
+                }
+            }
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn invoke_scalar(geom: &Geometry) -> Result<Option<i32>> {
+    match geom.geometry_type() {
+        GeometryTypes::LineString | GeometryTypes::LinearRing => {
+            let count = geom.get_num_points().map_err(|e| {
+                DataFusionError::Execution(format!("Failed to get num points: {e}"))
+            })?;
+            Ok(Some(count as i32))
+        }
+        _ => Ok(None),
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use std::sync::Arc;
+
+    use arrow_array::{ArrayRef, Int32Array};
+    use arrow_schema::DataType;
+    use datafusion_common::ScalarValue;
+    use rstest::rstest;
+    use sedona_expr::scalar_udf::SedonaScalarUDF;
+    use sedona_schema::datatypes::{SedonaType, WKB_GEOMETRY, WKB_VIEW_GEOMETRY};
+    use sedona_testing::compare::assert_array_equal;
+    use sedona_testing::testers::ScalarUdfTester;
+
+    use super::*;
+
+    #[rstest]
+    fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) {
+        let udf = SedonaScalarUDF::from_kernel("st_numpoints", st_num_points_impl());
+        let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type]);
+        tester.assert_return_type(DataType::Int32);
+        let result = tester
+            .invoke_scalar(
+                "POLYGON(
+                    (0 0,10 0,10 6,0 6,0 0),
+                    (1 1,2 1,2 5,1 5,1 1),
+                    (8 5,8 4,9 4,9 5,8 5)
+                )",
+            )
+            .unwrap();
+        assert!(result.is_null());

Review Comment:
   ```suggestion
           let result = tester
               .invoke_scalar("LINESTRING (1 2, 3 4)")
               .unwrap();
           tester.assert_scalar_result_equals(result, 2_i32);
   ```
   optional nit: For the one scalar test, it's nice to test a case that highlights the main functionality, since this is more readable than the array tests below. Since st_numpoints really only operates on LineStrings (returning Null for everything else), let's test one here instead of a polygon that returns null. You already have tests below to make sure we return null properly. (This is more of a style thing, not a requirement)



##########
c/sedona-geos/src/st_numpoints.rs:
##########
@@ -0,0 +1,142 @@
+// 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 crate::executor::GeosExecutor;
+use arrow_array::builder::Int32Builder;
+use arrow_schema::DataType;
+use datafusion_common::{error::Result, DataFusionError};
+use datafusion_expr::ColumnarValue;
+use geos::{Geom, Geometry, GeometryTypes};
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+
+pub fn st_num_points_impl() -> ScalarKernelRef {
+    Arc::new(STNumPoints {})
+}
+
+#[derive(Debug)]
+struct STNumPoints {}
+
+impl SedonaScalarKernel for STNumPoints {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_geometry()],
+            SedonaType::Arrow(DataType::Int32),
+        );
+
+        matcher.match_args(args)
+    }
+
+    fn invoke_batch(
+        &self,
+        arg_types: &[SedonaType],
+        args: &[ColumnarValue],
+    ) -> Result<ColumnarValue> {
+        let executor = GeosExecutor::new(arg_types, args);
+        let mut builder = Int32Builder::with_capacity(executor.num_iterations());
+        executor.execute_wkb_void(|maybe_geom| {
+            match maybe_geom {
+                None => builder.append_null(),
+                Some(geom) => {
+                    let res = invoke_scalar(&geom)?;
+                    match res {
+                        Some(n) => builder.append_value(n),
+                        None => builder.append_null(),
+                    }
+                }
+            }
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn invoke_scalar(geom: &Geometry) -> Result<Option<i32>> {
+    match geom.geometry_type() {
+        GeometryTypes::LineString | GeometryTypes::LinearRing => {

Review Comment:
   ```suggestion
           GeometryTypes::LineString => {
   ```
   
   Sedona converts `LinearRing`s into LineStrings before it gets here, since they are equivalent.
   
   e.g. `select st_geomfromtext('LINEARRING EMPTY');` returns `LINESTRING EMPTY`.



##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -2742,3 +2742,32 @@ def test_st_numinteriorrings_basic(eng, geom, expected):
         f"SELECT ST_NumInteriorRings({geom_or_null(geom)})",
         expected,
     )
+
+
+@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
+@pytest.mark.parametrize(
+    ("geom", "expected"),
+    [
+        (None, None),
+        ("POINT EMPTY", None),
+        ("LINESTRING EMPTY", 0),
+        ("POLYGON EMPTY", None),
+        ("MULTIPOINT EMPTY", None),
+        ("MULTILINESTRING EMPTY", None),
+        ("MULTIPOLYGON EMPTY", None),
+        ("GEOMETRYCOLLECTION EMPTY", None),
+        ("POINT (1 2)", None),
+        ("LINESTRING (0 0, 1 1, 2 2)", 3),
+        ("LINESTRING (0 0, 1 1, 0 0)", 3),

Review Comment:
   ```suggestion
           ("LINESTRING (0 0, 1 1, 0 0)", 3),
           ("LINESTRING Z (0 0 0, 1 1 1, 2 2 2, 3 3 3)", 4),
           ("LINESTRING M (0 0 0, 1 1 1, 2 2 2, 3 3 3)", 4),
           ("LINESTRING ZM (0 0 0 2, 1 1 1 4)", 2),
   ```
   Let's also test Z/M coordinates for this function to be sure they don't mess anything up.



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] added st_numpoints file [sedona-db]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on PR #386:
URL: https://github.com/apache/sedona-db/pull/386#issuecomment-3591890245

   Last thing, two things. The first is good practice for all repos. The second is specific to this SedonaDB repo.
   
   - Could you mention the [issue](https://github.com/apache/sedona-db/issues/224) that this is a part of inside the PR description?
     - We don't want to say "closes" since the issue isn't done, but I like to do something like `"part of <issue link>"`
     - It's important to mention it, so that GitHub creates a link in that issue to this PR (e.g. shown below). When others view that issue, they can see that you have already started on implementing this function. When you don't do this, it increases the odds of someone trying to implement it too, since they might not see that you opened a PR for it already.
   - Could you also update the PR title to follow this convention we've been following for this repo:
     - `feat(c/sedona-geos): Implement ST_NumPoints`
        - "feat" is short for for feature. "c/sedona-geos" is the directory the change is in. It doesn't matter if you say "Add" or "Implement"
   
   Also, for the future, try to do both of these when you first open the PR, especially the first one, to avoid chances of duplicate PRs.


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(c/sedona-geos): Implement ST_NumPoints [sedona-db]

Posted by "LakshmiSowmya04 (via GitHub)" <gi...@apache.org>.
LakshmiSowmya04 commented on PR #386:
URL: https://github.com/apache/sedona-db/pull/386#issuecomment-3592192955

   > Last two things. The first is good practice for all repos. The second is specific to this SedonaDB repo.
   > 
   > * Could you mention the [issue](https://github.com/apache/sedona-db/issues/224) that this is a part of inside the PR description?
   >   
   >   * We don't want to say "closes" since the issue isn't done, but I like to do something like `"part of <issue link>"`
   >   * It's important to mention it, so that GitHub creates a link in that issue to this PR (e.g. shown below). When others view that issue, they can see that you have already started on implementing this function. When you don't do this, it increases the odds of someone trying to implement it too, since they might not see that you opened a PR for it already.
   > * Could you also update the PR title to follow this convention we've been following for this repo:
   >   
   >   * `feat(c/sedona-geos): Implement ST_NumPoints`
   >     
   >     * "feat" is short for feature. "c/sedona-geos" is the directory the change is in. It doesn't matter if you say "Add" or "Implement"
   > 
   > Also, for the future, try to do both of these when you first open the PR, especially the first one, to avoid chances of duplicate PRs.
   
   okay  I will make sure follow these rules.


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org