You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ji...@apache.org on 2022/09/19 22:47:34 UTC

[incubator-sedona] branch master updated: [SEDONA-169] Fix ST_RemovePoint in accordance with the API document (#691)

This is an automated email from the ASF dual-hosted git repository.

jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-sedona.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a1f5459 [SEDONA-169] Fix ST_RemovePoint in accordance with the API document (#691)
9a1f5459 is described below

commit 9a1f54593fa60cdfb49bb1de77a5e17b332a6a02
Author: Kengo Seki <se...@apache.org>
AuthorDate: Tue Sep 20 07:47:28 2022 +0900

    [SEDONA-169] Fix ST_RemovePoint in accordance with the API document (#691)
---
 .../org/apache/spark/sql/sedona_sql/expressions/Functions.scala  | 5 +++--
 sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala | 9 +++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala b/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala
index aec70692..1da93a84 100644
--- a/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala
+++ b/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala
@@ -892,7 +892,7 @@ case class ST_AddPoint(inputExpressions: Seq[Expression])
 
 case class ST_RemovePoint(inputExpressions: Seq[Expression])
   extends Expression with CodegenFallback {
-  assert(inputExpressions.length == 2)
+  inputExpressions.betweenLength(1, 2)
 
   private val geometryFactory = new GeometryFactory()
 
@@ -900,11 +900,12 @@ case class ST_RemovePoint(inputExpressions: Seq[Expression])
 
   override def eval(input: InternalRow): Any = {
     val linesString = inputExpressions(0).toGeometry(input)
-    val pointToRemove = inputExpressions(1).eval(input).asInstanceOf[Int]
     linesString match {
       case string: LineString =>
         val coordinates = string.getCoordinates
         val length = coordinates.length
+        val pointToRemove = if (inputExpressions.length < 2) coordinates.length - 1
+          else inputExpressions(1).eval(input).asInstanceOf[Int]
         if (coordinates.length <= pointToRemove | coordinates.length <= 2) null
         else {
           val coordinatesWithPointRemoved = coordinates.slice(0, pointToRemove) ++ coordinates.slice(pointToRemove + 1, length)
diff --git a/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala b/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
index ea2fcc07..453e72d0 100644
--- a/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
+++ b/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
@@ -979,6 +979,7 @@ class functionTestScala extends TestBaseScala with Matchers with GeometrySample
   }
 
   it("Should correctly remove using ST_RemovePoint") {
+    calculateStRemovePointOption("Linestring(0 0, 1 1, 1 0, 0 0)") shouldBe Some("LINESTRING (0 0, 1 1, 1 0)")
     calculateStRemovePointOption("Linestring(0 0, 1 1, 1 0, 0 0)", 0) shouldBe Some("LINESTRING (1 1, 1 0, 0 0)")
     calculateStRemovePointOption("Linestring(0 0, 1 1)", 0) shouldBe None
     calculateStRemovePointOption("Linestring(0 0, 1 1, 1 0, 0 0)", 1) shouldBe Some("LINESTRING (0 0, 1 0, 0 0)")
@@ -1135,6 +1136,14 @@ class functionTestScala extends TestBaseScala with Matchers with GeometrySample
   private def wktToDf(wkt: String): DataFrame =
     Seq(Tuple1(wktReader.read(wkt))).toDF("geom")
 
+  private def calculateStRemovePointOption(wkt: String): Option[String] =
+    calculateStRemovePoint(wkt).headOption
+
+  private def calculateStRemovePoint(wkt: String): Array[String] =
+    wktToDf(wkt).selectExpr(s"ST_RemovePoint(geom) as geom")
+      .filter("geom is not null")
+      .selectExpr("ST_AsText(geom)").as[String].collect()
+
   private def calculateStRemovePointOption(wkt: String, index: Int): Option[String] =
     calculateStRemovePoint(wkt, index).headOption