Skip to content

Commit e18568f

Browse files
committed
Test for legacy and new implementation of size()
1 parent 2338b78 commit e18568f

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -487,26 +487,29 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext {
487487
}.getMessage().contains("only supports array input"))
488488
}
489489

490-
test("array size function") {
490+
def testSizeOfArray(sizeOfNull: Any): Unit = {
491491
val df = Seq(
492492
(Seq[Int](1, 2), "x"),
493493
(Seq[Int](), "y"),
494494
(Seq[Int](1, 2, 3), "z"),
495495
(null, "empty")
496496
).toDF("a", "b")
497-
checkAnswer(
498-
df.select(size($"a")),
499-
Seq(Row(2), Row(0), Row(3), Row(-1))
500-
)
501-
checkAnswer(
502-
df.selectExpr("size(a)"),
503-
Seq(Row(2), Row(0), Row(3), Row(-1))
504-
)
505497

506-
checkAnswer(
507-
df.selectExpr("cardinality(a)"),
508-
Seq(Row(2L), Row(0L), Row(3L), Row(-1L))
509-
)
498+
checkAnswer(df.select(size($"a")), Seq(Row(2), Row(0), Row(3), Row(sizeOfNull)))
499+
checkAnswer(df.selectExpr("size(a)"), Seq(Row(2), Row(0), Row(3), Row(sizeOfNull)))
500+
checkAnswer(df.selectExpr("cardinality(a)"), Seq(Row(2L), Row(0L), Row(3L), Row(sizeOfNull)))
501+
}
502+
503+
test("array size function - legacy") {
504+
withSQLConf("spark.sql.legacy.sizeOfNull" -> "true") {
505+
testSizeOfArray(sizeOfNull = -1)
506+
}
507+
}
508+
509+
test("array size function") {
510+
withSQLConf("spark.sql.legacy.sizeOfNull" -> "false") {
511+
testSizeOfArray(sizeOfNull = null)
512+
}
510513
}
511514

512515
test("dataframe arrays_zip function") {
@@ -556,21 +559,28 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext {
556559
checkAnswer(df8.selectExpr("arrays_zip(v1, v2)"), expectedValue8)
557560
}
558561

559-
test("map size function") {
562+
def testSizeOfMap(sizeOfNull: Any): Unit = {
560563
val df = Seq(
561564
(Map[Int, Int](1 -> 1, 2 -> 2), "x"),
562565
(Map[Int, Int](), "y"),
563566
(Map[Int, Int](1 -> 1, 2 -> 2, 3 -> 3), "z"),
564567
(null, "empty")
565568
).toDF("a", "b")
566-
checkAnswer(
567-
df.select(size($"a")),
568-
Seq(Row(2), Row(0), Row(3), Row(-1))
569-
)
570-
checkAnswer(
571-
df.selectExpr("size(a)"),
572-
Seq(Row(2), Row(0), Row(3), Row(-1))
573-
)
569+
570+
checkAnswer(df.select(size($"a")), Seq(Row(2), Row(0), Row(3), Row(sizeOfNull)))
571+
checkAnswer(df.selectExpr("size(a)"), Seq(Row(2), Row(0), Row(3), Row(sizeOfNull)))
572+
}
573+
574+
test("map size function - legacy") {
575+
withSQLConf("spark.sql.legacy.sizeOfNull" -> "true") {
576+
testSizeOfMap(sizeOfNull = -1: Int)
577+
}
578+
}
579+
580+
test("map size function") {
581+
withSQLConf("spark.sql.legacy.sizeOfNull" -> "false") {
582+
testSizeOfMap(sizeOfNull = null)
583+
}
574584
}
575585

576586
test("map_keys/map_values function") {

0 commit comments

Comments
 (0)