-
Notifications
You must be signed in to change notification settings - Fork 67
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 #507 from databrickslabs/st_z
add ST_Z
- Loading branch information
Showing
11 changed files
with
267 additions
and
7 deletions.
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
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
57 changes: 57 additions & 0 deletions
57
src/main/scala/com/databricks/labs/mosaic/expressions/geometry/ST_Z.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,57 @@ | ||
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.UnaryVectorExpression | ||
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.{DataType, DoubleType} | ||
|
||
/** | ||
* SQL expression that returns Z coordinate of the input point. Input must be a point. | ||
* | ||
* @param inputGeom | ||
* Expression containing the geometry. | ||
* @param expressionConfig | ||
* Mosaic execution context, e.g. geometryAPI, indexSystem, etc. Additional | ||
* arguments for the expression (expressionConfigs). | ||
*/ | ||
case class ST_Z( | ||
inputGeom: Expression, | ||
expressionConfig: MosaicExpressionConfig | ||
) extends UnaryVectorExpression[ST_Z](inputGeom, returnsGeometry = false, expressionConfig) { | ||
|
||
override def dataType: DataType = DoubleType | ||
|
||
override def geometryTransform(geometry: MosaicGeometry): Any = geometry.getAnyPoint.getZ | ||
|
||
override def geometryCodeGen(geometryRef: String, ctx: CodegenContext): (String, String) = { | ||
val resultRef = ctx.freshName("result") | ||
val code = s"""double $resultRef = $geometryRef.getAnyPoint().getZ();""" | ||
(code, resultRef) | ||
} | ||
|
||
} | ||
|
||
/** Expression info required for the expression registration for spark SQL. */ | ||
object ST_Z extends WithExpressionInfo { | ||
|
||
override def name: String = "st_z" | ||
|
||
override def usage: String = | ||
"_FUNC_(expr1) - Returns z coordinate of a point or z coordinate of an arbitrary point in geometry if it isn't a point." | ||
|
||
override def example: String = | ||
""" | ||
| Examples: | ||
| > SELECT _FUNC_(a); | ||
| 12.3 | ||
| """.stripMargin | ||
|
||
override def builder(expressionConfig: MosaicExpressionConfig): FunctionBuilder = { | ||
GenericExpressionFactory.getBaseBuilder[ST_Z](1, 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
87 changes: 87 additions & 0 deletions
87
src/test/scala/com/databricks/labs/mosaic/expressions/geometry/ST_ZBehaviors.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,87 @@ | ||
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_ZBehaviors extends MosaicSpatialQueryTest { | ||
|
||
def stzBehavior(mosaicContext: MosaicContext): Unit = { | ||
spark.sparkContext.setLogLevel("FATAL") | ||
val mc = mosaicContext | ||
import mc.functions._ | ||
val sc = spark | ||
import sc.implicits._ | ||
mc.register(spark) | ||
|
||
val rows = List( | ||
("POINT (2 3 5)", 5), | ||
("POINT (7 11 13)", 13), | ||
("POINT (17 19 23)", 23), | ||
("POINT (29 31 37)", 37) | ||
) | ||
|
||
val result = rows | ||
.toDF("wkt", "expected") | ||
.withColumn("result", st_z($"wkt")) | ||
.where($"expected" === $"result") | ||
|
||
result.count shouldBe 4 | ||
} | ||
|
||
def stzCodegen(mosaicContext: MosaicContext): Unit = { | ||
spark.sparkContext.setLogLevel("FATAL") | ||
val mc = mosaicContext | ||
val sc = spark | ||
import mc.functions._ | ||
import sc.implicits._ | ||
mc.register(spark) | ||
|
||
val rows = List( | ||
("POINT (2 3 5)", 5), | ||
("POINT (7 11 13)", 13), | ||
("POINT (17 19 23)", 23), | ||
("POINT (29 31 37)", 37) | ||
) | ||
|
||
val points = rows.toDF("wkt", "expected") | ||
|
||
val result = points | ||
.withColumn("result", st_z($"wkt")) | ||
.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 stZ = ST_Z(lit(1).expr, mc.expressionConfig) | ||
val ctx = new CodegenContext | ||
an[Error] should be thrownBy stZ.genCode(ctx) | ||
} | ||
|
||
def auxiliaryMethods(mosaicContext: MosaicContext): Unit = { | ||
spark.sparkContext.setLogLevel("FATAL") | ||
val mc = mosaicContext | ||
mc.register(spark) | ||
|
||
val stZ = ST_Z(lit("POINT (2 3 4)").expr, mc.expressionConfig) | ||
|
||
stZ.child shouldEqual lit("POINT (2 3 4)").expr | ||
stZ.dataType shouldEqual DoubleType | ||
noException should be thrownBy stZ.makeCopy(Array(stZ.child)) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/test/scala/com/databricks/labs/mosaic/expressions/geometry/ST_ZTest.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_ZTest extends MosaicSpatialQueryTest with SharedSparkSession with ST_ZBehaviors { | ||
|
||
testAllGeometriesNoCodegen("Testing stZ NO_CODEGEN") { stzBehavior } | ||
testAllGeometriesCodegen("Testing stZ CODEGEN") { stzBehavior } | ||
testAllGeometriesCodegen("Testing stZ CODEGEN compilation") { stzCodegen } | ||
testAllGeometriesNoCodegen("Testing stZ auxiliary methods") { auxiliaryMethods } | ||
|
||
} |