Skip to content

Commit f96a8bf

Browse files
committed
[SPARK-12321][SQL][FOLLOW-UP] Add tests for fromString
## What changes were proposed in this pull request? Add test cases for fromString ## How was this patch tested? N/A Closes #22345 from gatorsmile/addTest. Authored-by: Xiao Li <[email protected]> Signed-off-by: gatorsmile <[email protected]>
1 parent 6d7bc5a commit f96a8bf

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,30 +128,36 @@ object Literal {
128128
val dataType = DataType.parseDataType(json \ "dataType")
129129
json \ "value" match {
130130
case JNull => Literal.create(null, dataType)
131-
case JString(str) =>
132-
val value = dataType match {
133-
case BooleanType => str.toBoolean
134-
case ByteType => str.toByte
135-
case ShortType => str.toShort
136-
case IntegerType => str.toInt
137-
case LongType => str.toLong
138-
case FloatType => str.toFloat
139-
case DoubleType => str.toDouble
140-
case StringType => UTF8String.fromString(str)
141-
case DateType => java.sql.Date.valueOf(str)
142-
case TimestampType => java.sql.Timestamp.valueOf(str)
143-
case CalendarIntervalType => CalendarInterval.fromString(str)
144-
case t: DecimalType =>
145-
val d = Decimal(str)
146-
assert(d.changePrecision(t.precision, t.scale))
147-
d
148-
case _ => null
149-
}
150-
Literal.create(value, dataType)
131+
case JString(str) => fromString(str, dataType)
151132
case other => sys.error(s"$other is not a valid Literal json value")
152133
}
153134
}
154135

136+
/**
137+
* Constructs a Literal from a String
138+
*/
139+
def fromString(str: String, dataType: DataType): Literal = {
140+
val value = dataType match {
141+
case BooleanType => str.toBoolean
142+
case ByteType => str.toByte
143+
case ShortType => str.toShort
144+
case IntegerType => str.toInt
145+
case LongType => str.toLong
146+
case FloatType => str.toFloat
147+
case DoubleType => str.toDouble
148+
case StringType => UTF8String.fromString(str)
149+
case DateType => java.sql.Date.valueOf(str)
150+
case TimestampType => java.sql.Timestamp.valueOf(str)
151+
case CalendarIntervalType => CalendarInterval.fromString(str)
152+
case t: DecimalType =>
153+
val d = Decimal(str)
154+
assert(d.changePrecision(t.precision, t.scale))
155+
d
156+
case _ => null
157+
}
158+
Literal.create(value, dataType)
159+
}
160+
155161
def create(v: Any, dataType: DataType): Literal = {
156162
Literal(CatalystTypeConverters.convertToCatalyst(v), dataType)
157163
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,25 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
226226
checkEvaluation(Literal('\u0000'), "\u0000")
227227
checkEvaluation(Literal.create('\n'), "\n")
228228
}
229+
230+
test("fromString converts String/DataType input correctly") {
231+
checkEvaluation(Literal.fromString(false.toString, BooleanType), false)
232+
checkEvaluation(Literal.fromString(null, NullType), null)
233+
checkEvaluation(Literal.fromString(Int.MaxValue.toByte.toString, ByteType), Int.MaxValue.toByte)
234+
checkEvaluation(Literal.fromString(Short.MaxValue.toShort.toString, ShortType), Short.MaxValue
235+
.toShort)
236+
checkEvaluation(Literal.fromString(Int.MaxValue.toString, IntegerType), Int.MaxValue)
237+
checkEvaluation(Literal.fromString(Long.MaxValue.toString, LongType), Long.MaxValue)
238+
checkEvaluation(Literal.fromString(Float.MaxValue.toString, FloatType), Float.MaxValue)
239+
checkEvaluation(Literal.fromString(Double.MaxValue.toString, DoubleType), Double.MaxValue)
240+
checkEvaluation(Literal.fromString("1.23456", DecimalType(10, 5)), Decimal(1.23456))
241+
checkEvaluation(Literal.fromString("Databricks", StringType), "Databricks")
242+
val dateString = "1970-01-01"
243+
checkEvaluation(Literal.fromString(dateString, DateType), java.sql.Date.valueOf(dateString))
244+
val timestampString = "0000-01-01 00:00:00"
245+
checkEvaluation(Literal.fromString(timestampString, TimestampType),
246+
java.sql.Timestamp.valueOf(timestampString))
247+
val calInterval = new CalendarInterval(1, 1)
248+
checkEvaluation(Literal.fromString(calInterval.toString, CalendarIntervalType), calInterval)
249+
}
229250
}

0 commit comments

Comments
 (0)