Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
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
30 changes: 0 additions & 30 deletions src/main/scala/com/databricks/spark/xml/util/TypeCast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,14 @@ object TypeCast {
.getOrElse(NumberFormat.getInstance(Locale.getDefault).parse(datum).doubleValue())
case _: BooleanType => datum.toBoolean
case _: DecimalType => new BigDecimal(datum.replaceAll(",", ""))
// TODO(hossein): would be good to support other common timestamp formats
case _: TimestampType => Timestamp.valueOf(datum)
// TODO(hossein): would be good to support other common date formats
case _: DateType => Date.valueOf(datum)
case _: StringType => datum
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File in #165

case _ => throw new RuntimeException(s"Unsupported type: ${castType.typeName}")
}
}
}

/**
* Helper method that converts string representation of a character to actual character.
* It handles some Java escaped strings and throws exception if given string is longer than one
* character.
*
*/
@throws[IllegalArgumentException]
private[xml] def toChar(str: String): Char = {
if (str.charAt(0) == '\\') {
str.charAt(1)
match {
case 't' => '\t'
case 'r' => '\r'
case 'b' => '\b'
case 'f' => '\f'
case '\"' => '\"' // In case user changes quote char and uses \" as delimiter in options
case '\'' => '\''
case 'u' if str == """\u0000""" => '\u0000'
case _ =>
throw new IllegalArgumentException(s"Unsupported special character for delimiter: $str")
}
} else if (str.length == 1) {
str.charAt(0)
} else {
throw new IllegalArgumentException(s"Delimiter cannot be more than one character: $str")
}
}

/**
* Helper method that checks and cast string representation of a numeric types.
*/
Expand Down
24 changes: 0 additions & 24 deletions src/test/scala/com/databricks/spark/xml/util/TypeCastSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@ class TypeCastSuite extends FunSuite {
}
}

test("Can parse escaped characters") {
assert(TypeCast.toChar("""\t""") === '\t')
assert(TypeCast.toChar("""\r""") === '\r')
assert(TypeCast.toChar("""\b""") === '\b')
assert(TypeCast.toChar("""\f""") === '\f')
assert(TypeCast.toChar("""\"""") === '\"')
assert(TypeCast.toChar("""\'""") === '\'')
assert(TypeCast.toChar("""\u0000""") === '\u0000')
}

test("Does not accept delimiter larger than one character") {
val exception = intercept[IllegalArgumentException]{
TypeCast.toChar("ab")
}
assert(exception.getMessage.contains("cannot be more than one character"))
}

test("Throws exception for unsupported escaped characters") {
val exception = intercept[IllegalArgumentException]{
TypeCast.toChar("""\1""")
}
assert(exception.getMessage.contains("Unsupported special character for delimiter"))
}

test("Nullable types are handled") {
assert(TypeCast.castTo("", IntegerType, nullable = true) == null)
}
Expand Down