Search in sources :

Example 96 with SchemaDescriptor

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

the class SchemaRegistryImplTest method testInitialSchemaWithTailHistory.

/**
 * Check schema registration with history tail.
 */
@Test
public void testInitialSchemaWithTailHistory() {
    final SchemaDescriptor schemaV1 = new SchemaDescriptor(1, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valBytesCol", BYTES, true) });
    final SchemaDescriptor schemaV2 = new SchemaDescriptor(2, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valBytesCol", BYTES, true), new Column("valStringCol", STRING, true) });
    final SchemaDescriptor schemaV3 = new SchemaDescriptor(3, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valStringCol", STRING, true) });
    final SchemaDescriptor schemaV4 = new SchemaDescriptor(4, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valBytesCol", BYTES, true), new Column("valStringCol", STRING, true) });
    final SchemaDescriptor schemaV5 = new SchemaDescriptor(5, new Column[] { new Column("keyLongCol", INT64, false) }, new Column[] { new Column("valStringCol", STRING, true) });
    Map<Integer, SchemaDescriptor> history = schemaHistory(schemaV2, schemaV3);
    final SchemaRegistryImpl reg = new SchemaRegistryImpl(3, history::get, () -> INITIAL_SCHEMA_VERSION);
    assertEquals(3, reg.lastSchemaVersion());
    assertSameSchema(schemaV3, reg.schema());
    assertThrows(SchemaRegistryException.class, () -> reg.schema(1));
    assertSameSchema(schemaV2, reg.schema(2));
    assertSameSchema(schemaV3, reg.schema(3));
    // Register schema with duplicate version.
    assertThrows(SchemaRegistrationConflictException.class, () -> reg.onSchemaRegistered(schemaV2));
    assertEquals(3, reg.lastSchemaVersion());
    assertSameSchema(schemaV3, reg.schema());
    // Register schema with out-of-order version.
    assertThrows(SchemaRegistryException.class, () -> reg.onSchemaRegistered(schemaV5));
    assertEquals(3, reg.lastSchemaVersion());
    assertSameSchema(schemaV3, reg.schema());
    // Register schema with outdated version.
    assertThrows(SchemaRegistrationConflictException.class, () -> reg.onSchemaRegistered(schemaV1));
    assertEquals(3, reg.lastSchemaVersion());
    assertSameSchema(schemaV3, reg.schema());
    assertThrows(SchemaRegistryException.class, () -> reg.schema(1));
    assertSameSchema(schemaV2, reg.schema(2));
    assertSameSchema(schemaV3, reg.schema(3));
    // Register schema with next version.
    reg.onSchemaRegistered(schemaV4);
    assertEquals(4, reg.lastSchemaVersion());
    assertSameSchema(schemaV4, reg.schema());
    assertSameSchema(schemaV2, reg.schema(2));
    assertSameSchema(schemaV3, reg.schema(3));
    assertSameSchema(schemaV4, reg.schema(4));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) Test(org.junit.jupiter.api.Test)

Example 97 with SchemaDescriptor

use of org.apache.ignite.internal.schema.SchemaDescriptor 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 98 with SchemaDescriptor

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

the class SchemaDescriptorConverterTest method testColumnOrder.

/**
 * Convert table with complex primary key and check it.
 */
@Test
public void testColumnOrder() {
    ColumnDefinition[] cols = { SchemaBuilders.column("ID", ColumnType.UUID).build(), SchemaBuilders.column("STRING", ColumnType.string()).build(), SchemaBuilders.column("INT32", ColumnType.INT32).build(), SchemaBuilders.column("INT64", ColumnType.INT64).build(), SchemaBuilders.column("DOUBLE", ColumnType.DOUBLE).build(), SchemaBuilders.column("UUID", ColumnType.UUID).build(), SchemaBuilders.column("INT16", ColumnType.INT16).build(), SchemaBuilders.column("BITMASK_FS10", ColumnType.bitmaskOf(10)).build() };
    TableDefinition tblSchm = SchemaBuilders.tableBuilder("SCHEMA", "TABLE").columns(cols).withPrimaryKey(SchemaBuilders.primaryKey().withColumns("INT32", "ID").withColocationColumns("INT32").build()).build();
    SchemaDescriptor tblDscr = SchemaDescriptorConverter.convert(1, tblSchm);
    for (int i = 0; i < cols.length; i++) {
        Column col = tblDscr.column(i);
        assertEquals(col.name(), cols[col.columnOrder()].name());
    }
    assertArrayEquals(Arrays.stream(cols).map(ColumnDefinition::name).toArray(String[]::new), tblDscr.columnNames().toArray(String[]::new));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 99 with SchemaDescriptor

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

the class SchemaDescriptorConverterTest method testConvert.

/**
 * Test set of columns.
 *
 * @param nullable Nullable flag.
 */
private void testConvert(boolean nullable) {
    TableDefinition tblSchm = getBuilder(nullable, true).build();
    SchemaDescriptor tblDscr = SchemaDescriptorConverter.convert(1, tblSchm);
    assertEquals(1, tblDscr.keyColumns().length());
    testCol(tblDscr.keyColumns(), "ID", NativeTypeSpec.UUID, false);
    assertEquals(columns - 1, tblDscr.valueColumns().length());
    testCol(tblDscr.valueColumns(), "INT8", NativeTypeSpec.INT8, nullable);
    testCol(tblDscr.valueColumns(), "INT16", NativeTypeSpec.INT16, nullable);
    testCol(tblDscr.valueColumns(), "INT32", NativeTypeSpec.INT32, nullable);
    testCol(tblDscr.valueColumns(), "INT64", NativeTypeSpec.INT64, nullable);
    testCol(tblDscr.valueColumns(), "FLOAT", NativeTypeSpec.FLOAT, nullable);
    testCol(tblDscr.valueColumns(), "DOUBLE", NativeTypeSpec.DOUBLE, nullable);
    testCol(tblDscr.valueColumns(), "UUID", NativeTypeSpec.UUID, nullable);
    testCol(tblDscr.valueColumns(), "STRING", NativeTypeSpec.STRING, nullable);
    testCol(tblDscr.valueColumns(), "STRING_FS10", NativeTypeSpec.STRING, nullable);
    testCol(tblDscr.valueColumns(), "BLOB", NativeTypeSpec.BYTES, nullable);
    testCol(tblDscr.valueColumns(), "DECIMAL", NativeTypeSpec.DECIMAL, nullable);
    testCol(tblDscr.valueColumns(), "NUMBER", NativeTypeSpec.NUMBER, nullable);
    testCol(tblDscr.valueColumns(), "DECIMAL", NativeTypeSpec.DECIMAL, nullable);
    testCol(tblDscr.valueColumns(), "BITMASK_FS10", NativeTypeSpec.BITMASK, nullable);
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableDefinition(org.apache.ignite.schema.definition.TableDefinition)

Example 100 with SchemaDescriptor

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

the class RecordMarshallerTest method classWithWrongFieldType.

@ParameterizedTest
@MethodSource("marshallerFactoryProvider")
public void classWithWrongFieldType(MarshallerFactory factory) {
    SchemaDescriptor schema = new SchemaDescriptor(1, keyColumns(), new Column[] { new Column("bitmaskCol".toUpperCase(), NativeTypes.bitmaskOf(42), true), new Column("shortCol".toUpperCase(), UUID, true) });
    RecordMarshaller<TestObjectWithAllTypes> marshaller = factory.create(schema, TestObjectWithAllTypes.class);
    final TestObjectWithAllTypes rec = TestObjectWithAllTypes.randomObject(rnd);
    assertThrows(MarshallerException.class, () -> marshaller.marshal(rec), "Failed to write field [name=shortCol]");
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) TestObjectWithAllTypes(org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)105 Column (org.apache.ignite.internal.schema.Column)78 Test (org.junit.jupiter.api.Test)48 Tuple (org.apache.ignite.table.Tuple)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 Row (org.apache.ignite.internal.schema.row.Row)32 MethodSource (org.junit.jupiter.params.provider.MethodSource)30 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)22 TestObjectWithAllTypes (org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes)11 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)11 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)10 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)10 List (java.util.List)9 ArrayList (java.util.ArrayList)8 NotNull (org.jetbrains.annotations.NotNull)8 UUID (java.util.UUID)7 Collectors (java.util.stream.Collectors)7 TableImpl (org.apache.ignite.internal.table.TableImpl)7 Random (java.util.Random)6 Map (java.util.Map)5