[SPARK-29554][SQL] Add version SQL function

### What changes were proposed in this pull request?

```
hive> select version();
OK
3.1.1 rf4e0529634b6231a0072295da48af466cf2f10b7
Time taken: 2.113 seconds, Fetched: 1 row(s)
```

### Why are the changes needed?

From hive behavior and I guess it is useful for debugging and developing etc.

### Does this PR introduce any user-facing change?

add a misc func

### How was this patch tested?

add ut

Closes #26209 from yaooqinn/SPARK-29554.

Authored-by: Kent Yao <yaooqinn@hotmail.com>
Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
This commit is contained in:
Kent Yao 2019-10-25 23:02:11 -07:00 committed by Dongjoon Hyun
parent 5bdc58bf8a
commit 9a46702791
3 changed files with 23 additions and 2 deletions

View file

@ -486,6 +486,7 @@ object FunctionRegistry {
expression[CurrentDatabase]("current_database"),
expression[CallMethodViaReflection]("reflect"),
expression[CallMethodViaReflection]("java_method"),
expression[Version]("version"),
// grouping sets
expression[Cube]("cube"),

View file

@ -17,8 +17,7 @@
package org.apache.spark.sql.catalyst.expressions
import java.util.UUID
import org.apache.spark.{SPARK_REVISION, SPARK_VERSION_SHORT}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
@ -164,3 +163,17 @@ case class Uuid(randomSeed: Option[Long] = None) extends LeafExpression with Sta
override def freshCopy(): Uuid = Uuid(randomSeed)
}
// scalastyle:off line.size.limit
@ExpressionDescription(
usage = """_FUNC_() - Returns the Spark version. The string contains 2 fields, the first being a release version and the second being a git revision.""",
since = "3.0.0")
// scalastyle:on line.size.limit
case class Version() extends LeafExpression with CodegenFallback {
override def nullable: Boolean = false
override def foldable: Boolean = true
override def dataType: DataType = StringType
override def eval(input: InternalRow): Any = {
UTF8String.fromString(SPARK_VERSION_SHORT + " " + SPARK_REVISION)
}
}

View file

@ -17,6 +17,7 @@
package org.apache.spark.sql
import org.apache.spark.{SPARK_REVISION, SPARK_VERSION_SHORT}
import org.apache.spark.sql.test.SharedSparkSession
class MiscFunctionsSuite extends QueryTest with SharedSparkSession {
@ -31,6 +32,12 @@ class MiscFunctionsSuite extends QueryTest with SharedSparkSession {
s"java_method('$className', 'method1', a, b)"),
Row("m1one", "m1one"))
}
test("version") {
checkAnswer(
Seq("").toDF("a").selectExpr("version()"),
Row(SPARK_VERSION_SHORT + " " + SPARK_REVISION))
}
}
object ReflectClass {