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);
 }