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

[SPARK-20931][SQL] ABS function support string type.

## What changes were proposed in this pull request?

ABS function support string type. Hive/MySQL support this feature.

Ref: https://github.com/apache/hive/blob/4ba713ccd85c3706d195aeef9476e6e6363f1c21/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAbs.java#L93

## How was this patch tested?
 unit tests

Author: Yuming Wang <wgyumg@gmail.com>

Closes #18153 from wangyum/SPARK-20931.
parent d1c333ac
No related branches found
No related tags found
No related merge requests found
......@@ -357,6 +357,7 @@ object TypeCoercion {
val commonType = findCommonTypeForBinaryComparison(left.dataType, right.dataType).get
p.makeCopy(Array(castExpr(left, commonType), castExpr(right, commonType)))
case Abs(e @ StringType()) => Abs(Cast(e, DoubleType))
case Sum(e @ StringType()) => Sum(Cast(e, DoubleType))
case Average(e @ StringType()) => Average(Cast(e, DoubleType))
case StddevPop(e @ StringType()) => StddevPop(Cast(e, DoubleType))
......
......@@ -57,7 +57,6 @@ class ExpressionTypeCheckingSuite extends SparkFunSuite {
test("check types for unary arithmetic") {
assertError(UnaryMinus('stringField), "(numeric or calendarinterval) type")
assertError(Abs('stringField), "requires numeric type")
assertError(BitwiseNot('stringField), "requires integral type")
}
......
......@@ -85,3 +85,6 @@ select mod(7, 2), mod(7, 0), mod(0, 2), mod(7, null), mod(null, 2), mod(null, nu
select BIT_LENGTH('abc');
select CHAR_LENGTH('abc');
select OCTET_LENGTH('abc');
-- abs
select abs(-3.13), abs('-2.19');
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 54
-- Number of queries: 55
-- !query 0
......@@ -444,3 +444,11 @@ select OCTET_LENGTH('abc')
struct<octetlength(abc):int>
-- !query 53 output
3
-- !query 54
select abs(-3.13), abs('-2.19')
-- !query 54 schema
struct<abs(-3.13):decimal(3,2),abs(CAST(-2.19 AS DOUBLE)):double>
-- !query 54 output
3.13 2.19
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