Skip to content

Commit 7ba8bf2

Browse files
10110346gatorsmile
authored andcommitted
[SPARK-21016][CORE] Improve code fault tolerance for converting string to number
## What changes were proposed in this pull request? When converting `string` to `number`(int, long or double), if the string has a space before or after,will lead to unnecessary mistakes. ## How was this patch tested? unit test Author: liuxian <liu.xian3@zte.com.cn> Closes #18238 from 10110346/lx-wip-0608.
1 parent bcf3643 commit 7ba8bf2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

core/src/main/scala/org/apache/spark/internal/config/ConfigBuilder.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private object ConfigHelpers {
2828

2929
def toNumber[T](s: String, converter: String => T, key: String, configType: String): T = {
3030
try {
31-
converter(s)
31+
converter(s.trim)
3232
} catch {
3333
case _: NumberFormatException =>
3434
throw new IllegalArgumentException(s"$key should be $configType, but was $s")
@@ -37,7 +37,7 @@ private object ConfigHelpers {
3737

3838
def toBoolean(s: String, key: String): Boolean = {
3939
try {
40-
s.toBoolean
40+
s.trim.toBoolean
4141
} catch {
4242
case _: IllegalArgumentException =>
4343
throw new IllegalArgumentException(s"$key should be boolean, but was $s")

sql/core/src/test/scala/org/apache/spark/sql/internal/SQLConfEntrySuite.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class SQLConfEntrySuite extends SparkFunSuite {
3737
assert(conf.getConfString(key) === "20")
3838
assert(conf.getConf(confEntry, 5) === 20)
3939

40+
conf.setConfString(key, " 20")
41+
assert(conf.getConf(confEntry, 5) === 20)
42+
4043
val e = intercept[IllegalArgumentException] {
4144
conf.setConfString(key, "abc")
4245
}
@@ -75,6 +78,8 @@ class SQLConfEntrySuite extends SparkFunSuite {
7578
assert(conf.getConfString(key) === "true")
7679
assert(conf.getConf(confEntry, false) === true)
7780

81+
conf.setConfString(key, " true ")
82+
assert(conf.getConf(confEntry, false) === true)
7883
val e = intercept[IllegalArgumentException] {
7984
conf.setConfString(key, "abc")
8085
}

0 commit comments

Comments
 (0)