Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,36 @@ object Literal {
val dataType = DataType.parseDataType(json \ "dataType")
json \ "value" match {
case JNull => Literal.create(null, dataType)
case JString(str) =>
val value = dataType match {
case BooleanType => str.toBoolean
case ByteType => str.toByte
case ShortType => str.toShort
case IntegerType => str.toInt
case LongType => str.toLong
case FloatType => str.toFloat
case DoubleType => str.toDouble
case StringType => UTF8String.fromString(str)
case DateType => java.sql.Date.valueOf(str)
case TimestampType => java.sql.Timestamp.valueOf(str)
case CalendarIntervalType => CalendarInterval.fromString(str)
case t: DecimalType =>
val d = Decimal(str)
assert(d.changePrecision(t.precision, t.scale))
d
case _ => null
}
Literal.create(value, dataType)
case JString(str) => fromString(str, dataType)
case other => sys.error(s"$other is not a valid Literal json value")
}
}

/**
* Constructs a Literal from a String
*/
def fromString(str: String, dataType: DataType): Literal = {
val value = dataType match {
case BooleanType => str.toBoolean
case ByteType => str.toByte
case ShortType => str.toShort
case IntegerType => str.toInt
case LongType => str.toLong
case FloatType => str.toFloat
case DoubleType => str.toDouble
case StringType => UTF8String.fromString(str)
case DateType => java.sql.Date.valueOf(str)
case TimestampType => java.sql.Timestamp.valueOf(str)
case CalendarIntervalType => CalendarInterval.fromString(str)
case t: DecimalType =>
val d = Decimal(str)
assert(d.changePrecision(t.precision, t.scale))
d
case _ => null
}
Literal.create(value, dataType)
}

def create(v: Any, dataType: DataType): Literal = {
Literal(CatalystTypeConverters.convertToCatalyst(v), dataType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,25 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Literal('\u0000'), "\u0000")
checkEvaluation(Literal.create('\n'), "\n")
}

test("fromString converts String/DataType input correctly") {
checkEvaluation(Literal.fromString(false.toString, BooleanType), false)
checkEvaluation(Literal.fromString(null, NullType), null)
checkEvaluation(Literal.fromString(Int.MaxValue.toByte.toString, ByteType), Int.MaxValue.toByte)
checkEvaluation(Literal.fromString(Short.MaxValue.toShort.toString, ShortType), Short.MaxValue
.toShort)
checkEvaluation(Literal.fromString(Int.MaxValue.toString, IntegerType), Int.MaxValue)
checkEvaluation(Literal.fromString(Long.MaxValue.toString, LongType), Long.MaxValue)
checkEvaluation(Literal.fromString(Float.MaxValue.toString, FloatType), Float.MaxValue)
checkEvaluation(Literal.fromString(Double.MaxValue.toString, DoubleType), Double.MaxValue)
checkEvaluation(Literal.fromString("1.23456", DecimalType(10, 5)), Decimal(1.23456))
checkEvaluation(Literal.fromString("Databricks", StringType), "Databricks")
val dateString = "1970-01-01"
checkEvaluation(Literal.fromString(dateString, DateType), java.sql.Date.valueOf(dateString))
val timestampString = "0000-01-01 00:00:00"
checkEvaluation(Literal.fromString(timestampString, TimestampType),
java.sql.Timestamp.valueOf(timestampString))
val calInterval = new CalendarInterval(1, 1)
checkEvaluation(Literal.fromString(calInterval.toString, CalendarIntervalType), calInterval)
}
}