Skip to content

Commit cb54407

Browse files
author
DB Tsai
committed
na.fill will change the values in long or integer when the default value is in double
1 parent 34fc48f commit cb54407

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,11 @@ final class DataFrameNaFunctions private[sql](df: DataFrame) {
407407
val quotedColName = "`" + col.name + "`"
408408
val colValue = col.dataType match {
409409
case DoubleType | FloatType =>
410-
nanvl(df.col(quotedColName), lit(null)) // nanvl only supports these types
410+
// nanvl only supports these types
411+
nanvl(df.col(quotedColName), lit(null).cast(col.dataType))
411412
case _ => df.col(quotedColName)
412413
}
413-
coalesce(colValue, lit(replacement)).cast(col.dataType).as(col.name)
414+
coalesce(colValue, lit(replacement).cast(col.dataType)).as(col.name)
414415
}
415416

416417
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ class DataFrameNaFunctionsSuite extends QueryTest with SharedSQLContext {
145145
Row(1, 2) :: Row(-1, -2) :: Row(9123146099426677101L, 9123146560113991650L) :: Nil
146146
)
147147

148+
checkAnswer(
149+
Seq[(java.lang.Long, java.lang.Double)]((null, 3.14), (9123146099426677101L, null),
150+
(9123146560113991650L, 1.6), (null, null)).toDF("a", "b").na.fill(0.2),
151+
Row(0, 3.14) :: Row(9123146099426677101L, 0.3) :: Row(9123146560113991650L, 1.6)
152+
:: Row(0, 0.2) :: Nil
153+
)
154+
148155
checkAnswer(
149156
Seq[(java.lang.Long, java.lang.Double)]((null, 1.23), (3L, null), (4L, 3.45))
150157
.toDF("a", "b").na.fill(2.34),

0 commit comments

Comments
 (0)