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);
}
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;
}
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);
}
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()));
}
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);
}
}
Aggregations