Search in sources :

Example 1 with InvalidTypeException

use of org.apache.ignite.internal.schema.InvalidTypeException in project ignite-3 by apache.

the class SchemaSerializerImpl method fromByteBuffer.

/**
 * Reads native type from byte buffer.
 *
 * @param buf Byte buffer.
 * @return Native type.
 */
private NativeType fromByteBuffer(ByteBuffer buf) {
    String nativeTypeSpecName = readString(buf);
    NativeTypeSpec spec = NativeTypeSpec.valueOf(nativeTypeSpecName);
    switch(spec) {
        case STRING:
            int strLen = buf.getInt();
            return NativeTypes.stringOf(strLen);
        case BYTES:
            int len = buf.getInt();
            return NativeTypes.blobOf(len);
        case BITMASK:
            int bits = buf.getInt();
            return NativeTypes.bitmaskOf(bits);
        case DECIMAL:
            {
                int precision = buf.getInt();
                int scale = buf.getInt();
                return NativeTypes.decimalOf(precision, scale);
            }
        case TIME:
            {
                int precision = buf.getInt();
                return NativeTypes.time(precision);
            }
        case DATETIME:
            {
                int precision = buf.getInt();
                return NativeTypes.datetime(precision);
            }
        case TIMESTAMP:
            {
                int precision = buf.getInt();
                return NativeTypes.timestamp(precision);
            }
        case NUMBER:
            {
                int precision = buf.getInt();
                return NativeTypes.numberOf(precision);
            }
        case INT8:
            return NativeTypes.INT8;
        case INT16:
            return NativeTypes.INT16;
        case INT32:
            return NativeTypes.INT32;
        case INT64:
            return NativeTypes.INT64;
        case FLOAT:
            return NativeTypes.FLOAT;
        case DOUBLE:
            return NativeTypes.DOUBLE;
        case UUID:
            return NativeTypes.UUID;
        case DATE:
            return NativeTypes.DATE;
        default:
            throw new InvalidTypeException("Unexpected type " + spec);
    }
}
Also used : NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) InvalidTypeException(org.apache.ignite.internal.schema.InvalidTypeException)

Example 2 with InvalidTypeException

use of org.apache.ignite.internal.schema.InvalidTypeException in project ignite-3 by apache.

the class Row method findColumn.

/**
 * Gets the column offset and length encoded into a single 8-byte value (4 least significant bytes encoding the offset from the
 * beginning of the row and 4 most significant bytes encoding the field length for varlength columns). The offset and length should be
 * extracted using {@link #offset(long)} and {@link #length(long)} methods. Will also validate that the actual column type matches the
 * requested column type, throwing {@link InvalidTypeException} if the types do not match.
 *
 * @param colIdx Column index.
 * @param type   Expected column type.
 * @return {@code -1} if value is {@code null} for a column, otherwise encoded offset + length of the column.
 * @see #offset(long)
 * @see #length(long)
 * @see InvalidTypeException If actual column type does not match the requested column type.
 */
protected long findColumn(int colIdx, NativeTypeSpec type) throws InvalidTypeException {
    // Get base offset (key start or value start) for the given column.
    boolean isKeyCol = schema.isKeyColumn(colIdx);
    Columns cols;
    int chunkBaseOff;
    int flags;
    if (isKeyCol) {
        cols = schema.keyColumns();
        chunkBaseOff = KEY_CHUNK_OFFSET;
        flags = keyFlags();
    } else {
        // Adjust the column index according to the number of key columns.
        if (!hasValue()) {
            throw new IllegalStateException("Row has no value.");
        }
        colIdx -= schema.keyColumns().length();
        cols = schema.valueColumns();
        chunkBaseOff = KEY_CHUNK_OFFSET + readInteger(KEY_CHUNK_OFFSET);
        flags = valueFlags();
    }
    if (cols.column(colIdx).type().spec() != type) {
        throw new InvalidTypeException("Invalid column type requested [requested=" + type + ", column=" + cols.column(colIdx) + ']');
    }
    int nullMapLen = cols.nullMapSize();
    VarTableFormat format = VarTableFormat.fromFlags(flags);
    if (nullMapLen > 0 && isNull(chunkBaseOff, colIdx)) {
        return -1;
    }
    int dataOffset = varTableOffset(chunkBaseOff, nullMapLen);
    dataOffset += format.vartableLength(format.readVartableSize(row, dataOffset));
    return type.fixedLength() ? fixedSizeColumnOffset(chunkBaseOff, dataOffset, cols, colIdx, nullMapLen > 0) : varlenColumnOffsetAndLength(chunkBaseOff, dataOffset, cols, colIdx, nullMapLen, format);
}
Also used : Columns(org.apache.ignite.internal.schema.Columns) InvalidTypeException(org.apache.ignite.internal.schema.InvalidTypeException)

Aggregations

InvalidTypeException (org.apache.ignite.internal.schema.InvalidTypeException)2 Columns (org.apache.ignite.internal.schema.Columns)1 NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)1