Search in sources :

Example 11 with IllegalDataException

use of org.apache.phoenix.schema.IllegalDataException in project phoenix by apache.

the class PTimestamp method getKeyRange.

/**
     * With timestamp, because our last 4 bytes store a value from [0 - 1000000), we need
     * to detect when the boundary is crossed if we increment to the nextKey.
     */
@Override
public KeyRange getKeyRange(byte[] lowerRange, boolean lowerInclusive, byte[] upperRange, boolean upperInclusive) {
    /*
         * Force lower bound to be inclusive for fixed width keys because it makes comparisons less expensive when you
         * can count on one bound or the other being inclusive. Comparing two fixed width exclusive bounds against each
         * other is inherently more expensive, because you need to take into account if the bigger key is equal to the
         * next key after the smaller key. For example: (A-B] compared against [A-B) An exclusive lower bound A is
         * bigger than an exclusive upper bound B. Forcing a fixed width exclusive lower bound key to be inclusive
         * prevents us from having to do this extra logic in the compare function.
         * 
         */
    if (lowerRange != KeyRange.UNBOUND && !lowerInclusive && isFixedWidth()) {
        if (lowerRange.length != MAX_TIMESTAMP_BYTES) {
            throw new IllegalDataException("Unexpected size of " + lowerRange.length + " for " + this);
        }
        // Infer sortOrder based on most significant byte
        SortOrder sortOrder = lowerRange[Bytes.SIZEOF_LONG] < 0 ? SortOrder.DESC : SortOrder.ASC;
        int nanos = PUnsignedInt.INSTANCE.getCodec().decodeInt(lowerRange, Bytes.SIZEOF_LONG, sortOrder);
        if ((sortOrder == SortOrder.DESC && nanos == 0) || (sortOrder == SortOrder.ASC && nanos == MAX_NANOS_VALUE_EXCLUSIVE - 1)) {
            // With timestamp, because our last 4 bytes store a value from [0 - 1000000), we need
            // to detect when the boundary is crossed with our nextKey
            byte[] newLowerRange = new byte[MAX_TIMESTAMP_BYTES];
            if (sortOrder == SortOrder.DESC) {
                // Set nanos part as inverted 999999 as it needs to be the max nano value
                // The millisecond part is moving to the previous value below
                System.arraycopy(lowerRange, 0, newLowerRange, 0, Bytes.SIZEOF_LONG);
                PUnsignedInt.INSTANCE.getCodec().encodeInt(MAX_NANOS_VALUE_EXCLUSIVE - 1, newLowerRange, Bytes.SIZEOF_LONG);
                SortOrder.invert(newLowerRange, Bytes.SIZEOF_LONG, newLowerRange, Bytes.SIZEOF_LONG, Bytes.SIZEOF_INT);
            } else {
                // Leave nanos part as zero as the millisecond part is rolling over to the next value
                System.arraycopy(lowerRange, 0, newLowerRange, 0, Bytes.SIZEOF_LONG);
            }
            // Increment millisecond part, but leave nanos alone
            if (ByteUtil.nextKey(newLowerRange, Bytes.SIZEOF_LONG)) {
                lowerRange = newLowerRange;
            } else {
                lowerRange = KeyRange.UNBOUND;
            }
            return KeyRange.getKeyRange(lowerRange, true, upperRange, upperInclusive);
        }
    }
    return super.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive);
}
Also used : SortOrder(org.apache.phoenix.schema.SortOrder) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Example 12 with IllegalDataException

use of org.apache.phoenix.schema.IllegalDataException in project phoenix by apache.

the class LiteralExpression method newConstant.

// 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);
    try {
        value = type.toObject(value, actualType);
    } catch (IllegalDataException e) {
        throw TypeMismatchException.newException(type, actualType, value.toString());
    }
    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);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PBoolean(org.apache.phoenix.schema.types.PBoolean) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Aggregations

IllegalDataException (org.apache.phoenix.schema.IllegalDataException)12 Expression (org.apache.phoenix.expression.Expression)5 PDataType (org.apache.phoenix.schema.types.PDataType)4 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)3 StringReader (java.io.StringReader)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Iterator (java.util.Iterator)1 List (java.util.List)1 CSVParser (org.apache.commons.csv.CSVParser)1 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 Pair (org.apache.hadoop.hbase.util.Pair)1 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)1 RowMutationState (org.apache.phoenix.execute.MutationState.RowMutationState)1 RowTimestampColInfo (org.apache.phoenix.execute.MutationState.RowTimestampColInfo)1 ProjectedValueTuple (org.apache.phoenix.execute.TupleProjector.ProjectedValueTuple)1 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)1 Hint (org.apache.phoenix.parse.HintNode.Hint)1