Skip to content

Commit 3affbd8

Browse files
committed
more fixes
1 parent 654d46a commit 3affbd8

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCheckResult.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package org.apache.spark.sql.catalyst.analysis
1919

2020
/**
21-
* todo
21+
* Represents the result of `Expression.checkInputDataTypes`.
22+
* We will throw `AnalysisException` in `CheckAnalysis` if error message is not null.
23+
*
2224
*/
2325
class TypeCheckResult(val errorMessage: String) extends AnyVal {
2426
def hasError: Boolean = errorMessage != null

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ abstract class BinaryArithmetic extends BinaryExpression {
9191
override def checkInputDataTypes(): TypeCheckResult = {
9292
if (left.dataType != right.dataType) {
9393
TypeCheckResult.fail(
94-
s"differing types in ${this.getClass.getSimpleName}, ${left.dataType} != ${right.dataType}")
94+
s"differing types in ${this.getClass.getSimpleName} " +
95+
s"(${left.dataType} and ${right.dataType}).")
9596
} else {
9697
checkTypesInternal(dataType)
9798
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ abstract class BinaryComparison extends BinaryExpression with Predicate {
175175
override def checkInputDataTypes(): TypeCheckResult = {
176176
if (left.dataType != right.dataType) {
177177
TypeCheckResult.fail(
178-
s"differing types in ${this.getClass.getSimpleName}, ${left.dataType} != ${right.dataType}")
178+
s"differing types in ${this.getClass.getSimpleName} " +
179+
s"(${left.dataType} and ${right.dataType}).")
179180
} else {
180181
TypeUtils.checkForOrderingExpr(left.dataType, "operator " + symbol)
181182
}
@@ -275,7 +276,7 @@ case class If(predicate: Expression, trueValue: Expression, falseValue: Expressi
275276
s"type of predicate expression in If should be boolean, not ${predicate.dataType}")
276277
} else if (trueValue.dataType != falseValue.dataType) {
277278
TypeCheckResult.fail(
278-
s"differing types in If, ${trueValue.dataType} != ${falseValue.dataType}")
279+
s"differing types in If (${trueValue.dataType} and ${falseValue.dataType}).")
279280
} else {
280281
TypeCheckResult.success
281282
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionTypeCheckingSuite.scala

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class ExpressionTypeCheckingSuite extends FunSuite {
4848
SimpleAnalyzer.checkAnalysis(analyzed)
4949
}
5050

51+
def assertErrorForDifferingTypes(expr: Expression): Unit = {
52+
assertError(expr,
53+
s"differing types in ${expr.getClass.getSimpleName} (IntegerType and BooleanType).")
54+
}
55+
5156
test("check types for unary arithmetic") {
5257
assertError(UnaryMinus('stringField), "operator - accepts numeric type")
5358
assertSuccess(Sqrt('stringField)) // We will cast String to Double for sqrt
@@ -65,17 +70,16 @@ class ExpressionTypeCheckingSuite extends FunSuite {
6570
assertSuccess(Remainder('intField, 'stringField))
6671
// checkAnalysis(BitwiseAnd('intField, 'stringField))
6772

68-
def msg(caller: String) = s"differing types in $caller, IntegerType != BooleanType"
69-
assertError(Add('intField, 'booleanField), msg("Add"))
70-
assertError(Subtract('intField, 'booleanField), msg("Subtract"))
71-
assertError(Multiply('intField, 'booleanField), msg("Multiply"))
72-
assertError(Divide('intField, 'booleanField), msg("Divide"))
73-
assertError(Remainder('intField, 'booleanField), msg("Remainder"))
74-
assertError(BitwiseAnd('intField, 'booleanField), msg("BitwiseAnd"))
75-
assertError(BitwiseOr('intField, 'booleanField), msg("BitwiseOr"))
76-
assertError(BitwiseXor('intField, 'booleanField), msg("BitwiseXor"))
77-
assertError(MaxOf('intField, 'booleanField), msg("MaxOf"))
78-
assertError(MinOf('intField, 'booleanField), msg("MinOf"))
73+
assertErrorForDifferingTypes(Add('intField, 'booleanField))
74+
assertErrorForDifferingTypes(Subtract('intField, 'booleanField))
75+
assertErrorForDifferingTypes(Multiply('intField, 'booleanField))
76+
assertErrorForDifferingTypes(Divide('intField, 'booleanField))
77+
assertErrorForDifferingTypes(Remainder('intField, 'booleanField))
78+
assertErrorForDifferingTypes(BitwiseAnd('intField, 'booleanField))
79+
assertErrorForDifferingTypes(BitwiseOr('intField, 'booleanField))
80+
assertErrorForDifferingTypes(BitwiseXor('intField, 'booleanField))
81+
assertErrorForDifferingTypes(MaxOf('intField, 'booleanField))
82+
assertErrorForDifferingTypes(MinOf('intField, 'booleanField))
7983

8084
assertError(Add('booleanField, 'booleanField), "operator + accepts numeric type")
8185
assertError(Subtract('booleanField, 'booleanField), "operator - accepts numeric type")
@@ -102,19 +106,24 @@ class ExpressionTypeCheckingSuite extends FunSuite {
102106
assertSuccess(GreaterThan('intField, 'stringField))
103107
assertSuccess(GreaterThanOrEqual('intField, 'stringField))
104108

105-
def msg(caller: String) = s"differing types in $caller, IntegerType != BooleanType"
106-
assertError(LessThan('intField, 'booleanField), msg("LessThan"))
107-
assertError(LessThanOrEqual('intField, 'booleanField), msg("LessThanOrEqual"))
108-
assertError(GreaterThan('intField, 'booleanField), msg("GreaterThan"))
109-
assertError(GreaterThanOrEqual('intField, 'booleanField), msg("GreaterThanOrEqual"))
110-
111-
assertError(LessThan('complexField, 'complexField), "operator < accepts non-complex type")
112-
assertError(LessThanOrEqual('complexField, 'complexField), "operator <= accepts non-complex type")
113-
assertError(GreaterThan('complexField, 'complexField), "operator > accepts non-complex type")
114-
assertError(GreaterThanOrEqual('complexField, 'complexField), "operator >= accepts non-complex type")
115-
116-
assertError(If('intField, 'stringField, 'stringField), "type of predicate expression in If should be boolean")
117-
assertError(If('booleanField, 'intField, 'stringField), "differing types in If, IntegerType != StringType")
109+
assertErrorForDifferingTypes(LessThan('intField, 'booleanField))
110+
assertErrorForDifferingTypes(LessThanOrEqual('intField, 'booleanField))
111+
assertErrorForDifferingTypes(GreaterThan('intField, 'booleanField))
112+
assertErrorForDifferingTypes(GreaterThanOrEqual('intField, 'booleanField))
113+
114+
assertError(
115+
LessThan('complexField, 'complexField), "operator < accepts non-complex type")
116+
assertError(
117+
LessThanOrEqual('complexField, 'complexField), "operator <= accepts non-complex type")
118+
assertError(
119+
GreaterThan('complexField, 'complexField), "operator > accepts non-complex type")
120+
assertError(
121+
GreaterThanOrEqual('complexField, 'complexField), "operator >= accepts non-complex type")
122+
123+
assertError(
124+
If('intField, 'stringField, 'stringField),
125+
"type of predicate expression in If should be boolean")
126+
assertErrorForDifferingTypes(If('booleanField, 'intField, 'booleanField))
118127

119128
// Will write tests for CaseWhen later,
120129
// as the error reporting of it is not handle by the new interface for now

0 commit comments

Comments
 (0)