diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/xml/TypeCast.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/xml/TypeCast.scala index a00f372da7f60..b065dd41f28f8 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/xml/TypeCast.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/xml/TypeCast.scala @@ -155,6 +155,12 @@ private[sql] object TypeCast { } else { value } + // A little shortcut to avoid trying many formatters in the common case that + // the input isn't a double. All built-in formats will start with a digit or period. + if (signSafeValue.isEmpty || + !(Character.isDigit(signSafeValue.head) || signSafeValue.head == '.')) { + return false + } // Rule out strings ending in D or F, as they will parse as double but should be disallowed if (value.nonEmpty && (value.last match { case 'd' | 'D' | 'f' | 'F' => true @@ -171,6 +177,11 @@ private[sql] object TypeCast { } else { value } + // A little shortcut to avoid trying many formatters in the common case that + // the input isn't a number. All built-in formats will start with a digit. + if (signSafeValue.isEmpty || !Character.isDigit(signSafeValue.head)) { + return false + } (allCatch opt signSafeValue.toInt).isDefined } @@ -180,6 +191,11 @@ private[sql] object TypeCast { } else { value } + // A little shortcut to avoid trying many formatters in the common case that + // the input isn't a number. All built-in formats will start with a digit. + if (signSafeValue.isEmpty || !Character.isDigit(signSafeValue.head)) { + return false + } (allCatch opt signSafeValue.toLong).isDefined }