aboutsummaryrefslogtreecommitdiff
path: root/sql/catalyst/src/main/scala/org/apache
diff options
context:
space:
mode:
authorpetermaxlee <petermaxlee@gmail.com>2016-08-18 13:44:13 +0200
committerHerman van Hovell <hvanhovell@databricks.com>2016-08-18 13:44:13 +0200
commit68f5087d2107d6afec5d5745f0cb0e9e3bdd6a0b (patch)
treebf354d1a234d37e7458451000778ca9f474d07a7 /sql/catalyst/src/main/scala/org/apache
parent412dba63b511474a6db3c43c8618d803e604bc6b (diff)
downloadspark-68f5087d2107d6afec5d5745f0cb0e9e3bdd6a0b.tar.gz
spark-68f5087d2107d6afec5d5745f0cb0e9e3bdd6a0b.tar.bz2
spark-68f5087d2107d6afec5d5745f0cb0e9e3bdd6a0b.zip
[SPARK-17117][SQL] 1 / NULL should not fail analysis
## What changes were proposed in this pull request? This patch fixes the problem described in SPARK-17117, i.e. "SELECT 1 / NULL" throws an analysis exception: ``` org.apache.spark.sql.AnalysisException: cannot resolve '(1 / NULL)' due to data type mismatch: differing types in '(1 / NULL)' (int and null). ``` The problem is that division type coercion did not take null type into account. ## How was this patch tested? A unit test for the type coercion, and a few end-to-end test cases using SQLQueryTestSuite. Author: petermaxlee <petermaxlee@gmail.com> Closes #14695 from petermaxlee/SPARK-17117.
Diffstat (limited to 'sql/catalyst/src/main/scala/org/apache')
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala7
1 files changed, 5 insertions, 2 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala
index 021952e716..21e96aaf53 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala
@@ -543,11 +543,14 @@ object TypeCoercion {
// Decimal and Double remain the same
case d: Divide if d.dataType == DoubleType => d
case d: Divide if d.dataType.isInstanceOf[DecimalType] => d
- case Divide(left, right) if isNumeric(left) && isNumeric(right) =>
+ case Divide(left, right) if isNumericOrNull(left) && isNumericOrNull(right) =>
Divide(Cast(left, DoubleType), Cast(right, DoubleType))
}
- private def isNumeric(ex: Expression): Boolean = ex.dataType.isInstanceOf[NumericType]
+ private def isNumericOrNull(ex: Expression): Boolean = {
+ // We need to handle null types in case a query contains null literals.
+ ex.dataType.isInstanceOf[NumericType] || ex.dataType == NullType
+ }
}
/**