tanclary commented on code in PR #3147:
URL: https://github.com/apache/calcite/pull/3147#discussion_r1254813751
##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -562,6 +564,91 @@ public static List<ByteString> split(ByteString s,
ByteString delimiter) {
}
}
+ public static @Nullable Boolean containsSubstr(@Nullable Object @Nullable []
s0, String s1) {
+ // If s0 has null arguments, it should return TRUE if substring is found,
otherwise NULL
+ boolean nullFlag = false;
+ if (s0 == null) {
+ return false;
+ }
+ for (Object obj : s0) {
+ if (obj == null) {
+ nullFlag = true;
+ } else if (obj instanceof Object[]) {
+ return containsSubstr((Object[]) obj, s1);
+ } else if (obj instanceof ArrayList) {
+ return containsSubstr((List) obj, s1);
+ } else if (normalize(obj.toString()).contains(normalize(s1))) {
+ return true;
+ }
+ }
+ return nullFlag ? null : false;
+ }
+
+ public static @Nullable Boolean containsSubstr(List s0, String s1) {
+ // If s0 has null arguments, it should return TRUE if substring is found,
otherwise NULL
+ boolean nullFlag = false;
+ for (Object item : s0) {
+ if (item == null) {
+ nullFlag = true;
+ }
+ if (item != null && containsSubstr(item, s1)) {
+ return true;
+ }
+ }
+ return nullFlag ? null : false;
+ }
+
+ public static @Nullable Boolean containsSubstr(String s0, String s1, String
s2) {
+ // The third argument specifies the json_scope, either keys, values, or
both
+ LinkedHashMap<String, String> map = (LinkedHashMap)
JsonFunctions.dejsonize(s0);
+ Set<String> keys = map.keySet();
+ Collection<String> values = map.values();
+ if (s2.equals("JSON_KEYS")) {
+ return keys.contains(s1);
+ } else if (s2.equals("JSON_VALUES")) {
+ return values.contains(s1);
+ } else if (s2.equals("JSON_KEYS_AND_VALUES")) {
+ return keys.contains(s1) || values.contains(s1);
+ } else {
+ throw new IllegalArgumentException("json_scope argument must be one of:
\"JSON_KEYS\", "
+ + "\"JSON_VALUES\", \"JSON_KEYS_AND_VALUES\".");
+ }
+ }
+
+ /** SQL {@code CONTAINS_SUBSTR(string, string)} function. */
+ public static Boolean containsSubstr(@Nullable Object s0, String s1) {
Review Comment:
I left some as `Boolean` because there are cases where the method should
return `NULL` even if none of the parameters are `NULL`. For example if the
argument is ROW(1, null, 3) and the substr is '2', it should return `NULL`.
--
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]