Skip to content

Commit 05bbfea

Browse files
committed
Infer date type before timestamp type
1 parent 9376832 commit 05bbfea

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/json/JsonInferSchema.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.spark.sql.catalyst.json
1919

20+
import java.text.ParsePosition
2021
import java.util.Comparator
2122

2223
import scala.util.control.Exception.allCatch
@@ -123,10 +124,18 @@ private[sql] class JsonInferSchema(options: JSONOptions) extends Serializable {
123124
decimalTry.getOrElse(StringType)
124125
case VALUE_STRING =>
125126
val stringValue = parser.getText
126-
if ((allCatch opt options.timestampFormat.parse(stringValue)).isDefined) {
127-
TimestampType
128-
} else if ((allCatch opt options.dateFormat.parse(stringValue)).isDefined) {
127+
val dateTry = allCatch opt {
128+
val pos = new ParsePosition(0)
129+
options.dateFormat.parse(stringValue, pos)
130+
if (pos.getErrorIndex != -1 || pos.getIndex != stringValue.length) {
131+
throw new IllegalArgumentException(
132+
s"$stringValue cannot be parsed as ${DateType.simpleString}")
133+
}
134+
}
135+
if (dateTry.isDefined) {
129136
DateType
137+
} else if ((allCatch opt options.timestampFormat.parse(stringValue)).isDefined) {
138+
TimestampType
130139
} else if ((allCatch opt DateTimeUtils.stringToTime(stringValue)).isDefined) {
131140
// We keep this for backwards compatibility.
132141
TimestampType

0 commit comments

Comments
 (0)