Search in sources :

Example 1 with NULL_VALUE

use of org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.NULL_VALUE in project ignite by apache.

the class QueryTypeDescriptorImpl method validateProps.

/**
 * Validate properties.
 */
private void validateProps(Object key, Object val) throws IgniteCheckedException {
    if (F.isEmpty(validateProps))
        return;
    final boolean validateTypes = coCtx.kernalContext().config().getSqlConfiguration().isValidationEnabled();
    for (int i = 0; i < validateProps.size(); ++i) {
        GridQueryProperty prop = validateProps.get(i);
        Object propVal;
        boolean isKey = false;
        if (F.eq(prop.name(), keyFieldAlias()) || (keyFieldName == null && F.eq(prop.name(), KEY_FIELD_NAME))) {
            propVal = key instanceof KeyCacheObject ? ((CacheObject) key).value(coCtx, true) : key;
            isKey = true;
        } else if (F.eq(prop.name(), valueFieldAlias()) || (valFieldName == null && F.eq(prop.name(), VAL_FIELD_NAME)))
            propVal = val instanceof CacheObject ? ((CacheObject) val).value(coCtx, true) : val;
        else
            propVal = prop.value(key, val);
        if (propVal == null && prop.notNull()) {
            throw new IgniteSQLException("Null value is not allowed for column '" + prop.name() + "'", isKey ? NULL_KEY : NULL_VALUE);
        }
        if (validateTypes && propVal != null) {
            if (!(propVal instanceof BinaryObject) || propVal instanceof BinaryArray) {
                if (!U.box(prop.type()).isAssignableFrom(U.box(propVal.getClass()))) {
                    // Some reference type arrays end up being converted to Object[]
                    if (!(prop.type().isArray() && BinaryUtils.isObjectArray(propVal.getClass()) && Arrays.stream(BinaryUtils.rawArrayFromBinary(propVal)).noneMatch(x -> x != null && !U.box(prop.type().getComponentType()).isAssignableFrom(U.box(x.getClass()))))) {
                        throw new IgniteSQLException("Type for a column '" + prop.name() + "' is not compatible with table definition. Expected '" + prop.type().getSimpleName() + "', actual type '" + propVal.getClass().getSimpleName() + "'");
                    }
                }
            } else if (coCtx.kernalContext().cacheObjects().typeId(prop.type().getName()) != ((BinaryObject) propVal).type().typeId()) {
                throw new IgniteSQLException("Type for a column '" + prop.name() + "' is not compatible with table definition. Expected '" + prop.type().getSimpleName() + "', actual type '" + ((BinaryObject) propVal).type().typeName() + "'");
            }
        }
        if (propVal == null || prop.precision() == -1)
            continue;
        if (String.class == propVal.getClass() || byte[].class == propVal.getClass()) {
            int propValLen = String.class == propVal.getClass() ? ((String) propVal).length() : ((byte[]) propVal).length;
            if (propValLen > prop.precision()) {
                throw new IgniteSQLException("Value for a column '" + prop.name() + "' is too long. " + "Maximum length: " + prop.precision() + ", actual length: " + propValLen, isKey ? TOO_LONG_KEY : TOO_LONG_VALUE);
            }
        } else if (BigDecimal.class == propVal.getClass()) {
            BigDecimal dec = (BigDecimal) propVal;
            if (dec.precision() > prop.precision()) {
                throw new IgniteSQLException("Value for a column '" + prop.name() + "' is out of range. " + "Maximum precision: " + prop.precision() + ", actual precision: " + dec.precision(), isKey ? TOO_LONG_KEY : TOO_LONG_VALUE);
            } else if (prop.scale() != -1 && dec.scale() > prop.scale()) {
                throw new IgniteSQLException("Value for a column '" + prop.name() + "' is out of range. " + "Maximum scale : " + prop.scale() + ", actual scale: " + dec.scale(), isKey ? KEY_SCALE_OUT_OF_RANGE : VALUE_SCALE_OUT_OF_RANGE);
            }
        }
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) TOO_LONG_KEY(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.TOO_LONG_KEY) Arrays(java.util.Arrays) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) IgniteLogger(org.apache.ignite.IgniteLogger) TOO_LONG_VALUE(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.TOO_LONG_VALUE) VAL_FIELD_NAME(org.apache.ignite.internal.processors.query.QueryUtils.VAL_FIELD_NAME) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) BinaryUtils(org.apache.ignite.internal.binary.BinaryUtils) NULL_VALUE(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.NULL_VALUE) Map(java.util.Map) BinaryArray(org.apache.ignite.internal.binary.BinaryArray) S(org.apache.ignite.internal.util.typedef.internal.S) NULL_KEY(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.NULL_KEY) KEY_FIELD_NAME(org.apache.ignite.internal.processors.query.QueryUtils.KEY_FIELD_NAME) QueryIndexType(org.apache.ignite.cache.QueryIndexType) F(org.apache.ignite.internal.util.typedef.F) A(org.apache.ignite.internal.util.typedef.internal.A) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) KEY_SCALE_OUT_OF_RANGE(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.KEY_SCALE_OUT_OF_RANGE) Set(java.util.Set) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) VALUE_SCALE_OUT_OF_RANGE(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.VALUE_SCALE_OUT_OF_RANGE) GridToStringInclude(org.apache.ignite.internal.util.tostring.GridToStringInclude) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Collections(java.util.Collections) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) BinaryArray(org.apache.ignite.internal.binary.BinaryArray) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) BigDecimal(java.math.BigDecimal) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 QueryIndexType (org.apache.ignite.cache.QueryIndexType)1 BinaryArray (org.apache.ignite.internal.binary.BinaryArray)1 BinaryUtils (org.apache.ignite.internal.binary.BinaryUtils)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 CacheObjectContext (org.apache.ignite.internal.processors.cache.CacheObjectContext)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1 KEY_SCALE_OUT_OF_RANGE (org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.KEY_SCALE_OUT_OF_RANGE)1