Caideyipi commented on code in PR #17850:
URL: https://github.com/apache/iotdb/pull/17850#discussion_r4373909128


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrExpressionInterpreter.java:
##########
@@ -981,6 +922,218 @@ private boolean hasUnresolvedValue(List<Object> values) {
       return values.stream().anyMatch(instanceOf(Expression.class));
     }
 
+    private boolean isSupportedArithmeticBinaryType(Type type) {
+      return type.equals(INT32) || type.equals(INT64) || type.equals(FLOAT) || 
type.equals(DOUBLE);
+    }
+
+    private boolean isSupportedArithmeticUnaryType(Type type) {
+      return isSupportedArithmeticBinaryType(type) || type.equals(TIMESTAMP);
+    }
+
+    private Object evaluateArithmeticNegation(Object value, Type returnType) {
+      if (returnType.equals(INT32)) {
+        int intValue = toInt(value);
+        if (intValue == Integer.MIN_VALUE) {
+          throw new SemanticException(
+              String.format(
+                  
DataNodeQueryMessages.EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_INT_F8E58471,
+                  intValue));
+        }
+        return -intValue;
+      }
+      if (returnType.equals(INT64) || returnType.equals(TIMESTAMP)) {
+        long longValue = toLong(value);
+        if (longValue == Long.MIN_VALUE) {
+          throw new SemanticException(
+              String.format(
+                  
DataNodeQueryMessages.EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_LONG_9B1C872A,
+                  longValue));
+        }
+        return -longValue;
+      }
+      if (returnType.equals(FLOAT)) {
+        return -toFloat(value);
+      }
+      if (returnType.equals(DOUBLE)) {
+        return -toDouble(value);
+      }
+      throw new SemanticException(DataNodeQueryMessages.UNKNOWN_TYPE_2 + 
returnType);
+    }
+
+    private Object evaluateArithmeticBinary(
+        ArithmeticBinaryExpression.Operator operator,
+        Object left,
+        Type leftType,
+        Object right,
+        Type returnType) {
+      if (returnType.equals(INT32)) {
+        return evaluateIntArithmetic(operator, toInt(left), toInt(right));
+      }
+      if (returnType.equals(INT64)) {
+        return evaluateLongArithmetic(operator, toLong(left), leftType, 
toLong(right));
+      }
+      if (returnType.equals(FLOAT)) {
+        return evaluateFloatArithmetic(operator, toFloat(left), 
toFloat(right));
+      }
+      if (returnType.equals(DOUBLE)) {
+        return evaluateDoubleArithmetic(operator, toDouble(left), 
toDouble(right));
+      }
+      throw new SemanticException(DataNodeQueryMessages.UNKNOWN_TYPE_2 + 
returnType);
+    }
+
+    private int evaluateIntArithmetic(
+        ArithmeticBinaryExpression.Operator operator, int left, int right) {
+      try {
+        switch (operator) {
+          case ADD:
+            return Math.addExact(left, right);
+          case SUBTRACT:
+            return Math.subtractExact(left, right);
+          case MULTIPLY:
+            return Math.multiplyExact(left, right);
+          case DIVIDE:
+            if (left == Integer.MIN_VALUE && right == -1) {
+              throw new SemanticException(
+                  String.format(
+                      
DataNodeQueryMessages.EXCEPTION_INT_OVERFLOW_ARG_ARG_BB287017, left, right));
+            }
+            return left / right;
+          case MODULUS:
+            return left % right;
+          default:
+            throw new UnsupportedOperationException(
+                DataNodeQueryMessages.UNSUPPORTED_EXPRESSION + operator);
+        }
+      } catch (ArithmeticException e) {
+        throw new SemanticException(e.getMessage());
+      }
+    }
+
+    private long evaluateLongArithmetic(
+        ArithmeticBinaryExpression.Operator operator, long left, Type 
leftType, long right) {
+      try {
+        switch (operator) {
+          case ADD:
+            return Math.addExact(left, right);
+          case SUBTRACT:
+            return Math.subtractExact(left, right);
+          case MULTIPLY:
+            return Math.multiplyExact(left, right);
+          case DIVIDE:
+            if ((leftType.equals(INT32) && left == Integer.MIN_VALUE && right 
== -1)
+                || (leftType.equals(INT64) && left == Long.MIN_VALUE && right 
== -1)) {
+              throw new SemanticException(
+                  String.format(
+                      
DataNodeQueryMessages.EXCEPTION_LONG_OVERFLOW_ARG_ARG_46CC95E2, left, right));
+            }
+            return left / right;
+          case MODULUS:
+            return left % right;
+          default:
+            throw new UnsupportedOperationException(
+                DataNodeQueryMessages.UNSUPPORTED_EXPRESSION + operator);
+        }
+      } catch (ArithmeticException e) {
+        throw new SemanticException(e.getMessage());
+      }
+    }
+
+    private float evaluateFloatArithmetic(
+        ArithmeticBinaryExpression.Operator operator, float left, float right) 
{
+      switch (operator) {
+        case ADD:
+          return left + right;
+        case SUBTRACT:
+          return left - right;
+        case MULTIPLY:
+          return left * right;
+        case DIVIDE:
+          return left / right;
+        case MODULUS:
+          return left % right;
+        default:
+          throw new UnsupportedOperationException(
+              DataNodeQueryMessages.UNSUPPORTED_EXPRESSION + operator);
+      }
+    }
+
+    private double evaluateDoubleArithmetic(
+        ArithmeticBinaryExpression.Operator operator, double left, double 
right) {
+      switch (operator) {
+        case ADD:
+          return left + right;
+        case SUBTRACT:
+          return left - right;
+        case MULTIPLY:
+          return left * right;
+        case DIVIDE:
+          return left / right;
+        case MODULUS:
+          return left % right;
+        default:
+          throw new UnsupportedOperationException(
+              DataNodeQueryMessages.UNSUPPORTED_EXPRESSION + operator);
+      }
+    }
+
+    private Object normalizeArithmeticValue(Object value, Type type) {
+      if (type.equals(INT32)) {
+        return toInt(value);
+      }
+      if (type.equals(INT64) || type.equals(TIMESTAMP)) {
+        return toLong(value);
+      }
+      if (type.equals(FLOAT)) {
+        return toFloat(value);
+      }
+      if (type.equals(DOUBLE)) {
+        return toDouble(value);
+      }
+      return value;
+    }
+
+    private Boolean evaluateNumericComparison(
+        ComparisonExpression.Operator operator, Number left, Number right) {
+      if (Double.isNaN(left.doubleValue()) || 
Double.isNaN(right.doubleValue())) {

Review Comment:
   Found and fixed in `939d153bdf9`: `Double.compare` orders NaN above finite 
values and treats NaN as equal to itself, so constant folding incorrectly made 
`1.0 < NaN`, `NaN >= 1.0`, `NaN = NaN`, and the derived `<>` predicate true. 
The interpreter now returns false whenever either operand is NaN, matching 
`CompareBinaryColumnTransformer`. `testFoldedNaNComparisons` and the focused IT 
cover these cases.



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