-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-28023][SQL] Trim the string when cast string type to Boolean/Numeric types #24872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -433,7 +433,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| private[this] def castToLong(from: DataType): Any => Any = from match { | ||
| case StringType => | ||
| val result = new LongWrapper() | ||
| buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null) | ||
| buildCast[UTF8String](_, s => if (s.trim.toLong(result)) result.value else null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be a correct fix. Do we have a possibility of performance regression? |
||
| case BooleanType => | ||
| buildCast[Boolean](_, b => if (b) 1L else 0L) | ||
| case DateType => | ||
|
|
@@ -448,7 +448,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| private[this] def castToInt(from: DataType): Any => Any = from match { | ||
| case StringType => | ||
| val result = new IntWrapper() | ||
| buildCast[UTF8String](_, s => if (s.toInt(result)) result.value else null) | ||
| buildCast[UTF8String](_, s => if (s.trim.toInt(result)) result.value else null) | ||
| case BooleanType => | ||
| buildCast[Boolean](_, b => if (b) 1 else 0) | ||
| case DateType => | ||
|
|
@@ -463,7 +463,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| private[this] def castToShort(from: DataType): Any => Any = from match { | ||
| case StringType => | ||
| val result = new IntWrapper() | ||
| buildCast[UTF8String](_, s => if (s.toShort(result)) { | ||
| buildCast[UTF8String](_, s => if (s.trim.toShort(result)) { | ||
| result.value.toShort | ||
| } else { | ||
| null | ||
|
|
@@ -482,7 +482,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| private[this] def castToByte(from: DataType): Any => Any = from match { | ||
| case StringType => | ||
| val result = new IntWrapper() | ||
| buildCast[UTF8String](_, s => if (s.toByte(result)) { | ||
| buildCast[UTF8String](_, s => if (s.trim.toByte(result)) { | ||
| result.value.toByte | ||
| } else { | ||
| null | ||
|
|
@@ -518,7 +518,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| private[this] def castToDecimal(from: DataType, target: DecimalType): Any => Any = from match { | ||
| case StringType => | ||
| buildCast[UTF8String](_, s => try { | ||
| changePrecision(Decimal(new JavaBigDecimal(s.toString)), target) | ||
| changePrecision(Decimal(new JavaBigDecimal(s.toString.trim)), target) | ||
| } catch { | ||
| case _: NumberFormatException => null | ||
| }) | ||
|
|
@@ -544,7 +544,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| // DoubleConverter | ||
| private[this] def castToDouble(from: DataType): Any => Any = from match { | ||
| case StringType => | ||
| buildCast[UTF8String](_, s => try s.toString.toDouble catch { | ||
| buildCast[UTF8String](_, s => try s.toString.trim.toDouble catch { | ||
| case _: NumberFormatException => null | ||
| }) | ||
| case BooleanType => | ||
|
|
@@ -560,7 +560,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| // FloatConverter | ||
| private[this] def castToFloat(from: DataType): Any => Any = from match { | ||
| case StringType => | ||
| buildCast[UTF8String](_, s => try s.toString.toFloat catch { | ||
| buildCast[UTF8String](_, s => try s.toString.trim.toFloat catch { | ||
| case _: NumberFormatException => null | ||
| }) | ||
| case BooleanType => | ||
|
|
@@ -983,7 +983,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| (c, evPrim, evNull) => | ||
| code""" | ||
| try { | ||
| Decimal $tmp = Decimal.apply(new java.math.BigDecimal($c.toString())); | ||
| Decimal $tmp = Decimal.apply(new java.math.BigDecimal($c.toString().trim())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast)} | ||
| } catch (java.lang.NumberFormatException e) { | ||
| $evNull = true; | ||
|
|
@@ -1136,7 +1136,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| (c, evPrim, evNull) => | ||
| code""" | ||
| UTF8String.IntWrapper $wrapper = new UTF8String.IntWrapper(); | ||
| if ($c.toByte($wrapper)) { | ||
| if ($c.trim().toByte($wrapper)) { | ||
| $evPrim = (byte) $wrapper.value; | ||
| } else { | ||
| $evNull = true; | ||
|
|
@@ -1163,7 +1163,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| (c, evPrim, evNull) => | ||
| code""" | ||
| UTF8String.IntWrapper $wrapper = new UTF8String.IntWrapper(); | ||
| if ($c.toShort($wrapper)) { | ||
| if ($c.trim().toShort($wrapper)) { | ||
| $evPrim = (short) $wrapper.value; | ||
| } else { | ||
| $evNull = true; | ||
|
|
@@ -1188,7 +1188,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| (c, evPrim, evNull) => | ||
| code""" | ||
| UTF8String.IntWrapper $wrapper = new UTF8String.IntWrapper(); | ||
| if ($c.toInt($wrapper)) { | ||
| if ($c.trim().toInt($wrapper)) { | ||
| $evPrim = $wrapper.value; | ||
| } else { | ||
| $evNull = true; | ||
|
|
@@ -1214,7 +1214,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| (c, evPrim, evNull) => | ||
| code""" | ||
| UTF8String.LongWrapper $wrapper = new UTF8String.LongWrapper(); | ||
| if ($c.toLong($wrapper)) { | ||
| if ($c.trim().toLong($wrapper)) { | ||
| $evPrim = $wrapper.value; | ||
| } else { | ||
| $evNull = true; | ||
|
|
@@ -1238,7 +1238,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| (c, evPrim, evNull) => | ||
| code""" | ||
| try { | ||
| $evPrim = Float.valueOf($c.toString()); | ||
| $evPrim = Float.valueOf($c.toString().trim()); | ||
| } catch (java.lang.NumberFormatException e) { | ||
| $evNull = true; | ||
| } | ||
|
|
@@ -1260,7 +1260,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| (c, evPrim, evNull) => | ||
| code""" | ||
| try { | ||
| $evPrim = Double.valueOf($c.toString()); | ||
| $evPrim = Double.valueOf($c.toString().trim()); | ||
| } catch (java.lang.NumberFormatException e) { | ||
| $evNull = true; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
Since Spark 3.0, when a string is cast to boolean/date/timestamp/numeric types, it is trimmed before it is parsed.