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


##########
rust/sedona-functions/src/st_start_point.rs:
##########
@@ -0,0 +1,276 @@
+// 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::builder::BinaryBuilder;
+use datafusion_common::error::Result;
+use datafusion_expr::{
+    scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, 
Volatility,
+};
+use geo_traits::{
+    CoordTrait, GeometryCollectionTrait, GeometryTrait, LineStringTrait, 
MultiLineStringTrait,
+    MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait,
+};
+use sedona_common::sedona_internal_err;
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_geometry::{
+    error::SedonaGeometryError,
+    wkb_factory::{write_wkb_coord_trait, write_wkb_point_header, 
WKB_MIN_PROBABLE_BYTES},
+};
+use sedona_schema::{
+    datatypes::{SedonaType, WKB_GEOMETRY},
+    matchers::ArgMatcher,
+};
+use std::{io::Write, sync::Arc};
+
+use crate::executor::WkbExecutor;
+
+/// ST_StartPoint() scalar UDF
+///
+/// Native implementation to get the start point of a geometry
+pub fn st_start_point_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_startpoint",
+        vec![Arc::new(STStartOrEndPoint::new(true))],
+        Volatility::Immutable,
+        Some(st_start_point_doc()),
+    )
+}
+
+fn st_start_point_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the start point of a LINESTRING geometry. Returns NULL if the 
geometry is not a LINESTRING.",
+        "ST_StartPoint (geom: Geometry)",
+    )
+    .with_argument("geom", "geometry: Input geometry")
+    .with_sql_example("SELECT ST_StartPoint(ST_GeomFromWKT('LINESTRING(0 1, 2 
3, 4 5)'))")
+    .build()
+}
+
+/// ST_EndPoint() scalar UDF
+///
+/// Native implementation to get the end point of a geometry
+pub fn st_end_point_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_endpoint",
+        vec![Arc::new(STStartOrEndPoint::new(false))],
+        Volatility::Immutable,
+        Some(st_end_point_doc()),
+    )
+}
+
+fn st_end_point_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the end point of a LINESTRING geometry. Returns NULL if the 
geometry is not a LINESTRING.",
+        "ST_EndPoint (geom: Geometry)",
+    )
+    .with_argument("geom", "geometry: Input geometry")
+    .with_sql_example("SELECT ST_EndPoint(ST_GeomFromWKT('LINESTRING(0 1, 2 3, 
4 5)'))")
+    .build()
+}
+
+#[derive(Debug)]
+struct STStartOrEndPoint {
+    from_start: bool,
+}
+
+impl STStartOrEndPoint {
+    fn new(from_start: bool) -> Self {
+        STStartOrEndPoint { from_start }
+    }
+}
+
+impl SedonaScalarKernel for STStartOrEndPoint {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], 
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(),
+        );
+
+        executor.execute_wkb_void(|maybe_wkb| {
+            if let Some(wkb) = maybe_wkb {
+                if let Some(coord) = extract_first_geometry(&wkb, 
self.from_start) {
+                    if write_wkb_start_point(&mut builder, coord).is_err() {
+                        return sedona_internal_err!("Failed to write WKB point 
header");
+                    };
+                    builder.append_value([]);
+                    return Ok(());
+                }
+            }
+
+            builder.append_null();
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn write_wkb_start_point(
+    buf: &mut impl Write,
+    coord: impl CoordTrait<T = f64>,
+) -> Result<(), SedonaGeometryError> {
+    write_wkb_point_header(buf, coord.dim())?;
+    write_wkb_coord_trait(buf, &coord)
+}
+
+// - ST_StartPoint returns result for all types of geometries
+// - ST_EndPoint returns result only for LINESTRING
+fn extract_first_geometry<'a>(

Review Comment:
   Optional nit:
   
   ```suggestion
   fn extract_start_or_end_coord<'a>(
   
   ```



##########
rust/sedona-functions/src/st_start_point.rs:
##########
@@ -0,0 +1,276 @@
+// 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::builder::BinaryBuilder;
+use datafusion_common::error::Result;
+use datafusion_expr::{
+    scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, 
Volatility,
+};
+use geo_traits::{
+    CoordTrait, GeometryCollectionTrait, GeometryTrait, LineStringTrait, 
MultiLineStringTrait,
+    MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait,
+};
+use sedona_common::sedona_internal_err;
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_geometry::{
+    error::SedonaGeometryError,
+    wkb_factory::{write_wkb_coord_trait, write_wkb_point_header, 
WKB_MIN_PROBABLE_BYTES},
+};
+use sedona_schema::{
+    datatypes::{SedonaType, WKB_GEOMETRY},
+    matchers::ArgMatcher,
+};
+use std::{io::Write, sync::Arc};
+
+use crate::executor::WkbExecutor;
+
+/// ST_StartPoint() scalar UDF
+///
+/// Native implementation to get the start point of a geometry
+pub fn st_start_point_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_startpoint",
+        vec![Arc::new(STStartOrEndPoint::new(true))],
+        Volatility::Immutable,
+        Some(st_start_point_doc()),
+    )
+}
+
+fn st_start_point_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the start point of a LINESTRING geometry. Returns NULL if the 
geometry is not a LINESTRING.",
+        "ST_StartPoint (geom: Geometry)",
+    )
+    .with_argument("geom", "geometry: Input geometry")
+    .with_sql_example("SELECT ST_StartPoint(ST_GeomFromWKT('LINESTRING(0 1, 2 
3, 4 5)'))")
+    .build()
+}
+
+/// ST_EndPoint() scalar UDF
+///
+/// Native implementation to get the end point of a geometry
+pub fn st_end_point_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_endpoint",
+        vec![Arc::new(STStartOrEndPoint::new(false))],
+        Volatility::Immutable,
+        Some(st_end_point_doc()),
+    )
+}
+
+fn st_end_point_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the end point of a LINESTRING geometry. Returns NULL if the 
geometry is not a LINESTRING.",
+        "ST_EndPoint (geom: Geometry)",
+    )
+    .with_argument("geom", "geometry: Input geometry")
+    .with_sql_example("SELECT ST_EndPoint(ST_GeomFromWKT('LINESTRING(0 1, 2 3, 
4 5)'))")
+    .build()
+}
+
+#[derive(Debug)]
+struct STStartOrEndPoint {
+    from_start: bool,
+}
+
+impl STStartOrEndPoint {
+    fn new(from_start: bool) -> Self {
+        STStartOrEndPoint { from_start }
+    }
+}
+
+impl SedonaScalarKernel for STStartOrEndPoint {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], 
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(),
+        );
+
+        executor.execute_wkb_void(|maybe_wkb| {
+            if let Some(wkb) = maybe_wkb {
+                if let Some(coord) = extract_first_geometry(&wkb, 
self.from_start) {
+                    if write_wkb_start_point(&mut builder, coord).is_err() {
+                        return sedona_internal_err!("Failed to write WKB point 
header");
+                    };
+                    builder.append_value([]);
+                    return Ok(());
+                }
+            }
+
+            builder.append_null();
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn write_wkb_start_point(
+    buf: &mut impl Write,
+    coord: impl CoordTrait<T = f64>,
+) -> Result<(), SedonaGeometryError> {
+    write_wkb_point_header(buf, coord.dim())?;
+    write_wkb_coord_trait(buf, &coord)
+}
+
+// - ST_StartPoint returns result for all types of geometries
+// - ST_EndPoint returns result only for LINESTRING
+fn extract_first_geometry<'a>(
+    wkb: &'a wkb::reader::Wkb<'a>,
+    from_start: bool,
+) -> Option<wkb::reader::Coord<'a>> {
+    match (wkb.as_type(), from_start) {
+        (geo_traits::GeometryType::Point(point), true) => point.coord(),
+        (geo_traits::GeometryType::LineString(line_string), true) => 
line_string.coord(0),
+        (geo_traits::GeometryType::LineString(line_string), false) => {
+            line_string.coord(line_string.num_coords() - 1)
+        }
+        (geo_traits::GeometryType::Polygon(polygon), true) => match 
polygon.exterior() {
+            Some(ring) => ring.coord(0),
+            None => None,
+        },
+        (geo_traits::GeometryType::MultiPoint(multi_point), true) => match 
multi_point.point(0) {
+            Some(point) => point.coord(),
+            None => None,
+        },
+        (geo_traits::GeometryType::MultiLineString(multi_line_string), true) 
=> {
+            match multi_line_string.line_string(0) {
+                Some(line_string) => line_string.coord(0),
+                None => None,
+            }
+        }
+        (geo_traits::GeometryType::MultiPolygon(multi_polygon), true) => {
+            match multi_polygon.polygon(0) {
+                Some(polygon) => match polygon.exterior() {
+                    Some(ring) => ring.coord(0),
+                    None => None,
+                },
+                None => None,
+            }
+        }
+        (geo_traits::GeometryType::GeometryCollection(geometry_collection), 
true) => {
+            match geometry_collection.geometry(0) {
+                Some(geometry) => extract_first_geometry(geometry, from_start),
+                None => None,
+            }
+        }
+        (geo_traits::GeometryType::Rect(_), true) => None,
+        (geo_traits::GeometryType::Triangle(_), true) => None,
+        (geo_traits::GeometryType::Line(_), true) => None,
+        _ => None,
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use datafusion_expr::ScalarUDF;
+    use rstest::rstest;
+    use sedona_schema::datatypes::WKB_VIEW_GEOMETRY;
+    use sedona_testing::{
+        compare::assert_array_equal, create::create_array, 
testers::ScalarUdfTester,
+    };
+
+    use super::*;
+
+    #[test]
+    fn udf_metadata() {
+        let st_start_point_udf: ScalarUDF = st_start_point_udf().into();
+        assert_eq!(st_start_point_udf.name(), "st_startpoint");
+        assert!(st_start_point_udf.documentation().is_some());
+
+        let st_end_point_udf: ScalarUDF = st_end_point_udf().into();
+        assert_eq!(st_end_point_udf.name(), "st_endpoint");
+        assert!(st_end_point_udf.documentation().is_some());
+    }
+
+    #[rstest]
+    fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) 
{
+        let tester_start_point =
+            ScalarUdfTester::new(st_start_point_udf().into(), 
vec![sedona_type.clone()]);
+        let tester_end_point =
+            ScalarUdfTester::new(st_end_point_udf().into(), 
vec![sedona_type.clone()]);
+
+        let input = create_array(
+            &[
+                Some("LINESTRING (1 2, 3 4, 5 6)"),

Review Comment:
   For full test coverage of your implementation I think we would need to test 
empty geometries here as well.



##########
rust/sedona-functions/src/st_start_point.rs:
##########
@@ -0,0 +1,276 @@
+// 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::builder::BinaryBuilder;
+use datafusion_common::error::Result;
+use datafusion_expr::{
+    scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, 
Volatility,
+};
+use geo_traits::{
+    CoordTrait, GeometryCollectionTrait, GeometryTrait, LineStringTrait, 
MultiLineStringTrait,
+    MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait,
+};
+use sedona_common::sedona_internal_err;
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_geometry::{
+    error::SedonaGeometryError,
+    wkb_factory::{write_wkb_coord_trait, write_wkb_point_header, 
WKB_MIN_PROBABLE_BYTES},
+};
+use sedona_schema::{
+    datatypes::{SedonaType, WKB_GEOMETRY},
+    matchers::ArgMatcher,
+};
+use std::{io::Write, sync::Arc};
+
+use crate::executor::WkbExecutor;
+
+/// ST_StartPoint() scalar UDF
+///
+/// Native implementation to get the start point of a geometry
+pub fn st_start_point_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_startpoint",
+        vec![Arc::new(STStartOrEndPoint::new(true))],
+        Volatility::Immutable,
+        Some(st_start_point_doc()),
+    )
+}
+
+fn st_start_point_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the start point of a LINESTRING geometry. Returns NULL if the 
geometry is not a LINESTRING.",
+        "ST_StartPoint (geom: Geometry)",
+    )
+    .with_argument("geom", "geometry: Input geometry")
+    .with_sql_example("SELECT ST_StartPoint(ST_GeomFromWKT('LINESTRING(0 1, 2 
3, 4 5)'))")
+    .build()
+}
+
+/// ST_EndPoint() scalar UDF
+///
+/// Native implementation to get the end point of a geometry
+pub fn st_end_point_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_endpoint",
+        vec![Arc::new(STStartOrEndPoint::new(false))],
+        Volatility::Immutable,
+        Some(st_end_point_doc()),
+    )
+}
+
+fn st_end_point_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the end point of a LINESTRING geometry. Returns NULL if the 
geometry is not a LINESTRING.",
+        "ST_EndPoint (geom: Geometry)",
+    )
+    .with_argument("geom", "geometry: Input geometry")
+    .with_sql_example("SELECT ST_EndPoint(ST_GeomFromWKT('LINESTRING(0 1, 2 3, 
4 5)'))")
+    .build()
+}
+
+#[derive(Debug)]
+struct STStartOrEndPoint {
+    from_start: bool,
+}
+
+impl STStartOrEndPoint {
+    fn new(from_start: bool) -> Self {
+        STStartOrEndPoint { from_start }
+    }
+}
+
+impl SedonaScalarKernel for STStartOrEndPoint {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], 
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(),
+        );
+
+        executor.execute_wkb_void(|maybe_wkb| {
+            if let Some(wkb) = maybe_wkb {
+                if let Some(coord) = extract_first_geometry(&wkb, 
self.from_start) {
+                    if write_wkb_start_point(&mut builder, coord).is_err() {
+                        return sedona_internal_err!("Failed to write WKB point 
header");
+                    };
+                    builder.append_value([]);
+                    return Ok(());
+                }
+            }
+
+            builder.append_null();
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn write_wkb_start_point(

Review Comment:
   Optional nit:
   
   ```suggestion
   fn write_wkb_coord(
   
   ```



##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -1016,6 +1016,55 @@ def test_st_pointm(eng, x, y, m, expected):
     )
 
 
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+    ("geometry", "expected"),
+    [
+        ("LINESTRING (1 2, 3 4, 5 6)", "POINT (1 2)"),

Review Comment:
   I think it would be good to verify that we have the same behaviour as 
PostGIS for empties as well:
   
   ```suggestion
           (None, None),
           ("POINT EMPTY", None),
           ("LINESTRING EMPTY", None),
           ("POLYGON EMPTY", None),
           ("MULTIPOINT EMPTY", None),
           ("MULTILINESTRING EMPTY", None),
           ("MULTIPOLYGON EMPTY", None),
           ("GEOMETRYCOLLECTION EMPTY", None),
           ("LINESTRING (1 2, 3 4, 5 6)", "POINT (1 2)"),
   ```



##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -1016,6 +1016,55 @@ def test_st_pointm(eng, x, y, m, expected):
     )
 
 
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+    ("geometry", "expected"),
+    [
+        ("LINESTRING (1 2, 3 4, 5 6)", "POINT (1 2)"),
+        ("LINESTRING Z (1 2 3, 3 4 5, 5 6 7)", "POINT Z (1 2 3)"),
+        ("LINESTRING M (1 2 3, 3 4 5, 5 6 7)", "POINT M (1 2 3)"),
+        ("LINESTRING ZM (1 2 3 4, 3 4 5 6, 5 6 7 8)", "POINT ZM (1 2 3 4)"),
+        ("POINT (1 2)", "POINT (1 2)"),
+        ("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", "POINT (0 0)"),
+        ("MULTIPOINT (0 0, 10 0, 10 10, 0 10, 0 0)", "POINT (0 0)"),
+        ("MULTILINESTRING ((1 2, 3 4), (5 6, 7 8))", "POINT (1 2)"),
+        ("MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0)))", "POINT (0 0)"),
+        ("GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (3 4, 5 6))", "POINT (1 
2)"),
+        (
+            "GEOMETRYCOLLECTION (GEOMETRYCOLLECTION (GEOMETRYCOLLECTION (POINT 
(1 2), LINESTRING (3 4, 5 6))))",
+            "POINT (1 2)",
+        ),
+    ],
+)
+def test_st_start_point(eng, geometry, expected):
+    eng = eng.create_or_skip()
+    eng.assert_query_result(
+        f"SELECT ST_StartPoint({geom_or_null(geometry)})",
+        expected,
+    )
+
+
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+    ("geometry", "expected"),
+    [
+        ("LINESTRING (1 2, 3 4, 5 6)", "POINT (5 6)"),

Review Comment:
   NULL and empties would be good to test here, too.



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