Search in sources :

Example 1 with ColumnDefinitionBuilder

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

Example 2 with ColumnDefinitionBuilder

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

the class SchemaDescriptorConverterTest method getBuilder.

/**
 * Get TableSchemaBuilder with default table.
 *
 * @param nullable If all columns should be nullable.
 * @param withPk   If builder should contains primary key index.
 * @return TableSchemaBuilder.
 */
private TableDefinitionBuilder getBuilder(boolean nullable, boolean withPk) {
    Function<ColumnDefinitionBuilder, ColumnDefinition> postProcess = builder -> builder.asNullable(nullable).build();
    TableDefinitionBuilder res = SchemaBuilders.tableBuilder("SCHEMA", "TABLE").columns(SchemaBuilders.column("ID", ColumnType.UUID).build(), postProcess.apply(SchemaBuilders.column("INT8", ColumnType.INT8)), postProcess.apply(SchemaBuilders.column("INT16", ColumnType.INT16)), postProcess.apply(SchemaBuilders.column("INT32", ColumnType.INT32)), postProcess.apply(SchemaBuilders.column("INT64", ColumnType.INT64)), postProcess.apply(SchemaBuilders.column("FLOAT", ColumnType.FLOAT)), postProcess.apply(SchemaBuilders.column("DOUBLE", ColumnType.DOUBLE)), postProcess.apply(SchemaBuilders.column("UUID", ColumnType.UUID)), postProcess.apply(SchemaBuilders.column("STRING", ColumnType.string())), postProcess.apply(SchemaBuilders.column("STRING_FS10", ColumnType.stringOf(10))), postProcess.apply(SchemaBuilders.column("BLOB", ColumnType.blobOf())), postProcess.apply(SchemaBuilders.column("BLOB_FS10", ColumnType.blobOf(10))), postProcess.apply(SchemaBuilders.column("DECIMAL", ColumnType.decimalOf(1, 1))), postProcess.apply(SchemaBuilders.column("NUMBER", ColumnType.numberOf(12))), postProcess.apply(SchemaBuilders.column("BITMASK_FS10", ColumnType.bitmaskOf(10))));
    if (withPk) {
        res.withPrimaryKey("ID");
    }
    return res;
}
Also used : Arrays(java.util.Arrays) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Columns(org.apache.ignite.internal.schema.Columns) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) ColumnType(org.apache.ignite.schema.definition.ColumnType) Function(java.util.function.Function) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) Test(org.junit.jupiter.api.Test) TableDefinitionBuilder(org.apache.ignite.schema.definition.builder.TableDefinitionBuilder) Column(org.apache.ignite.internal.schema.Column) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) TableDefinitionBuilder(org.apache.ignite.schema.definition.builder.TableDefinitionBuilder) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Example 3 with ColumnDefinitionBuilder

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

the class ColumnDefinitionBuilderTest method testCreateColumn.

/**
 * Check column parameters.
 */
@Test
public void testCreateColumn() {
    ColumnDefinitionBuilder builder = SchemaBuilders.column("TEST", ColumnType.DOUBLE);
    ColumnDefinition col = builder.withDefaultValueExpression("NOW()").build();
    assertEquals("TEST", col.name());
    assertEquals(ColumnType.DOUBLE, col.type());
    assertEquals("NOW()", col.defaultValue());
    assertFalse(col.nullable());
}
Also used : ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

ColumnDefinitionBuilder (org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder)3 SchemaBuilders (org.apache.ignite.schema.SchemaBuilders)2 ColumnDefinition (org.apache.ignite.schema.definition.ColumnDefinition)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)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 Function (java.util.function.Function)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 Column (org.apache.ignite.internal.schema.Column)1 Columns (org.apache.ignite.internal.schema.Columns)1