use of org.apache.ignite.internal.processors.query.GridQueryProperty in project ignite by apache.
the class UpdatePlanBuilder method planForBulkLoad.
/**
* Prepare update plan for COPY command (AKA bulk load).
*
* @param cmd Bulk load command
* @return The update plan for this command.
* @throws IgniteCheckedException if failed.
*/
public static UpdatePlan planForBulkLoad(SqlBulkLoadCommand cmd, GridH2Table tbl) throws IgniteCheckedException {
GridH2RowDescriptor desc = tbl.rowDescriptor();
if (desc == null)
throw new IgniteSQLException("Row descriptor undefined for table '" + tbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
GridCacheContext<?, ?> cctx = desc.context();
List<String> cols = cmd.columns();
if (cols == null)
throw new IgniteSQLException("Columns are not defined", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
String[] colNames = new String[cols.size()];
Column[] h2Cols = new Column[cols.size()];
int[] colTypes = new int[cols.size()];
int keyColIdx = -1;
int valColIdx = -1;
boolean hasKeyProps = false;
boolean hasValProps = false;
for (int i = 0; i < cols.size(); i++) {
String colName = cols.get(i);
colNames[i] = colName;
Column h2Col = tbl.getColumn(colName);
h2Cols[i] = h2Col;
colTypes[i] = h2Col.getType();
int colId = h2Col.getColumnId();
if (desc.isKeyColumn(colId)) {
keyColIdx = i;
continue;
}
if (desc.isValueColumn(colId)) {
valColIdx = i;
continue;
}
GridQueryProperty prop = desc.type().property(colName);
assert prop != null : "Property '" + colName + "' not found.";
if (prop.key())
hasKeyProps = true;
else
hasValProps = true;
}
verifyDmlColumns(tbl, Arrays.asList(h2Cols));
KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
return new UpdatePlan(UpdateMode.BULK_LOAD, tbl, colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, null, true, null, 0, null, null, true, false);
}
use of org.apache.ignite.internal.processors.query.GridQueryProperty in project ignite by apache.
the class GridH2RowDescriptor method refreshMetadataFromTypeDescriptor.
/**
* Update metadata of this row descriptor according to current state of type descriptor.
*/
@SuppressWarnings({ "WeakerAccess", "ToArrayCallWithZeroLengthArrayArgument" })
public final void refreshMetadataFromTypeDescriptor() {
Map<String, Class<?>> allFields = new LinkedHashMap<>(type.fields());
fields = allFields.keySet().toArray(new String[allFields.size()]);
fieldTypes = new int[fields.length];
Class[] classes = allFields.values().toArray(new Class[fields.length]);
for (int i = 0; i < fieldTypes.length; i++) fieldTypes[i] = DataType.getTypeFromClass(classes[i]);
props = new GridQueryProperty[fields.length];
for (int i = 0; i < fields.length; i++) {
GridQueryProperty p = type.property(fields[i]);
assert p != null : fields[i];
props[i] = p;
}
List<String> fieldsList = Arrays.asList(fields);
keyAliasColId = (type.keyFieldName() != null) ? QueryUtils.DEFAULT_COLUMNS_COUNT + fieldsList.indexOf(type.keyFieldAlias()) : COL_NOT_EXISTS;
valAliasColId = (type.valueFieldName() != null) ? QueryUtils.DEFAULT_COLUMNS_COUNT + fieldsList.indexOf(type.valueFieldAlias()) : COL_NOT_EXISTS;
rowKeyColumnNames = Arrays.stream(props).filter(GridQueryProperty::key).map(GridQueryProperty::name).collect(Collectors.toSet());
}
use of org.apache.ignite.internal.processors.query.GridQueryProperty in project ignite by apache.
the class H2TableDescriptor method extractKeyColumns.
/**
* Create list of affinity and key index columns. Key, if it possible, partitions into simple components.
*
* @param tbl GridH2Table instance
* @param keyCol Key index column.
* @param affCol Affinity index column.
*
* @return List of key and affinity columns. Key's, if it possible, splitted into simple components.
*/
@NotNull
private List<IndexColumn> extractKeyColumns(GridH2Table tbl, IndexColumn keyCol, IndexColumn affCol) {
ArrayList<IndexColumn> keyCols;
if (isSql) {
keyCols = new ArrayList<>(type.fields().size() + 1);
// Check if key is simple type.
if (QueryUtils.isSqlType(type.keyClass()))
keyCols.add(keyCol);
else {
if (!type.primaryKeyFields().isEmpty()) {
for (String keyName : type.primaryKeyFields()) {
GridQueryProperty prop = type.property(keyName);
assert prop.key() : keyName + " is not a key field";
Column col = tbl.getColumn(prop.name());
keyCols.add(tbl.indexColumn(col.getColumnId(), SortOrder.ASCENDING));
}
} else {
for (String propName : type.fields().keySet()) {
GridQueryProperty prop = type.property(propName);
if (prop.key()) {
Column col = tbl.getColumn(propName);
keyCols.add(tbl.indexColumn(col.getColumnId(), SortOrder.ASCENDING));
}
}
}
// we have to fall back to whole-key index.
if (keyCols.isEmpty())
keyCols.add(keyCol);
}
} else {
keyCols = new ArrayList<>(2);
keyCols.add(keyCol);
}
if (affCol != null && !H2Utils.containsColumn(keyCols, affCol))
keyCols.add(affCol);
else
keyCols.trimToSize();
return Collections.unmodifiableList(keyCols);
}
use of org.apache.ignite.internal.processors.query.GridQueryProperty in project ignite by apache.
the class IgniteH2Indexing method columnsInformation.
/**
* {@inheritDoc}
*/
@Override
public Collection<ColumnInformation> columnsInformation(String schemaNamePtrn, String tblNamePtrn, String colNamePtrn) {
Collection<ColumnInformation> infos = new ArrayList<>();
// Gather information about tables.
schemaMgr.dataTables().stream().filter(t -> matches(t.getSchema().getName(), schemaNamePtrn)).filter(t -> matches(t.getName(), tblNamePtrn)).flatMap(tbl -> {
IndexColumn affCol = tbl.getAffinityKeyColumn();
return Stream.of(tbl.getColumns()).filter(Column::getVisible).filter(c -> matches(c.getName(), colNamePtrn)).map(c -> {
GridQueryProperty prop = tbl.rowDescriptor().type().property(c.getName());
boolean isAff = affCol != null && c.getColumnId() == affCol.column.getColumnId();
return new ColumnInformation(c.getColumnId() - QueryUtils.DEFAULT_COLUMNS_COUNT + 1, tbl.getSchema().getName(), tbl.getName(), c.getName(), prop.type(), c.isNullable(), prop.defaultValue(), prop.precision(), prop.scale(), isAff);
});
}).forEach(infos::add);
// Gather information about system views.
if (matches(QueryUtils.SCHEMA_SYS, schemaNamePtrn)) {
schemaMgr.systemViews().stream().filter(v -> matches(v.getTableName(), tblNamePtrn)).flatMap(view -> Stream.of(view.getColumns()).filter(c -> matches(c.getName(), colNamePtrn)).map(c -> new ColumnInformation(c.getColumnId() + 1, QueryUtils.SCHEMA_SYS, view.getTableName(), c.getName(), IgniteUtils.classForName(DataType.getTypeClassName(c.getType()), Object.class), c.isNullable(), null, (int) c.getPrecision(), c.getScale(), false))).forEach(infos::add);
}
return infos;
}
use of org.apache.ignite.internal.processors.query.GridQueryProperty in project ignite by apache.
the class CommandProcessor method runCommandH2.
/**
* Execute DDL statement.
*
* @param sql SQL.
* @param cmdH2 Command.
*/
private void runCommandH2(String sql, GridSqlStatement cmdH2) {
IgniteInternalFuture fut = null;
try {
finishActiveTxIfNecessary();
if (cmdH2 instanceof GridSqlCreateIndex) {
GridSqlCreateIndex cmd = (GridSqlCreateIndex) cmdH2;
isDdlOnSchemaSupported(cmd.schemaName());
GridH2Table tbl = schemaMgr.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl == null)
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
assert tbl.rowDescriptor() != null;
ensureDdlSupported(tbl);
QueryIndex newIdx = new QueryIndex();
newIdx.setName(cmd.index().getName());
newIdx.setIndexType(cmd.index().getIndexType());
LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>();
// Let's replace H2's table and property names by those operated by GridQueryProcessor.
GridQueryTypeDescriptor typeDesc = tbl.rowDescriptor().type();
for (Map.Entry<String, Boolean> e : cmd.index().getFields().entrySet()) {
GridQueryProperty prop = typeDesc.property(e.getKey());
if (prop == null)
throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, e.getKey());
flds.put(prop.name(), e.getValue());
}
newIdx.setFields(flds);
fut = ctx.query().dynamicIndexCreate(tbl.cacheName(), cmd.schemaName(), typeDesc.tableName(), newIdx, cmd.ifNotExists(), 0);
} else if (cmdH2 instanceof GridSqlDropIndex) {
GridSqlDropIndex cmd = (GridSqlDropIndex) cmdH2;
isDdlOnSchemaSupported(cmd.schemaName());
GridH2Table tbl = schemaMgr.dataTableForIndex(cmd.schemaName(), cmd.indexName());
if (tbl != null) {
ensureDdlSupported(tbl);
fut = ctx.query().dynamicIndexDrop(tbl.cacheName(), cmd.schemaName(), cmd.indexName(), cmd.ifExists());
} else {
if (cmd.ifExists())
fut = new GridFinishedFuture();
else
throw new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, cmd.indexName());
}
} else if (cmdH2 instanceof GridSqlCreateTable) {
GridSqlCreateTable cmd = (GridSqlCreateTable) cmdH2;
ctx.security().authorize(cmd.cacheName(), SecurityPermission.CACHE_CREATE);
isDdlOnSchemaSupported(cmd.schemaName());
GridH2Table tbl = schemaMgr.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl != null) {
if (!cmd.ifNotExists())
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_EXISTS, cmd.tableName());
} else {
QueryEntity e = toQueryEntity(cmd);
CacheConfiguration<?, ?> ccfg = new CacheConfiguration<>(cmd.tableName());
ccfg.setQueryEntities(Collections.singleton(e));
ccfg.setSqlSchema(cmd.schemaName());
SchemaOperationException err = QueryUtils.checkQueryEntityConflicts(ccfg, ctx.cache().cacheDescriptors().values());
if (err != null)
throw err;
if (!F.isEmpty(cmd.cacheName()) && ctx.cache().cacheDescriptor(cmd.cacheName()) != null) {
ctx.query().dynamicAddQueryEntity(cmd.cacheName(), cmd.schemaName(), e, cmd.parallelism(), true).get();
} else {
ctx.query().dynamicTableCreate(cmd.schemaName(), e, cmd.templateName(), cmd.cacheName(), cmd.cacheGroup(), cmd.dataRegionName(), cmd.affinityKey(), cmd.atomicityMode(), cmd.writeSynchronizationMode(), cmd.backups(), cmd.ifNotExists(), cmd.encrypted(), cmd.parallelism());
}
}
} else if (cmdH2 instanceof GridSqlDropTable) {
GridSqlDropTable cmd = (GridSqlDropTable) cmdH2;
isDdlOnSchemaSupported(cmd.schemaName());
GridH2Table tbl = schemaMgr.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl == null) {
if (!cmd.ifExists())
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
} else {
ctx.security().authorize(tbl.cacheName(), SecurityPermission.CACHE_DESTROY);
ctx.query().dynamicTableDrop(tbl.cacheName(), cmd.tableName(), cmd.ifExists());
}
} else if (cmdH2 instanceof GridSqlAlterTableAddColumn) {
GridSqlAlterTableAddColumn cmd = (GridSqlAlterTableAddColumn) cmdH2;
isDdlOnSchemaSupported(cmd.schemaName());
GridH2Table tbl = schemaMgr.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl == null) {
if (!cmd.ifTableExists())
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
} else {
if (QueryUtils.isSqlType(tbl.rowDescriptor().type().valueClass()))
throw new SchemaOperationException("Cannot add column(s) because table was created " + "with " + PARAM_WRAP_VALUE + "=false option.");
List<QueryField> cols = new ArrayList<>(cmd.columns().length);
boolean allFieldsNullable = true;
for (GridSqlColumn col : cmd.columns()) {
if (tbl.doesColumnExist(col.columnName())) {
if ((!cmd.ifNotExists() || cmd.columns().length != 1)) {
throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_EXISTS, col.columnName());
} else {
cols = null;
break;
}
}
QueryField field = new QueryField(col.columnName(), getTypeClassName(col), col.column().isNullable(), col.defaultValue(), col.precision(), col.scale());
cols.add(field);
allFieldsNullable &= field.isNullable();
}
if (cols != null) {
assert tbl.rowDescriptor() != null;
if (!allFieldsNullable)
QueryUtils.checkNotNullAllowed(tbl.cacheInfo().config());
fut = ctx.query().dynamicColumnAdd(tbl.cacheName(), cmd.schemaName(), tbl.rowDescriptor().type().tableName(), cols, cmd.ifTableExists(), cmd.ifNotExists());
}
}
} else if (cmdH2 instanceof GridSqlAlterTableDropColumn) {
GridSqlAlterTableDropColumn cmd = (GridSqlAlterTableDropColumn) cmdH2;
isDdlOnSchemaSupported(cmd.schemaName());
GridH2Table tbl = schemaMgr.dataTable(cmd.schemaName(), cmd.tableName());
if (tbl == null) {
if (!cmd.ifTableExists())
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
} else {
assert tbl.rowDescriptor() != null;
GridCacheContext cctx = tbl.cacheContext();
assert cctx != null;
if (cctx.mvccEnabled())
throw new IgniteSQLException("Cannot drop column(s) with enabled MVCC. " + "Operation is unsupported at the moment.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (QueryUtils.isSqlType(tbl.rowDescriptor().type().valueClass()))
throw new SchemaOperationException("Cannot drop column(s) because table was created " + "with " + PARAM_WRAP_VALUE + "=false option.");
List<String> cols = new ArrayList<>(cmd.columns().length);
GridQueryTypeDescriptor type = tbl.rowDescriptor().type();
for (String colName : cmd.columns()) {
if (!tbl.doesColumnExist(colName)) {
if ((!cmd.ifExists() || cmd.columns().length != 1)) {
throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, colName);
} else {
cols = null;
break;
}
}
SchemaOperationException err = QueryUtils.validateDropColumn(type, colName);
if (err != null)
throw err;
cols.add(colName);
}
if (cols != null) {
fut = ctx.query().dynamicColumnRemove(tbl.cacheName(), cmd.schemaName(), type.tableName(), cols, cmd.ifTableExists(), cmd.ifExists());
}
}
} else
throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (fut != null)
fut.get();
} catch (SchemaOperationException e) {
U.error(null, "DDL operation failure", e);
throw convert(e);
} catch (IgniteSQLException e) {
throw e;
} catch (Exception e) {
throw new IgniteSQLException(e.getMessage(), e);
}
}
Aggregations