use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.
the class IgniteH2Indexing method addInitialUserIndex.
/**
* Add initial user index.
*
* @param schemaName Schema name.
* @param desc Table descriptor.
* @param h2Idx User index.
* @throws IgniteCheckedException If failed.
*/
private void addInitialUserIndex(String schemaName, H2TableDescriptor desc, GridH2IndexBase h2Idx) throws IgniteCheckedException {
GridH2Table h2Tbl = desc.table();
h2Tbl.proposeUserIndex(h2Idx);
try {
String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, false);
executeSql(schemaName, sql);
} catch (Exception e) {
// Rollback and re-throw.
h2Tbl.rollbackUserIndex(h2Idx.getName());
throw e;
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.
the class IgniteH2Indexing method dynamicIndexCreate.
/**
* {@inheritDoc}
*/
@Override
public void dynamicIndexCreate(final String schemaName, final String tblName, final QueryIndexDescriptorImpl idxDesc, boolean ifNotExists, SchemaIndexCacheVisitor cacheVisitor) throws IgniteCheckedException {
// Locate table.
H2Schema schema = schemas.get(schemaName);
H2TableDescriptor desc = (schema != null ? schema.tableByName(tblName) : null);
if (desc == null)
throw new IgniteCheckedException("Table not found in internal H2 database [schemaName=" + schemaName + ", tblName=" + tblName + ']');
GridH2Table h2Tbl = desc.table();
// Create index.
final GridH2IndexBase h2Idx = desc.createUserIndex(idxDesc);
h2Tbl.proposeUserIndex(h2Idx);
try {
// Populate index with existing cache data.
final GridH2RowDescriptor rowDesc = h2Tbl.rowDescriptor();
SchemaIndexCacheVisitorClosure clo = new SchemaIndexCacheVisitorClosure() {
@Override
public void apply(CacheDataRow row) throws IgniteCheckedException {
GridH2Row h2Row = rowDesc.createRow(row);
h2Idx.putx(h2Row);
}
};
cacheVisitor.visit(clo);
// At this point index is in consistent state, promote it through H2 SQL statement, so that cached
// prepared statements are re-built.
String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, ifNotExists);
executeSql(schemaName, sql);
} catch (Exception e) {
// Rollback and re-throw.
h2Tbl.rollbackUserIndex(h2Idx.getName());
throw e;
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.
the class IgniteH2Indexing method collectCacheIds.
/**
* Collect cache identifiers from two-step query.
*
* @param mainCacheId Id of main cache.
* @param twoStepQry Two-step query.
* @return Result.
*/
@Nullable
public List<Integer> collectCacheIds(@Nullable Integer mainCacheId, GridCacheTwoStepQuery twoStepQry) {
LinkedHashSet<Integer> caches0 = new LinkedHashSet<>();
int tblCnt = twoStepQry.tablesCount();
if (mainCacheId != null)
caches0.add(mainCacheId);
if (tblCnt > 0) {
for (QueryTable tblKey : twoStepQry.tables()) {
GridH2Table tbl = dataTable(tblKey);
int cacheId = tbl.cacheId();
caches0.add(cacheId);
}
}
if (caches0.isEmpty())
return null;
else {
// Prohibit usage indices with different numbers of segments in same query.
List<Integer> cacheIds = new ArrayList<>(caches0);
checkCacheIndexSegmentation(cacheIds);
return cacheIds;
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.
the class UpdatePlanBuilder method planForUpdate.
/**
* Prepare update plan for UPDATE or DELETE.
*
* @param stmt UPDATE or DELETE statement.
* @param loc Local query flag.
* @param idx Indexing.
* @param conn Connection.
* @param fieldsQuery Original query.
* @param errKeysPos index to inject param for re-run keys at. Null if it's not a re-run plan.
* @return Update plan.
* @throws IgniteCheckedException if failed.
*/
private static UpdatePlan planForUpdate(GridSqlStatement stmt, boolean loc, IgniteH2Indexing idx, @Nullable Connection conn, @Nullable SqlFieldsQuery fieldsQuery, @Nullable Integer errKeysPos) throws IgniteCheckedException {
GridSqlElement target;
FastUpdate fastUpdate;
UpdateMode mode;
if (stmt instanceof GridSqlUpdate) {
// Let's verify that user is not trying to mess with key's columns directly
verifyUpdateColumns(stmt);
GridSqlUpdate update = (GridSqlUpdate) stmt;
target = update.target();
fastUpdate = DmlAstUtils.getFastUpdateArgs(update);
mode = UpdateMode.UPDATE;
} else if (stmt instanceof GridSqlDelete) {
GridSqlDelete del = (GridSqlDelete) stmt;
target = del.from();
fastUpdate = DmlAstUtils.getFastDeleteArgs(del);
mode = UpdateMode.DELETE;
} else
throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
GridSqlTable tbl = DmlAstUtils.gridTableForElement(target);
GridH2Table h2Tbl = tbl.dataTable();
GridH2RowDescriptor desc = h2Tbl.rowDescriptor();
if (desc == null)
throw new IgniteSQLException("Row descriptor undefined for table '" + h2Tbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
if (fastUpdate != null) {
return new UpdatePlan(mode, h2Tbl, null, fastUpdate, null);
} else {
GridSqlSelect sel;
if (stmt instanceof GridSqlUpdate) {
List<GridSqlColumn> updatedCols = ((GridSqlUpdate) stmt).cols();
int valColIdx = -1;
String[] colNames = new String[updatedCols.size()];
int[] colTypes = new int[updatedCols.size()];
for (int i = 0; i < updatedCols.size(); i++) {
colNames[i] = updatedCols.get(i).columnName();
colTypes[i] = updatedCols.get(i).resultType().type();
Column col = updatedCols.get(i).column();
if (desc.isValueColumn(col.getColumnId()))
valColIdx = i;
}
boolean hasNewVal = (valColIdx != -1);
// Statement updates distinct properties if it does not have _val in updated columns list
// or if its list of updated columns includes only _val, i.e. is single element.
boolean hasProps = !hasNewVal || updatedCols.size() > 1;
// Index of new _val in results of SELECT
if (hasNewVal)
valColIdx += 2;
int newValColIdx = (hasNewVal ? valColIdx : 1);
KeyValueSupplier valSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt, errKeysPos);
String selectSql = sel.getSQL();
DmlDistributedPlanInfo distributed = F.isEmpty(selectSql) ? null : checkPlanCanBeDistributed(idx, conn, fieldsQuery, loc, selectSql, tbl.dataTable().cacheName());
return new UpdatePlan(UpdateMode.UPDATE, h2Tbl, colNames, colTypes, null, valSupplier, -1, valColIdx, selectSql, false, null, 0, null, distributed);
} else {
sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt, errKeysPos);
String selectSql = sel.getSQL();
DmlDistributedPlanInfo distributed = F.isEmpty(selectSql) ? null : checkPlanCanBeDistributed(idx, conn, fieldsQuery, loc, selectSql, tbl.dataTable().cacheName());
return new UpdatePlan(UpdateMode.DELETE, h2Tbl, selectSql, null, distributed);
}
}
}
use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.
the class GridSqlQueryParser method isLocalQuery.
/**
* Check if query may be run locally on all caches mentioned in the query.
* @param replicatedOnlyQry replicated-only query flag from original {@link SqlFieldsQuery}.
* @return {@code true} if query may be run locally on all caches mentioned in the query, i.e. there's no need
* to run distributed query.
* @see SqlFieldsQuery#isReplicatedOnly()
*/
public boolean isLocalQuery(boolean replicatedOnlyQry) {
boolean hasCaches = false;
for (Object o : h2ObjToGridObj.values()) {
if (o instanceof GridSqlAlias)
o = GridSqlAlias.unwrap((GridSqlAst) o);
if (o instanceof GridSqlTable) {
GridH2Table tbl = ((GridSqlTable) o).dataTable();
if (tbl != null) {
hasCaches = true;
GridCacheContext cctx = tbl.cache();
if (!cctx.isLocal() && !(replicatedOnlyQry && cctx.isReplicatedAffinityNode()))
return false;
}
}
}
// if there are no caches, original SqlFieldsQuery's isLocal flag will be used.
return hasCaches;
}
Aggregations