Skip to content
Snippets Groups Projects
Commit 7ba8bf28 authored by liuxian's avatar liuxian Committed by Xiao Li
Browse files

[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.
parent bcf3643f
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@ private object ConfigHelpers {
def toNumber[T](s: String, converter: String => T, key: String, configType: String): T = {
try {
converter(s)
converter(s.trim)
} catch {
case _: NumberFormatException =>
throw new IllegalArgumentException(s"$key should be $configType, but was $s")
......@@ -37,7 +37,7 @@ private object ConfigHelpers {
def toBoolean(s: String, key: String): Boolean = {
try {
s.toBoolean
s.trim.toBoolean
} catch {
case _: IllegalArgumentException =>
throw new IllegalArgumentException(s"$key should be boolean, but was $s")
......
......@@ -37,6 +37,9 @@ class SQLConfEntrySuite extends SparkFunSuite {
assert(conf.getConfString(key) === "20")
assert(conf.getConf(confEntry, 5) === 20)
conf.setConfString(key, " 20")
assert(conf.getConf(confEntry, 5) === 20)
val e = intercept[IllegalArgumentException] {
conf.setConfString(key, "abc")
}
......@@ -75,6 +78,8 @@ class SQLConfEntrySuite extends SparkFunSuite {
assert(conf.getConfString(key) === "true")
assert(conf.getConf(confEntry, false) === true)
conf.setConfString(key, " true ")
assert(conf.getConf(confEntry, false) === true)
val e = intercept[IllegalArgumentException] {
conf.setConfString(key, "abc")
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment