Ejemplo n.º 1
0
  @Override
  public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal result = null;
    for (int i = 0; i < children.size(); i++) {
      Expression childExpr = children.get(i);
      if (!childExpr.evaluate(tuple, ptr)) {
        return false;
      }
      if (ptr.getLength() == 0) {
        return true;
      }

      PDataType childType = children.get(i).getDataType();
      BigDecimal bd = (BigDecimal) PDataType.DECIMAL.toObject(ptr, childType);

      if (result == null) {
        result = bd;
      } else {
        result = result.divide(bd, PDataType.DEFAULT_MATH_CONTEXT);
      }
    }
    if (maxLength != null && scale != null) {
      result = NumberUtil.setDecimalWidthAndScale(result, maxLength, scale);
    }
    if (result == null) {
      throw new ValueTypeIncompatibleException(PDataType.DECIMAL, maxLength, scale);
    }
    ptr.set(PDataType.DECIMAL.toBytes(result));
    return true;
  }
  @Override
  public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal result = null;
    DateNative dateNative = DateNative.getInstance();
    for (int i = 0; i < children.size(); i++) {
      if (!children.get(i).evaluate(tuple, ptr) || ptr.getLength() == 0) {
        return false;
      }

      PDataType childType = children.get(i).getDataType();
      boolean isDate = childType.isCoercibleTo(PDataType.DATE);
      BigDecimal bd =
          isDate
              ? BigDecimal.valueOf(dateNative.toLong(ptr))
              : (BigDecimal) PDataType.DECIMAL.toObject(ptr, childType);

      if (result == null) {
        result = bd;
      } else {
        result = result.subtract(bd);
        /*
         * Special case for date subtraction - note that only first two expression may be dates.
         * We need to convert the date to a unit of "days" because that's what sql expects.
         */
        if (isDate) {
          result = result.divide(BD_MILLIS_IN_DAY, PDataType.DEFAULT_MATH_CONTEXT);
        }
      }
    }
    ptr.set(PDataType.DECIMAL.toBytes(result));
    return true;
  }
  @Override
  public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal result = null;
    for (int i = 0; i < children.size(); i++) {
      if (!children.get(i).evaluate(tuple, ptr) || ptr.getLength() == 0) {
        return false;
      }

      PDataType childType = children.get(i).getDataType();
      BigDecimal bd = (BigDecimal) PDataType.DECIMAL.toObject(ptr, childType);

      if (result == null) {
        result = bd;
      } else {
        result = result.divide(bd, PDataType.DEFAULT_MATH_CONTEXT);
      }
    }
    ptr.set(PDataType.DECIMAL.toBytes(result));
    return true;
  }
Ejemplo n.º 4
0
 @Override
 public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
   if (!super.evaluate(tuple, ptr)) {
     return false;
   }
   if (isConstantExpression()) {
     PDataType type = getDataType();
     Object constantValue = ((LiteralExpression) children.get(0)).getValue();
     if (type == PDataType.DECIMAL) {
       BigDecimal value =
           ((BigDecimal) constantValue)
               .multiply((BigDecimal) PDataType.DECIMAL.toObject(ptr, PDataType.LONG));
       ptr.set(PDataType.DECIMAL.toBytes(value));
     } else {
       long constantLongValue = ((Number) constantValue).longValue();
       long value = constantLongValue * type.getCodec().decodeLong(ptr, null);
       ptr.set(new byte[type.getByteSize()]);
       type.getCodec().encodeLong(value, ptr);
     }
   }
   return true;
 }
  @Override
  public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal finalResult = BigDecimal.ZERO;

    for (int i = 0; i < children.size(); i++) {
      if (!children.get(i).evaluate(tuple, ptr)) {
        return false;
      }
      if (ptr.getLength() == 0) {
        return true;
      }
      BigDecimal value;
      PDataType type = children.get(i).getDataType();
      ColumnModifier columnModifier = children.get(i).getColumnModifier();
      if (type == PDataType.TIMESTAMP || type == PDataType.UNSIGNED_TIMESTAMP) {
        value = (BigDecimal) (PDataType.DECIMAL.toObject(ptr, type, columnModifier));
      } else if (type.isCoercibleTo(PDataType.DECIMAL)) {
        value =
            (((BigDecimal) PDataType.DECIMAL.toObject(ptr, columnModifier))
                    .multiply(QueryConstants.BD_MILLIS_IN_DAY))
                .setScale(6, RoundingMode.HALF_UP);
      } else if (type.isCoercibleTo(PDataType.DOUBLE)) {
        value =
            ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, columnModifier)))
                    .multiply(QueryConstants.BD_MILLIS_IN_DAY))
                .setScale(6, RoundingMode.HALF_UP);
      } else {
        value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, columnModifier));
      }
      finalResult = finalResult.add(value);
    }
    Timestamp ts = DateUtil.getTimestamp(finalResult);
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    PDataType.TIMESTAMP.toBytes(ts, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
  }