Skip to content

Commit

Permalink
[SPARK-20948][SQL] Built-in SQL Function UnaryMinus/UnaryPositive sup…
Browse files Browse the repository at this point in the history
…port 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.
  • Loading branch information
wangyum authored and gatorsmile committed Jun 19, 2017
1 parent ce49428 commit f913f15
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
3 changes: 3 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/operators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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 comments on commit f913f15

Please sign in to comment.