Skip to content

Commit

Permalink
[SPARK-38307][SQL][TESTS] Fix ExpressionTypeCheckingSuite and Collect…
Browse files Browse the repository at this point in the history
…ionExpressionsSuite under ANSI mode

### What changes were proposed in this pull request?
Fix the ExpressionTypeCheckingSuite and CollectionExpressionsSuite under ANSI mode.
For all the failed test cases, wrap with ANSI as false. For some test cases, also add exact same test input wrapping with ANSI true, but set the expectation to the exceptions.

### Why are the changes needed?
To set up a new GA job to run tests with ANSI mode before 3.3.0 release.

### Does this PR introduce _any_ user-facing change?
No.

### How was this patch tested?
Test locally with both ANSI on and off, both passed.

Closes apache#35634 from anchovYu/ansi-tests-expressiontypechecking-collectionexpressions.

Authored-by: Xinyi Yu <[email protected]>
Signed-off-by: Gengliang Wang <[email protected]>
  • Loading branch information
anchovYu authored and gengliangwang committed Feb 24, 2022
1 parent 9257224 commit b28241d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.aggregate._
import org.apache.spark.sql.catalyst.plans.SQLHelper
import org.apache.spark.sql.catalyst.plans.logical.LocalRelation
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._

class ExpressionTypeCheckingSuite extends SparkFunSuite {
class ExpressionTypeCheckingSuite extends SparkFunSuite with SQLHelper {

val testRelation = LocalRelation(
Symbol("intField").int,
Expand Down Expand Up @@ -103,8 +105,14 @@ class ExpressionTypeCheckingSuite extends SparkFunSuite {
assertSuccess(GreaterThanOrEqual(Symbol("intField"), Symbol("stringField")))

// We will transform EqualTo with numeric and boolean types to CaseKeyWhen
assertSuccess(EqualTo(Symbol("intField"), Symbol("booleanField")))
assertSuccess(EqualNullSafe(Symbol("intField"), Symbol("booleanField")))
withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") {
assertSuccess(EqualTo(Symbol("intField"), Symbol("booleanField")))
assertSuccess(EqualNullSafe(Symbol("intField"), Symbol("booleanField")))
}
withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
assertError(EqualTo(Symbol("intField"), Symbol("booleanField")), "differing types")
assertError(EqualNullSafe(Symbol("intField"), Symbol("booleanField")), "differing types")
}

assertErrorForDifferingTypes(EqualTo(Symbol("intField"), Symbol("mapField")))
assertErrorForDifferingTypes(EqualNullSafe(Symbol("intField"), Symbol("mapField")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
}

test("Array and Map Size - legacy") {
withSQLConf(SQLConf.LEGACY_SIZE_OF_NULL.key -> "true") {
withSQLConf(
SQLConf.LEGACY_SIZE_OF_NULL.key -> "true",
SQLConf.ANSI_ENABLED.key -> "false") {
testSize(sizeOfNull = -1)
}
}
Expand Down Expand Up @@ -1437,8 +1439,10 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(ElementAt(a0, Literal(0)), null)
}.getMessage.contains("SQL array indices start at 1")
intercept[Exception] { checkEvaluation(ElementAt(a0, Literal(1.1)), null) }
checkEvaluation(ElementAt(a0, Literal(4)), null)
checkEvaluation(ElementAt(a0, Literal(-4)), null)
withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") {
checkEvaluation(ElementAt(a0, Literal(4)), null)
checkEvaluation(ElementAt(a0, Literal(-4)), null)
}

checkEvaluation(ElementAt(a0, Literal(1)), 1)
checkEvaluation(ElementAt(a0, Literal(2)), 2)
Expand All @@ -1464,9 +1468,10 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper

assert(ElementAt(m0, Literal(1.0)).checkInputDataTypes().isFailure)

checkEvaluation(ElementAt(m0, Literal("d")), null)

checkEvaluation(ElementAt(m1, Literal("a")), null)
withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") {
checkEvaluation(ElementAt(m0, Literal("d")), null)
checkEvaluation(ElementAt(m1, Literal("a")), null)
}

checkEvaluation(ElementAt(m0, Literal("a")), "1")
checkEvaluation(ElementAt(m0, Literal("b")), "2")
Expand All @@ -1480,9 +1485,10 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
MapType(BinaryType, StringType))
val mb1 = Literal.create(Map[Array[Byte], String](), MapType(BinaryType, StringType))

checkEvaluation(ElementAt(mb0, Literal(Array[Byte](1, 2, 3))), null)

checkEvaluation(ElementAt(mb1, Literal(Array[Byte](1, 2))), null)
withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") {
checkEvaluation(ElementAt(mb0, Literal(Array[Byte](1, 2, 3))), null)
checkEvaluation(ElementAt(mb1, Literal(Array[Byte](1, 2))), null)
}
checkEvaluation(ElementAt(mb0, Literal(Array[Byte](2, 1), BinaryType)), "2")
checkEvaluation(ElementAt(mb0, Literal(Array[Byte](3, 4))), null)
}
Expand Down

0 comments on commit b28241d

Please sign in to comment.