Copilot commented on code in PR #2214:
URL: https://github.com/apache/sedona/pull/2214#discussion_r2249355592


##########
spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/optimization/SpatialFilterPushDownForGeoParquet.scala:
##########
@@ -161,17 +152,66 @@ class SpatialFilterPushDownForGeoParquet(sparkSession: 
SparkSession) extends Rul
         for ((name, value) <- resolveNameAndLiteral(distArgs, pushableColumn))
           yield distanceFilter(name, GeometryUDT.deserialize(value), 
d.asInstanceOf[Double])
 
+      case LessThan(ST_DistanceSpheroid(distArgs), Literal(d, DoubleType)) =>
+        for ((name, value) <- resolveNameAndLiteral(distArgs, pushableColumn))
+          yield distanceFilter(
+            name,
+            GeometryUDT.deserialize(value),
+            d.asInstanceOf[Double],
+            useSpheroid = true)
+
+      case LessThanOrEqual(ST_DistanceSpheroid(distArgs), Literal(d, 
DoubleType)) =>
+        for ((name, value) <- resolveNameAndLiteral(distArgs, pushableColumn))
+          yield distanceFilter(
+            name,
+            GeometryUDT.deserialize(value),
+            d.asInstanceOf[Double],
+            useSpheroid = true)
+
+      case ST_DWithin(args) if args.length == 3 || args.length == 4 =>
+        val distanceLit = args(2)
+        val useSpheroid = if (args.length == 4) {
+          args(3) match {
+            case Literal(flag: Boolean, _) => flag
+            case _ => false
+          }
+        } else false
+
+        distanceLit match {
+          case Literal(distance: Double, DoubleType) =>
+            resolveNameAndLiteral(args.take(2), pushableColumn).map { case 
(name, value) =>
+              distanceFilter(name, GeometryUDT.deserialize(value), distance, 
useSpheroid)
+            }
+          case _ => None
+        }
       case _ => None
     }
   }
 
-  private def distanceFilter(name: String, geom: Geometry, distance: Double) = 
{
-    val queryWindow = geom match {
-      case point: Point => new Circle(point, distance)
-      case _ =>
-        val envelope = geom.getEnvelopeInternal
-        envelope.expandBy(distance)
-        geom.getFactory.toGeometry(envelope)
+  private def distanceFilter(
+      name: String,
+      geom: Geometry,
+      distance: Double,
+      useSpheroid: Boolean = false): GeoParquetSpatialFilter = {
+    val queryWindow: Geometry = if (useSpheroid) {
+      // Spheroidal buffer
+      // Increase buffer distance by 3% to account for false negatives with 
Spheroidal Buffer calculations
+      val distanceLit = Literal(distance * 1.03)

Review Comment:
   The magic number 1.03 (3% inflation) should be extracted to a named constant 
with documentation explaining why this specific value is used to avoid false 
negatives in spheroidal buffer calculations.
   ```suggestion
         val distanceLit = Literal(distance * SpheroidalBufferInflationFactor)
   ```



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