Skip to content

Commit

Permalink
[SPARK-42176][SQL] Fix cast of a boolean value to timestamp
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

The PR fixes an issue when casting a boolean to timestamp.

While `select cast(true as timestamp)` works and returns `1970-01-01 00:00:00.000001`, casting `false` to timestamp fails with the following error:
> IllegalArgumentException: requirement failed: Literal must have a corresponding value to timestamp, but class Integer found.

SBT test also fails with this error:
```
[info]   java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
[info]   at scala.runtime.BoxesRunTime.unboxToLong(BoxesRunTime.java:107)
[info]   at org.apache.spark.sql.catalyst.InternalRow$.$anonfun$getWriter$5(InternalRow.scala:178)
[info]   at org.apache.spark.sql.catalyst.InternalRow$.$anonfun$getWriter$5$adapted(InternalRow.scala:178)
```

The issue was that we need to return `0L` instead of `0` when converting `false` to a long.

### Why are the changes needed?

Fixes a small bug in cast.

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

No.

### How was this patch tested?

I added a unit test to verify the fix.

Closes apache#39729 from sadikovi/fix_spark_boolean_to_timestamp.

Authored-by: Ivan Sadikov <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
  • Loading branch information
sadikovi authored and HyukjinKwon committed Jan 25, 2023
1 parent c43be4e commit 866343c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ case class Cast(
}
})
case BooleanType =>
buildCast[Boolean](_, b => if (b) 1L else 0)
buildCast[Boolean](_, b => if (b) 1L else 0L)
case LongType =>
buildCast[Long](_, l => longToTimestamp(l))
case IntegerType =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ class CastWithAnsiOffSuite extends CastSuiteBase {
checkEvaluation(cast(input, StringType), "1.23E-7")
}

test("SPARK-42176: cast boolean to timestamp") {
checkEvaluation(cast(true, TimestampType), 1L)
checkEvaluation(cast(false, TimestampType), 0L)
}

private def castOverflowErrMsg(targetType: DataType): String = {
s"""cannot be cast to "${targetType.sql}" due to an overflow."""
}
Expand Down

0 comments on commit 866343c

Please sign in to comment.