diff options
author | Dilip Biswal <dbiswal@us.ibm.com> | 2015-12-22 15:21:49 -0800 |
---|---|---|
committer | Yin Huai <yhuai@databricks.com> | 2015-12-22 15:21:49 -0800 |
commit | b374a25831af031f461716c52b615665aa5392c2 (patch) | |
tree | 593841d7f7c1532902b7a410fc1d22774ee6776e | |
parent | 575a1327976202614a6d3268918ae8dad49fcd72 (diff) | |
download | spark-b374a25831af031f461716c52b615665aa5392c2.tar.gz spark-b374a25831af031f461716c52b615665aa5392c2.tar.bz2 spark-b374a25831af031f461716c52b615665aa5392c2.zip |
[SPARK-12102][SQL] Cast a non-nullable struct field to a nullable field during analysis
Compare both left and right side of the case expression ignoring nullablity when checking for type equality.
Author: Dilip Biswal <dbiswal@us.ibm.com>
Closes #10156 from dilipbiswal/spark-12102.
2 files changed, 9 insertions, 1 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala index 40b1eec63e..f79c8676fb 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala @@ -91,7 +91,9 @@ trait CaseWhenLike extends Expression { // both then and else expressions should be considered. def valueTypes: Seq[DataType] = (thenList ++ elseValue).map(_.dataType) - def valueTypesEqual: Boolean = valueTypes.distinct.size == 1 + def valueTypesEqual: Boolean = valueTypes.size <= 1 || valueTypes.sliding(2, 1).forall { + case Seq(dt1, dt2) => dt1.sameType(dt2) + } override def checkInputDataTypes(): TypeCheckResult = { if (valueTypesEqual) { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala index aeeca802d8..fa823e3021 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala @@ -274,4 +274,10 @@ class AnalysisSuite extends AnalysisTest { assert(lits(1) >= min && lits(1) <= max) assert(lits(0) == lits(1)) } + + test("SPARK-12102: Ignore nullablity when comparing two sides of case") { + val relation = LocalRelation('a.struct('x.int), 'b.struct('x.int.withNullability(false))) + val plan = relation.select(CaseWhen(Seq(Literal(true), 'a, 'b)).as("val")) + assertAnalysisSuccess(plan) + } } |