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