Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/6405#discussion_r31300685
--- Diff:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionTypeCheckingSuite.scala
---
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.expressions
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.analysis.SimpleAnalyzer
+import org.apache.spark.sql.catalyst.plans.logical.LocalRelation
+import org.apache.spark.sql.catalyst.dsl.plans._
+import org.apache.spark.sql.catalyst.dsl.expressions._
+import org.apache.spark.sql.types.{BooleanType, StringType}
+import org.scalatest.FunSuite
+
+
+class ExpressionTypeCheckingSuite extends FunSuite {
+
+ val testRelation = LocalRelation('a.int, 'b.string, 'c.boolean,
'd.array(StringType))
+
+ def checkError(expr: Expression, errorMessage: String): Unit = {
+ val e = intercept[AnalysisException] {
+ checkAnalysis(expr)
+ }
+ assert(e.getMessage.contains(
+ s"cannot resolve '${expr.prettyString}' due to data type mismatch:"))
+ assert(e.getMessage.contains(errorMessage))
+ }
+
+ def checkAnalysis(expr: Expression): Unit = {
+ val analyzed = testRelation.select(expr.as("_c")).analyze
+ SimpleAnalyzer.checkAnalysis(analyzed)
+ }
+
+ test("check types for unary arithmetic") {
+ checkError(UnaryMinus('b), "operator - requires numeric type")
+ checkAnalysis(Sqrt('b)) // We will cast String to Double for sqrt
+ checkError(Sqrt('c), "function sqrt requires numeric type")
+ checkError(Abs('b), "function abs requires numeric type")
+ checkError(BitwiseNot('b), "operator ~ requires integral type")
+ }
+
+ test("check types for binary arithmetic") {
+ // We will cast String to Double for binary arithmetic
+ checkAnalysis(Add('a, 'b))
+ checkAnalysis(Subtract('a, 'b))
+ checkAnalysis(Multiply('a, 'b))
+ checkAnalysis(Divide('a, 'b))
+ checkAnalysis(Remainder('a, 'b))
+ // checkAnalysis(BitwiseAnd('a, 'b))
+
+ val msg = "differing types in BinaryArithmetic, IntegerType !=
BooleanType"
+ checkError(Add('a, 'c), msg)
+ checkError(Subtract('a, 'c), msg)
+ checkError(Multiply('a, 'c), msg)
+ checkError(Divide('a, 'c), msg)
+ checkError(Remainder('a, 'c), msg)
+ checkError(BitwiseAnd('a, 'c), msg)
+ checkError(BitwiseOr('a, 'c), msg)
+ checkError(BitwiseXor('a, 'c), msg)
+ checkError(MaxOf('a, 'c), msg)
+ checkError(MinOf('a, 'c), msg)
+
+ checkError(Add('c, 'c), "operator + requires numeric type")
+ checkError(Subtract('c, 'c), "operator - requires numeric type")
+ checkError(Multiply('c, 'c), "operator * requires numeric type")
+ checkError(Divide('c, 'c), "operator / requires numeric type")
+ checkError(Remainder('c, 'c), "operator % requires numeric type")
+
+ checkError(BitwiseAnd('c, 'c), "operator & requires integral type")
+ checkError(BitwiseOr('c, 'c), "operator | requires integral type")
+ checkError(BitwiseXor('c, 'c), "operator ^ requires integral type")
+
+ checkError(MaxOf('d, 'd), "function maxOf requires atomic type")
+ checkError(MinOf('d, 'd), "function minOf requires atomic type")
+ }
+
+ test("check types for predicates") {
+ // EqualTo don't have type constraint
+ checkAnalysis(EqualTo('a, 'c))
--- End diff --
don't the two need to have the same type?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]