Search in sources :

Example 11 with GridSqlTable

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable 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);
        }
    }
}
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) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) 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 12 with GridSqlTable

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable 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;
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

Aggregations

GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)9 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)8 HashSet (java.util.HashSet)7 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)7 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)6 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)6 Column (org.h2.table.Column)6 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)4 GridSqlUpdate (org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)3 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)2 GridSqlDelete (org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete)2 GridSqlInsert (org.apache.ignite.internal.processors.query.h2.sql.GridSqlInsert)2 GridSqlMerge (org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge)2 GridSqlQuery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery)2 GridSqlUnion (org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion)2 ValueString (org.h2.value.ValueString)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CacheException (javax.cache.CacheException)1