Search in sources :

Example 1 with PrimaryKeyDefinitionBuilder

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

the class PrimaryKeyDefinitionDefinitionBuilderTest method testPrimaryKeyWithColocationColumns.

/**
 * Test primary key parameters.
 */
@Test
public void testPrimaryKeyWithColocationColumns() {
    PrimaryKeyDefinitionBuilder builder = SchemaBuilders.primaryKey();
    builder.withColumns("A", "B", "C").withColocationColumns("B").build();
    PrimaryKeyDefinition idx = builder.build();
    assertEquals(3, idx.columns().size());
    assertEquals(1, idx.colocationColumns().size());
    assertTrue(idx.columns().containsAll(List.of("A", "B", "C")));
    assertTrue(idx.colocationColumns().contains("B"));
    assertFalse(idx.colocationColumns().contains("A"));
    assertFalse(idx.colocationColumns().contains("C"));
}
Also used : PrimaryKeyDefinition(org.apache.ignite.schema.definition.PrimaryKeyDefinition) PrimaryKeyDefinitionBuilder(org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with PrimaryKeyDefinitionBuilder

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

the class PrimaryKeyDefinitionDefinitionBuilderTest method testPrimaryKey.

/**
 * Test primary key parameters.
 */
@Test
public void testPrimaryKey() {
    PrimaryKeyDefinitionBuilder builder = SchemaBuilders.primaryKey();
    builder.withColumns("A", "B", "C").build();
    PrimaryKeyDefinition idx = builder.build();
    assertEquals(3, idx.columns().size());
    assertEquals(3, idx.colocationColumns().size());
    assertTrue(idx.columns().containsAll(List.of("A", "B", "C")));
    assertTrue(idx.colocationColumns().containsAll(List.of("A", "B", "C")));
}
Also used : PrimaryKeyDefinition(org.apache.ignite.schema.definition.PrimaryKeyDefinition) PrimaryKeyDefinitionBuilder(org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with PrimaryKeyDefinitionBuilder

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

the class PrimaryKeyDefinitionDefinitionBuilderTest method testPrimaryKeyWrongColocationColumn.

/**
 * Test primary key parameters.
 */
@Test
public void testPrimaryKeyWrongColocationColumn() {
    PrimaryKeyDefinitionBuilder builder = SchemaBuilders.primaryKey().withColumns("A", "B").withColocationColumns("C");
    assertThrows(IllegalStateException.class, builder::build);
}
Also used : PrimaryKeyDefinitionBuilder(org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with PrimaryKeyDefinitionBuilder

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

the class DdlCommandHandler method handleCreateTable.

/**
 * Handles create table command.
 */
private void handleCreateTable(CreateTableCommand cmd) {
    final PrimaryKeyDefinitionBuilder pkeyDef = SchemaBuilders.primaryKey();
    pkeyDef.withColumns(IgniteObjectName.quoteNames(cmd.primaryKeyColumns()));
    pkeyDef.withColocationColumns(IgniteObjectName.quoteNames(cmd.affColumns()));
    final IgniteTypeFactory typeFactory = Commons.typeFactory();
    final List<org.apache.ignite.schema.definition.ColumnDefinition> colsInner = new ArrayList<>();
    for (ColumnDefinition col : cmd.columns()) {
        ColumnDefinitionBuilder col0 = SchemaBuilders.column(IgniteObjectName.quote(col.name()), typeFactory.columnType(col.type())).asNullable(col.nullable()).withDefaultValueExpression(col.defaultValue());
        colsInner.add(col0.build());
    }
    Consumer<TableChange> tblChanger = tblCh -> {
        TableChange conv = convert(SchemaBuilders.tableBuilder(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName())).columns(colsInner).withPrimaryKey(pkeyDef.build()).build(), tblCh);
        if (cmd.partitions() != null) {
            conv.changePartitions(cmd.partitions());
        }
        if (cmd.replicas() != null) {
            conv.changeReplicas(cmd.replicas());
        }
    };
    String fullName = TableDefinitionImpl.canonicalName(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName()));
    try {
        tableManager.createTable(fullName, tblChanger);
    } catch (TableAlreadyExistsException ex) {
        if (!cmd.ifTableExists()) {
            throw ex;
        }
    }
}
Also used : SortedIndexDefinitionBuilder(org.apache.ignite.schema.definition.builder.SortedIndexDefinitionBuilder) Pair(org.apache.ignite.internal.util.Pair) CollectionUtils.nullOrEmpty(org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty) SchemaConfigurationConverter.convert(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert) IgniteException(org.apache.ignite.lang.IgniteException) ColumnDefinition(org.apache.ignite.internal.sql.engine.prepare.ddl.ColumnDefinition) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) DropTableCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.DropTableCommand) Map(java.util.Map) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) PrimaryKeyView(org.apache.ignite.configuration.schemas.table.PrimaryKeyView) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) PrimaryKeyDefinitionBuilder(org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder) TableDefinitionImpl(org.apache.ignite.internal.schema.definition.TableDefinitionImpl) TableManager(org.apache.ignite.internal.table.distributed.TableManager) ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) SortedIndexColumnBuilder(org.apache.ignite.schema.definition.builder.SortedIndexDefinitionBuilder.SortedIndexColumnBuilder) NamedListView(org.apache.ignite.configuration.NamedListView) AbstractTableDdlCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.AbstractTableDdlCommand) CreateIndexCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.CreateIndexCommand) Set(java.util.Set) IgniteObjectName(org.apache.ignite.internal.util.IgniteObjectName) Collectors(java.util.stream.Collectors) Commons(org.apache.ignite.internal.sql.engine.util.Commons) AlterTableDropCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.AlterTableDropCommand) Consumer(java.util.function.Consumer) DdlCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.DdlCommand) CreateTableCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.CreateTableCommand) List(java.util.List) IgniteInternalCheckedException(org.apache.ignite.lang.IgniteInternalCheckedException) AlterTableAddCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.AlterTableAddCommand) DropIndexCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.DropIndexCommand) ColumnAlreadyExistsException(org.apache.ignite.lang.ColumnAlreadyExistsException) ColumnNotFoundException(org.apache.ignite.lang.ColumnNotFoundException) IndexAlreadyExistsException(org.apache.ignite.lang.IndexAlreadyExistsException) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ArrayList(java.util.ArrayList) ColumnDefinition(org.apache.ignite.internal.sql.engine.prepare.ddl.ColumnDefinition) PrimaryKeyDefinitionBuilder(org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder) TableChange(org.apache.ignite.configuration.schemas.table.TableChange)

Aggregations

PrimaryKeyDefinitionBuilder (org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder)4 Test (org.junit.jupiter.api.Test)3 PrimaryKeyDefinition (org.apache.ignite.schema.definition.PrimaryKeyDefinition)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 NamedListView (org.apache.ignite.configuration.NamedListView)1 ColumnView (org.apache.ignite.configuration.schemas.table.ColumnView)1 PrimaryKeyView (org.apache.ignite.configuration.schemas.table.PrimaryKeyView)1 TableChange (org.apache.ignite.configuration.schemas.table.TableChange)1 SchemaConfigurationConverter.convert (org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert)1 TableDefinitionImpl (org.apache.ignite.internal.schema.definition.TableDefinitionImpl)1 AbstractTableDdlCommand (org.apache.ignite.internal.sql.engine.prepare.ddl.AbstractTableDdlCommand)1 AlterTableAddCommand (org.apache.ignite.internal.sql.engine.prepare.ddl.AlterTableAddCommand)1 AlterTableDropCommand (org.apache.ignite.internal.sql.engine.prepare.ddl.AlterTableDropCommand)1