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

Change json_format to return java null when java null is received #11673

Merged
merged 2 commits into from Sep 27, 2023

Conversation

gortiz
Copy link
Contributor

@gortiz gortiz commented Sep 25, 2023

This PR changes json_extract in order to return the Java value null when the input is the Java value null.

The following table shows the difference between the old and new semantic:

Input (JsonNode) Old Output (String value) New output (String value)
A map whose key "a" values 1 "{\"a\": 1}" "{\"a\": 1}"
The json null literal "null" "null"
The java null value "null" null

This is a bugfix, but can also be considered as a backward incompatible change.

public void jsonFormatWithJavaNullReturnsJavaNull() {
GenericRow row = new GenericRow();
row.putValue("jsonMap", null);
testFunction("json_format(jsonMap)", Lists.newArrayList("jsonMap"), row, null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test fails if executed in master. Instead of Java null, what was returned was the String "null"

/**
* If empty, FunctionsRegistry registers the method name as function name;
* If not empty, FunctionsRegistry only registers the function names specified here, the method name is ignored.
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just applying the Boy Scouting Rule, “Always leave the code you are working on a little bit better than you found it.”

@gortiz
Copy link
Contributor Author

gortiz commented Sep 25, 2023

cc @klsince

@codecov-commenter
Copy link

codecov-commenter commented Sep 25, 2023

Codecov Report

Merging #11673 (60dacad) into master (17ff44e) will decrease coverage by 0.03%.
The diff coverage is n/a.

@@             Coverage Diff              @@
##             master   #11673      +/-   ##
============================================
- Coverage     63.08%   63.06%   -0.03%     
+ Complexity     1121     1120       -1     
============================================
  Files          2343     2343              
  Lines        125676   125676              
  Branches      19314    19314              
============================================
- Hits          79285    79252      -33     
- Misses        40739    40773      +34     
+ Partials       5652     5651       -1     
Flag Coverage Δ
integration <0.01% <ø> (ø)
integration1 <0.01% <ø> (ø)
integration2 0.00% <ø> (ø)
java-11 63.02% <ø> (-0.01%) ⬇️
java-17 62.91% <ø> (-0.01%) ⬇️
java-20 62.90% <ø> (+12.93%) ⬆️
temurin 63.06% <ø> (-0.03%) ⬇️
unittests 63.05% <ø> (-0.03%) ⬇️
unittests1 67.22% <ø> (ø)
unittests2 14.44% <ø> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...he/pinot/common/function/scalar/JsonFunctions.java 87.87% <ø> (ø)

... and 13 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@klsince
Copy link
Contributor

klsince commented Sep 25, 2023

what's the difference between "json null literal" vs. "java null literal"? a simple example could help here. thanks!

// Whether the scalar function expects and can handle null arguments.
/**
* Whether the scalar function expects and can handle null arguments.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove this empty line

@gortiz
Copy link
Contributor Author

gortiz commented Sep 25, 2023

what's the difference between "json null literal" vs. "java null literal"? a simple example could help here. thanks!

In Jackson, the null json value is represented as an instance of JsonNode which returns true for JsonNode.isNull(). Meanwhile java null is the typical null we use in Java.

@klsince
Copy link
Contributor

klsince commented Sep 25, 2023

In Jackson, the null json value is represented as an instance of JsonNode which returns true for JsonNode.isNull().

Got it! Just wondering for json_format(), what kind of user input could lead to a JsonNode.isNull()? Would a "null" string value result in a JsonNode that's isNull()?

@@ -79,6 +79,9 @@ public static String toJsonMapStr(@Nullable Map map)
@ScalarFunction(nullableParameters = true)
public static String jsonFormat(Object object)
throws JsonProcessingException {
if (object == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is intentionally added in #8382. If we want to return null, removing the nullableParameters = true will achieve that. Any specific reason why you don't want string 'null'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

@gortiz
Copy link
Contributor Author

gortiz commented Sep 26, 2023

Got it! Just wondering for json_format(), what kind of user input could lead to a JsonNode.isNull()? Would a "null" string value result in a JsonNode that's isNull()?

Yes

This is intentionally added in #8382. If we want to return null, removing the nullableParameters = true will achieve that. Any specific reason why you don't want string 'null'?

I guess to conserve the nullability. I don't have a strong preference, but some users have been asking to change the behavior. Maybe it is just better to keep the old behavior and to specify the null semantic in the documentation.

@Jackie-Jiang
Copy link
Contributor

I guess to conserve the nullability. I don't have a strong preference, but some users have been asking to change the behavior. Maybe it is just better to keep the old behavior and to specify the null semantic in the documentation.

I see. I think it is good to conserve the nullability. Since we have "null" as the default value, it should be fine to return the real null from the scalar function. Can you modify the code and remove nullableParameters = true

@@ -76,7 +76,7 @@ public static String toJsonMapStr(@Nullable Map map)
/**
* Convert object to Json String
*/
@ScalarFunction(nullableParameters = true)
@ScalarFunction
Copy link
Contributor

Choose a reason for hiding this comment

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

neat 👍

@Jackie-Jiang Jackie-Jiang merged commit 1f93870 into apache:master Sep 27, 2023
19 checks passed
@gortiz gortiz deleted the jsonFormat-with-null branch January 5, 2024 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants