-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #500 from databrickslabs/feature/st_within
add st_within
- Loading branch information
Showing
9 changed files
with
302 additions
and
1 deletion.
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
64 changes: 64 additions & 0 deletions
64
src/main/scala/com/databricks/labs/mosaic/expressions/geometry/ST_Within.scala
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.databricks.labs.mosaic.expressions.geometry | ||
|
||
import com.databricks.labs.mosaic.core.geometry.MosaicGeometry | ||
import com.databricks.labs.mosaic.expressions.base.{GenericExpressionFactory, WithExpressionInfo} | ||
import com.databricks.labs.mosaic.expressions.geometry.base.BinaryVectorExpression | ||
import com.databricks.labs.mosaic.functions.MosaicExpressionConfig | ||
import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder | ||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext | ||
import org.apache.spark.sql.types.{BooleanType, DataType} | ||
|
||
/** | ||
* Returns true if leftGeom is within rightGeom. | ||
* @param leftGeom | ||
* The left geometry. | ||
* @param rightGeom | ||
* The right geometry. | ||
* @param expressionConfig | ||
* Additional arguments for the expression (expressionConfigs). | ||
*/ | ||
case class ST_Within( | ||
leftGeom: Expression, | ||
rightGeom: Expression, | ||
expressionConfig: MosaicExpressionConfig | ||
) extends BinaryVectorExpression[ST_Within]( | ||
leftGeom, | ||
rightGeom, | ||
returnsGeometry = false, | ||
expressionConfig | ||
) { | ||
|
||
override def dataType: DataType = BooleanType | ||
|
||
override def geometryTransform(leftGeometry: MosaicGeometry, rightGeometry: MosaicGeometry): Any = { | ||
leftGeometry.within(rightGeometry) | ||
} | ||
|
||
override def geometryCodeGen(leftGeometryRef: String, rightGeometryRef: String, ctx: CodegenContext): (String, String) = { | ||
val within = ctx.freshName("within") | ||
val code = s"""boolean $within = $leftGeometryRef.within($rightGeometryRef);""" | ||
(code, within) | ||
} | ||
|
||
} | ||
|
||
/** Expression info required for the expression registration for spark SQL. */ | ||
object ST_Within extends WithExpressionInfo { | ||
|
||
override def name: String = "st_within" | ||
|
||
override def usage: String = "_FUNC_(expr1, expr2) - Returns true if expr1 is within expr2." | ||
|
||
override def example: String = | ||
""" | ||
| Examples: | ||
| > SELECT _FUNC_(A, B); | ||
| true | ||
| """.stripMargin | ||
|
||
override def builder(expressionConfig: MosaicExpressionConfig): FunctionBuilder = { | ||
GenericExpressionFactory.getBaseBuilder[ST_Within](2, expressionConfig) | ||
} | ||
|
||
} |
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
104 changes: 104 additions & 0 deletions
104
src/test/scala/com/databricks/labs/mosaic/expressions/geometry/ST_WithinBehaviors.scala
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.databricks.labs.mosaic.expressions.geometry | ||
|
||
import com.databricks.labs.mosaic.functions.MosaicContext | ||
import com.databricks.labs.mosaic.test.MosaicSpatialQueryTest | ||
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator} | ||
import org.apache.spark.sql.execution.WholeStageCodegenExec | ||
import org.apache.spark.sql.functions.lit | ||
import org.apache.spark.sql.types._ | ||
import org.scalatest.matchers.must.Matchers.noException | ||
import org.scalatest.matchers.should.Matchers.{an, be, convertToAnyShouldWrapper} | ||
|
||
trait ST_WithinBehaviors extends MosaicSpatialQueryTest { | ||
|
||
def withinBehavior(mosaicContext: MosaicContext): Unit = { | ||
spark.sparkContext.setLogLevel("FATAL") | ||
val mc = mosaicContext | ||
import mc.functions._ | ||
val sc = spark | ||
import sc.implicits._ | ||
mc.register(spark) | ||
|
||
val poly = """POLYGON ((10 10, 110 10, 110 110, 10 110, 10 10), | ||
| (20 20, 20 30, 30 30, 30 20, 20 20), | ||
| (40 20, 40 30, 50 30, 50 20, 40 20))""".stripMargin.filter(_ >= ' ') | ||
|
||
val rows = List( | ||
("POINT (35 25)", poly, true), | ||
("POINT (25 25)", poly, false) | ||
) | ||
|
||
val results = rows | ||
.toDF("leftGeom", "rightGeom", "expected") | ||
.withColumn("result", st_within($"leftGeom", $"rightGeom")) | ||
.where($"expected" === $"result") | ||
|
||
results.count shouldBe 2 | ||
} | ||
|
||
def withinCodegen(mosaicContext: MosaicContext): Unit = { | ||
spark.sparkContext.setLogLevel("FATAL") | ||
val mc = mosaicContext | ||
val sc = spark | ||
import mc.functions._ | ||
import sc.implicits._ | ||
mc.register(spark) | ||
|
||
val poly = """POLYGON ((10 10, 110 10, 110 110, 10 110, 10 10), | ||
| (20 20, 20 30, 30 30, 30 20, 20 20), | ||
| (40 20, 40 30, 50 30, 50 20, 40 20))""".stripMargin.filter(_ >= ' ') | ||
|
||
val rows = List( | ||
("POINT (35 25)", true), | ||
("POINT (25 25)", false) | ||
) | ||
|
||
val polygons = List(poly).toDF("rightGeom") | ||
val points = rows.toDF("leftGeom", "expected") | ||
|
||
val result = polygons | ||
.crossJoin(points) | ||
.withColumn("result", st_within($"leftGeom", $"rightGeom")) | ||
.where($"expected" === $"result") | ||
|
||
val queryExecution = result.queryExecution | ||
val plan = queryExecution.executedPlan | ||
|
||
val wholeStageCodegenExec = plan.find(_.isInstanceOf[WholeStageCodegenExec]) | ||
|
||
wholeStageCodegenExec.isDefined shouldBe true | ||
|
||
val codeGenStage = wholeStageCodegenExec.get.asInstanceOf[WholeStageCodegenExec] | ||
val (_, code) = codeGenStage.doCodeGen() | ||
|
||
noException should be thrownBy CodeGenerator.compile(code) | ||
|
||
val stWithin = ST_Within(lit(rows.head._1).expr, lit(1).expr, mc.expressionConfig) | ||
val ctx = new CodegenContext | ||
an[Error] should be thrownBy stWithin.genCode(ctx) | ||
} | ||
|
||
def auxiliaryMethods(mosaicContext: MosaicContext): Unit = { | ||
spark.sparkContext.setLogLevel("FATAL") | ||
val mc = mosaicContext | ||
mc.register(spark) | ||
|
||
val poly = """POLYGON ((10 10, 110 10, 110 110, 10 110, 10 10), | ||
| (20 20, 20 30, 30 30, 30 20, 20 20), | ||
| (40 20, 40 30, 50 30, 50 20, 40 20))""".stripMargin.filter(_ >= ' ') | ||
|
||
val rows = List( | ||
("POINT (35 25)", true), | ||
("POINT (25 25)", false) | ||
) | ||
|
||
val stWithin = ST_Within(lit(rows.head._1).expr, lit(poly).expr, mc.expressionConfig) | ||
|
||
stWithin.left shouldEqual lit(rows.head._1).expr | ||
stWithin.right shouldEqual lit(poly).expr | ||
stWithin.dataType shouldEqual BooleanType | ||
noException should be thrownBy stWithin.makeCopy(Array(stWithin.left, stWithin.right)) | ||
|
||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/test/scala/com/databricks/labs/mosaic/expressions/geometry/ST_WithinTest.scala
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.databricks.labs.mosaic.expressions.geometry | ||
|
||
import com.databricks.labs.mosaic.test.MosaicSpatialQueryTest | ||
import org.apache.spark.sql.test.SharedSparkSession | ||
|
||
class ST_WithinTest extends MosaicSpatialQueryTest with SharedSparkSession with ST_WithinBehaviors { | ||
|
||
testAllGeometriesNoCodegen("ST_Within behavior") { withinBehavior } | ||
testAllGeometriesCodegen("ST_Within codegen compilation") { withinCodegen } | ||
testAllGeometriesCodegen("ST_Within codegen behavior") { withinBehavior } | ||
testAllGeometriesNoCodegen("ST_Within auxiliary methods") { auxiliaryMethods } | ||
|
||
} |