use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL in project ignite by apache.
the class GridSubqueryJoinOptimizer method pullOutSubQryFromInClause.
/**
* Pull out sub-select from IN clause to the parent select level.
* <p>
* Example:
* <pre>
* Before:
* SELECT name
* FROM emp e
* WHERE e.dep_id IN (SELECT d.id FROM dep d WHERE d.name = 'dep1')
* AND sal > 2000
*
* After:
* SELECT name
* FROM emp e
* JOIN dep d
* WHERE sal > 2000 AND d.id = e.dep_id and d.name = 'dep1
* </pre>
*
* @param parent Parent select.
* @param targetEl Target sql element. Can be {@code null}.
* @param childInd Column index.
*/
private static boolean pullOutSubQryFromInClause(GridSqlSelect parent, @Nullable GridSqlAst targetEl, int childInd) {
// extract sub-query
GridSqlSubquery subQry = targetEl != null ? targetEl.child(childInd).child(1) : parent.where().child(1);
if (!isSimpleSelect(subQry.subquery()))
return false;
GridSqlSelect subS = subQry.subquery();
GridSqlAst leftExpr = targetEl != null ? targetEl.child(childInd).child(0) : parent.where().child(0);
// 4) c1 + c2/const IN (SELECT in_col FROM ...)
if (subS.visibleColumns() != 1)
return false;
List<GridSqlElement> conditions = new ArrayList<>();
if (leftExpr instanceof GridSqlArray) {
GridSqlAst col = subS.columns(true).get(0);
if (!(col instanceof GridSqlArray) || leftExpr.size() != col.size())
return false;
for (int i = 0; i < col.size(); i++) {
GridSqlElement el = leftExpr.child(i);
conditions.add(new GridSqlOperation(EQUAL, el, col.child(i)));
}
} else if (targetEl instanceof GridSqlFunction)
return false;
else
conditions.add(new GridSqlOperation(EQUAL, leftExpr, subS.columns(true).get(0)));
// save old condition and IN expression to restore them
// in case of unsuccessfull pull out
GridSqlElement oldCond = (GridSqlElement) subS.where();
if (oldCond != null)
conditions.add(oldCond);
GridSqlElement oldInExpr = targetEl != null ? targetEl.child(childInd) : (GridSqlElement) parent.where();
// update sub-query condition and convert IN clause to EXISTS
subS.where(buildConditionBush(conditions));
GridSqlOperation existsExpr = new GridSqlOperation(EXISTS, subQry);
if (targetEl != null)
targetEl.child(childInd, existsExpr);
else
parent.where(existsExpr);
boolean res = pullOutSubQryFromExistsClause(parent, targetEl, childInd);
if (!res) {
// restore original query in case of failure
if (targetEl != null)
targetEl.child(childInd, oldInExpr);
else
parent.where(oldInExpr);
subS.where(oldCond);
}
return res;
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL in project ignite by apache.
the class GridH2Table method checkIndexPresence.
/**
* Checks index presence, return {@link Index} if index with same name or same fields and search direction already
* exist or {@code null} othervise.
*
* @param curIdx Index to check.
* @return Index if equal or subset index exist.
* @throws IgniteCheckedException If failed.
*/
@Nullable
private Index checkIndexPresence(Index curIdx) throws IgniteCheckedException {
IndexColumn[] curColumns = curIdx.getIndexColumns();
Index registredIdx = null;
for (Index idx : idxs) {
if (!(idx instanceof H2TreeIndex))
continue;
if (F.eq(curIdx.getName(), idx.getName()))
throw new IgniteCheckedException("Index already exists: " + idx.getName());
IndexColumn[] idxColumns = idx.getIndexColumns();
for (int i = 0; i < Math.min(idxColumns.length, curColumns.length); ++i) {
IndexColumn idxCol = idxColumns[i];
IndexColumn curCol = curColumns[i];
// pk attach at the end of listed fields.
if (curCol.column.getColumnId() == 0 && registredIdx != null)
continue;
if (H2Utils.equals(idxCol, curCol) && idxCol.sortType == curCol.sortType)
registredIdx = idx;
else {
registredIdx = null;
break;
}
}
if (registredIdx != null)
return registredIdx;
}
return null;
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL in project ignite by apache.
the class ValidateIndexesClosure method checkSizes.
/**
* Checking size of records in cache and indexes with a record into
* {@code checkSizeRes} if they are not equal.
*
* @param cacheSizesFutures Futures calculating size of records in caches.
* @param idxSizeFutures Futures calculating size of indexes of caches.
* @param checkSizeRes Result of size check.
*/
private void checkSizes(List<T3<CacheGroupContext, GridDhtLocalPartition, Future<CacheSize>>> cacheSizesFutures, List<T3<GridCacheContext, Index, Future<T2<Throwable, Long>>>> idxSizeFutures, Map<String, ValidateIndexesCheckSizeResult> checkSizeRes) throws ExecutionException, InterruptedException {
if (!checkSizes)
return;
Map<Integer, CacheSize> cacheSizeTotal = new HashMap<>();
for (T3<CacheGroupContext, GridDhtLocalPartition, Future<CacheSize>> cacheSizeFut : cacheSizesFutures) {
CacheGroupContext cacheGrpCtx = cacheSizeFut.get1();
CacheSize cacheSize = cacheSizeFut.get3().get();
Throwable cacheSizeErr = cacheSize.err;
int grpId = cacheGrpCtx.groupId();
if (failCalcCacheSizeGrpIds.contains(grpId) && nonNull(cacheSizeErr)) {
checkSizeRes.computeIfAbsent(cacheGrpInfo(cacheGrpCtx), s -> new ValidateIndexesCheckSizeResult(0, new ArrayList<>())).issues().add(new ValidateIndexesCheckSizeIssue(null, 0, cacheSizeErr));
} else {
cacheSizeTotal.computeIfAbsent(grpId, i -> new CacheSize(null, new HashMap<>())).merge(cacheSize.cacheSizePerTbl);
}
}
for (T3<GridCacheContext, Index, Future<T2<Throwable, Long>>> idxSizeFut : idxSizeFutures) {
GridCacheContext cacheCtx = idxSizeFut.get1();
int grpId = cacheCtx.groupId();
if (failCalcCacheSizeGrpIds.contains(grpId))
continue;
Index idx = idxSizeFut.get2();
String tblName = idx.getTable().getName();
AtomicLong cacheSizeObj = cacheSizeTotal.get(grpId).cacheSizePerTbl.getOrDefault(cacheCtx.cacheId(), emptyMap()).get(tblName);
long cacheSizeByTbl = isNull(cacheSizeObj) ? 0L : cacheSizeObj.get();
T2<Throwable, Long> idxSizeRes = idxSizeFut.get3().get();
Throwable err = idxSizeRes.get1();
long idxSize = idxSizeRes.get2();
if (isNull(err) && idxSize != cacheSizeByTbl)
err = new IgniteException("Cache and index size not same.");
if (nonNull(err)) {
checkSizeRes.computeIfAbsent("[" + cacheGrpInfo(cacheCtx.group()) + ", " + cacheInfo(cacheCtx) + ", tableName=" + tblName + "]", s -> new ValidateIndexesCheckSizeResult(cacheSizeByTbl, new ArrayList<>())).issues().add(new ValidateIndexesCheckSizeIssue(idx.getName(), idxSize, err));
}
}
}
Aggregations