aboutsummaryrefslogtreecommitdiff
path: root/sql/hive
diff options
context:
space:
mode:
authorDilip Biswal <dbiswal@us.ibm.com>2016-05-31 15:49:45 -0700
committerDavies Liu <davies.liu@gmail.com>2016-05-31 15:49:45 -0700
commitdfe2cbeb437a4fa69bec3eca4ac9242f3eb51c81 (patch)
treed8e4dc60ec5aaf443ac50c13456d0eb67b7eb793 /sql/hive
parent2df6ca848e99b90acd11c3a3de342fa4d77015d6 (diff)
downloadspark-dfe2cbeb437a4fa69bec3eca4ac9242f3eb51c81.tar.gz
spark-dfe2cbeb437a4fa69bec3eca4ac9242f3eb51c81.tar.bz2
spark-dfe2cbeb437a4fa69bec3eca4ac9242f3eb51c81.zip
[SPARK-15557] [SQL] cast the string into DoubleType when it's used together with decimal
In this case, the result type of the expression becomes DECIMAL(38, 36) as we promote the individual string literals to DECIMAL(38, 18) when we handle string promotions for `BinaryArthmaticExpression`. I think we need to cast the string literals to Double type instead. I looked at the history and found that this was changed to use decimal instead of double to avoid potential loss of precision when we cast decimal to double. To double check i ran the query against hive, mysql. This query returns non NULL result for both the databases and both promote the expression to use double. Here is the output. - Hive ```SQL hive> create table l2 as select (cast(99 as decimal(19,6)) + '2') from l1; OK hive> describe l2; OK _c0 double ``` - MySQL ```SQL mysql> create table foo2 as select (cast(99 as decimal(19,6)) + '2') from test; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> describe foo2; +-----------------------------------+--------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------------------+--------+------+-----+---------+-------+ | (cast(99 as decimal(19,6)) + '2') | double | NO | | 0 | | +-----------------------------------+--------+------+-----+---------+-------+ ``` ## How was this patch tested? Added a new test in SQLQuerySuite Author: Dilip Biswal <dbiswal@us.ibm.com> Closes #13368 from dilipbiswal/spark-15557.
Diffstat (limited to 'sql/hive')
-rw-r--r--sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala19
1 files changed, 19 insertions, 0 deletions
diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
index 4b51f021bf..2a9b06b75e 100644
--- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
+++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
@@ -1560,4 +1560,23 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
checkAnswer(sql("SELECT * FROM tbl"), Row(1, "a"))
}
}
+
+ test("spark-15557 promote string test") {
+ withTable("tbl") {
+ sql("CREATE TABLE tbl(c1 string, c2 string)")
+ sql("insert into tbl values ('3', '2.3')")
+ checkAnswer(
+ sql("select (cast (99 as decimal(19,6)) + cast('3' as decimal)) * cast('2.3' as decimal)"),
+ Row(204.0)
+ )
+ checkAnswer(
+ sql("select (cast(99 as decimal(19,6)) + '3') *'2.3' from tbl"),
+ Row(234.6)
+ )
+ checkAnswer(
+ sql("select (cast(99 as decimal(19,6)) + c1) * c2 from tbl"),
+ Row(234.6)
+ )
+ }
+ }
}