Example #1
0
 // TODO: cache?
 public static LiteralExpression newConstant(
     Object value,
     PDataType type,
     Integer maxLength,
     Integer scale,
     SortOrder sortOrder,
     Determinism determinism,
     boolean rowKeyOrderOptimizable)
     throws SQLException {
   if (value == null) {
     return (type == null)
         ? getNullLiteralExpression(determinism)
         : getTypedNullLiteralExpression(type, determinism);
   } else if (value instanceof Boolean) {
     return getBooleanLiteralExpression((Boolean) value, determinism);
   }
   PDataType actualType = PDataType.fromLiteral(value);
   // For array we should check individual element in it?
   // It would be costly though!!!!!
   // UpsertStatement can try to cast varchar to date type but PVarchar can't CoercibleTo Date or
   // Timestamp
   // otherwise TO_NUMBER like functions will fail
   if (!actualType.isCoercibleTo(type, value)
       && (!actualType.equals(PVarchar.INSTANCE)
           || !(type.equals(PDate.INSTANCE)
               || type.equals(PTimestamp.INSTANCE)
               || type.equals(PTime.INSTANCE)))) {
     throw TypeMismatchException.newException(type, actualType, value.toString());
   }
   value = type.toObject(value, actualType);
   byte[] b =
       type.isArrayType()
           ? ((PArrayDataType) type)
               .toBytes(
                   value, PArrayDataType.arrayBaseType(type), sortOrder, rowKeyOrderOptimizable)
           : type.toBytes(value, sortOrder);
   if (type == PVarchar.INSTANCE || type == PChar.INSTANCE) {
     if (type == PChar.INSTANCE && maxLength != null && b.length < maxLength) {
       if (rowKeyOrderOptimizable) {
         b = type.pad(b, maxLength, sortOrder);
       } else {
         b = StringUtil.padChar(b, maxLength);
       }
     } else if (value != null) {
       maxLength = ((String) value).length();
     }
   } else if (type.isArrayType()) {
     maxLength = ((PhoenixArray) value).getMaxLength();
   }
   if (b.length == 0) {
     return getTypedNullLiteralExpression(type, determinism);
   }
   if (maxLength == null) {
     maxLength = type == null || !type.isFixedWidth() ? null : type.getMaxLength(value);
   }
   return new LiteralExpression(value, type, b, maxLength, scale, sortOrder, determinism);
 }
Example #2
0
 @Override
 public String toString() {
   if (value == null) {
     return "null";
   }
   // TODO: move into PDataType?
   if (type.isCoercibleTo(PTimestamp.INSTANCE)) {
     return type + " " + type.toStringLiteral(value, null);
   }
   return type.toStringLiteral(value, null);
 }
 public LiteralParseNode literal(String value, String sqlTypeName) throws SQLException {
   PDataType expectedType =
       sqlTypeName == null
           ? null
           : PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(sqlTypeName));
   if (expectedType == null || !expectedType.isCoercibleTo(PTimestamp.INSTANCE)) {
     throw TypeMismatchException.newException(expectedType, PTimestamp.INSTANCE);
   }
   Object typedValue = expectedType.toObject(value);
   return new LiteralParseNode(typedValue);
 }
  @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();
      SortOrder sortOrder = children.get(i).getSortOrder();
      if (type == PTimestamp.INSTANCE || type == PUnsignedTimestamp.INSTANCE) {
        value = (BigDecimal) (PDecimal.INSTANCE.toObject(ptr, type, sortOrder));
      } else if (type.isCoercibleTo(PDecimal.INSTANCE)) {
        value =
            (((BigDecimal) PDecimal.INSTANCE.toObject(ptr, sortOrder))
                    .multiply(QueryConstants.BD_MILLIS_IN_DAY))
                .setScale(6, RoundingMode.HALF_UP);
      } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
        value =
            ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, sortOrder)))
                    .multiply(QueryConstants.BD_MILLIS_IN_DAY))
                .setScale(6, RoundingMode.HALF_UP);
      } else {
        value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, sortOrder));
      }
      finalResult = finalResult.add(value);
    }
    Timestamp ts = DateUtil.getTimestamp(finalResult);
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    PTimestamp.INSTANCE.toBytes(ts, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
  }
Example #5
0
 public static String toString(PDataType type, byte[] value) {
   boolean isString = type.isCoercibleTo(PVarchar.INSTANCE);
   return isString
       ? ("'" + type.toObject(value).toString() + "'")
       : type.toObject(value).toString();
 }
 private void checkTypeMatch(PDataType expectedType, PDataType actualType) throws SQLException {
   if (!expectedType.isCoercibleTo(actualType)) {
     throw TypeMismatchException.newException(expectedType, actualType);
   }
 }