carloea2 commented on code in PR #4024:
URL: https://github.com/apache/texera/pull/4024#discussion_r2528857741


##########
common/workflow-core/src/main/scala/org/apache/amber/core/tuple/AttributeTypeUtils.scala:
##########
@@ -387,6 +387,136 @@ object AttributeTypeUtils extends Serializable {
     }
   }
 
+  /** Three-way compare for the given attribute type.
+    * Returns < 0 if left < right, > 0 if left > right, 0 if equal.
+    * Null semantics: null < non-null (both null => 0).
+    */
+  @throws[UnsupportedOperationException]
+  def compare(left: Any, right: Any, attrType: AttributeType): Int =
+    (left, right) match {
+      case (null, null) => 0
+      case (null, _)    => -1
+      case (_, null)    => 1
+      case _ =>
+        attrType match {
+          case AttributeType.INTEGER =>
+            java.lang.Integer.compare(
+              left.asInstanceOf[Number].intValue(),
+              right.asInstanceOf[Number].intValue()
+            )
+          case AttributeType.LONG =>
+            java.lang.Long.compare(
+              left.asInstanceOf[Number].longValue(),
+              right.asInstanceOf[Number].longValue()
+            )
+          case AttributeType.DOUBLE =>
+            java.lang.Double.compare(
+              left.asInstanceOf[Number].doubleValue(),
+              right.asInstanceOf[Number].doubleValue()
+            ) // handles ±Inf/NaN per JDK
+          case AttributeType.BOOLEAN =>
+            java.lang.Boolean.compare(
+              left.asInstanceOf[Boolean],
+              right.asInstanceOf[Boolean]
+            )
+          case AttributeType.TIMESTAMP =>
+            java.lang.Long.compare(
+              left.asInstanceOf[Timestamp].getTime,
+              right.asInstanceOf[Timestamp].getTime
+            )
+          case AttributeType.STRING =>
+            left.toString.compareTo(right.toString)
+          case AttributeType.BINARY =>
+            java.util.Arrays.compareUnsigned(
+              left.asInstanceOf[Array[Byte]],
+              right.asInstanceOf[Array[Byte]]
+            )
+          case _ =>
+            throw new UnsupportedOperationException(
+              s"Unsupported attribute type for compare: $attrType"
+            )
+        }
+    }
+
+  /** Type-aware addition (null is identity). */
+  @throws[UnsupportedOperationException]
+  def add(left: Object, right: Object, attrType: AttributeType): Object = {
+    if (left == null && right == null) return zeroValue(attrType)
+    if (left == null) return right
+    if (right == null) return left
+
+    attrType match {
+      case AttributeType.INTEGER =>
+        java.lang.Integer.valueOf(
+          left.asInstanceOf[Number].intValue() + 
right.asInstanceOf[Number].intValue()
+        )
+      case AttributeType.LONG =>
+        java.lang.Long.valueOf(
+          left.asInstanceOf[Number].longValue() + 
right.asInstanceOf[Number].longValue()
+        )
+      case AttributeType.DOUBLE =>
+        java.lang.Double.valueOf(
+          left.asInstanceOf[Number].doubleValue() + 
right.asInstanceOf[Number].doubleValue()
+        )
+      case AttributeType.TIMESTAMP =>
+        new Timestamp(
+          left.asInstanceOf[Timestamp].getTime + 
right.asInstanceOf[Timestamp].getTime
+        )
+      case _ =>
+        throw new UnsupportedOperationException(
+          s"Unsupported attribute type for addition: $attrType"
+        )
+    }
+  }
+
+  /** Additive identity for supported numeric/timestamp types.
+    * For BINARY an empty array is returned as a benign identity value.
+    */
+  @throws[UnsupportedOperationException]
+  def zeroValue(attrType: AttributeType): Object =
+    attrType match {
+      case AttributeType.INTEGER   => java.lang.Integer.valueOf(0)
+      case AttributeType.LONG      => java.lang.Long.valueOf(0L)
+      case AttributeType.DOUBLE    => java.lang.Double.valueOf(0.0d)
+      case AttributeType.TIMESTAMP => new Timestamp(0L)
+      case AttributeType.BINARY    => Array.emptyByteArray
+      case _ =>
+        throw new UnsupportedOperationException(
+          s"Unsupported attribute type for zero value: $attrType"
+        )
+    }
+
+  /** Maximum sentinel. */

Review Comment:
   So how do you call them?



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