/** * @return true when {@code lhs} equals any of {@code rhs}. */ public static boolean equalsAny(PDataType lhs, PDataType... rhs) { for (int i = 0; i < rhs.length; i++) { if (lhs.equals(rhs[i])) return true; } return false; }
@Override public boolean isCoercibleTo(PDataType targetType, Object value) { if (value != null) { float f = (Float) value; if (targetType.equals(PUnsignedFloat.INSTANCE)) { return f >= 0; } else if (targetType.equals(PUnsignedLong.INSTANCE)) { return (f >= 0 && f <= Long.MAX_VALUE); } else if (targetType.equals(PLong.INSTANCE)) { return (f >= Long.MIN_VALUE && f <= Long.MAX_VALUE); } else if (targetType.equals(PUnsignedInt.INSTANCE)) { return (f >= 0 && f <= Integer.MAX_VALUE); } else if (targetType.equals(PInteger.INSTANCE)) { return (f >= Integer.MIN_VALUE && f <= Integer.MAX_VALUE); } else if (targetType.equals(PUnsignedSmallint.INSTANCE)) { return (f >= 0 && f <= Short.MAX_VALUE); } else if (targetType.equals(PSmallint.INSTANCE)) { return (f >= Short.MIN_VALUE && f <= Short.MAX_VALUE); } else if (targetType.equals(PTinyint.INSTANCE)) { return (f >= Byte.MIN_VALUE && f <= Byte.MAX_VALUE); } else if (targetType.equals(PUnsignedTinyint.INSTANCE)) { return (f >= 0 && f <= Byte.MAX_VALUE); } } return super.isCoercibleTo(targetType, value); }
@Override public boolean isCoercibleTo(PDataType targetType, Object value) { if (isCoercibleTo(targetType)) { if (targetType.equals(PChar.INSTANCE)) { return value != null; } return true; } return false; }
@Override public boolean isCoercibleTo(PDataType targetType) { return targetType.equals(this) || PUnsignedDate.INSTANCE.isCoercibleTo(targetType); }
@Override public boolean isCastableTo(PDataType targetType) { // Allow cast to BOOLEAN so it can be used in an index or group by return super.isCastableTo(targetType) || targetType.equals(PDecimal.INSTANCE); }
@Override public boolean isCoercibleTo(PDataType targetType, Object value) { if (value != null) { int i; if (equalsAny(targetType, PUnsignedDouble.INSTANCE, PUnsignedFloat.INSTANCE, PUnsignedLong.INSTANCE, PUnsignedInt.INSTANCE)) { i = (Integer) value; return i >= 0; } else if (targetType.equals(PUnsignedSmallint.INSTANCE)) { i = (Integer) value; return (i >= 0 && i <= Short.MAX_VALUE); } else if (targetType.equals(PSmallint.INSTANCE)) { i = (Integer) value; return (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE); } else if (targetType.equals(PTinyint.INSTANCE)) { i = (Integer) value; return (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE); } else if (targetType.equals(PUnsignedTinyint.INSTANCE)) { i = (Integer) value; return (i >= 0 && i <= Byte.MAX_VALUE); } } return super.isCoercibleTo(targetType, value); }
@Override public boolean isBytesComparableWith(PDataType otherType) { return super.isBytesComparableWith(otherType) || otherType.equals(PVarchar.INSTANCE); }
@Override public boolean isCoercibleTo(PDataType targetType) { return super.isCoercibleTo(targetType) || targetType.equals(PBinary.INSTANCE); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; CoerceExpression other = (CoerceExpression)obj; if (maxLength == null) { if (other.maxLength != null) return false; } else if (!maxLength.equals(other.maxLength)) return false; if (toSortOrder != other.toSortOrder) return false; if (toType == null) { if (other.toType != null) return false; } else if (!toType.equals(other.toType)) return false; return rowKeyOrderOptimizable == other.rowKeyOrderOptimizable; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; CastParseNode other = (CastParseNode) obj; if (dt == null) { if (other.dt != null) return false; } else if (!dt.equals(other.dt)) return false; if (maxLength == null) { if (other.maxLength != null) return false; } else if (!maxLength.equals(other.maxLength)) return false; if (scale == null) { if (other.scale != null) return false; } else if (!scale.equals(other.scale)) return false; return true; }
@Override public boolean isCoercibleTo(PDataType targetType, Object value) { if (value != null) { if (targetType.equals(PUnsignedTimestamp.INSTANCE)) { return ((java.util.Date) value).getTime() >= 0; } else if (equalsAny(targetType, PUnsignedDate.INSTANCE, PUnsignedTime.INSTANCE)) { return ((java.util.Date) value).getTime() >= 0 && ((java.sql.Timestamp) value).getNanos() == 0; } else if (equalsAny(targetType, PDate.INSTANCE, PTime.INSTANCE)) { return ((java.sql.Timestamp) value).getNanos() == 0; } } return super.isCoercibleTo(targetType, value); }
private Long getViewIndexValue(PDataType type, byte[] range) { boolean useLongViewIndex = MetaDataUtil.getViewIndexIdDataType().equals(type); Object s = type.toObject(range); return (useLongViewIndex ? (Long) s : (Short) s) - (useLongViewIndex ? Long.MAX_VALUE : Short.MAX_VALUE); }
@Override public boolean isCoercibleTo(PDataType targetType) { return targetType.equals(this) || PUnsignedSmallint.INSTANCE.isCoercibleTo(targetType) || PTinyint.INSTANCE.isCoercibleTo(targetType); }
@Override public boolean isCastableTo(PDataType targetType) { return super.isCastableTo(targetType) || targetType.isCoercibleTo( PTimestamp.INSTANCE) || targetType.equals(PBoolean.INSTANCE); }
@Override public boolean isCoercibleTo(PDataType targetType) { return targetType.equals(this) || PUnsignedInt.INSTANCE.isCoercibleTo(targetType) || PSmallint.INSTANCE .isCoercibleTo(targetType); }
@Override public void coerceBytes(ImmutableBytesWritable ptr, Object o, PDataType actualType, Integer actualMaxLength, Integer actualScale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale, SortOrder expectedModifier) { if (o != null && actualType.equals(PVarchar.INSTANCE) && ((String)o).length() != ptr.getLength()) { throw newIllegalDataException("CHAR types may only contain single byte characters (" + o + ")"); } super.coerceBytes(ptr, o, actualType, actualMaxLength, actualScale, actualModifier, desiredMaxLength, desiredScale, expectedModifier); if (ptr.getLength() > 0 && desiredMaxLength != null && desiredMaxLength > ptr.getLength()) { pad(ptr, desiredMaxLength, expectedModifier); } }
private void assertValueAtIndex(ImmutableBytesPtr ptr, int index, Object value, PDataType type) { ImmutableBytesPtr ptrCopy = new ImmutableBytesPtr(ptr); ColumnValueDecoder decoder = immutableStorageScheme.getDecoder(); assertTrue(decoder.decode(ptrCopy, index)); if (value == null) { assertArrayEquals(EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary()); return; } Object decoded; if (type.equals(PSmallint.INSTANCE)) { decoded = type.getCodec().decodeShort(ptrCopy.copyBytesIfNecessary(), 0, SortOrder.ASC); } else if (type.equals(PInteger.INSTANCE)) { decoded = type.getCodec().decodeInt(ptrCopy.copyBytesIfNecessary(), 0, SortOrder.ASC); } else { // assume byte for all other types decoded = type.getCodec().decodeByte(ptrCopy.copyBytesIfNecessary(), 0, SortOrder.ASC); } assertEquals(value, decoded); }
@Override protected boolean modifierFunction(ImmutableBytesWritable ptr, int length, int offset, byte[] arrayBytes, PDataType baseType, int arrayLength, Integer maxLength, Expression arrayExp) { SortOrder sortOrder = arrayExp.getSortOrder(); if (ptr.getLength() == 0 || arrayBytes.length == 0) { ptr.set(arrayBytes, offset, length); return true; } PArrayDataTypeEncoder arrayDataTypeEncoder = new PArrayDataTypeEncoder(baseType, sortOrder); if (getRHSBaseType().equals(PChar.INSTANCE)) { int unpaddedCharLength = StringUtil.getUnpaddedCharLength(ptr.get(), ptr.getOffset(), ptr.getLength(), sortOrder); ptr.set(ptr.get(), offset, unpaddedCharLength); } for (int arrayIndex = 0; arrayIndex < arrayLength; arrayIndex++) { ImmutableBytesWritable ptr2 = new ImmutableBytesWritable(arrayBytes, offset, length); PArrayDataTypeDecoder.positionAtArrayElement(ptr2, arrayIndex, baseType, maxLength); if (baseType.compareTo(ptr2, sortOrder, ptr, sortOrder, baseType) != 0) { arrayDataTypeEncoder.appendValue(ptr2.get(), ptr2.getOffset(), ptr2.getLength()); } } ptr.set(arrayDataTypeEncoder.encode()); return true; }