use of org.apache.ignite.internal.schema.Column in project ignite-3 by apache.
the class AbstractSerializerTest method columnOrderSerializeTest.
/**
* (de)Serialize schema test.
*/
@Test
public void columnOrderSerializeTest() {
AbstractSchemaSerializer assembler = SchemaSerializerImpl.INSTANCE;
Column[] keyCols = { new Column(0, "A", NativeTypes.UUID, false), new Column(1, "B", NativeTypes.INT64, false), new Column(2, "C", NativeTypes.INT8, false) };
Column[] valCols = { new Column(3, "A1", NativeTypes.stringOf(128), false), new Column(4, "B1", NativeTypes.INT64, false) };
SchemaDescriptor desc = new SchemaDescriptor(100500, keyCols, valCols);
byte[] serialize = assembler.serialize(desc);
SchemaDescriptor deserialize = assembler.deserialize(serialize);
assertEquals(desc.version(), deserialize.version());
ArrayList<Column> columns = new ArrayList<>();
Collections.addAll(columns, keyCols);
Collections.addAll(columns, valCols);
for (int i = 0; i < columns.size(); i++) {
Column col = deserialize.column(i);
assertEquals(columns.get(col.columnOrder()), col);
}
assertArrayEquals(columns.stream().map(Column::name).toArray(String[]::new), desc.columnNames().toArray(String[]::new));
}
use of org.apache.ignite.internal.schema.Column in project ignite-3 by apache.
the class AbstractSerializerTest method columnMappingSerializeTest.
/**
* (de)Serialize column mapping test.
*/
@Test
public void columnMappingSerializeTest() {
final AbstractSchemaSerializer assembler = SchemaSerializerImpl.INSTANCE;
SchemaDescriptor desc = new SchemaDescriptor(100500, new Column[] { new Column("A", NativeTypes.INT8, false, () -> (byte) 1) }, new Column[] { new Column("A1", NativeTypes.stringOf(128), false, () -> "test"), new Column("B1", NativeTypes.numberOf(255), false, () -> BigInteger.TEN) });
ColumnMapper mapper = ColumnMapping.createMapper(desc);
mapper.add(0, 1);
Column c1 = new Column("C1", NativeTypes.stringOf(128), false, () -> "brandNewColumn").copy(2);
mapper.add(c1);
desc.columnMapping(mapper);
byte[] serialize = assembler.serialize(desc);
SchemaDescriptor deserialize = assembler.deserialize(serialize);
ColumnMapper mapper1 = deserialize.columnMapping();
assertEquals(1, mapper1.map(0));
assertEquals(c1, mapper1.mappedColumn(2));
}
use of org.apache.ignite.internal.schema.Column in project ignite-3 by apache.
the class BinaryRowComparator method columnComparator.
/**
* Creates a comparator for comparing table columns.
*/
private static Comparator<Row> columnComparator(Column column) {
int schemaIndex = column.schemaIndex();
NativeTypeSpec typeSpec = column.type().spec();
switch(typeSpec) {
case INT8:
return (row1, row2) -> {
byte value1 = row1.byteValue(schemaIndex);
byte value2 = row2.byteValue(schemaIndex);
return Byte.compare(value1, value2);
};
case INT16:
return (row1, row2) -> {
short value1 = row1.shortValue(schemaIndex);
short value2 = row2.shortValue(schemaIndex);
return Short.compare(value1, value2);
};
case INT32:
return comparingInt(row -> row.intValue(schemaIndex));
case INT64:
return comparingLong(row -> row.longValue(schemaIndex));
case FLOAT:
return (row1, row2) -> {
float value1 = row1.floatValue(schemaIndex);
float value2 = row2.floatValue(schemaIndex);
return Float.compare(value1, value2);
};
case DOUBLE:
return comparingDouble(row -> row.doubleValue(schemaIndex));
case BYTES:
return comparing(row -> row.bytesValue(schemaIndex), Arrays::compare);
case BITMASK:
return comparing(row -> row.bitmaskValue(schemaIndex).toLongArray(), Arrays::compare);
// all other types implement Comparable
case DECIMAL:
case UUID:
case STRING:
case NUMBER:
case TIMESTAMP:
case DATE:
case TIME:
case DATETIME:
return comparing(row -> (Comparable) typeSpec.objectValue(row, schemaIndex));
default:
throw new IllegalArgumentException(String.format("Unsupported column schema for creating a sorted index. Column name: %s, column type: %s", column.name(), column.type()));
}
}
use of org.apache.ignite.internal.schema.Column in project ignite-3 by apache.
the class BinaryRowComparator method rowComparator.
/**
* Creates a comparator that compares two {@link Row}s by comparing individual columns.
*/
private static Comparator<Row> rowComparator(SortedIndexDescriptor descriptor) {
return descriptor.indexRowColumns().stream().map(columnDescriptor -> {
Column column = columnDescriptor.column();
Comparator<Row> columnComparator = columnComparator(column);
if (columnDescriptor.nullable()) {
columnComparator = comparingNull(row -> row.hasNullValue(column.schemaIndex(), column.type().spec()) ? null : row, columnComparator);
}
return columnDescriptor.asc() ? columnComparator : columnComparator.reversed();
}).reduce(Comparator::thenComparing).orElseThrow();
}
use of org.apache.ignite.internal.schema.Column 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);
}
Aggregations