Search in sources :

Example 1 with SchemaMismatchException

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

the class TupleMarshallerImpl method toInternalTuple.

/**
 * Analyze tuple and wrap into internal tuple.
 *
 * @param schema  Schema.
 * @param tuple   Key or value tuple.
 * @param keyFlag If {@code true} marshal key columns, otherwise marshall value columns.
 * @return Internal tuple.
 * @throws SchemaMismatchException If tuple doesn't match the schema.
 */
@NotNull
private InternalTuple toInternalTuple(SchemaDescriptor schema, Tuple tuple, boolean keyFlag) throws SchemaMismatchException {
    if (tuple == null) {
        return InternalTuple.NO_VALUE;
    }
    Columns columns = keyFlag ? schema.keyColumns() : schema.valueColumns();
    int nonNullVarlen = 0;
    int nonNullVarlenSize = 0;
    int knownColumns = 0;
    Map<String, Object> defaults = new HashMap<>();
    if (tuple instanceof SchemaAware && Objects.equals(((SchemaAware) tuple).schema(), schema)) {
        for (int i = 0, len = columns.length(); i < len; i++) {
            final Column col = columns.column(i);
            Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
            assert val != POISON_OBJECT;
            if (val == null || columns.firstVarlengthColumn() < i) {
                continue;
            }
            nonNullVarlenSize += getValueSize(val, col.type());
            nonNullVarlen++;
        }
    } else {
        for (int i = 0, len = columns.length(); i < len; i++) {
            final Column col = columns.column(i);
            Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
            if (val == POISON_OBJECT) {
                if (keyFlag) {
                    throw new SchemaMismatchException("Missed key column: " + col.name());
                }
                val = col.defaultValue();
                defaults.put(col.name(), val);
            } else {
                knownColumns++;
            }
            col.validate(val);
            if (val == null || columns.isFixedSize(i)) {
                continue;
            }
            nonNullVarlenSize += getValueSize(val, col.type());
            nonNullVarlen++;
        }
    }
    return new InternalTuple(tuple, nonNullVarlen, nonNullVarlenSize, defaults, knownColumns);
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) HashMap(java.util.HashMap) Column(org.apache.ignite.internal.schema.Column) Columns(org.apache.ignite.internal.schema.Columns) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SchemaMismatchException

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

the class RowAssembler method appendNumber.

/**
 * Appends BigInteger value for the current column to the chunk.
 *
 * @param val Column value.
 * @return {@code this} for chaining.
 * @throws SchemaMismatchException If a value doesn't match the current column type.
 */
public RowAssembler appendNumber(BigInteger val) throws SchemaMismatchException {
    checkType(NativeTypeSpec.NUMBER);
    Column col = curCols.column(curCol);
    NumberNativeType type = (NumberNativeType) col.type();
    // 0 is a magic number for "unlimited precision"
    if (type.precision() > 0 && new BigDecimal(val).precision() > type.precision()) {
        throw new SchemaMismatchException("Failed to set number value for column '" + col.name() + "' " + "(max precision exceeds allocated precision) " + "[number=" + val + ", max precision=" + type.precision() + "]");
    }
    byte[] bytes = val.toByteArray();
    buf.putBytes(curOff, bytes);
    writeVarlenOffset(curVartblEntry, curOff - dataOff);
    curVartblEntry++;
    shiftColumn(bytes.length);
    return this;
}
Also used : SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) Column(org.apache.ignite.internal.schema.Column) NumberNativeType(org.apache.ignite.internal.schema.NumberNativeType) BigDecimal(java.math.BigDecimal)

Example 3 with SchemaMismatchException

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

the class ColumnBinding method createIdentityBinding.

/**
 * Binds a column with an object of given type.
 *
 * @param col       Column.
 * @param type      Object type.
 * @param converter Type converter or {@code null}.
 * @return Column to object binding.
 */
@NotNull
static ColumnBinding createIdentityBinding(Column col, Class<?> type, @Nullable TypeConverter<?, ?> converter) {
    final BinaryMode mode = MarshallerUtil.mode(type);
    if (mode.typeSpec() != col.type().spec()) {
        throw new SchemaMismatchException(String.format("Object can't be mapped to a column of incompatible type: columnType=%s, mappedType=%s", col.type().spec(), type.getName()));
    }
    final MethodHandle identityHandle = MethodHandles.identity(type);
    return create(col, type, identityHandle, identityHandle, converter);
}
Also used : BinaryMode(org.apache.ignite.internal.schema.marshaller.BinaryMode) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) MethodHandle(java.lang.invoke.MethodHandle) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with SchemaMismatchException

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

the class Marshaller method simpleMarshaller.

/**
 * Creates a marshaller for class.
 *
 * @param cols   Columns.
 * @param mapper Mapper.
 * @return Marshaller.
 */
static <T> SimpleMarshaller simpleMarshaller(Column[] cols, @NotNull OneColumnMapper<T> mapper) {
    final Class<T> targetType = mapper.targetType();
    Column col = (mapper.mappedColumn() == null && cols.length == 1) ? cols[0] : Arrays.stream(cols).filter(c -> c.name().equals(mapper.mappedColumn())).findFirst().orElseThrow(() -> new SchemaMismatchException("Failed to map object to a single column:" + mapper.mappedColumn()));
    assert !targetType.isPrimitive() : "Non-nullable types are not allowed.";
    return new SimpleMarshaller(ColumnBinding.createIdentityBinding(col, targetType, mapper.converter()));
}
Also used : OneColumnMapper(org.apache.ignite.table.mapper.OneColumnMapper) Arrays(java.util.Arrays) MarshallerException(org.apache.ignite.internal.schema.marshaller.MarshallerException) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) Factory(org.apache.ignite.internal.util.Factory) Objects(java.util.Objects) PojoMapper(org.apache.ignite.table.mapper.PojoMapper) Nullable(org.jetbrains.annotations.Nullable) ObjectFactory(org.apache.ignite.internal.util.ObjectFactory) Row(org.apache.ignite.internal.schema.row.Row) Column(org.apache.ignite.internal.schema.Column) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) Mapper(org.apache.ignite.table.mapper.Mapper) NotNull(org.jetbrains.annotations.NotNull) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) Column(org.apache.ignite.internal.schema.Column)

Example 5 with SchemaMismatchException

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

the class TupleMarshallerImpl method marshal.

/**
 * {@inheritDoc}
 */
@Override
public Row marshal(@NotNull Tuple tuple) throws TupleMarshallerException {
    try {
        SchemaDescriptor schema = schemaReg.schema();
        InternalTuple keyTuple0 = toInternalTuple(schema, tuple, true);
        InternalTuple valTuple0 = toInternalTuple(schema, tuple, false);
        if (valTuple0.knownColumns() + keyTuple0.knownColumns() != tuple.columnCount()) {
            throw new SchemaMismatchException(String.format("Tuple doesn't match schema: schemaVersion=%s, extraColumns=%s", schema.version(), extraColumnNames(tuple, schema)));
        }
        return buildRow(schema, keyTuple0, valTuple0);
    } catch (Exception ex) {
        throw new TupleMarshallerException("Failed to marshal tuple.", ex);
    }
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException)

Aggregations

SchemaMismatchException (org.apache.ignite.internal.schema.SchemaMismatchException)7 Column (org.apache.ignite.internal.schema.Column)5 NotNull (org.jetbrains.annotations.NotNull)3 Columns (org.apache.ignite.internal.schema.Columns)2 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)2 Row (org.apache.ignite.internal.schema.row.Row)2 RowAssembler (org.apache.ignite.internal.schema.row.RowAssembler)2 MethodHandle (java.lang.invoke.MethodHandle)1 BigDecimal (java.math.BigDecimal)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 DecimalNativeType (org.apache.ignite.internal.schema.DecimalNativeType)1 NumberNativeType (org.apache.ignite.internal.schema.NumberNativeType)1 SchemaAware (org.apache.ignite.internal.schema.SchemaAware)1 BinaryMode (org.apache.ignite.internal.schema.marshaller.BinaryMode)1 MarshallerException (org.apache.ignite.internal.schema.marshaller.MarshallerException)1 Factory (org.apache.ignite.internal.util.Factory)1 ObjectFactory (org.apache.ignite.internal.util.ObjectFactory)1 Mapper (org.apache.ignite.table.mapper.Mapper)1