Skip to content

Commit 8f7953b

Browse files
committed
Revert "Throw exception when column is not nullable"
This reverts commit fcb617e.
1 parent dfbcaf3 commit 8f7953b

2 files changed

Lines changed: 4 additions & 23 deletions

File tree

sql/core/src/main/scala/org/apache/spark/sql/DataFrameNaFunctions.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,6 @@ final class DataFrameNaFunctions private[sql](df: DataFrame) {
453453
* TODO: This can be optimized to use broadcast join when replacementMap is large.
454454
*/
455455
private def replaceCol(col: StructField, replacementMap: Map[_, _]): Column = {
456-
if (!col.nullable && !replacementMap.values.forall(_ != null)) {
457-
throw new IllegalArgumentException(s"Column '${col.name}' is not nullable " +
458-
s"and can not be replaced to null.")
459-
}
460456
val keyExpr = df.col(col.name).expr
461457
def buildExpr(v: Any) = Cast(Literal(v), keyExpr.dataType)
462458
val branches = replacementMap.flatMap { case (source, target) =>

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import scala.collection.JavaConverters._
2121

2222
import org.apache.spark.sql.internal.SQLConf
2323
import org.apache.spark.sql.test.SharedSQLContext
24-
import org.apache.spark.sql.types._
2524

2625
class DataFrameNaFunctionsSuite extends QueryTest with SharedSQLContext {
2726
import testImplicits._
@@ -233,10 +232,10 @@ class DataFrameNaFunctionsSuite extends QueryTest with SharedSQLContext {
233232
}
234233

235234
test("replace") {
236-
val input1 = createDF()
235+
val input = createDF()
237236

238237
// Replace two numeric columns: age and height
239-
val out = input1.na.replace(Seq("age", "height"), Map(
238+
val out = input.na.replace(Seq("age", "height"), Map(
240239
16 -> 61,
241240
60 -> 6,
242241
164.3 -> 461.3 // Alice is really tall
@@ -249,8 +248,8 @@ class DataFrameNaFunctionsSuite extends QueryTest with SharedSQLContext {
249248
assert(out(4) === Row("Amy", null, null))
250249
assert(out(5) === Row(null, null, null))
251250

252-
// Replace only the age column and with null
253-
val out1 = input1.na.replace("age", Map[Any, Any](
251+
// Replace only the age column
252+
val out1 = input.na.replace("age", Map[Any, Any](
254253
16 -> 61,
255254
60 -> null,
256255
164.3 -> 461.3 // Alice is really tall
@@ -262,19 +261,5 @@ class DataFrameNaFunctionsSuite extends QueryTest with SharedSQLContext {
262261
assert(out1(3).get(2).asInstanceOf[Double].isNaN)
263262
assert(out1(4) === Row("Amy", null, null))
264263
assert(out1(5) === Row(null, null, null))
265-
266-
// Replace with null on a column that is not nullable
267-
val rows = spark.sparkContext.parallelize(Seq(
268-
Row("Bravo", 28, 183.5),
269-
Row("Jessie", 18, 165.8)))
270-
val schema = StructType(Seq(
271-
StructField("name", StringType, nullable = false),
272-
StructField("age", IntegerType, nullable = true),
273-
StructField("height", DoubleType, nullable = true)))
274-
val input2 = spark.createDataFrame(rows, schema)
275-
val message = intercept[IllegalArgumentException] {
276-
input2.na.replace("name", Map("Bravo" -> null))
277-
}.getMessage
278-
assert(message === "Column 'name' is not nullable and can not be replaced to null.")
279264
}
280265
}

0 commit comments

Comments
 (0)