You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "jiayuasu (via GitHub)" <gi...@apache.org> on 2025/09/07 06:46:53 UTC
[PR] [GH-2337] Fix the DateTimeParseException in GeoPackage reader [sedona]
jiayuasu opened a new pull request, #2339:
URL: https://github.com/apache/sedona/pull/2339
## Did you read the Contributor Guide?
- Yes, I have read the [Contributor Rules](https://sedona.apache.org/latest/community/rule/) and [Contributor Development Guide](https://sedona.apache.org/latest/community/develop/)
## Is this PR related to a ticket?
- Yes, and the PR name follows the format `[GH-2337] my subject`. Closes #2337
## What changes were proposed in this PR?
Problem: DateTimeParseException: Text '2023-12-31T12:00:00.000' could not be parsed at index 23
Root Cause: Instant.parse() requires timezone information, but GeoPackage datetime fields often don't include it
Solution: Enhanced the epoch() method with fallback parsing that treats datetime strings without timezone as UTC
Modified:
* spark/common/src/main/scala/org/apache/sedona/sql/datasources/geopackage/transform/DataTypesTransformations.scala
* Enhanced: The epoch() method to handle multiple datetime formats
* Added: Support for various millisecond formats (1, 2, 3 digits, or none)
* Maintained: Full backward compatibility with timezone-aware formats
## How was this patch tested?
Added 1 more test case and a hand-made geopackage file that contains problematic datetime data
## Did this PR include necessary documentation updates?
- No, this PR does not affect any public API so no need to change the documentation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [GH-2337] Fix the DateTimeParseException in GeoPackage reader [sedona]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2339:
URL: https://github.com/apache/sedona/pull/2339#discussion_r2328553067
##########
spark/common/src/main/scala/org/apache/sedona/sql/datasources/geopackage/transform/DataTypesTransformations.scala:
##########
@@ -18,22 +18,50 @@
*/
package org.apache.sedona.sql.datasources.geopackage.transform
-import java.time.{Instant, LocalDate}
+import java.time.{Instant, LocalDate, LocalDateTime, ZoneOffset}
import java.time.format.DateTimeFormatter
+import java.time.format.DateTimeParseException
import java.time.temporal.ChronoUnit
object DataTypesTransformations {
- def getDays(dateString: String): Int = {
- val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
-
- val date = LocalDate.parse(dateString, formatter)
+ // Pre-created formatters to avoid repeated object creation
+ private val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
+ private val datetimeFormatters = Array(
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"), // 3 digits
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SS"), // 2 digits
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S"), // 1 digit
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss") // no milliseconds
+ )
+ def getDays(dateString: String): Int = {
+ val date = LocalDate.parse(dateString, dateFormatter)
val epochDate = LocalDate.of(1970, 1, 1)
-
ChronoUnit.DAYS.between(epochDate, date).toInt
}
def epoch(timestampStr: String): Long = {
- Instant.parse(timestampStr).toEpochMilli
+ try {
+ // Try parsing as-is first (works for timestamps with timezone info)
+ Instant.parse(timestampStr).toEpochMilli
+ } catch {
+ case _: DateTimeParseException =>
+ // If parsing fails, try treating it as UTC (common case for GeoPackage)
+ // Handle various datetime formats without timezone info
+ // Try different patterns to handle various millisecond formats
+ for (formatter <- datetimeFormatters) {
+ try {
+ val localDateTime = LocalDateTime.parse(timestampStr, formatter)
+ return localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli
+ } catch {
+ case _: DateTimeParseException =>
+ // Continue to next formatter
+ }
+ }
+
+ // If all formatters failed, throw a descriptive exception
+ throw new IllegalArgumentException(
+ s"Unable to parse datetime: $timestampStr. " +
+ s"Expected formats: 'yyyy-MM-ddTHH:mm:ss[.S]' or 'yyyy-MM-ddTHH:mm:ss[.S]Z'")
Review Comment:
[nitpick] Using early return in a for loop is not optimal for functional programming. Consider using `datetimeFormatters.view.map(...)` with `find` or `collectFirst` to make the code more functional and potentially more efficient.
```suggestion
datetimeFormatters.view
.map { formatter =>
try {
Some(LocalDateTime.parse(timestampStr, formatter).toInstant(ZoneOffset.UTC).toEpochMilli)
} catch {
case _: DateTimeParseException => None
}
}
.collectFirst { case Some(epochMilli) => epochMilli }
.getOrElse {
// If all formatters failed, throw a descriptive exception
throw new IllegalArgumentException(
s"Unable to parse datetime: $timestampStr. " +
s"Expected formats: 'yyyy-MM-ddTHH:mm:ss[.S]' or 'yyyy-MM-ddTHH:mm:ss[.S]Z'")
}
```
##########
spark/common/src/main/scala/org/apache/sedona/sql/datasources/geopackage/transform/DataTypesTransformations.scala:
##########
@@ -18,22 +18,50 @@
*/
package org.apache.sedona.sql.datasources.geopackage.transform
-import java.time.{Instant, LocalDate}
+import java.time.{Instant, LocalDate, LocalDateTime, ZoneOffset}
import java.time.format.DateTimeFormatter
+import java.time.format.DateTimeParseException
import java.time.temporal.ChronoUnit
object DataTypesTransformations {
- def getDays(dateString: String): Int = {
- val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
-
- val date = LocalDate.parse(dateString, formatter)
+ // Pre-created formatters to avoid repeated object creation
+ private val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
+ private val datetimeFormatters = Array(
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"), // 3 digits
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SS"), // 2 digits
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S"), // 1 digit
+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss") // no milliseconds
+ )
+ def getDays(dateString: String): Int = {
+ val date = LocalDate.parse(dateString, dateFormatter)
val epochDate = LocalDate.of(1970, 1, 1)
-
ChronoUnit.DAYS.between(epochDate, date).toInt
}
def epoch(timestampStr: String): Long = {
- Instant.parse(timestampStr).toEpochMilli
+ try {
+ // Try parsing as-is first (works for timestamps with timezone info)
+ Instant.parse(timestampStr).toEpochMilli
+ } catch {
+ case _: DateTimeParseException =>
+ // If parsing fails, try treating it as UTC (common case for GeoPackage)
+ // Handle various datetime formats without timezone info
+ // Try different patterns to handle various millisecond formats
+ for (formatter <- datetimeFormatters) {
+ try {
+ val localDateTime = LocalDateTime.parse(timestampStr, formatter)
+ return localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli
+ } catch {
+ case _: DateTimeParseException =>
+ // Continue to next formatter
+ }
+ }
+
+ // If all formatters failed, throw a descriptive exception
+ throw new IllegalArgumentException(
+ s"Unable to parse datetime: $timestampStr. " +
+ s"Expected formats: 'yyyy-MM-ddTHH:mm:ss[.S]' or 'yyyy-MM-ddTHH:mm:ss[.S]Z'")
Review Comment:
The error message format examples don't accurately reflect all supported patterns. It shows '[.S]' but the code supports .S, .SS, and .SSS formats. Consider updating to: 'yyyy-MM-ddTHH:mm:ss[.SSS|.SS|.S]' or 'yyyy-MM-ddTHH:mm:ss[.SSS|.SS|.S]Z'
```suggestion
s"Expected formats: 'yyyy-MM-ddTHH:mm:ss[.SSS|.SS|.S]' or 'yyyy-MM-ddTHH:mm:ss[.SSS|.SS|.S]Z'")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [GH-2337] Fix the DateTimeParseException in GeoPackage reader [sedona]
Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu merged PR #2339:
URL: https://github.com/apache/sedona/pull/2339
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [GH-2337] Fix the DateTimeParseException in GeoPackage reader [sedona]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2339:
URL: https://github.com/apache/sedona/pull/2339#discussion_r2328543804
##########
spark/common/src/main/scala/org/apache/sedona/sql/datasources/geopackage/transform/DataTypesTransformations.scala:
##########
@@ -34,6 +35,48 @@ object DataTypesTransformations {
}
def epoch(timestampStr: String): Long = {
- Instant.parse(timestampStr).toEpochMilli
+ try {
+ // Try parsing as-is first (works for timestamps with timezone info)
+ Instant.parse(timestampStr).toEpochMilli
+ } catch {
+ case _: DateTimeParseException =>
+ // If parsing fails, try treating it as UTC (common case for GeoPackage)
+ try {
+ // Handle various datetime formats without timezone info
+ // Try different patterns to handle various millisecond formats
+ val patterns = Array(
+ "yyyy-MM-dd'T'HH:mm:ss.SSS", // 3 digits
+ "yyyy-MM-dd'T'HH:mm:ss.SS", // 2 digits
+ "yyyy-MM-dd'T'HH:mm:ss.S", // 1 digit
+ "yyyy-MM-dd'T'HH:mm:ss" // no milliseconds
+ )
+
+ var localDateTime: LocalDateTime = null
+ var lastException: DateTimeParseException = null
+
+ for (pattern <- patterns) {
+ try {
+ val formatter = DateTimeFormatter.ofPattern(pattern)
+ localDateTime = LocalDateTime.parse(timestampStr, formatter)
+ lastException = null
+ } catch {
+ case e: DateTimeParseException =>
+ lastException = e
+ }
+ }
+
+ if (localDateTime != null) {
+ localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli
+ } else {
+ throw lastException
+ }
Review Comment:
The logic could be simplified by using early returns instead of null checks. Consider breaking out of the loop immediately when a successful parse occurs, eliminating the need for null checks.
```suggestion
var lastException: DateTimeParseException = null
for (pattern <- patterns) {
try {
val formatter = DateTimeFormatter.ofPattern(pattern)
val localDateTime = LocalDateTime.parse(timestampStr, formatter)
return localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli
} catch {
case e: DateTimeParseException =>
lastException = e
}
}
throw lastException
```
##########
spark/common/src/main/scala/org/apache/sedona/sql/datasources/geopackage/transform/DataTypesTransformations.scala:
##########
@@ -34,6 +35,48 @@ object DataTypesTransformations {
}
def epoch(timestampStr: String): Long = {
- Instant.parse(timestampStr).toEpochMilli
+ try {
+ // Try parsing as-is first (works for timestamps with timezone info)
+ Instant.parse(timestampStr).toEpochMilli
+ } catch {
+ case _: DateTimeParseException =>
+ // If parsing fails, try treating it as UTC (common case for GeoPackage)
+ try {
+ // Handle various datetime formats without timezone info
+ // Try different patterns to handle various millisecond formats
+ val patterns = Array(
+ "yyyy-MM-dd'T'HH:mm:ss.SSS", // 3 digits
+ "yyyy-MM-dd'T'HH:mm:ss.SS", // 2 digits
+ "yyyy-MM-dd'T'HH:mm:ss.S", // 1 digit
+ "yyyy-MM-dd'T'HH:mm:ss" // no milliseconds
+ )
+
+ var localDateTime: LocalDateTime = null
+ var lastException: DateTimeParseException = null
+
+ for (pattern <- patterns) {
+ try {
+ val formatter = DateTimeFormatter.ofPattern(pattern)
+ localDateTime = LocalDateTime.parse(timestampStr, formatter)
+ lastException = null
+ } catch {
+ case e: DateTimeParseException =>
+ lastException = e
+ }
+ }
Review Comment:
Creating a new `DateTimeFormatter` instance for each pattern in every iteration is inefficient. Consider pre-creating the formatters as static values or using a lazy initialization pattern to avoid repeated object creation.
##########
spark/common/src/main/scala/org/apache/sedona/sql/datasources/geopackage/transform/DataTypesTransformations.scala:
##########
@@ -34,6 +35,48 @@ object DataTypesTransformations {
}
def epoch(timestampStr: String): Long = {
- Instant.parse(timestampStr).toEpochMilli
+ try {
+ // Try parsing as-is first (works for timestamps with timezone info)
+ Instant.parse(timestampStr).toEpochMilli
+ } catch {
+ case _: DateTimeParseException =>
+ // If parsing fails, try treating it as UTC (common case for GeoPackage)
+ try {
+ // Handle various datetime formats without timezone info
+ // Try different patterns to handle various millisecond formats
+ val patterns = Array(
+ "yyyy-MM-dd'T'HH:mm:ss.SSS", // 3 digits
+ "yyyy-MM-dd'T'HH:mm:ss.SS", // 2 digits
+ "yyyy-MM-dd'T'HH:mm:ss.S", // 1 digit
+ "yyyy-MM-dd'T'HH:mm:ss" // no milliseconds
+ )
+
+ var localDateTime: LocalDateTime = null
+ var lastException: DateTimeParseException = null
+
+ for (pattern <- patterns) {
+ try {
+ val formatter = DateTimeFormatter.ofPattern(pattern)
+ localDateTime = LocalDateTime.parse(timestampStr, formatter)
+ lastException = null
+ } catch {
+ case e: DateTimeParseException =>
+ lastException = e
+ }
+ }
+
+ if (localDateTime != null) {
+ localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli
+ } else {
+ throw lastException
Review Comment:
Using `null` initialization is not idiomatic Scala. Consider using `Option[LocalDateTime]` and `Option[DateTimeParseException]` instead to follow functional programming best practices and avoid null pointer exceptions.
```suggestion
var localDateTimeOpt: Option[LocalDateTime] = None
var lastExceptionOpt: Option[DateTimeParseException] = None
for (pattern <- patterns) {
try {
val formatter = DateTimeFormatter.ofPattern(pattern)
localDateTimeOpt = Some(LocalDateTime.parse(timestampStr, formatter))
lastExceptionOpt = None
} catch {
case e: DateTimeParseException =>
lastExceptionOpt = Some(e)
}
}
localDateTimeOpt match {
case Some(localDateTime) =>
localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli
case None =>
throw lastExceptionOpt.getOrElse(new DateTimeParseException("Unknown parse error", timestampStr, 0))
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org