Skip to content

Commit f88a67b

Browse files
skambhacloud-fan
authored andcommitted
[SPARK-22452][SQL] Add getDouble to DataSourceV2Options
- Implemented getDouble method in DataSourceV2Options - Add unit test Author: Sunitha Kambhampati <[email protected]> Closes #19921 from skambha/ds2.
1 parent b11869b commit f88a67b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,13 @@ public long getLong(String key, long defaultValue) {
8080
Long.parseLong(keyLowerCasedMap.get(lcaseKey)) : defaultValue;
8181
}
8282

83+
/**
84+
* Returns the double value to which the specified key is mapped,
85+
* or defaultValue if there is no mapping for the key. The key match is case-insensitive
86+
*/
87+
public double getDouble(String key, double defaultValue) {
88+
String lcaseKey = toLowerCase(key);
89+
return keyLowerCasedMap.containsKey(lcaseKey) ?
90+
Double.parseDouble(keyLowerCasedMap.get(lcaseKey)) : defaultValue;
91+
}
8392
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,15 @@ class DataSourceV2OptionsSuite extends SparkFunSuite {
6868
options.getLong("foo", 0L)
6969
}
7070
}
71+
72+
test("getDouble") {
73+
val options = new DataSourceV2Options(Map("numFoo" -> "922337.1",
74+
"foo" -> "bar").asJava)
75+
assert(options.getDouble("numFOO", 0d) == 922337.1d)
76+
assert(options.getDouble("numFoo2", -1.02d) == -1.02d)
77+
78+
intercept[NumberFormatException]{
79+
options.getDouble("foo", 0.1d)
80+
}
81+
}
7182
}

0 commit comments

Comments
 (0)