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