spark-test commented on code in PR #59975:
URL: https://github.com/apache/spark/pull/59975#discussion_r4577099149


##########
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:
   **Nit (P3):** 'plain' infers StringType whatever the formatter, so this case 
can only catch a construction error escaping inference. The JSON (`{"a": 
"plain"}`) and XML (`inferFrom("plain", NullType)`) inference suites have the 
same gap, and the end-to-end cases probe only 'plain' and 'text'. None of them 
would fail if an unusable pattern fell back to the default formatter, which 
infers a time-shaped value such as '12:13:14' as TIME (line 284). The base 
branch never infers TIME for an unusable pattern, and with TIME disabled 
'12:13:14' infers TimestampType here (line 308). Adding '12:13:14' to the 
unusable-format case in each of the three inference suites, and asserting that 
it gets the same type as with TIME disabled, would make them check that 
inference skips TIME, not only that it finishes.
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/59975#discussion_r4466697321).
   
   **Verification:**
   - **Regression:** With TIME enabled and timeFormat 'EEE HH:mm', a 
time-shaped value such as '12:13:14' that the default formatter infers as TIME 
is inferred by CSV, JSON (with inferTimestamp) and XML inference as the same 
type it gets with TIME disabled, so an implementation that fell back to the 
default formatter for an unusable pattern fails the cases.



##########
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:
   **Nit (P3):** This wording follows the earlier review's suggestion, which 
missed a case in the default formatter. With no `timeFormat`, 
`DefaultTimeFormatter.parseOptional` calls `stringToTime`, whose 
`parseTimestampString` treats any token after an `HH:mm:ss` prefix as a zone ID 
and resolves it with `getZoneId` (SparkDateTimeUtils.scala:818 and 878). For a 
value such as '10:00:00 abc', that code has `ZoneId.of` reject 'abc', 
`getZoneId` rethrow it as a `SparkDateTimeException`, and `stringToTime` catch 
that and return None. So a value that does not match the time format and has no 
out-of-range field still pays two exceptions, for example on every row of a 
column of log lines that start with a clock time. Results are unaffected, and 
the timestamp probe's `stringToTimestamp` shares the same zone lookup, so only 
the comment needs to change, for example:
   
   ```suggestion
     // non-time value. Subclasses override it to reject most input that does 
not match the time
     // format without throwing; some malformed input, such as out-of-range 
fields or a trailing token
     // that is not a valid zone ID, may still raise an exception that is 
caught internally.
   ```
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/59975#discussion_r4466697292).
   
   **Verification:**
   - **Inspection:** The comment on `TimeFormatter.parseOptional` describes the 
inputs that may still raise an exception internally in terms that cover both 
out-of-range fields and a trailing token after an `HH:mm:ss` prefix that is not 
a valid zone ID, and does not claim that all other non-matching input is 
rejected without an exception.



##########
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:
   **Nit (P3):** Both iterations call `readData`, which wraps the string in a 
`Dataset[String]` and calls `spark.read.options(...).xml(ds)` (lines 90-100). 
That reader always infers through `TextInputXmlDataSource.inferFromDataset`, 
and only `XmlDataSource.apply` reads `multiLine`, when it picks a file source 
(XmlDataSource.scala:149), so the `multiLine = true` iteration repeats the 
first one. A multi-line XML file read, the default for XML files, goes through 
`MultiLineXmlDataSource` and `inferFromReaders`, whose handler rethrows a 
`RuntimeException` when `ignoreCorruptFiles` is off 
(XmlInferSchema.scala:329-332). That is where an escaping formatter error would 
fail the query (the Dataset path would produce a corrupt-record column 
instead), and it stays untested. Writing the record to a temporary file and 
reading it with `.xml(path)` in that iteration, as the JSON test does, would 
reach it.
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/59975#discussion_r4466697284).
   
   **Verification:**
   - **Regression:** With TIME enabled and an unusable timeFormat such as 'EEE 
HH:mm', schema inference over a multi-line XML file read completes with a 
string column, no corrupt-record column and the expected rows, and the case 
fails if a formatter construction error escapes the TIME probe on that path, 
where it would fail the query.



-- 
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