Skip to content

Commit 3ba34d4

Browse files
committed
[SPARK-14990][SQL] Fix checkForSameTypeInputExpr (ignore nullability)
## What changes were proposed in this pull request? This patch fixes a bug in TypeUtils.checkForSameTypeInputExpr. Previously the code was testing on strict equality, which does not taking nullability into account. This is based on #12768. This patch fixed a bug there (with empty expression) and added a test case. ## How was this patch tested? Added a new test suite and test case. Closes #12768. Author: Reynold Xin <[email protected]> Author: Oleg Danilov <[email protected]> Closes #13208 from rxin/SPARK-14990.
1 parent f2ee0ed commit 3ba34d4

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ object TypeUtils {
4242
}
4343

4444
def checkForSameTypeInputExpr(types: Seq[DataType], caller: String): TypeCheckResult = {
45-
if (types.distinct.size > 1) {
46-
TypeCheckResult.TypeCheckFailure(
47-
s"input to $caller should all be the same type, but it's " +
48-
types.map(_.simpleString).mkString("[", ", ", "]"))
45+
if (types.size <= 1) {
46+
TypeCheckResult.TypeCheckSuccess
4947
} else {
48+
val firstType = types.head
49+
types.foreach { t =>
50+
if (!t.sameType(firstType)) {
51+
return TypeCheckResult.TypeCheckFailure(
52+
s"input to $caller should all be the same type, but it's " +
53+
types.map(_.simpleString).mkString("[", ", ", "]"))
54+
}
55+
}
5056
TypeCheckResult.TypeCheckSuccess
5157
}
5258
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst.util
19+
20+
import org.apache.spark.SparkFunSuite
21+
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
22+
import org.apache.spark.sql.types._
23+
24+
class TypeUtilsSuite extends SparkFunSuite {
25+
26+
private def typeCheckPass(types: Seq[DataType]): Unit = {
27+
assert(TypeUtils.checkForSameTypeInputExpr(types, "a") == TypeCheckSuccess)
28+
}
29+
30+
private def typeCheckFail(types: Seq[DataType]): Unit = {
31+
assert(TypeUtils.checkForSameTypeInputExpr(types, "a").isInstanceOf[TypeCheckFailure])
32+
}
33+
34+
test("checkForSameTypeInputExpr") {
35+
typeCheckPass(Nil)
36+
typeCheckPass(StringType :: Nil)
37+
typeCheckPass(StringType :: StringType :: Nil)
38+
39+
typeCheckFail(StringType :: IntegerType :: Nil)
40+
typeCheckFail(StringType :: IntegerType :: Nil)
41+
42+
// Should also work on arrays. See SPARK-14990
43+
typeCheckPass(ArrayType(StringType, containsNull = true) ::
44+
ArrayType(StringType, containsNull = false) :: Nil)
45+
}
46+
}

0 commit comments

Comments
 (0)