Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isJson UDF #12603

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.pinot.common.utils.RegexpPatternConverterUtils;
import org.apache.pinot.spi.annotations.ScalarFunction;
import org.apache.pinot.spi.utils.JsonUtils;


/**
Expand Down Expand Up @@ -831,4 +832,24 @@ public static boolean like(String inputStr, String likePatternStr) {
String regexPatternStr = RegexpPatternConverterUtils.likeToRegexpLike(likePatternStr);
return regexpLike(inputStr, regexPatternStr);
}

/**
* Checks whether the input string can be parsed into a json node or not. Useful for scenarios where we want
* to filter out malformed json. In case of nulls we return null itself, as null can be treated as valid json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement does not seem to be aligned with what is implemented and merged here. If the argument is null, it returns false as JsonUtils.stringToJsonNode(inputStr); throws an Exception.

The discussion in the comment thread is also confusing to what is finally added here. It is good to add the conclusion in that thread for readers if it is decided based on offline discussion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tibrewalpratik17 We may update the javadoc. null values are handled by the caller (function invoker), instead of within this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

* in partial-upsert scenarios. Upto the user to use null response accordingly.
*
* @param inputStr Input string to test for valid json
* @return in case of null value, it returns null. In case of non-null, it returns true in case of valid json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as earlier for a null argument.

* parsing else false
*
*/
@ScalarFunction(names = {"isJson", "is_json"})
public static boolean isJson(String inputStr) {
try {
JsonUtils.stringToJsonNode(inputStr);
return true;
} catch (Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pinot.common.function.scalar;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;


public class StringFunctionsTest {

@DataProvider(name = "isJson")
public static Object[][] isJsonTestCases() {
return new Object[][]{
{"", true},
{"{\"key\": \"value\"}", true},
{"{\"key\": \"value\", }", false},
{"{\"key\": \"va", false}
};
}

@Test(dataProvider = "isJson")
public void testIsJson(String input, boolean expectedValue) {
assertEquals(StringFunctions.isJson(input), expectedValue);
}
}
Loading