holdenk commented on code in PR #59975:
URL: https://github.com/apache/spark/pull/59975#discussion_r4580861921


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/csv/CSVInferSchemaSuite.scala:
##########
@@ -324,4 +324,22 @@ class CSVInferSchemaSuite extends SparkFunSuite with 
SQLHelper {
       assert(inferSchema.inferField(timeType, "2023-01-01") == StringType)
     }
   }
+
+  test("SPARK-59546: TimeType inference handles custom and unusable formats") {
+    Seq("true", "false").foreach { enabled =>
+      withSQLConf(SQLConf.TIME_TYPE_ENABLED.key -> enabled) {
+        val options = new CSVOptions(Map("timeFormat" -> "EEE HH:mm"),
+          columnPruning = false, defaultTimeZoneId = "UTC")
+        assert(new CSVInferSchema(options).inferField(NullType, "plain") == 
StringType)

Review Comment:
   Fixed in e51e0fa176d. Added '12:13:14' to the unusable-timeFormat case in 
CSVInferSchemaSuite, JsonInferSchemaSuite and XmlInferSchemaTypeCastingSuite, 
asserting TimestampType. The case already loops over TIME enabled and disabled, 
so it now pins the same type with TIME on and off.
   
   Checked that it bites: with the three inference classes changed to fall back 
to the default formatter for an unusable pattern, all three new assertions fail 
(`TimeType(6) did not equal TimestampType`), while the old 'plain' assertions 
pass.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/xml/XmlSuite.scala:
##########
@@ -3678,6 +3678,19 @@ class XmlSuite
     }
   }
 
+  test("SPARK-59546: unusable timeFormat does not break XML inference") {
+    withSQLConf(SQLConf.TIME_TYPE_ENABLED.key -> "true") {
+      for (multiLine <- Seq(false, true)) {
+        val xml = "<ROW><time>plain</time></ROW>"
+        val df = readData(xml, None,

Review Comment:
   Fixed in e51e0fa176d. The test now writes the record to a temp file and 
reads it with `.xml(path)` (multiLine defaults to true there, so it goes 
through MultiLineXmlDataSource and `inferFromReaders`), alongside the 
Dataset[String] read.
   
   Checked that it bites: with the construction guard removed from 
XmlInferSchema, the case now fails with INVALID_DATETIME_PATTERN thrown from 
`inferFromReaders`. The old version read only Dataset[String] input twice, so 
it never reached that path.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->



##########
sql/api/src/main/scala/org/apache/spark/sql/catalyst/util/TimeFormatter.scala:
##########
@@ -17,16 +17,30 @@
 
 package org.apache.spark.sql.catalyst.util
 
+import java.text.ParsePosition
 import java.time.LocalTime
 import java.time.format.DateTimeFormatter
 import java.util.Locale
 
+import scala.util.control.NonFatal
+
 import org.apache.spark.sql.catalyst.util.SparkDateTimeUtils._
 import org.apache.spark.unsafe.types.UTF8String
 
 sealed trait TimeFormatter extends Serializable {
   def parse(s: String): Long // returns nanoseconds since midnight
 
+  // A non-throwing variant of `parse`, returning None instead of throwing 
when the input cannot be
+  // parsed. Used by schema inference to probe the time type without paying an 
exception per
+  // non-time value. Subclasses override it to return None without throwing 
for input that does
+  // not match the time format; out-of-range fields may still raise an 
exception caught internally.

Review Comment:
   Agreed on the substance, and I checked it: for '10:00:00 abc', 
`parseTimestampString` hands 'abc' to `getZoneId`, which throws via 
`zoneOffsetError`, and `stringToTime`'s NonFatal catch turns that into None. So 
the comment overstates it.
   
   It is not specific to this backport, though. 
`DefaultTimeFormatter.parseOptional` is identical on master (9eae2a8e99b), and 
master's comment is stronger still ("more efficient, exception-free 
implementation"). A backport should stay the same change as #58834, so this 
should be fixed on master first and then backported. Leaving this thread open; 
master follow-up is to reword the `parseOptional` comment along the suggested 
lines.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to