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


##########
rust/sedona-functions/src/st_start_point.rs:
##########
@@ -0,0 +1,248 @@
+// 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, GeometryTrait, LineStringTrait};
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_geometry::{
+    error::SedonaGeometryError,
+    wkb_factory::{write_wkb_coord, 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_start_point",
+        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_end_point",
+        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 geo_traits::GeometryType::LineString(line_string) = 
wkb.as_type() {
+                    let maybe_coord = if self.from_start {
+                        line_string.coord(0)
+                    } else {
+                        line_string.coord(line_string.num_coords() - 1)
+                    };
+
+                    if let Some(coord) = maybe_coord {
+                        write_wkb_start_point(&mut builder, coord).map_err(|_| 
{
+                            datafusion_common::DataFusionError::Internal(
+                                "Failed to write WKB point header".to_string(),
+                            )

Review Comment:
   ```suggestion
                               sedona_internal_err!("Failed to write WKB point 
header")
   
   ```



##########
rust/sedona-functions/src/st_start_point.rs:
##########
@@ -0,0 +1,248 @@
+// 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, GeometryTrait, LineStringTrait};
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_geometry::{
+    error::SedonaGeometryError,
+    wkb_factory::{write_wkb_coord, 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_start_point",
+        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_end_point",
+        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 geo_traits::GeometryType::LineString(line_string) = 
wkb.as_type() {
+                    let maybe_coord = if self.from_start {
+                        line_string.coord(0)
+                    } else {
+                        line_string.coord(line_string.num_coords() - 1)
+                    };
+
+                    if let Some(coord) = maybe_coord {
+                        write_wkb_start_point(&mut builder, coord).map_err(|_| 
{
+                            datafusion_common::DataFusionError::Internal(
+                                "Failed to write WKB point header".to_string(),
+                            )
+                        })?;
+                        builder.append_value([]);
+                        return Ok(());
+                    }
+                }
+            }
+
+            builder.append_null();
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn write_wkb_start_point(
+    buf: &mut impl Write,
+    coords: impl CoordTrait<T = f64>,
+) -> Result<(), SedonaGeometryError> {
+    let dim = coords.dim();
+    write_wkb_point_header(buf, dim)?;
+
+    match dim.size() {
+        2 => {
+            let coords_tuple = coords.x_y();
+            write_wkb_coord(buf, coords_tuple)
+        }
+        3 => {
+            let coords_tuple = (coords.x(), coords.y(), 
coords.nth_or_panic(2));
+            write_wkb_coord(buf, coords_tuple)
+        }
+        4 => {
+            let coords_tuple = (
+                coords.x(),
+                coords.y(),
+                coords.nth_or_panic(2),
+                coords.nth_or_panic(3),
+            );
+            write_wkb_coord(buf, coords_tuple)
+        }
+        _ => Err(SedonaGeometryError::Invalid(
+            "Unsupported number of dimensions".to_string(),
+        )),
+    }

Review Comment:
   This would fit nicely as `wkb_factory::write_wkb_coord_trait()` (I'm sure 
we'll need it again!)



##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -1015,6 +1015,45 @@ def test_st_pointm(eng, x, y, m, expected):
         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)",                               None                ),
+        ("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",   None                ),
+        ("MULTILINESTRING ((1 2, 3 4), (5 6, 7 8))",  None                ),   
             ),

Review Comment:
   ```suggestion
           ("MULTILINESTRING ((1 2, 3 4), (5 6, 7 8))",  None                ),
   ```



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