mihaibudiu commented on code in PR #4730:
URL: https://github.com/apache/calcite/pull/4730#discussion_r2677303599


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -2180,6 +2189,144 @@ public static boolean neAny(Object b0, Object b1) {
     return !eqAny(b0, b1);
   }
 
+  private static boolean eqNullable(@Nullable Object b0, @Nullable Object b1) {
+    if (b0 == b1) {
+      return true;
+    }
+    if (b0 == null || b1 == null) {
+      return false;
+    }
+    if (b0 instanceof List && b1 instanceof List) {
+      final List<?> l0 = (List<?>) b0;
+      final List<?> l1 = (List<?>) b1;
+      if (l0.size() != l1.size()) {
+        return false;
+      }
+      for (int i = 0; i < l0.size(); i++) {
+        if (!eqNullable(l0.get(i), l1.get(i))) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Map && b1 instanceof Map) {
+      final Map<?, ?> m0 = (Map<?, ?>) b0;
+      final Map<?, ?> m1 = (Map<?, ?>) b1;
+      if (m0.size() != m1.size()) {
+        return false;
+      }
+      final Iterator<? extends Map.Entry<?, ?>> i0 = m0.entrySet().iterator();
+      final Iterator<? extends Map.Entry<?, ?>> i1 = m1.entrySet().iterator();
+      while (i0.hasNext() && i1.hasNext()) {
+        final Map.Entry<?, ?> e0 = i0.next();
+        final Map.Entry<?, ?> e1 = i1.next();
+        if (!eqNullable(e0.getKey(), e1.getKey())) {
+          return false;
+        }
+        if (!eqNullable(e0.getValue(), e1.getValue())) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Object[] && b1 instanceof Object[]) {
+      final Object[] a0 = (Object[]) b0;
+      final Object[] a1 = (Object[]) b1;
+      if (a0.length != a1.length) {
+        return false;
+      }
+      for (int i = 0; i < a0.length; i++) {
+        if (!eqNullable(a0[i], a1[i])) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0.getClass().equals(b1.getClass())) {
+      if (b0 instanceof BigDecimal) {
+        return eq((BigDecimal) b0, (BigDecimal) b1);
+      }
+      return b0.equals(b1);
+    }
+    if (b0 instanceof Number && b1 instanceof Number) {
+      return eq(toBigDecimal((Number) b0), toBigDecimal((Number) b1));
+    }
+    return false;
+  }
+
+  private static int compareNullable(@Nullable Object b0, @Nullable Object b1, 
String op) {

Review Comment:
   please document in JavaDoc how maps are compared, there are multiple 
possible choices.
   The other types are more straightforward.



##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -2180,6 +2189,144 @@ public static boolean neAny(Object b0, Object b1) {
     return !eqAny(b0, b1);
   }
 
+  private static boolean eqNullable(@Nullable Object b0, @Nullable Object b1) {
+    if (b0 == b1) {
+      return true;
+    }
+    if (b0 == null || b1 == null) {
+      return false;
+    }
+    if (b0 instanceof List && b1 instanceof List) {
+      final List<?> l0 = (List<?>) b0;
+      final List<?> l1 = (List<?>) b1;
+      if (l0.size() != l1.size()) {
+        return false;
+      }
+      for (int i = 0; i < l0.size(); i++) {
+        if (!eqNullable(l0.get(i), l1.get(i))) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Map && b1 instanceof Map) {
+      final Map<?, ?> m0 = (Map<?, ?>) b0;
+      final Map<?, ?> m1 = (Map<?, ?>) b1;
+      if (m0.size() != m1.size()) {
+        return false;
+      }
+      final Iterator<? extends Map.Entry<?, ?>> i0 = m0.entrySet().iterator();
+      final Iterator<? extends Map.Entry<?, ?>> i1 = m1.entrySet().iterator();
+      while (i0.hasNext() && i1.hasNext()) {
+        final Map.Entry<?, ?> e0 = i0.next();
+        final Map.Entry<?, ?> e1 = i1.next();
+        if (!eqNullable(e0.getKey(), e1.getKey())) {
+          return false;
+        }
+        if (!eqNullable(e0.getValue(), e1.getValue())) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Object[] && b1 instanceof Object[]) {
+      final Object[] a0 = (Object[]) b0;
+      final Object[] a1 = (Object[]) b1;
+      if (a0.length != a1.length) {
+        return false;
+      }
+      for (int i = 0; i < a0.length; i++) {
+        if (!eqNullable(a0[i], a1[i])) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0.getClass().equals(b1.getClass())) {
+      if (b0 instanceof BigDecimal) {
+        return eq((BigDecimal) b0, (BigDecimal) b1);
+      }
+      return b0.equals(b1);
+    }
+    if (b0 instanceof Number && b1 instanceof Number) {
+      return eq(toBigDecimal((Number) b0), toBigDecimal((Number) b1));
+    }
+    return false;
+  }
+
+  private static int compareNullable(@Nullable Object b0, @Nullable Object b1, 
String op) {
+    if (b0 == b1) {
+      return 0;
+    }
+    if (b0 == null) {
+      return 1;
+    }
+    if (b1 == null) {
+      return -1;
+    }
+    if (b0 instanceof List && b1 instanceof List) {
+      final List<?> l0 = (List<?>) b0;
+      final List<?> l1 = (List<?>) b1;
+      final int n0 = l0.size();
+      final int n1 = l1.size();
+      final int n = Math.min(n0, n1);
+      for (int i = 0; i < n; i++) {
+        final int c = compareNullable(l0.get(i), l1.get(i), op);
+        if (c != 0) {
+          return c;
+        }
+      }
+      return Integer.compare(n0, n1);
+    }
+    if (b0 instanceof Map && b1 instanceof Map) {
+      final Map<?, ?> m0 = (Map<?, ?>) b0;
+      final Map<?, ?> m1 = (Map<?, ?>) b1;
+      final Iterator<? extends Map.Entry<?, ?>> i0 = m0.entrySet().iterator();
+      final Iterator<? extends Map.Entry<?, ?>> i1 = m1.entrySet().iterator();
+      while (i0.hasNext() && i1.hasNext()) {
+        final Map.Entry<?, ?> e0 = i0.next();
+        final Map.Entry<?, ?> e1 = i1.next();
+        int c = compareNullable(e0.getKey(), e1.getKey(), op);
+        if (c != 0) {
+          return c;
+        }
+        c = compareNullable(e0.getValue(), e1.getValue(), op);
+        if (c != 0) {
+          return c;
+        }
+      }
+      if (i0.hasNext()) {
+        return 1;
+      }
+      if (i1.hasNext()) {
+        return -1;
+      }
+      return 0;
+    }
+    if (b0 instanceof Object[] && b1 instanceof Object[]) {
+      final Object[] a0 = (Object[]) b0;
+      final Object[] a1 = (Object[]) b1;
+      final int n0 = a0.length;
+      final int n1 = a1.length;
+      final int n = Math.min(n0, n1);
+      for (int i = 0; i < n; i++) {

Review Comment:
   is it ever legal for the arrays to have different lengths?



##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -2180,6 +2189,144 @@ public static boolean neAny(Object b0, Object b1) {
     return !eqAny(b0, b1);
   }
 
+  private static boolean eqNullable(@Nullable Object b0, @Nullable Object b1) {
+    if (b0 == b1) {
+      return true;
+    }
+    if (b0 == null || b1 == null) {
+      return false;
+    }
+    if (b0 instanceof List && b1 instanceof List) {
+      final List<?> l0 = (List<?>) b0;
+      final List<?> l1 = (List<?>) b1;
+      if (l0.size() != l1.size()) {
+        return false;
+      }
+      for (int i = 0; i < l0.size(); i++) {
+        if (!eqNullable(l0.get(i), l1.get(i))) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Map && b1 instanceof Map) {
+      final Map<?, ?> m0 = (Map<?, ?>) b0;
+      final Map<?, ?> m1 = (Map<?, ?>) b1;
+      if (m0.size() != m1.size()) {
+        return false;
+      }
+      final Iterator<? extends Map.Entry<?, ?>> i0 = m0.entrySet().iterator();
+      final Iterator<? extends Map.Entry<?, ?>> i1 = m1.entrySet().iterator();
+      while (i0.hasNext() && i1.hasNext()) {
+        final Map.Entry<?, ?> e0 = i0.next();
+        final Map.Entry<?, ?> e1 = i1.next();
+        if (!eqNullable(e0.getKey(), e1.getKey())) {
+          return false;
+        }
+        if (!eqNullable(e0.getValue(), e1.getValue())) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Object[] && b1 instanceof Object[]) {
+      final Object[] a0 = (Object[]) b0;
+      final Object[] a1 = (Object[]) b1;
+      if (a0.length != a1.length) {
+        return false;
+      }
+      for (int i = 0; i < a0.length; i++) {
+        if (!eqNullable(a0[i], a1[i])) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0.getClass().equals(b1.getClass())) {
+      if (b0 instanceof BigDecimal) {
+        return eq((BigDecimal) b0, (BigDecimal) b1);
+      }
+      return b0.equals(b1);
+    }
+    if (b0 instanceof Number && b1 instanceof Number) {
+      return eq(toBigDecimal((Number) b0), toBigDecimal((Number) b1));
+    }
+    return false;

Review Comment:
   I wonder whether this is reachable.
   Normally a validated program should ensure that it's not.
   You may want to try to replace it with an assert to check.
   If the assertion fails, you should add a comment explaining a circumstance 
where this is reachable.



##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -2180,6 +2189,144 @@ public static boolean neAny(Object b0, Object b1) {
     return !eqAny(b0, b1);
   }
 
+  private static boolean eqNullable(@Nullable Object b0, @Nullable Object b1) {
+    if (b0 == b1) {
+      return true;
+    }
+    if (b0 == null || b1 == null) {
+      return false;
+    }
+    if (b0 instanceof List && b1 instanceof List) {
+      final List<?> l0 = (List<?>) b0;
+      final List<?> l1 = (List<?>) b1;
+      if (l0.size() != l1.size()) {
+        return false;
+      }
+      for (int i = 0; i < l0.size(); i++) {
+        if (!eqNullable(l0.get(i), l1.get(i))) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Map && b1 instanceof Map) {
+      final Map<?, ?> m0 = (Map<?, ?>) b0;
+      final Map<?, ?> m1 = (Map<?, ?>) b1;
+      if (m0.size() != m1.size()) {
+        return false;
+      }
+      final Iterator<? extends Map.Entry<?, ?>> i0 = m0.entrySet().iterator();
+      final Iterator<? extends Map.Entry<?, ?>> i1 = m1.entrySet().iterator();
+      while (i0.hasNext() && i1.hasNext()) {
+        final Map.Entry<?, ?> e0 = i0.next();
+        final Map.Entry<?, ?> e1 = i1.next();
+        if (!eqNullable(e0.getKey(), e1.getKey())) {
+          return false;
+        }
+        if (!eqNullable(e0.getValue(), e1.getValue())) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0 instanceof Object[] && b1 instanceof Object[]) {
+      final Object[] a0 = (Object[]) b0;
+      final Object[] a1 = (Object[]) b1;
+      if (a0.length != a1.length) {
+        return false;
+      }
+      for (int i = 0; i < a0.length; i++) {
+        if (!eqNullable(a0[i], a1[i])) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if (b0.getClass().equals(b1.getClass())) {
+      if (b0 instanceof BigDecimal) {
+        return eq((BigDecimal) b0, (BigDecimal) b1);
+      }
+      return b0.equals(b1);
+    }
+    if (b0 instanceof Number && b1 instanceof Number) {
+      return eq(toBigDecimal((Number) b0), toBigDecimal((Number) b1));
+    }
+    return false;
+  }
+
+  private static int compareNullable(@Nullable Object b0, @Nullable Object b1, 
String op) {
+    if (b0 == b1) {
+      return 0;
+    }
+    if (b0 == null) {
+      return 1;
+    }
+    if (b1 == null) {
+      return -1;
+    }
+    if (b0 instanceof List && b1 instanceof List) {
+      final List<?> l0 = (List<?>) b0;
+      final List<?> l1 = (List<?>) b1;
+      final int n0 = l0.size();
+      final int n1 = l1.size();
+      final int n = Math.min(n0, n1);
+      for (int i = 0; i < n; i++) {
+        final int c = compareNullable(l0.get(i), l1.get(i), op);
+        if (c != 0) {
+          return c;
+        }
+      }
+      return Integer.compare(n0, n1);
+    }
+    if (b0 instanceof Map && b1 instanceof Map) {
+      final Map<?, ?> m0 = (Map<?, ?>) b0;
+      final Map<?, ?> m1 = (Map<?, ?>) b1;
+      final Iterator<? extends Map.Entry<?, ?>> i0 = m0.entrySet().iterator();
+      final Iterator<? extends Map.Entry<?, ?>> i1 = m1.entrySet().iterator();
+      while (i0.hasNext() && i1.hasNext()) {
+        final Map.Entry<?, ?> e0 = i0.next();
+        final Map.Entry<?, ?> e1 = i1.next();
+        int c = compareNullable(e0.getKey(), e1.getKey(), op);
+        if (c != 0) {
+          return c;
+        }
+        c = compareNullable(e0.getValue(), e1.getValue(), op);
+        if (c != 0) {
+          return c;
+        }
+      }
+      if (i0.hasNext()) {
+        return 1;
+      }
+      if (i1.hasNext()) {
+        return -1;
+      }
+      return 0;
+    }
+    if (b0 instanceof Object[] && b1 instanceof Object[]) {
+      final Object[] a0 = (Object[]) b0;
+      final Object[] a1 = (Object[]) b1;
+      final int n0 = a0.length;
+      final int n1 = a1.length;
+      final int n = Math.min(n0, n1);
+      for (int i = 0; i < n; i++) {
+        final int c = compareNullable(a0[i], a1[i], op);
+        if (c != 0) {
+          return c;
+        }
+      }
+      return Integer.compare(n0, n1);
+    }
+    if (b0.getClass().equals(b1.getClass()) && b0 instanceof Comparable) {

Review Comment:
   This cannot be right, it ignores op.



-- 
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]

Reply via email to