-
Notifications
You must be signed in to change notification settings - Fork 662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SEDONA-630] Improve ST_Union_Aggr performance #1526
Merged
jiayuasu
merged 5 commits into
apache:master
from
zhangfengcdt:sedona-630-improveSTUnionAggr
Jul 22, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6aec540
[SEDONA-630] Improve ST_Union_Aggr performance
zhangfengcdt a217d4f
fix pythion test
zhangfengcdt b380387
add unit test to measure the ST_Union_aggr time
zhangfengcdt f8a2245
address review comments by refactoring unit tests
zhangfengcdt db116d6
rename test table
zhangfengcdt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,12 @@ | |
*/ | ||
package org.apache.sedona.sql | ||
|
||
import org.locationtech.jts.geom.{Coordinate, Geometry, GeometryFactory} | ||
import org.apache.spark.sql.expressions.javalang.typed | ||
import org.apache.spark.sql.sedona_sql.expressions.ST_Union_Aggr | ||
import org.locationtech.jts.geom.{Coordinate, Geometry, GeometryFactory, Polygon} | ||
import org.locationtech.jts.io.WKTReader | ||
|
||
import scala.util.Random | ||
|
||
class aggregateFunctionTestScala extends TestBaseScala { | ||
|
||
|
@@ -62,6 +67,26 @@ class aggregateFunctionTestScala extends TestBaseScala { | |
assert(union.take(1)(0).get(0).asInstanceOf[Geometry].getArea == 10100) | ||
} | ||
|
||
it("Measured ST_Union_aggr wall time") { | ||
// number of random polygons to generate | ||
val numPolygons = 1000 | ||
createPolygonDataFrame(numPolygons) | ||
|
||
// cache the table to eliminate the time of table scan | ||
sparkSession.sql("cache table geometry_table") | ||
sparkSession.sql("select count(*) from geometry_table").take(1)(0).get(0) | ||
|
||
// measure time for optimized ST_Union_Aggr | ||
val startTimeOptimized = System.currentTimeMillis() | ||
val unionOptimized = | ||
sparkSession.sql("SELECT ST_Union_Aggr(geom) AS union_geom FROM geometry_table") | ||
assert(unionOptimized.take(1)(0).get(0).asInstanceOf[Geometry].getArea > 0) | ||
val endTimeOptimized = System.currentTimeMillis() | ||
val durationOptimized = endTimeOptimized - startTimeOptimized | ||
|
||
assert(durationOptimized > 0, "Duration of optimized ST_Union_Aggr should be positive") | ||
} | ||
|
||
it("Passed ST_Intersection_aggr") { | ||
|
||
val twoPolygonsAsWktDf = | ||
|
@@ -97,4 +122,24 @@ class aggregateFunctionTestScala extends TestBaseScala { | |
assertResult(0.0)(intersectionDF.take(1)(0).get(0).asInstanceOf[Geometry].getArea) | ||
} | ||
} | ||
|
||
def generateRandomPolygon(index: Int): String = { | ||
val random = new Random() | ||
val x = random.nextDouble() * index | ||
val y = random.nextDouble() * index | ||
s"POLYGON (($x $y, ${x + 1} $y, ${x + 1} ${y + 1}, $x ${y + 1}, $x $y))" | ||
} | ||
|
||
def createPolygonDataFrame(numPolygons: Int): Unit = { | ||
val polygons = (1 to numPolygons).map(generateRandomPolygon).toArray | ||
val polygonArray = polygons.map(polygon => s"ST_GeomFromWKT('$polygon')") | ||
val polygonArrayStr = polygonArray.mkString(", ") | ||
|
||
val sqlQuery = | ||
s""" | ||
|SELECT explode(array($polygonArrayStr)) AS geom | ||
""".stripMargin | ||
|
||
sparkSession.sql(sqlQuery).createOrReplaceTempView("geometry_table") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return a reference of the DF as the return value of the function, instead of creating a new temp view? Otherwise this might pollute the global namespace and lead to bugs that are hard to find. |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also unpersist this table at the end of the test case? Otherwise this will lead to memory leak.