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

Improved null check for varargs #12673

Merged
merged 3 commits into from
Apr 1, 2024
Merged

Conversation

soumitra-st
Copy link
Contributor

Adding null checks for cases:

  1. If literal null is passes
  2. No value passed
  3. A variable is passed with null value during runtime

cleanup

Copy link
Contributor

@zhtaoxiang zhtaoxiang left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov-commenter
Copy link

codecov-commenter commented Mar 19, 2024

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 61.52%. Comparing base (59551e4) to head (0e835db).
Report is 168 commits behind head on master.

Files Patch % Lines
...main/java/org/apache/pinot/client/BrokerCache.java 0.00% 2 Missing ⚠️
...e/pinot/common/function/scalar/ArrayFunctions.java 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #12673      +/-   ##
============================================
- Coverage     61.75%   61.52%   -0.23%     
+ Complexity      207      198       -9     
============================================
  Files          2436     2457      +21     
  Lines        133233   134417    +1184     
  Branches      20636    20804     +168     
============================================
+ Hits          82274    82701     +427     
- Misses        44911    45542     +631     
- Partials       6048     6174     +126     
Flag Coverage Δ
custom-integration1 <0.01% <0.00%> (-0.01%) ⬇️
integration <0.01% <0.00%> (-0.01%) ⬇️
integration1 <0.01% <0.00%> (-0.01%) ⬇️
integration2 0.00% <0.00%> (ø)
java-11 61.49% <0.00%> (-0.22%) ⬇️
java-21 61.38% <0.00%> (-0.25%) ⬇️
skip-bytebuffers-false 61.51% <0.00%> (-0.24%) ⬇️
skip-bytebuffers-true 34.39% <0.00%> (+6.66%) ⬆️
temurin 61.52% <0.00%> (-0.23%) ⬇️
unittests 61.52% <0.00%> (-0.23%) ⬇️
unittests1 46.15% <0.00%> (-0.74%) ⬇️
unittests2 27.92% <0.00%> (+0.19%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -230,7 +230,7 @@ public static String arrayElementAtString(String[] arr, int idx) {

@ScalarFunction(names = {"array", "arrayValueConstructor"}, isVarArg = true)
public static Object arrayValueConstructor(Object... arr) {
if (arr.length == 0) {
if (arr == null || arr.length == 0 || arr[0] == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we are changing the semantic, right? We are treating [null] in the same way we treated [] in the past.

I would feel more confident with something like if (arr == null || arr.length == 0), which is what we may wanted to do in the first place

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Firstly, to give the context:
foo(String... args) {
args == null // foo(null)
args.length == 0 // foo()
args[0] == null // String bar = null; foo(bar)

In this specific case, if arr[0] == null, the code below (arr[0].getClass()) will cause null pointer exception, hence the additional checks is an improvement. I didn't follow why it would change the semantics.

I have tried to only add the additional checks in places which can cause NPE, vs changing all varargs usages in the codebase.

Copy link
Contributor

Choose a reason for hiding this comment

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

It changes semantics because foo(null, 1, 2, 3) will behave differently foo(1, 2, 3, null). Do we want to enter on this if when any value in arr is null? Ok, but doing so only when the first value is null looks very strange.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

foo(null, 1, 2, 3) will cause NPE below (arr[0].getClass()), are we ok with that? This PR address that.
if (arr == null || arr.length == 0) check will not prevent the NPE.

Copy link
Contributor

Choose a reason for hiding this comment

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

My point is that foo(1, null, 2, 3) will also cause a NPE when doing intArr[i] = (Integer) arr[i];.

It is very strange to do not throw NPE when the first element is null but do it when the second is. We should either always throw NPE when any value in the array is null (like before) or never do that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. I missed (Integer) arr[i] code, let me modify the PR.

@@ -190,7 +190,7 @@ protected void updateBrokerData()

public String getBroker(String... tableNames) {
List<String> brokers = null;
if (tableNames != null) {
if (!(tableNames == null || tableNames.length == 0 || tableNames[0] == null)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for not saying that before, but I think the same can be said here. Either all or none of the actual elements should be compared to 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.

Pushed a change to filter out nulls if the tableNames is non-null.

@gortiz gortiz merged commit 3185e30 into apache:master Apr 1, 2024
18 of 19 checks passed
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.

5 participants