Search in sources :

Example 6 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition in project ignite-3 by apache.

the class SchemaConfigurationConverter method convert.

/**
 * Convert table schema to table changer.
 *
 * @param tbl    Table schema to convert.
 * @param tblChg Change to fulfill.
 * @return TableChange to get result from.
 */
public static TableChange convert(TableDefinition tbl, TableChange tblChg) {
    tblChg.changeName(tbl.canonicalName());
    tblChg.changeIndices(idxsChg -> {
        for (IndexDefinition idx : tbl.indices()) {
            idxsChg.create(idx.name(), idxInit -> convert(idx, idxInit));
        }
    });
    tblChg.changeColumns(colsChg -> {
        for (ColumnDefinition col : tbl.columns()) {
            colsChg.create(col.name(), colChg -> convert(col, colChg));
        }
    });
    tblChg.changePrimaryKey(pkCng -> {
        pkCng.changeColumns(tbl.keyColumns().toArray(String[]::new)).changeColocationColumns(tbl.colocationColumns().toArray(String[]::new));
    });
    return tblChg;
}
Also used : HashIndexDefinition(org.apache.ignite.schema.definition.index.HashIndexDefinition) SortedIndexDefinition(org.apache.ignite.schema.definition.index.SortedIndexDefinition) IndexDefinition(org.apache.ignite.schema.definition.index.IndexDefinition) PartialIndexDefinition(org.apache.ignite.schema.definition.index.PartialIndexDefinition) IndexColumnDefinition(org.apache.ignite.schema.definition.index.IndexColumnDefinition) SortedIndexColumnDefinition(org.apache.ignite.schema.definition.index.SortedIndexColumnDefinition) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Example 7 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition in project ignite-3 by apache.

the class ItSchemaChangeKvViewTest method testMergeChangesAddDropAdd.

/**
 * Check merge table schema changes.
 */
@Test
public void testMergeChangesAddDropAdd() {
    List<Ignite> grid = startGrid();
    createTable(grid);
    final ColumnDefinition column = SchemaBuilders.column("val", ColumnType.string()).asNullable(true).withDefaultValueExpression("default").build();
    KeyValueView<Tuple, Tuple> kvView = grid.get(0).tables().table(TABLE).keyValueView();
    kvView.put(null, Tuple.create().set("key", 1L), Tuple.create().set("valInt", 111));
    assertThrowsWithCause(SchemaMismatchException.class, () -> kvView.put(null, Tuple.create().set("key", 2L), Tuple.create().set("val", "I'not exists")));
    addColumn(grid, column);
    assertNull(kvView.get(null, Tuple.create().set("key", 2L)));
    kvView.put(null, Tuple.create().set("key", 2L), Tuple.create().set("valInt", 222).set("val", "string"));
    kvView.put(null, Tuple.create().set("key", 3L), Tuple.create().set("valInt", 333));
    dropColumn(grid, column.name());
    kvView.put(null, Tuple.create().set("key", 4L), Tuple.create().set("valInt", 444));
    assertThrowsWithCause(SchemaMismatchException.class, () -> kvView.put(null, Tuple.create().set("key", 4L), Tuple.create().set("val", "I'm not exist")));
    addColumn(grid, SchemaBuilders.column("val", ColumnType.string()).asNullable(true).withDefaultValueExpression("default").build());
    kvView.put(null, Tuple.create().set("key", 5L), Tuple.create().set("valInt", 555));
    // Check old row conversion.
    Tuple keyTuple1 = Tuple.create().set("key", 1L);
    assertEquals(111, (Integer) kvView.get(null, keyTuple1).value("valInt"));
    assertEquals("default", kvView.get(null, keyTuple1).value("val"));
    Tuple keyTuple2 = Tuple.create().set("key", 2L);
    assertEquals(222, (Integer) kvView.get(null, keyTuple2).value("valInt"));
    assertEquals("default", kvView.get(null, keyTuple2).value("val"));
    Tuple keyTuple3 = Tuple.create().set("key", 3L);
    assertEquals(333, (Integer) kvView.get(null, keyTuple3).value("valInt"));
    assertEquals("default", kvView.get(null, keyTuple3).value("val"));
    Tuple keyTuple4 = Tuple.create().set("key", 4L);
    assertEquals(444, (Integer) kvView.get(null, keyTuple4).value("valInt"));
    assertEquals("default", kvView.get(null, keyTuple4).value("val"));
    Tuple keyTuple5 = Tuple.create().set("key", 5L);
    assertEquals(555, (Integer) kvView.get(null, keyTuple5).value("valInt"));
    assertEquals("default", kvView.get(null, keyTuple5).value("val"));
}
Also used : Ignite(org.apache.ignite.Ignite) Tuple(org.apache.ignite.table.Tuple) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 8 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition in project ignite-3 by apache.

the class ItSchemaChangeTableViewTest method testMergeChangesAddDropAdd.

/**
 * Check merge table schema changes.
 */
@Test
public void testMergeChangesAddDropAdd() {
    List<Ignite> grid = startGrid();
    createTable(grid);
    final ColumnDefinition column = SchemaBuilders.column("val", ColumnType.string()).asNullable(true).withDefaultValueExpression("default").build();
    RecordView<Tuple> tbl = grid.get(0).tables().table(TABLE).recordView();
    tbl.insert(null, Tuple.create().set("key", 1L).set("valInt", 111));
    assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.insert(null, Tuple.create().set("key", 2L).set("val", "I'not exists")));
    addColumn(grid, column);
    assertNull(tbl.get(null, Tuple.create().set("key", 2L)));
    tbl.insert(null, Tuple.create().set("key", 2L).set("valInt", 222).set("val", "string"));
    tbl.insert(null, Tuple.create().set("key", 3L).set("valInt", 333));
    dropColumn(grid, column.name());
    tbl.insert(null, Tuple.create().set("key", 4L).set("valInt", 444));
    assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.insert(null, Tuple.create().set("key", 4L).set("val", "I'm not exist")));
    addColumn(grid, SchemaBuilders.column("val", ColumnType.string()).withDefaultValueExpression("default").build());
    tbl.insert(null, Tuple.create().set("key", 5L).set("valInt", 555));
    // Check old row conversion.
    Tuple keyTuple1 = Tuple.create().set("key", 1L);
    assertEquals(111, (Integer) tbl.get(null, keyTuple1).value("valInt"));
    assertEquals("default", tbl.get(null, keyTuple1).value("val"));
    Tuple keyTuple2 = Tuple.create().set("key", 2L);
    assertEquals(222, (Integer) tbl.get(null, keyTuple2).value("valInt"));
    assertEquals("default", tbl.get(null, keyTuple2).value("val"));
    Tuple keyTuple3 = Tuple.create().set("key", 3L);
    assertEquals(333, (Integer) tbl.get(null, keyTuple3).value("valInt"));
    assertEquals("default", tbl.get(null, keyTuple3).value("val"));
    Tuple keyTuple4 = Tuple.create().set("key", 4L);
    assertEquals(444, (Integer) tbl.get(null, keyTuple4).value("valInt"));
    assertEquals("default", tbl.get(null, keyTuple4).value("val"));
    Tuple keyTuple5 = Tuple.create().set("key", 5L);
    assertEquals(555, (Integer) tbl.get(null, keyTuple5).value("valInt"));
    assertEquals("default", tbl.get(null, keyTuple5).value("val"));
}
Also used : Ignite(org.apache.ignite.Ignite) Tuple(org.apache.ignite.table.Tuple) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 9 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition in project ignite-3 by apache.

the class ItJdbcResultSetSelfTest method beforeClass.

@BeforeAll
public static void beforeClass() {
    Ignite ignite = clusterNodes.get(0);
    List<ColumnDefinition> columns = new ArrayList<>();
    columns.add(SchemaBuilders.column("ID", ColumnType.INT32).build());
    columns.add(SchemaBuilders.column("BOOLVAL", ColumnType.INT8).asNullable(true).build());
    columns.add(SchemaBuilders.column("BYTEVAL", ColumnType.INT8).asNullable(true).build());
    columns.add(SchemaBuilders.column("SHORTVAL", ColumnType.INT16).asNullable(true).build());
    columns.add(SchemaBuilders.column("INTVAL", ColumnType.INT32).asNullable(true).build());
    columns.add(SchemaBuilders.column("LONGVAL", ColumnType.INT64).asNullable(true).build());
    columns.add(SchemaBuilders.column("FLOATVAL", ColumnType.FLOAT).asNullable(true).build());
    columns.add(SchemaBuilders.column("DOUBLEVAL", ColumnType.DOUBLE).asNullable(true).build());
    columns.add(SchemaBuilders.column("BIGVAL", ColumnType.decimalOf()).asNullable(true).build());
    columns.add(SchemaBuilders.column("STRVAL", ColumnType.string()).asNullable(true).build());
    columns.add(SchemaBuilders.column("ARRVAL", ColumnType.blobOf()).asNullable(true).build());
    columns.add(SchemaBuilders.column("DATEVAL", ColumnType.DATE).asNullable(true).build());
    columns.add(SchemaBuilders.column("TIMEVAL", ColumnType.TemporalColumnType.time()).asNullable(true).build());
    columns.add(SchemaBuilders.column("TSVAL", ColumnType.TemporalColumnType.timestamp()).asNullable(true).build());
    columns.add(SchemaBuilders.column("URLVAL", ColumnType.blobOf()).asNullable(true).build());
    TableDefinition personTableDef = SchemaBuilders.tableBuilder("PUBLIC", "TEST").columns(columns).withPrimaryKey("ID").build();
    ignite.tables().createTable(personTableDef.canonicalName(), (tableChange) -> SchemaConfigurationConverter.convert(personTableDef, tableChange).changeReplicas(1).changePartitions(10));
    RecordView<Tuple> tupleRecordView = ignite.tables().table("PUBLIC.TEST").recordView();
    Tuple tuple = Tuple.create();
    tuple.set("BOOLVAL", (byte) 1).set("BYTEVAL", (byte) 1).set("SHORTVAL", (short) 1).set("INTVAL", 1).set("LONGVAL", 1L).set("FLOATVAL", 1.0f).set("DOUBLEVAL", 1.0d).set("BIGVAL", new BigDecimal("1")).set("STRVAL", "1").set("DATEVAL", LocalDate.parse("1901-02-01")).set("TIMEVAL", LocalTime.parse("01:01:01")).set("TSVAL", Instant.ofEpochMilli(1));
    tupleRecordView.insert(null, tuple.set("ID", 1));
    tupleRecordView.insert(null, tuple.set("ID", 2));
}
Also used : ArrayList(java.util.ArrayList) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) Ignite(org.apache.ignite.Ignite) Tuple(org.apache.ignite.table.Tuple) BigDecimal(java.math.BigDecimal) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 10 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition 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)

Aggregations

ColumnDefinition (org.apache.ignite.schema.definition.ColumnDefinition)12 Test (org.junit.jupiter.api.Test)5 Ignite (org.apache.ignite.Ignite)4 Column (org.apache.ignite.internal.schema.Column)4 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)4 TableDefinition (org.apache.ignite.schema.definition.TableDefinition)4 Tuple (org.apache.ignite.table.Tuple)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)2 ColumnView (org.apache.ignite.configuration.schemas.table.ColumnView)2 IndexColumnView (org.apache.ignite.configuration.schemas.table.IndexColumnView)2 SchemaBuilders (org.apache.ignite.schema.SchemaBuilders)2 ColumnType (org.apache.ignite.schema.definition.ColumnType)2 ColumnDefinitionBuilder (org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder)2 HashIndexDefinition (org.apache.ignite.schema.definition.index.HashIndexDefinition)2 IndexColumnDefinition (org.apache.ignite.schema.definition.index.IndexColumnDefinition)2 IndexDefinition (org.apache.ignite.schema.definition.index.IndexDefinition)2 PartialIndexDefinition (org.apache.ignite.schema.definition.index.PartialIndexDefinition)2 SortedIndexColumnDefinition (org.apache.ignite.schema.definition.index.SortedIndexColumnDefinition)2 SortedIndexDefinition (org.apache.ignite.schema.definition.index.SortedIndexDefinition)2