carloea2 commented on code in PR #4024:
URL: https://github.com/apache/texera/pull/4024#discussion_r2525160709
##########
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.
Review Comment:
Identity value: a value z such that combining it with x gives you x (like 0
for addition, 1 for multiplication).
Benign: “safe / harmless” i.e., it won’t throw, corrupt data, or surprise
callers.
For numeric/timestamp types the identity is clear (0, 0L, 0.0, epoch
timestamp).
For BINARY, there is no obvious numeric zero, so I picked
Array.emptyByteArray as a safe default
--
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]