use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.
the class JdbcThinPartitionAwarenessTransactionsSelfTest method checkNodesUsage.
/**
* Utility method that:
* 1. warms up an affinity cache;
* 2. resets query history;
* 3. executes given query multiple times;
* 4. checks query history metrics in order to verify that not more than expected nodes were used.
*
* @param sql Sql query, either prepared statement or sql query should be used.
* @param expRowsCnt Expected rows count within result.
* @param dml Flag that signals whether we execute dml or not.
* @throws Exception If failed.
*/
private void checkNodesUsage(String sql, int expRowsCnt, boolean dml) throws Exception {
// Warm up an affinity cache.
if (dml)
stmt.executeUpdate(sql);
else
stmt.executeQuery(sql);
// Reset query history.
for (int i = 0; i < NODES_CNT; i++) {
((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().resetQueryHistoryMetrics();
}
// Execute query multiple times
for (int i = 0; i < NODES_CNT * QUERY_EXECUTION_MULTIPLIER; i++) {
ResultSet rs = null;
int updatedRowsCnt = 0;
if (dml)
updatedRowsCnt = stmt.executeUpdate(sql);
else
rs = stmt.executeQuery(sql);
if (dml) {
assertEquals("Unexpected updated rows count: expected [" + expRowsCnt + "]," + " got [" + updatedRowsCnt + "]", expRowsCnt, updatedRowsCnt);
} else {
assert rs != null;
int gotRowsCnt = 0;
while (rs.next()) gotRowsCnt++;
assertEquals("Unexpected rows count: expected [" + expRowsCnt + "], got [" + gotRowsCnt + "]", expRowsCnt, gotRowsCnt);
}
}
// Check query history metrics in order to verify that not more than expected nodes were used.
int nonEmptyMetricsCntr = 0;
int qryExecutionsCntr = 0;
for (int i = 0; i < NODES_CNT; i++) {
Collection<QueryHistory> metrics = ((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().queryHistoryMetrics().values();
if (!metrics.isEmpty()) {
nonEmptyMetricsCntr++;
qryExecutionsCntr += new ArrayList<>(metrics).get(0).executions();
}
}
assertTrue("Unexpected amount of used nodes: expected [0 < nodesCnt <= 1" + "], got [" + nonEmptyMetricsCntr + "]", nonEmptyMetricsCntr == 1);
assertEquals("Executions count doesn't match expeted value: expected [" + NODES_CNT * QUERY_EXECUTION_MULTIPLIER + "], got [" + qryExecutionsCntr + "]", NODES_CNT * QUERY_EXECUTION_MULTIPLIER, qryExecutionsCntr);
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.
the class JdbcThinPartitionAwarenessSelfTest method checkNodesUsage.
/**
* Utility method that:
* 1. warms up an affinity cache;
* 2. resets query history;
* 3. executes given query multiple times;
* 4. checks query history metrics in order to verify that not more than expected nodes were used.
*
* @param ps Prepared statement, either prepared statement or sql query should be used.
* @param sql Sql query, either prepared statement or sql query should be used.
* @param maxNodesUsedCnt Expected maximum number of used nodes.
* @param expRowsCnt Expected rows count within result.
* @param dml Flag that signals whether we execute dml or not.
* @throws Exception If failed.
*/
private void checkNodesUsage(PreparedStatement ps, Statement stmt, String sql, int maxNodesUsedCnt, int expRowsCnt, boolean dml) throws Exception {
// Warm up an affinity cache.
if (ps != null)
if (dml)
ps.executeUpdate();
else
ps.executeQuery();
else {
if (dml)
stmt.executeUpdate(sql);
else
stmt.executeQuery(sql);
}
// Reset query history.
for (int i = 0; i < NODES_CNT; i++) {
((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().resetQueryHistoryMetrics();
}
// Execute query multiple times
for (int i = 0; i < NODES_CNT * QUERY_EXECUTION_MULTIPLIER; i++) {
ResultSet rs = null;
int updatedRowsCnt = 0;
if (ps != null)
if (dml)
updatedRowsCnt = ps.executeUpdate();
else
rs = ps.executeQuery();
else {
if (dml)
updatedRowsCnt = stmt.executeUpdate(sql);
else
rs = stmt.executeQuery(sql);
}
if (dml) {
assertEquals("Unexpected updated rows count: expected [" + expRowsCnt + "]," + " got [" + updatedRowsCnt + "]", expRowsCnt, updatedRowsCnt);
} else {
assert rs != null;
int gotRowsCnt = 0;
while (rs.next()) gotRowsCnt++;
assertEquals("Unexpected rows count: expected [" + expRowsCnt + "], got [" + gotRowsCnt + "]", expRowsCnt, gotRowsCnt);
}
}
// Check query history metrics in order to verify that not more than expected nodes were used.
int nonEmptyMetricsCntr = 0;
int qryExecutionsCntr = 0;
for (int i = 0; i < NODES_CNT; i++) {
Collection<QueryHistory> metrics = ((IgniteH2Indexing) grid(i).context().query().getIndexing()).runningQueryManager().queryHistoryMetrics().values();
if (!metrics.isEmpty()) {
nonEmptyMetricsCntr++;
qryExecutionsCntr += new ArrayList<>(metrics).get(0).executions();
}
}
assertTrue("Unexpected amount of used nodes: expected [0 < nodesCnt <= " + maxNodesUsedCnt + "], got [" + nonEmptyMetricsCntr + "]", nonEmptyMetricsCntr > 0 && nonEmptyMetricsCntr <= maxNodesUsedCnt);
assertEquals("Executions count doesn't match expected value: expected [" + NODES_CNT * QUERY_EXECUTION_MULTIPLIER + "], got [" + qryExecutionsCntr + "]", NODES_CNT * QUERY_EXECUTION_MULTIPLIER, qryExecutionsCntr);
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.
the class UpdatePlanBuilder method planForUpdate.
/**
* Prepare update plan for UPDATE or DELETE.
*
* @param planKey Plan key.
* @param stmt UPDATE or DELETE statement.
* @param idx Indexing.
* @param mvccEnabled MVCC flag.
* @return Update plan.
* @throws IgniteCheckedException if failed.
*/
private static UpdatePlan planForUpdate(QueryDescriptor planKey, GridSqlStatement stmt, IgniteH2Indexing idx, boolean mvccEnabled, IgniteLogger log) 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();
assert h2Tbl != null;
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);
String selectSql = sel.getSQL();
DmlDistributedPlanInfo distributed = null;
if (!F.isEmpty(selectSql)) {
distributed = checkPlanCanBeDistributed(idx, mvccEnabled, planKey, selectSql, tbl.dataTable().cacheName(), log);
}
return new UpdatePlan(UpdateMode.UPDATE, h2Tbl, colNames, colTypes, null, valSupplier, -1, valColIdx, selectSql, false, null, 0, null, distributed, sel.canBeLazy(), false);
} else {
sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt);
String selectSql = sel.getSQL();
DmlDistributedPlanInfo distributed = null;
if (!F.isEmpty(selectSql)) {
distributed = checkPlanCanBeDistributed(idx, mvccEnabled, planKey, selectSql, tbl.dataTable().cacheName(), log);
}
return new UpdatePlan(UpdateMode.DELETE, h2Tbl, selectSql, null, distributed);
}
}
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.
the class GridH2Table method proposeUserIndex.
/**
* Add index that is in an intermediate state and is still being built, thus is not used in queries until it is
* promoted.
*
* @param idx Index to add.
* @throws IgniteCheckedException If failed.
*/
public void proposeUserIndex(Index idx) throws IgniteCheckedException {
assert idx instanceof GridH2IndexBase;
lock(true);
try {
ensureNotDestroyed();
Index idxExist = checkIndexPresence(idx);
if (idxExist != null) {
String idxCols = Stream.of(idxExist.getIndexColumns()).map(k -> k.columnName).collect(Collectors.joining(", "));
U.warn(log, "Index with the given set or subset of columns already exists " + "(consider dropping either new or existing index) [cacheName=" + cacheInfo.name() + ", " + "schemaName=" + getSchema().getName() + ", tableName=" + getName() + ", newIndexName=" + idx.getName() + ", existingIndexName=" + idxExist.getName() + ", existingIndexColumns=[" + idxCols + "]]");
}
Index oldTmpIdx = tmpIdxs.put(idx.getName(), (GridH2IndexBase) idx);
assert oldTmpIdx == null;
} finally {
unlock(true);
}
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.
the class QueryIndexDefinition method treeName.
/**
* {@inheritDoc}
*/
@Override
public String treeName() {
GridH2RowDescriptor rowDesc = table.rowDescriptor();
String typeIdStr = "";
if (rowDesc != null) {
GridQueryTypeDescriptor typeDesc = rowDesc.type();
int typeId = cctx.binaryMarshaller() ? typeDesc.typeId() : typeDesc.valueClass().hashCode();
typeIdStr = typeId + "_";
}
// Legacy in treeName from H2Tree.
return BPlusTree.treeName(typeIdStr + idxName().idxName(), "H2Tree");
}
Aggregations