Search in sources :

Example 1 with GridH2RowDescriptor

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor in project ignite by apache.

the class DmlAstUtils method getFastUpdateArgs.

/**
     * @param update UPDATE statement.
     * @return {@code null} if given statement directly updates {@code _val} column with a literal or param value
     * and filters by single non expression key (and, optionally,  by single non expression value).
     */
public static FastUpdateArguments getFastUpdateArgs(GridSqlUpdate update) {
    IgnitePair<GridSqlElement> filter = findKeyValueEqualityCondition(update.where());
    if (filter == null)
        return null;
    if (update.cols().size() != 1)
        return null;
    Table tbl = update.cols().get(0).column().getTable();
    if (!(tbl instanceof GridH2Table))
        return null;
    GridH2RowDescriptor desc = ((GridH2Table) tbl).rowDescriptor();
    if (!desc.isValueColumn(update.cols().get(0).column().getColumnId()))
        return null;
    GridSqlElement set = update.set().get(update.cols().get(0).columnName());
    if (!(set instanceof GridSqlConst || set instanceof GridSqlParameter))
        return null;
    return new FastUpdateArguments(operandForElement(filter.getKey()), operandForElement(filter.getValue()), operandForElement(set));
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) FastUpdateArguments(org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)

Example 2 with GridH2RowDescriptor

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor in project ignite by apache.

the class GridSqlQuerySplitter method extractPartitionFromEquality.

/**
 * Analyses the equality operation and extracts the partition if possible
 *
 * @param op AST equality operation.
 * @param ctx Kernal Context.
 * @return partition info, or {@code null} if none identified
 */
private static CacheQueryPartitionInfo extractPartitionFromEquality(GridSqlOperation op, GridKernalContext ctx) throws IgniteCheckedException {
    assert op.operationType() == GridSqlOperationType.EQUAL;
    GridSqlElement left = op.child(0);
    GridSqlElement right = op.child(1);
    if (!(left instanceof GridSqlColumn))
        return null;
    if (!(right instanceof GridSqlConst) && !(right instanceof GridSqlParameter))
        return null;
    GridSqlColumn column = (GridSqlColumn) left;
    assert column.column().getTable() instanceof GridH2Table;
    GridH2Table tbl = (GridH2Table) column.column().getTable();
    GridH2RowDescriptor desc = tbl.rowDescriptor();
    IndexColumn affKeyCol = tbl.getAffinityKeyColumn();
    int colId = column.column().getColumnId();
    if ((affKeyCol == null || colId != affKeyCol.column.getColumnId()) && !desc.isKeyColumn(colId))
        return null;
    if (right instanceof GridSqlConst) {
        GridSqlConst constant = (GridSqlConst) right;
        return new CacheQueryPartitionInfo(ctx.affinity().partition(tbl.cacheName(), constant.value().getObject()), null, null, -1, -1);
    }
    GridSqlParameter param = (GridSqlParameter) right;
    return new CacheQueryPartitionInfo(-1, tbl.cacheName(), tbl.getName(), column.column().getType(), param.index());
}
Also used : GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) CacheQueryPartitionInfo(org.apache.ignite.internal.processors.cache.query.CacheQueryPartitionInfo) IndexColumn(org.h2.table.IndexColumn)

Example 3 with GridH2RowDescriptor

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor in project ignite by apache.

the class IgniteH2Indexing method createTable.

/**
 * Create db table by using given table descriptor.
 *
 * @param schemaName Schema name.
 * @param schema Schema.
 * @param tbl Table descriptor.
 * @param conn Connection.
 * @throws SQLException If failed to create db table.
 * @throws IgniteCheckedException If failed.
 */
private void createTable(String schemaName, H2Schema schema, H2TableDescriptor tbl, Connection conn) throws SQLException, IgniteCheckedException {
    assert schema != null;
    assert tbl != null;
    String keyType = dbTypeFromClass(tbl.type().keyClass());
    String valTypeStr = dbTypeFromClass(tbl.type().valueClass());
    SB sql = new SB();
    String keyValVisibility = tbl.type().fields().isEmpty() ? " VISIBLE" : " INVISIBLE";
    sql.a("CREATE TABLE ").a(tbl.fullTableName()).a(" (").a(KEY_FIELD_NAME).a(' ').a(keyType).a(keyValVisibility).a(" NOT NULL");
    sql.a(',').a(VAL_FIELD_NAME).a(' ').a(valTypeStr).a(keyValVisibility);
    sql.a(',').a(VER_FIELD_NAME).a(" OTHER INVISIBLE");
    for (Map.Entry<String, Class<?>> e : tbl.type().fields().entrySet()) sql.a(',').a(H2Utils.withQuotes(e.getKey())).a(' ').a(dbTypeFromClass(e.getValue())).a(tbl.type().property(e.getKey()).notNull() ? " NOT NULL" : "");
    sql.a(')');
    if (log.isDebugEnabled())
        log.debug("Creating DB table with SQL: " + sql);
    GridH2RowDescriptor rowDesc = new GridH2RowDescriptor(this, tbl, tbl.type());
    H2RowFactory rowFactory = tbl.rowFactory(rowDesc);
    GridH2Table h2Tbl = H2TableEngine.createTable(conn, sql.toString(), rowDesc, rowFactory, tbl);
    for (GridH2IndexBase usrIdx : tbl.createUserIndexes()) addInitialUserIndex(schemaName, tbl, usrIdx);
    if (dataTables.putIfAbsent(h2Tbl.identifier(), h2Tbl) != null)
        throw new IllegalStateException("Table already exists: " + h2Tbl.identifierString());
}
Also used : GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) H2RowFactory(org.apache.ignite.internal.processors.query.h2.database.H2RowFactory) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 4 with GridH2RowDescriptor

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor in project ignite by apache.

the class ValidateIndexesClosure method processPartIterator.

/**
 * Process partition iterator.
 *
 * @param grpCtx Cache group context.
 * @param partRes Result object.
 * @param session H2 session.
 * @param it Partition iterator.
 * @throws IgniteCheckedException
 */
private void processPartIterator(CacheGroupContext grpCtx, ValidateIndexesPartitionResult partRes, Session session, GridIterator<CacheDataRow> it) throws IgniteCheckedException {
    boolean enoughIssues = false;
    GridQueryProcessor qryProcessor = ignite.context().query();
    final boolean skipConditions = checkFirst > 0 || checkThrough > 0;
    final boolean bothSkipConditions = checkFirst > 0 && checkThrough > 0;
    long current = 0;
    long processedNumber = 0;
    while (it.hasNextX() && !validateCtx.isCancelled()) {
        if (enoughIssues)
            break;
        CacheDataRow row = it.nextX();
        if (skipConditions) {
            if (bothSkipConditions) {
                if (processedNumber > checkFirst)
                    break;
                else if (current++ % checkThrough > 0)
                    continue;
                else
                    processedNumber++;
            } else {
                if (checkFirst > 0) {
                    if (current++ > checkFirst)
                        break;
                } else {
                    if (current++ % checkThrough > 0)
                        continue;
                }
            }
        }
        int cacheId = row.cacheId() == 0 ? grpCtx.groupId() : row.cacheId();
        GridCacheContext<?, ?> cacheCtx = row.cacheId() == 0 ? grpCtx.singleCacheContext() : grpCtx.shared().cacheContext(row.cacheId());
        if (cacheCtx == null)
            throw new IgniteException("Unknown cacheId of CacheDataRow: " + cacheId);
        if (row.link() == 0L) {
            String errMsg = "Invalid partition row, possibly deleted";
            log.error(errMsg);
            IndexValidationIssue is = new IndexValidationIssue(null, cacheCtx.name(), null, new IgniteCheckedException(errMsg));
            enoughIssues |= partRes.reportIssue(is);
            continue;
        }
        QueryTypeDescriptorImpl res = qryProcessor.typeByValue(cacheCtx.name(), cacheCtx.cacheObjectContext(), row.key(), row.value(), true);
        if (res == null)
            // Tolerate - (k, v) is just not indexed.
            continue;
        IgniteH2Indexing indexing = (IgniteH2Indexing) qryProcessor.getIndexing();
        GridH2Table gridH2Tbl = indexing.schemaManager().dataTable(cacheCtx.name(), res.tableName());
        if (gridH2Tbl == null)
            // Tolerate - (k, v) is just not indexed.
            continue;
        GridH2RowDescriptor gridH2RowDesc = gridH2Tbl.rowDescriptor();
        H2CacheRow h2Row = gridH2RowDesc.createRow(row);
        ArrayList<Index> indexes = gridH2Tbl.getIndexes();
        for (Index idx : indexes) {
            if (validateCtx.isCancelled())
                break;
            if (!(idx instanceof H2TreeIndexBase))
                continue;
            try {
                Cursor cursor = idx.find(session, h2Row, h2Row);
                if (cursor == null || !cursor.next())
                    throw new IgniteCheckedException("Key is present in CacheDataTree, but can't be found in SQL index.");
            } catch (Throwable t) {
                Object o = CacheObjectUtils.unwrapBinaryIfNeeded(grpCtx.cacheObjectContext(), row.key(), true, true);
                IndexValidationIssue is = new IndexValidationIssue(o.toString(), cacheCtx.name(), idx.getName(), t);
                log.error("Failed to lookup key: " + is.toString(), t);
                enoughIssues |= partRes.reportIssue(is);
            }
        }
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) QueryTypeDescriptorImpl(org.apache.ignite.internal.processors.query.QueryTypeDescriptorImpl) H2TreeIndexBase(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase) Index(org.h2.index.Index) H2CacheRow(org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow) Cursor(org.h2.index.Cursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) IgniteException(org.apache.ignite.IgniteException) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)

Example 5 with GridH2RowDescriptor

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor in project ignite by apache.

the class SchemaManager method createTable.

/**
 * Create db table by using given table descriptor.
 *
 * @param schemaName Schema name.
 * @param schema Schema.
 * @param tbl Table descriptor.
 * @param conn Connection.
 * @throws SQLException If failed to create db table.
 * @throws IgniteCheckedException If failed.
 */
private GridH2Table createTable(String schemaName, H2Schema schema, H2TableDescriptor tbl, H2PooledConnection conn) throws SQLException, IgniteCheckedException {
    assert schema != null;
    assert tbl != null;
    String sql = H2Utils.tableCreateSql(tbl);
    if (log.isDebugEnabled())
        log.debug("Creating DB table with SQL: " + sql);
    GridH2RowDescriptor rowDesc = new GridH2RowDescriptor(tbl, tbl.type());
    GridH2Table h2Tbl = H2TableEngine.createTable(conn.connection(), sql, rowDesc, tbl, ctx.indexProcessor());
    for (GridH2IndexBase usrIdx : tbl.createUserIndexes()) createInitialUserIndex(schemaName, tbl, usrIdx);
    return h2Tbl;
}
Also used : GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

Aggregations

GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)28 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)14 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)12 Column (org.h2.table.Column)12 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)9 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)8 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)7 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)7 ArrayList (java.util.ArrayList)6 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)6 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)6 HashMap (java.util.HashMap)4 List (java.util.List)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4 IndexColumn (org.h2.table.IndexColumn)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteException (org.apache.ignite.IgniteException)3 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)3 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)3 GridSqlDelete (org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete)3