Skip to content

Commit 2be4482

Browse files
skambhacloud-fan
authored andcommitted
[SPARK-22452][SQL] Add getInt, getLong, getBoolean to DataSourceV2Options
- Implemented methods getInt, getLong, getBoolean for DataSourceV2Options - Added new unit tests to exercise these methods Author: Sunitha Kambhampati <[email protected]> Closes #19902 from skambha/spark22452.
1 parent ea2fbf4 commit 2be4482

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

sql/core/src/main/java/org/apache/spark/sql/sources/v2/DataSourceV2Options.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,35 @@ public DataSourceV2Options(Map<String, String> originalMap) {
4949
public Optional<String> get(String key) {
5050
return Optional.ofNullable(keyLowerCasedMap.get(toLowerCase(key)));
5151
}
52+
53+
/**
54+
* Returns the boolean value to which the specified key is mapped,
55+
* or defaultValue if there is no mapping for the key. The key match is case-insensitive
56+
*/
57+
public boolean getBoolean(String key, boolean defaultValue) {
58+
String lcaseKey = toLowerCase(key);
59+
return keyLowerCasedMap.containsKey(lcaseKey) ?
60+
Boolean.parseBoolean(keyLowerCasedMap.get(lcaseKey)) : defaultValue;
61+
}
62+
63+
/**
64+
* Returns the integer value to which the specified key is mapped,
65+
* or defaultValue if there is no mapping for the key. The key match is case-insensitive
66+
*/
67+
public int getInt(String key, int defaultValue) {
68+
String lcaseKey = toLowerCase(key);
69+
return keyLowerCasedMap.containsKey(lcaseKey) ?
70+
Integer.parseInt(keyLowerCasedMap.get(lcaseKey)) : defaultValue;
71+
}
72+
73+
/**
74+
* Returns the long value to which the specified key is mapped,
75+
* or defaultValue if there is no mapping for the key. The key match is case-insensitive
76+
*/
77+
public long getLong(String key, long defaultValue) {
78+
String lcaseKey = toLowerCase(key);
79+
return keyLowerCasedMap.containsKey(lcaseKey) ?
80+
Long.parseLong(keyLowerCasedMap.get(lcaseKey)) : defaultValue;
81+
}
82+
5283
}

sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2OptionsSuite.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,35 @@ class DataSourceV2OptionsSuite extends SparkFunSuite {
3737
val options = new DataSourceV2Options(Map("foo" -> "bAr").asJava)
3838
assert(options.get("foo").get == "bAr")
3939
}
40+
41+
test("getInt") {
42+
val options = new DataSourceV2Options(Map("numFOo" -> "1", "foo" -> "bar").asJava)
43+
assert(options.getInt("numFOO", 10) == 1)
44+
assert(options.getInt("numFOO2", 10) == 10)
45+
46+
intercept[NumberFormatException]{
47+
options.getInt("foo", 1)
48+
}
49+
}
50+
51+
test("getBoolean") {
52+
val options = new DataSourceV2Options(
53+
Map("isFoo" -> "true", "isFOO2" -> "false", "foo" -> "bar").asJava)
54+
assert(options.getBoolean("isFoo", false))
55+
assert(!options.getBoolean("isFoo2", true))
56+
assert(options.getBoolean("isBar", true))
57+
assert(!options.getBoolean("isBar", false))
58+
assert(!options.getBoolean("FOO", true))
59+
}
60+
61+
test("getLong") {
62+
val options = new DataSourceV2Options(Map("numFoo" -> "9223372036854775807",
63+
"foo" -> "bar").asJava)
64+
assert(options.getLong("numFOO", 0L) == 9223372036854775807L)
65+
assert(options.getLong("numFoo2", -1L) == -1L)
66+
67+
intercept[NumberFormatException]{
68+
options.getLong("foo", 0L)
69+
}
70+
}
4071
}

0 commit comments

Comments
 (0)