Skip to content
Snippets Groups Projects
Commit f913f158 authored by Yuming Wang's avatar Yuming Wang Committed by gatorsmile
Browse files

[SPARK-20948][SQL] Built-in SQL Function UnaryMinus/UnaryPositive support string type

## What changes were proposed in this pull request?

Built-in SQL Function UnaryMinus/UnaryPositive support string type, if it's string type, convert it to double type, after this PR:
```sql
spark-sql> select positive('-1.11'), negative('-1.11');
-1.11   1.11
spark-sql>
```

## How was this patch tested?

unit tests

Author: Yuming Wang <wgyumg@gmail.com>

Closes #18173 from wangyum/SPARK-20948.
parent ce49428e
No related branches found
No related tags found
No related merge requests found
......@@ -362,6 +362,8 @@ object TypeCoercion {
case Average(e @ StringType()) => Average(Cast(e, DoubleType))
case StddevPop(e @ StringType()) => StddevPop(Cast(e, DoubleType))
case StddevSamp(e @ StringType()) => StddevSamp(Cast(e, DoubleType))
case UnaryMinus(e @ StringType()) => UnaryMinus(Cast(e, DoubleType))
case UnaryPositive(e @ StringType()) => UnaryPositive(Cast(e, DoubleType))
case VariancePop(e @ StringType()) => VariancePop(Cast(e, DoubleType))
case VarianceSamp(e @ StringType()) => VarianceSamp(Cast(e, DoubleType))
case Skewness(e @ StringType()) => Skewness(Cast(e, DoubleType))
......
......@@ -56,7 +56,6 @@ class ExpressionTypeCheckingSuite extends SparkFunSuite {
}
test("check types for unary arithmetic") {
assertError(UnaryMinus('stringField), "(numeric or calendarinterval) type")
assertError(BitwiseNot('stringField), "requires integral type")
}
......
......@@ -89,3 +89,6 @@ select OCTET_LENGTH('abc');
-- abs
select abs(-3.13), abs('-2.19');
-- positive/negative
select positive('-1.11'), positive(-1.11), negative('-1.11'), negative(-1.11);
......@@ -460,3 +460,11 @@ select abs(-3.13), abs('-2.19')
struct<abs(-3.13):decimal(3,2),abs(CAST(-2.19 AS DOUBLE)):double>
-- !query 55 output
3.13 2.19
-- !query 55
select positive('-1.11'), positive(-1.11), negative('-1.11'), negative(-1.11)
-- !query 55 schema
struct<(+ CAST(-1.11 AS DOUBLE)):double,(+ -1.11):decimal(3,2),(- CAST(-1.11 AS DOUBLE)):double,(- -1.11):decimal(3,2)>
-- !query 55 output
-1.11 -1.11 1.11 1.11
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment