abhishekrb19 commented on code in PR #17082:
URL: https://github.com/apache/druid/pull/17082#discussion_r1766131470
##########
processing/src/main/java/org/apache/druid/java/util/common/parsers/ParserUtils.java:
##########
@@ -52,22 +55,61 @@ public class ParserUtils
}
}
- public static Function<String, Object> getMultiValueFunction(
+ /**
+ * @return a function that processes a given string input by splitting it
into multiple values
+ * using the {@code listSplitter} if thge {@code list delimiter} is present
in the input. If {@code shouldParseNumbers}
+ * is enabled, the function will also try to parse any numeric values
present in the input -- integers as {@code Long}
+ * and floating-point numbers as {@code Double}.
+ */
+ public static Function<String, Object> getMultiValueAndParseNumbersFunction(
final String listDelimiter,
- final Splitter listSplitter
+ final Splitter listSplitter,
+ final boolean shouldParseNumbers
)
{
return (input) -> {
- if (input != null && input.contains(listDelimiter)) {
+ if (input == null) {
+ return NullHandling.emptyToNullIfNeeded(input);
+ }
+
+ if (input.contains(listDelimiter)) {
return StreamSupport.stream(listSplitter.split(input).spliterator(),
false)
- .map(NullHandling::emptyToNullIfNeeded)
- .collect(Collectors.toList());
+ .map(NullHandling::emptyToNullIfNeeded)
+ .map(value -> shouldParseNumbers ?
ParserUtils.tryParseStringAsNumber(value) : value)
+ .collect(Collectors.toList());
} else {
- return NullHandling.emptyToNullIfNeeded(input);
+ return shouldParseNumbers ?
+ tryParseStringAsNumber(input) :
+ NullHandling.emptyToNullIfNeeded(input);
+
}
};
}
+ /**
+ * Attempts to parse the input string into a numeric value, if applicable.
If the input is a number, the method first
+ * tries to parse the input number as a {@code Long}. If parsing as a {@code
Long} fails, it then attempts to parse
+ * the input number as a {@code Double}. For all other scenarios, the input
is returned as-is as a {@code String} type.
+ */
+ @Nullable
+ private static Object tryParseStringAsNumber(@Nullable final String input)
+ {
+ if (!NumberUtils.isNumber(input)) {
Review Comment:
Yeah, I considered something like that. However, it adds an additional
overhead of regex that you note for string inputs, so I kept the current
approach, which is optimized for non-numeric strings
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]