Search in sources :

Example 6 with ColumnMapper

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

the class UpgradingRowAdapterTest method testVariousColumnTypes.

@Test
public void testVariousColumnTypes() {
    SchemaDescriptor schema = new SchemaDescriptor(1, new Column[] { new Column("keyUuidCol", NativeTypes.UUID, false) }, new Column[] { new Column("valByteCol", INT8, true), new Column("valShortCol", INT16, true), new Column("valIntCol", INT32, true), new Column("valLongCol", INT64, true), new Column("valFloatCol", FLOAT, true), new Column("valDoubleCol", DOUBLE, true), new Column("valDateCol", DATE, true), new Column("valTimeCol", time(), true), new Column("valDateTimeCol", datetime(), true), new Column("valTimeStampCol", timestamp(), true), new Column("valBitmask1Col", NativeTypes.bitmaskOf(22), true), new Column("valBytesCol", BYTES, false), new Column("valStringCol", STRING, false), new Column("valNumberCol", NativeTypes.numberOf(20), false), new Column("valDecimalCol", NativeTypes.decimalOf(25, 5), false) });
    SchemaDescriptor schema2 = new SchemaDescriptor(2, new Column[] { new Column("keyUuidCol", NativeTypes.UUID, false) }, new Column[] { new Column("added", INT8, true), new Column("valByteCol", INT8, true), new Column("valShortCol", INT16, true), new Column("valIntCol", INT32, true), new Column("valLongCol", INT64, true), new Column("valFloatCol", FLOAT, true), new Column("valDoubleCol", DOUBLE, true), new Column("valDateCol", DATE, true), new Column("valTimeCol", time(), true), new Column("valDateTimeCol", datetime(), true), new Column("valTimeStampCol", timestamp(), true), new Column("valBitmask1Col", NativeTypes.bitmaskOf(22), true), new Column("valBytesCol", BYTES, false), new Column("valStringCol", STRING, false), new Column("valNumberCol", NativeTypes.numberOf(20), false), new Column("valDecimalCol", NativeTypes.decimalOf(25, 5), false) });
    int addedColumnIndex = schema2.column("added").schemaIndex();
    schema2.columnMapping(new ColumnMapper() {

        @Override
        public ColumnMapper add(@NotNull Column col) {
            return null;
        }

        @Override
        public ColumnMapper add(int from, int to) {
            return null;
        }

        @Override
        public int map(int idx) {
            return idx < addedColumnIndex ? idx : idx == addedColumnIndex ? -1 : idx - 1;
        }

        @Override
        public Column mappedColumn(int idx) {
            return idx == addedColumnIndex ? schema2.column(idx) : null;
        }
    });
    List<Object> values = generateRowValues(schema);
    ByteBufferRow row = new ByteBufferRow(serializeValuesToRow(schema, values));
    // Validate row.
    validateRow(values, new SchemaRegistryImpl(1, v -> v == 1 ? schema : schema2, () -> INITIAL_SCHEMA_VERSION), row);
    // Validate upgraded row.
    values.add(addedColumnIndex, null);
    validateRow(values, new SchemaRegistryImpl(2, v -> v == 1 ? schema : schema2, () -> INITIAL_SCHEMA_VERSION), row);
}
Also used : DATE(org.apache.ignite.internal.schema.NativeTypes.DATE) BeforeEach(org.junit.jupiter.api.BeforeEach) LocalDateTime(java.time.LocalDateTime) Random(java.util.Random) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ArrayList(java.util.ArrayList) DOUBLE(org.apache.ignite.internal.schema.NativeTypes.DOUBLE) BigDecimal(java.math.BigDecimal) INT64(org.apache.ignite.internal.schema.NativeTypes.INT64) Row(org.apache.ignite.internal.schema.row.Row) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) INITIAL_SCHEMA_VERSION(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl.INITIAL_SCHEMA_VERSION) INT8(org.apache.ignite.internal.schema.NativeTypes.INT8) LocalTime(java.time.LocalTime) BigInteger(java.math.BigInteger) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NativeType(org.apache.ignite.internal.schema.NativeType) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) FLOAT(org.apache.ignite.internal.schema.NativeTypes.FLOAT) NativeTypes.datetime(org.apache.ignite.internal.schema.NativeTypes.datetime) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) NativeTypes.timestamp(org.apache.ignite.internal.schema.NativeTypes.timestamp) SchemaTestUtils(org.apache.ignite.internal.schema.SchemaTestUtils) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) Instant(java.time.Instant) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Test(org.junit.jupiter.api.Test) BYTES(org.apache.ignite.internal.schema.NativeTypes.BYTES) List(java.util.List) NativeTypes(org.apache.ignite.internal.schema.NativeTypes) STRING(org.apache.ignite.internal.schema.NativeTypes.STRING) Column(org.apache.ignite.internal.schema.Column) LocalDate(java.time.LocalDate) NativeTypes.time(org.apache.ignite.internal.schema.NativeTypes.time) INT16(org.apache.ignite.internal.schema.NativeTypes.INT16) BitSet(java.util.BitSet) INT32(org.apache.ignite.internal.schema.NativeTypes.INT32) NotNull(org.jetbrains.annotations.NotNull) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) Test(org.junit.jupiter.api.Test)

Example 7 with ColumnMapper

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

the class SchemaRegistryImpl method resolveInternal.

/**
 * Resolves a schema for row.
 * The method is optimal when the latest schema is already gotten.
 *
 * @param row       Binary row.
 * @param curSchema The latest available local schema.
 * @return Schema-aware rows.
 */
@Nullable
private Row resolveInternal(BinaryRow row, SchemaDescriptor curSchema) {
    if (row == null) {
        return null;
    }
    final SchemaDescriptor rowSchema = schema(row.schemaVersion());
    if (curSchema.version() == rowSchema.version()) {
        return new Row(rowSchema, row);
    }
    ColumnMapper mapping = resolveMapping(curSchema, rowSchema);
    return new UpgradingRowAdapter(curSchema, rowSchema, row, mapping);
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with ColumnMapper

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

the class SchemaRegistryImpl method resolveMapping.

/**
 * ResolveMapping.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 *
 * @param curSchema Target schema.
 * @param rowSchema Row schema.
 * @return Column mapper for target schema.
 */
ColumnMapper resolveMapping(SchemaDescriptor curSchema, SchemaDescriptor rowSchema) {
    assert curSchema.version() > rowSchema.version();
    if (curSchema.version() == rowSchema.version() + 1) {
        return curSchema.columnMapping();
    }
    final long mappingKey = (((long) curSchema.version()) << 32) | (rowSchema.version());
    ColumnMapper mapping;
    if ((mapping = mappingCache.get(mappingKey)) != null) {
        return mapping;
    }
    mapping = schema(rowSchema.version() + 1).columnMapping();
    for (int i = rowSchema.version() + 2; i <= curSchema.version(); i++) {
        mapping = ColumnMapping.mergeMapping(mapping, schema(i));
    }
    mappingCache.putIfAbsent(mappingKey, mapping);
    return mapping;
}
Also used : ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper)

Aggregations

ColumnMapper (org.apache.ignite.internal.schema.mapping.ColumnMapper)8 Column (org.apache.ignite.internal.schema.Column)5 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)5 Test (org.junit.jupiter.api.Test)3 Row (org.apache.ignite.internal.schema.row.Row)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Random (java.util.Random)1 NamedListView (org.apache.ignite.configuration.NamedListView)1 ColumnView (org.apache.ignite.configuration.schemas.table.ColumnView)1 TableChange (org.apache.ignite.configuration.schemas.table.TableChange)1 TableView (org.apache.ignite.configuration.schemas.table.TableView)1