Github user jaredjstewart commented on a diff in the pull request:

    https://github.com/apache/geode/pull/580#discussion_r122066276
  
    --- Diff: 
geode-core/src/main/java/org/apache/geode/cache/query/internal/OrderByComparator.java
 ---
    @@ -228,4 +139,55 @@ void addEvaluatedSortCriteria(Object row, 
ExecutionContext context)
         // No op
       }
     
    +  private int compareHelperMethod(Object obj1, Object obj2) {
    +    if (obj1 == null || obj2 == null) {
    +      return compareIfOneOrMoreNull(obj1, obj2);
    +    } else if (obj1 == QueryService.UNDEFINED || obj2 == 
QueryService.UNDEFINED) {
    +      return compareIfOneOrMoreQueryServiceUndefined(obj1, obj2);
    +    } else {
    +      return compareTwoObjects(obj1, obj2);
    +    }
    +  }
    +
    +  private int compareIfOneOrMoreNull(Object obj1, Object obj2) {
    +    if (obj1 == null) {
    +      return obj2 == null ? 0 : -1;
    +    } else {
    +      return 1;
    +    }
    +  }
    +
    +  private int compareIfOneOrMoreQueryServiceUndefined(Object obj1, Object 
obj2) {
    +    if (obj1 == QueryService.UNDEFINED) {
    +      return obj2 == QueryService.UNDEFINED ? 0 : -1;
    +    } else {
    +      return 1;
    +    }
    +  }
    +
    +  private int compareTwoObjects(Object obj1, Object obj2) {
    +    if (obj1 instanceof Number && obj2 instanceof Number) {
    +      return compareTwoNumbers(obj1, obj2);
    +    } else {
    +      return compareTwoStrings(obj1, obj2);
    +    }
    +  }
    +
    +  private int compareTwoNumbers(Object obj1, Object obj2) {
    --- End diff --
    
    I see it was not introduced by your changes, but the subtraction on 177 is 
not a safe way to compare doubles as it can result in under/overflow.  For 
example, consider the result of `compareTwoNumbers( Double.MAX_VALUE, -1d)`.  
Here's a safer way to do it:
    
    ```
      private int compareTwoNumbers(Object obj1, Object obj2) {
        Number number1 = (Number) obj1;
        Number number2 = (Number) obj2 ;
        return Double.compare(number1.doubleValue(), number2.doubleValue());
      }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to