Skip to content

Commit e1dcaa0

Browse files
committed
Refactor: separate geoip function implementation into w/ and w/o options versions
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 1db8839 commit e1dcaa0

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

core/src/main/java/org/opensearch/sql/expression/function/udf/ip/GeoIpFunction.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ public Expression implement(
7373
return Expressions.call(GeoIPImplementor.class, "fetchIpEnrichment", operandsWithClient);
7474
}
7575

76-
public static Object fetchIpEnrichment(
76+
public static Map<String, ?> fetchIpEnrichment(
7777
String dataSource, String ipAddress, NodeClient nodeClient) {
78-
return fetchIpEnrichment(dataSource, ipAddress, "", nodeClient);
78+
return fetchIpEnrichment(dataSource, ipAddress, Collections.emptySet(), nodeClient);
7979
}
8080

81-
public static Object fetchIpEnrichment(
81+
public static Map<String, ?> fetchIpEnrichment(
8282
String dataSource, String ipAddress, String commaSeparatedOptions, NodeClient nodeClient) {
83+
String unquotedOptions = StringUtils.unquoteText(commaSeparatedOptions);
84+
final Set<String> options =
85+
Arrays.stream(unquotedOptions.split(",")).map(String::trim).collect(Collectors.toSet());
86+
return fetchIpEnrichment(dataSource, ipAddress, options, nodeClient);
87+
}
88+
89+
private static Map<String, ?> fetchIpEnrichment(
90+
String dataSource, String ipAddress, Set<String> options, NodeClient nodeClient) {
8391
IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(nodeClient);
8492
dataSource = StringUtils.unquoteText(dataSource);
85-
String option = StringUtils.unquoteText(commaSeparatedOptions);
86-
// Convert the option into a set.
87-
final Set<String> options = new HashSet<>();
88-
if (!commaSeparatedOptions.isEmpty()) {
89-
options.addAll(
90-
Arrays.stream(option.split(",")).map(String::trim).collect(Collectors.toSet()));
91-
}
92-
9393
try {
9494
Map<String, Object> geoLocationData = ipClient.getGeoLocationData(ipAddress, dataSource);
9595
Map<String, ExprValue> enrichmentResult =
@@ -98,7 +98,10 @@ public static Object fetchIpEnrichment(
9898
.collect(
9999
Collectors.toMap(
100100
Map.Entry::getKey, v -> new ExprStringValue(v.getValue().toString())));
101-
return ExprTupleValue.fromExprValueMap(enrichmentResult).valueForCalcite();
101+
@SuppressWarnings("unchecked")
102+
Map<String, ?> result =
103+
(Map<String, ?>) ExprTupleValue.fromExprValueMap(enrichmentResult).valueForCalcite();
104+
return result;
102105
} catch (Exception e) {
103106
throw new RuntimeException(e);
104107
}

0 commit comments

Comments
 (0)