Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added timeZone option #616

Closed
wants to merge 1 commit into from
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
2 changes: 2 additions & 0 deletions src/main/scala/com/databricks/spark/xml/XmlOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private[xml] class XmlOptions(
parameters.getOrElse("wildcardColName", XmlOptions.DEFAULT_WILDCARD_COL_NAME)
val ignoreNamespace = parameters.get("ignoreNamespace").map(_.toBoolean).getOrElse(false)
val timestampFormat = parameters.get("timestampFormat")
val timeZone = parameters.getOrElse("timeZone", XmlOptions.DEFAULT_TIME_ZONE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of exposing an option, use the value of Spark conf spark.sql.session.timeZone below. We would need to document this behavior in the README

val dateFormat = parameters.get("dateFormat")
}

Expand All @@ -77,6 +78,7 @@ private[xml] object XmlOptions {
val DEFAULT_CHARSET: String = StandardCharsets.UTF_8.name
val DEFAULT_NULL_VALUE: String = null
val DEFAULT_WILDCARD_COL_NAME = "xs_any"
val DEFAULT_TIME_ZONE = "UTC"

def apply(parameters: Map[String, String]): XmlOptions = new XmlOptions(parameters)
}
5 changes: 4 additions & 1 deletion src/main/scala/com/databricks/spark/xml/util/TypeCast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ private[xml] object TypeCast {
)

private def parseXmlTimestamp(value: String, options: XmlOptions): Timestamp = {
val timeZone = options.timeZone
val formatters = options.timestampFormat.map(DateTimeFormatter.ofPattern).
map(supportedXmlTimestampFormatters :+ _).getOrElse(supportedXmlTimestampFormatters)
formatters.foreach { format =>
try {
return Timestamp.from(ZonedDateTime.parse(value, format).toInstant)
return Timestamp.from(
ZonedDateTime.parse(value, format.withZone(ZoneId.of(timeZone))).toInstant
Copy link
Collaborator

Choose a reason for hiding this comment

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

The new logic should only be applied to the custom timestamp format, not the built-in ones.
I suppose, if possible, you check to see if the pattern has a TZ and augment it with the TZ specified in spark.sql.session.timeZone if not.

)
} catch {
case _: Exception => // continue
}
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/time.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<author>John Smith</author>
<time>2011-12-03T10:15:30Z</time>
<time2>12-03-2011 10:15:30 PST</time2>
<time3>2011/12/03 06:15:30</time3>
</book>
30 changes: 28 additions & 2 deletions src/test/scala/com/databricks/spark/xml/XmlSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,12 @@ final class XmlSuite extends AnyFunSuite with BeforeAndAfterAll {
.option("rowTag", "book")
.xml(resDir + "time.xml")
val expectedSchema =
buildSchema(field("author"), field("time", TimestampType), field("time2", StringType))
buildSchema(
field("author"),
field("time", TimestampType),
field("time2", StringType),
field("time3", StringType)
)
assert(df.schema === expectedSchema)
assert(df.collect().head.getAs[Timestamp](1).getTime === 1322907330000L)
}
Expand All @@ -1379,11 +1384,32 @@ final class XmlSuite extends AnyFunSuite with BeforeAndAfterAll {
.option("timestampFormat", "MM-dd-yyyy HH:mm:ss z")
.xml(resDir + "time.xml")
val expectedSchema =
buildSchema(field("author"), field("time", TimestampType), field("time2", TimestampType))
buildSchema(
field("author"),
field("time", TimestampType),
field("time2", TimestampType),
field("time3", StringType)
)
assert(df.schema === expectedSchema)
assert(df.collect().head.getAs[Timestamp](2).getTime === 1322936130000L)
}

test("Test custom timestampFormat") {
val df = spark.read
.option("rowTag", "book")
.option("timestampFormat", "yyyy/MM/dd HH:mm:ss")
.xml(resDir + "time.xml")
val expectedSchema =
buildSchema(
field("author"),
field("time", TimestampType),
field("time2", StringType),
field("time3", TimestampType)
)
assert(df.schema === expectedSchema)
assert(df.collect().head.getAs[Timestamp](3).getTime === 1322892930000L)
}

test("Test null number type is null not 0.0") {
val schema = buildSchema(
struct("Header",
Expand Down