Skip to content

Commit 8e37ed6

Browse files
ueshinrxin
authored andcommitted
[SPARK-1608] [SQL] Fix Cast.nullable when cast from StringType to NumericType/TimestampType.
`Cast.nullable` should be `true` when cast from `StringType` to `NumericType` or `TimestampType`. Because if `StringType` expression has an illegal number string or illegal timestamp string, the casted value becomes `null`. Author: Takuya UESHIN <[email protected]> Closes mesos#532 from ueshin/issues/SPARK-1608 and squashes the following commits: 065d37c [Takuya UESHIN] Add tests to check nullabilities of cast expressions. f278ed7 [Takuya UESHIN] Revert test to keep it readable and concise. 9fc9380 [Takuya UESHIN] Fix Cast.nullable when cast from StringType to NumericType/TimestampType.
1 parent e6e44e4 commit 8e37ed6

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import org.apache.spark.sql.catalyst.types._
2424
/** Cast the child expression to the target data type. */
2525
case class Cast(child: Expression, dataType: DataType) extends UnaryExpression {
2626
override def foldable = child.foldable
27-
def nullable = child.nullable
27+
def nullable = (child.dataType, dataType) match {
28+
case (StringType, _: NumericType) => true
29+
case (StringType, TimestampType) => true
30+
case _ => child.nullable
31+
}
2832
override def toString = s"CAST($child, $dataType)"
2933

3034
type EvaluatedType = Any

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ class ExpressionEvaluationSuite extends FunSuite {
245245
checkEvaluation(Literal(23.toShort) + Cast(true, ShortType), 24)
246246

247247
intercept[Exception] {evaluate(Literal(1) cast BinaryType, null)}
248+
249+
assert(("abcdef" cast StringType).nullable === false)
250+
assert(("abcdef" cast BinaryType).nullable === false)
251+
assert(("abcdef" cast BooleanType).nullable === false)
252+
assert(("abcdef" cast TimestampType).nullable === true)
253+
assert(("abcdef" cast LongType).nullable === true)
254+
assert(("abcdef" cast IntegerType).nullable === true)
255+
assert(("abcdef" cast ShortType).nullable === true)
256+
assert(("abcdef" cast ByteType).nullable === true)
257+
assert(("abcdef" cast DecimalType).nullable === true)
258+
assert(("abcdef" cast DoubleType).nullable === true)
259+
assert(("abcdef" cast FloatType).nullable === true)
248260
}
249261

250262
test("timestamp") {

0 commit comments

Comments
 (0)