Search in sources :

Example 56 with IN

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);
}
Also used : ResultSet(java.sql.ResultSet) QueryHistory(org.apache.ignite.internal.processors.query.QueryHistory) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)

Example 57 with IN

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);
}
Also used : ResultSet(java.sql.ResultSet) QueryHistory(org.apache.ignite.internal.processors.query.QueryHistory) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)

Example 58 with IN

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);
        }
    }
}
Also used : GridSqlDelete(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Column(org.h2.table.Column) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlUpdate(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)

Example 59 with IN

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);
    }
}
Also used : DbObject(org.h2.engine.DbObject) QueryUtils(org.apache.ignite.internal.processors.query.QueryUtils) H2IndexType(org.apache.ignite.internal.processors.query.h2.database.H2IndexType) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) DbException(org.h2.message.DbException) H2Utils(org.apache.ignite.internal.processors.query.h2.H2Utils) Index(org.h2.index.Index) SCAN_INDEX_NAME_SUFFIX(org.apache.ignite.internal.processors.query.h2.opt.H2TableScanIndex.SCAN_INDEX_NAME_SUFFIX) IndexKeyDefinition(org.apache.ignite.internal.cache.query.index.sorted.IndexKeyDefinition) SysProperties(org.h2.engine.SysProperties) Map(java.util.Map) Row(org.h2.result.Row) IndexingQueryCacheFilter(org.apache.ignite.spi.indexing.IndexingQueryCacheFilter) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) IndexProcessor(org.apache.ignite.internal.cache.query.index.IndexProcessor) QueryTable(org.apache.ignite.internal.processors.cache.query.QueryTable) CachePeekMode(org.apache.ignite.cache.CachePeekMode) QueryField(org.apache.ignite.internal.processors.query.QueryField) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortOrder(org.h2.result.SortOrder) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) Collectors(java.util.stream.Collectors) Insert(org.h2.command.dml.Insert) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) IndexColumn(org.h2.table.IndexColumn) Stream(java.util.stream.Stream) DataType(org.h2.value.DataType) CreateTableData(org.h2.command.ddl.CreateTableData) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) LongAdder(java.util.concurrent.atomic.LongAdder) AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) SchemaObject(org.h2.schema.SchemaObject) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) IgniteLogger(org.apache.ignite.IgniteLogger) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) IndexInformation(org.apache.ignite.internal.processors.query.h2.database.IndexInformation) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridKernalContext(org.apache.ignite.internal.GridKernalContext) LinkedHashMap(java.util.LinkedHashMap) Column(org.h2.table.Column) ObjectStatistics(org.apache.ignite.internal.processors.query.stat.ObjectStatistics) Session(org.h2.engine.Session) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) IndexType(org.h2.index.IndexType) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IndexName(org.apache.ignite.internal.cache.query.index.IndexName) H2TableDescriptor(org.apache.ignite.internal.processors.query.h2.H2TableDescriptor) Table(org.h2.table.Table) StatisticsKey(org.apache.ignite.internal.processors.query.stat.StatisticsKey) F(org.apache.ignite.internal.util.typedef.F) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) H2TreeIndexBase(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase) GridCacheContextInfo(org.apache.ignite.internal.processors.cache.GridCacheContextInfo) TableType(org.h2.table.TableType) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) SpatialIndex(org.h2.index.SpatialIndex) Lock(java.util.concurrent.locks.Lock) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IndexDefinition(org.apache.ignite.internal.cache.query.index.IndexDefinition) TableBase(org.h2.table.TableBase) PK_HASH_IDX_NAME(org.apache.ignite.internal.processors.query.h2.H2TableDescriptor.PK_HASH_IDX_NAME) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) SpatialIndex(org.h2.index.SpatialIndex)

Example 60 with IN

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");
}
Also used : GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)35 ArrayList (java.util.ArrayList)29 IgniteException (org.apache.ignite.IgniteException)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)26 SQLException (java.sql.SQLException)21 List (java.util.List)21 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)19 CacheException (javax.cache.CacheException)15 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)13 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)13 Column (org.h2.table.Column)13 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)10 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)10 ResultSet (java.sql.ResultSet)9 HashMap (java.util.HashMap)9 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)9 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)9 PreparedStatement (java.sql.PreparedStatement)8 HashSet (java.util.HashSet)8