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


##########
spark/common/src/main/scala/org/apache/sedona/stats/Weighting.scala:
##########
@@ -109,30 +109,42 @@ object Weighting {
 
     val formattedDataFrame = dataframe.withColumn(ID_COLUMN, 
sha2(to_json(struct("*")), 256))
 
-    formattedDataFrame
+    val spatiallyJoined = formattedDataFrame
       .alias("l")
       .join(
         formattedDataFrame.alias("r"),
-        joinCondition && col(s"l.$ID_COLUMN") =!= col(
-          s"r.$ID_COLUMN"
-        ), // we will add self back later if self.includeSelf
+        joinCondition && col(s"l.$ID_COLUMN") =!= col(s"r.$ID_COLUMN"),
+        "inner")
+      .select(struct("l.*").alias("left"), struct("r.*").alias("right"))
+
+    val mapped = formattedDataFrame
+      .alias("f")
+      .join(
+        spatiallyJoined.alias("s"),
+        col(s"s.left.$ID_COLUMN") === col(s"f.$ID_COLUMN"),
         "left")
       .select(
-        col(s"l.$ID_COLUMN"),
-        struct("l.*").alias("left_contents"),
-        struct(
-          (
-            savedAttributesWithGeom match {
-              case null => struct(col("r.*")).dropFields(ID_COLUMN)
-              case _ =>
-                struct(savedAttributesWithGeom.map(c => col(s"r.$c")): _*)
-            }
-          ).alias("neighbor"),
-          if (!binary)
-            pow(distanceFunction(col(s"l.$geometryColumn"), 
col(s"r.$geometryColumn")), alpha)
-              .alias("value")
-          else lit(1.0).alias("value")).alias("weight"))
-      .groupBy(s"l.$ID_COLUMN")
+        col(ID_COLUMN),
+        struct("f.*").alias("left_contents"),
+        when(col(ID_COLUMN).isNull, lit(null))

Review Comment:
   The condition `col(ID_COLUMN).isNull` will always be false because 
`ID_COLUMN` is from the left side of the left join (from `formattedDataFrame` 
alias 'f'), which always has values. To properly handle rows with no matches 
from the spatial join, check if `col(s\"s.left.$ID_COLUMN\").isNull` instead.
   ```suggestion
           when(col(s"left.$ID_COLUMN").isNull, lit(null))
   ```



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