Search in sources :

Example 21 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory 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 22 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class ModifyNode method flushTuples.

private void flushTuples(boolean force) {
    if (nullOrEmpty(rows) || !force && rows.size() < MODIFY_BATCH_SIZE) {
        return;
    }
    List<ModifyRow> rows = this.rows;
    this.rows = new ArrayList<>(MODIFY_BATCH_SIZE);
    Map<ModifyRow.Operation, Collection<BinaryRow>> operations = getOperationsPerAction(rows);
    // TODO: IGNITE-15087 Implement support for transactional SQL
    for (Map.Entry<ModifyRow.Operation, Collection<BinaryRow>> op : operations.entrySet()) {
        switch(op.getKey()) {
            case INSERT_ROW:
                Collection<BinaryRow> conflictKeys = tableView.insertAll(op.getValue(), null).join();
                if (!conflictKeys.isEmpty()) {
                    IgniteTypeFactory typeFactory = context().getTypeFactory();
                    RowHandler.RowFactory<RowT> rowFactory = context().rowHandler().factory(context().getTypeFactory(), table.descriptor().insertRowType(typeFactory));
                    List<String> conflictKeys0 = conflictKeys.stream().map(binRow -> table.toRow(context(), binRow, rowFactory, null)).map(context().rowHandler()::toString).collect(Collectors.toList());
                    throw conflictKeysException(conflictKeys0);
                }
                break;
            case UPDATE_ROW:
                tableView.upsertAll(op.getValue(), null).join();
                break;
            case DELETE_ROW:
                tableView.deleteAll(op.getValue(), null).join();
                break;
            default:
                throw new UnsupportedOperationException(op.getKey().name());
        }
    }
    updatedRows += rows.size();
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ModifyRow(org.apache.ignite.internal.sql.engine.schema.ModifyRow) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) RowHandler(org.apache.ignite.internal.sql.engine.exec.RowHandler) Collection(java.util.Collection) EnumMap(java.util.EnumMap) Map(java.util.Map)

Example 23 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class RuntimeSortedIndexTest method test.

@Test
public void test() throws Exception {
    IgniteTypeFactory tf = new IgniteTypeFactory();
    List<Pair<RelDataType, ImmutableIntList>> testIndexes = Arrays.stream(ROW_TYPES).map(rt -> Pair.of(TypeUtils.createRowType(tf, rt.getKey()), rt.getValue())).collect(Collectors.toList());
    for (Pair<RelDataType, ImmutableIntList> testIdx : testIndexes) {
        for (int notUnique : NOT_UNIQUE_ROWS_IN_GROUP) {
            RuntimeSortedIndex<Object[]> idx0 = generate(testIdx.getKey(), testIdx.getValue(), notUnique);
            int rowIdLow = ThreadLocalRandom.current().nextInt(UNIQUE_GROUPS * notUnique);
            int rowIdUp = rowIdLow + ThreadLocalRandom.current().nextInt(UNIQUE_GROUPS * notUnique - rowIdLow);
            for (int searchNum = 0; searchNum < SEARCH_CNT; ++searchNum) {
                Object[] lower = generateFindRow(rowIdLow, testIdx.getKey(), notUnique, testIdx.getValue());
                Object[] upper = generateFindRow(rowIdUp, testIdx.getKey(), notUnique, testIdx.getValue());
                Cursor<Object[]> cur = idx0.find(lower, upper);
                int rows = 0;
                while (cur.hasNext()) {
                    cur.next();
                    rows++;
                }
                assertEquals((rowIdUp / notUnique - rowIdLow / notUnique + 1) * notUnique, rows, "Invalid results [rowType=" + testIdx.getKey() + ", notUnique=" + notUnique + ", rowIdLow=" + rowIdLow + ", rowIdUp=" + rowIdUp);
            }
        }
    }
}
Also used : RelCollations(org.apache.calcite.rel.RelCollations) RelDataType(org.apache.calcite.rel.type.RelDataType) Arrays(java.util.Arrays) Time(java.sql.Time) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) ImmutableMap(com.google.common.collect.ImmutableMap) Date(java.util.Date) Timestamp(java.sql.Timestamp) BaseQueryContext(org.apache.ignite.internal.sql.engine.util.BaseQueryContext) TypeUtils(org.apache.ignite.internal.sql.engine.util.TypeUtils) Cursor(org.apache.ignite.internal.util.Cursor) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Pair(org.apache.calcite.util.Pair) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BitSet(java.util.BitSet) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) Pair(org.apache.calcite.util.Pair) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 24 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class SortedIndexSpoolExecutionTest method testIndexSpool.

@Test
public void testIndexSpool() {
    ExecutionContext<Object[]> ctx = executionContext();
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, String.class, int.class);
    int inBufSize = Commons.IN_BUFFER_SIZE;
    int[] sizes = { 1, inBufSize / 2 - 1, inBufSize / 2, inBufSize / 2 + 1, inBufSize, inBufSize + 1, inBufSize * 4 };
    for (int size : sizes) {
        // (filter, lower, upper, expected result size)
        TestParams[] testParams;
        if (size == 1) {
            testParams = new TestParams[] { new TestParams(null, new Object[] { null, null, null }, new Object[] { null, null, null }, 1), new TestParams(null, new Object[] { 0, null, null }, new Object[] { 0, null, null }, 1) };
        } else {
            testParams = new TestParams[] { new TestParams(null, new Object[] { null, null, null }, new Object[] { null, null, null }, size), new TestParams(null, new Object[] { size / 2, null, null }, new Object[] { size / 2, null, null }, 1), new TestParams(null, new Object[] { size / 2 + 1, null, null }, new Object[] { size / 2 + 1, null, null }, 1), new TestParams(null, new Object[] { size / 2, null, null }, new Object[] { null, null, null }, size - size / 2), new TestParams(null, new Object[] { null, null, null }, new Object[] { size / 2, null, null }, size / 2 + 1), new TestParams((r -> ((int) r[0]) < size / 2), new Object[] { null, null, null }, new Object[] { size / 2, null, null }, size / 2) };
        }
        log.info("Check: size=" + size);
        ScanNode<Object[]> scan = new ScanNode<>(ctx, rowType, new TestTable(size, rowType) {

            boolean first = true;

            @Override
            @NotNull
            public Iterator<Object[]> iterator() {
                assertTrue(first, "Rewind right");
                first = false;
                return super.iterator();
            }
        });
        Object[] lower = new Object[3];
        Object[] upper = new Object[3];
        TestPredicate testFilter = new TestPredicate();
        IndexSpoolNode<Object[]> spool = IndexSpoolNode.createTreeSpool(ctx, rowType, RelCollations.of(ImmutableIntList.of(0)), (o1, o2) -> o1[0] != null ? ((Comparable) o1[0]).compareTo(o2[0]) : 0, testFilter, () -> lower, () -> upper);
        spool.register(singletonList(scan));
        RootRewindable<Object[]> root = new RootRewindable<>(ctx, rowType);
        root.register(spool);
        for (TestParams param : testParams) {
            log.info("Check: param=" + param);
            // Set up bounds
            testFilter.delegate = param.pred;
            System.arraycopy(param.lower, 0, lower, 0, lower.length);
            System.arraycopy(param.upper, 0, upper, 0, upper.length);
            int cnt = 0;
            while (root.hasNext()) {
                root.next();
                cnt++;
            }
            assertEquals(param.expectedResultSize, cnt, "Invalid result size");
            root.rewind();
        }
        root.closeRewindableRoot();
    }
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) NotNull(org.jetbrains.annotations.NotNull) Iterator(java.util.Iterator) Test(org.junit.jupiter.api.Test)

Example 25 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class TableSpoolExecutionTest method testEagerSpoolReadsWholeInput.

/**
 * Ensure eager spool reads underlying input till the end before emmitting the very first row.
 */
@Test
public void testEagerSpoolReadsWholeInput() {
    ExecutionContext<Object[]> ctx = executionContext();
    IgniteTypeFactory tf = ctx.getTypeFactory();
    RelDataType rowType = TypeUtils.createRowType(tf, int.class, String.class, int.class);
    int inBufSize = Commons.IN_BUFFER_SIZE;
    int[] sizes = { inBufSize / 2, inBufSize, inBufSize + 1, inBufSize * 2 };
    for (int size : sizes) {
        log.info("Check: size=" + size);
        AtomicReference<Iterator<Object[]>> itRef = new AtomicReference<>();
        ScanNode<Object[]> scan = new ScanNode<>(ctx, rowType, () -> {
            if (itRef.get() != null) {
                throw new AssertionError();
            }
            itRef.set(IntStream.range(0, size).boxed().map(i -> new Object[] { i }).iterator());
            return itRef.get();
        });
        TableSpoolNode<Object[]> spool = new TableSpoolNode<>(ctx, rowType, false);
        spool.register(singletonList(scan));
        RootNode<Object[]> root = new RootNode<>(ctx, rowType);
        root.register(spool);
        assertTrue(root.hasNext());
        root.next();
        assertFalse(itRef.get().hasNext());
    }
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) AtomicReference(java.util.concurrent.atomic.AtomicReference) Iterator(java.util.Iterator) Test(org.junit.jupiter.api.Test)

Aggregations

IgniteTypeFactory (org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory)80 Test (org.junit.jupiter.api.Test)48 RelDataType (org.apache.calcite.rel.type.RelDataType)47 IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)27 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 AggregateCall (org.apache.calcite.rel.core.AggregateCall)19 IgniteRel (org.apache.ignite.internal.sql.engine.rel.IgniteRel)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)17 RexNode (org.apache.calcite.rex.RexNode)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 ArrayList (java.util.ArrayList)10 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 IgniteDistribution (org.apache.ignite.internal.sql.engine.trait.IgniteDistribution)8 PlanningContext (org.apache.ignite.internal.sql.engine.prepare.PlanningContext)7 List (java.util.List)6 MappingQueryContext (org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext)6 MultiStepPlan (org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan)6 MultiStepQueryPlan (org.apache.ignite.internal.sql.engine.prepare.MultiStepQueryPlan)6 QueryTemplate (org.apache.ignite.internal.sql.engine.prepare.QueryTemplate)6