Search in sources :

Example 6 with DmlDistributedPlanInfo

use of org.apache.ignite.internal.processors.query.h2.dml.DmlDistributedPlanInfo in project ignite by apache.

the class UpdatePlanBuilder method planForInsert.

/**
 * Prepare update plan for INSERT or MERGE.
 *
 * @param planKey Plan key.
 * @param stmt INSERT or MERGE statement.
 * @param idx Indexing.
 * @param mvccEnabled Mvcc flag.
 * @return Update plan.
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings("ConstantConditions")
private static UpdatePlan planForInsert(QueryDescriptor planKey, GridSqlStatement stmt, IgniteH2Indexing idx, boolean mvccEnabled, IgniteLogger log, boolean forceFillAbsentPKsWithDefaults) throws IgniteCheckedException {
    GridSqlQuery sel = null;
    GridSqlElement target;
    GridSqlColumn[] cols;
    boolean isTwoStepSubqry;
    int rowsNum;
    GridSqlTable tbl;
    GridH2RowDescriptor desc;
    List<GridSqlElement[]> elRows = null;
    UpdateMode mode;
    if (stmt instanceof GridSqlInsert) {
        mode = UpdateMode.INSERT;
        GridSqlInsert ins = (GridSqlInsert) stmt;
        target = ins.into();
        tbl = DmlAstUtils.gridTableForElement(target);
        GridH2Table h2Tbl = tbl.dataTable();
        assert h2Tbl != null;
        desc = h2Tbl.rowDescriptor();
        cols = ins.columns();
        if (noQuery(ins.rows()))
            elRows = ins.rows();
        else
            sel = DmlAstUtils.selectForInsertOrMerge(cols, ins.rows(), ins.query());
        isTwoStepSubqry = (ins.query() != null);
        rowsNum = isTwoStepSubqry ? 0 : ins.rows().size();
    } else if (stmt instanceof GridSqlMerge) {
        mode = UpdateMode.MERGE;
        GridSqlMerge merge = (GridSqlMerge) stmt;
        target = merge.into();
        tbl = DmlAstUtils.gridTableForElement(target);
        desc = tbl.dataTable().rowDescriptor();
        cols = merge.columns();
        if (noQuery(merge.rows()))
            elRows = merge.rows();
        else
            sel = DmlAstUtils.selectForInsertOrMerge(cols, merge.rows(), merge.query());
        isTwoStepSubqry = (merge.query() != null);
        rowsNum = isTwoStepSubqry ? 0 : merge.rows().size();
    } else {
        throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
    }
    // Let's set the flag only for subqueries that have their FROM specified.
    isTwoStepSubqry &= (sel != null && (sel instanceof GridSqlUnion || (sel instanceof GridSqlSelect && ((GridSqlSelect) sel).from() != null)));
    int keyColIdx = -1;
    int valColIdx = -1;
    boolean hasKeyProps = false;
    boolean hasValProps = false;
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + tbl.dataTable().getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    GridCacheContext<?, ?> cctx = desc.context();
    String[] colNames = new String[cols.length];
    int[] colTypes = new int[cols.length];
    GridQueryTypeDescriptor type = desc.type();
    Set<String> rowKeys = desc.getRowKeyColumnNames();
    boolean onlyVisibleColumns = true;
    for (int i = 0; i < cols.length; i++) {
        GridSqlColumn col = cols[i];
        if (!col.column().getVisible())
            onlyVisibleColumns = false;
        String colName = col.columnName();
        colNames[i] = colName;
        colTypes[i] = col.resultType().type();
        rowKeys.remove(colName);
        int colId = col.column().getColumnId();
        if (desc.isKeyColumn(colId)) {
            keyColIdx = i;
            continue;
        }
        if (desc.isValueColumn(colId)) {
            valColIdx = i;
            continue;
        }
        GridQueryProperty prop = desc.type().property(colName);
        assert prop != null : "Property '" + colName + "' not found.";
        if (prop.key())
            hasKeyProps = true;
        else
            hasValProps = true;
    }
    rowKeys.removeIf(rowKey -> desc.type().property(rowKey).defaultValue() != null);
    boolean fillAbsentPKsWithNullsOrDefaults = type.fillAbsentPKsWithDefaults() || forceFillAbsentPKsWithDefaults;
    if (fillAbsentPKsWithNullsOrDefaults && onlyVisibleColumns && !rowKeys.isEmpty()) {
        String[] extendedColNames = new String[rowKeys.size() + colNames.length];
        int[] extendedColTypes = new int[rowKeys.size() + colTypes.length];
        System.arraycopy(colNames, 0, extendedColNames, 0, colNames.length);
        System.arraycopy(colTypes, 0, extendedColTypes, 0, colTypes.length);
        int currId = colNames.length;
        for (String key : rowKeys) {
            Column col = tbl.dataTable().getColumn(key);
            extendedColNames[currId] = col.getName();
            extendedColTypes[currId] = col.getType();
            currId++;
        }
        colNames = extendedColNames;
        colTypes = extendedColTypes;
    }
    verifyDmlColumns(tbl.dataTable(), F.viewReadOnly(Arrays.asList(cols), TO_H2_COL));
    KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
    KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
    String selectSql = sel != null ? sel.getSQL() : null;
    DmlDistributedPlanInfo distributed = null;
    if (rowsNum == 0 && !F.isEmpty(selectSql)) {
        distributed = checkPlanCanBeDistributed(idx, mvccEnabled, planKey, selectSql, tbl.dataTable().cacheName(), log);
    }
    List<List<DmlArgument>> rows = null;
    if (elRows != null) {
        assert sel == null;
        rows = new ArrayList<>(elRows.size());
        for (GridSqlElement[] elRow : elRows) {
            List<DmlArgument> row = new ArrayList<>(cols.length);
            for (GridSqlElement el : elRow) {
                DmlArgument arg = DmlArguments.create(el);
                row.add(arg);
            }
            rows.add(row);
        }
    }
    return new UpdatePlan(mode, tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, selectSql, !isTwoStepSubqry, rows, rowsNum, null, distributed, false, fillAbsentPKsWithNullsOrDefaults);
}
Also used : GridSqlMerge(org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge) ArrayList(java.util.ArrayList) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) 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) GridSqlInsert(org.apache.ignite.internal.processors.query.h2.sql.GridSqlInsert) List(java.util.List) ArrayList(java.util.ArrayList) GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridSqlQuery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) 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)

Example 7 with DmlDistributedPlanInfo

use of org.apache.ignite.internal.processors.query.h2.dml.DmlDistributedPlanInfo in project ignite by apache.

the class IgniteH2Indexing method executeUpdateTransactional.

/**
 * Execute update in transactional mode.
 *
 * @param qryId Query id.
 * @param qryDesc Query descriptor.
 * @param qryParams Query parameters.
 * @param dml Plan.
 * @param loc Local flag.
 * @param cancel Cancel hook.
 * @return Update result.
 * @throws IgniteCheckedException If failed.
 */
private UpdateResult executeUpdateTransactional(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultDml dml, boolean loc, GridQueryCancel cancel) throws IgniteCheckedException {
    UpdatePlan plan = dml.plan();
    GridCacheContext cctx = plan.cacheContext();
    assert cctx != null;
    assert cctx.transactional();
    GridNearTxLocal tx = tx(ctx);
    boolean implicit = (tx == null);
    boolean commit = implicit && qryParams.autoCommit();
    if (implicit)
        tx = txStart(cctx, qryParams.timeout());
    requestSnapshot(tx);
    try (GridNearTxLocal toCommit = commit ? tx : null) {
        DmlDistributedPlanInfo distributedPlan = loc ? null : plan.distributedPlan();
        long timeout = implicit ? tx.remainingTime() : operationTimeout(qryParams.timeout(), tx);
        if (cctx.isReplicated() || distributedPlan == null || ((plan.mode() == UpdateMode.INSERT || plan.mode() == UpdateMode.MERGE) && !plan.isLocalSubquery())) {
            boolean sequential = true;
            UpdateSourceIterator<?> it;
            if (plan.fastResult()) {
                IgniteBiTuple row = plan.getFastRow(qryParams.arguments());
                assert row != null;
                EnlistOperation op = UpdatePlan.enlistOperation(plan.mode());
                it = new DmlUpdateSingleEntryIterator<>(op, op.isDeleteOrLock() ? row.getKey() : row);
            } else if (plan.hasRows()) {
                it = new DmlUpdateResultsIterator(UpdatePlan.enlistOperation(plan.mode()), plan, plan.createRows(qryParams.arguments()));
            } else {
                SqlFieldsQuery selectFieldsQry = new SqlFieldsQuery(plan.selectQuery(), qryDesc.collocated()).setArgs(qryParams.arguments()).setDistributedJoins(qryDesc.distributedJoins()).setEnforceJoinOrder(qryDesc.enforceJoinOrder()).setLocal(qryDesc.local()).setPageSize(qryParams.pageSize()).setTimeout((int) timeout, TimeUnit.MILLISECONDS).setLazy(qryParams.lazy());
                FieldsQueryCursor<List<?>> cur = executeSelectForDml(qryId, qryDesc.schemaName(), selectFieldsQry, MvccUtils.mvccTracker(cctx, tx), cancel, (int) timeout);
                it = plan.iteratorForTransaction(connMgr, cur);
            }
            // TODO: IGNITE-11176 - Need to support cancellation
            IgniteInternalFuture<Long> fut = tx.updateAsync(cctx, it, qryParams.pageSize(), timeout, sequential);
            UpdateResult res = new UpdateResult(fut.get(), X.EMPTY_OBJECT_ARRAY, plan.distributedPlan() != null ? plan.distributedPlan().derivedPartitions() : null);
            if (commit)
                toCommit.commit();
            return res;
        }
        int[] ids = U.toIntArray(distributedPlan.getCacheIds());
        int flags = 0;
        if (qryDesc.enforceJoinOrder())
            flags |= GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER;
        if (distributedPlan.isReplicatedOnly())
            flags |= GridH2QueryRequest.FLAG_REPLICATED;
        if (qryParams.lazy())
            flags |= GridH2QueryRequest.FLAG_LAZY;
        flags = GridH2QueryRequest.setDataPageScanEnabled(flags, qryParams.dataPageScanEnabled());
        int[] parts = PartitionResult.calculatePartitions(qryParams.partitions(), distributedPlan.derivedPartitions(), qryParams.arguments());
        if (parts != null && parts.length == 0)
            return new UpdateResult(0, X.EMPTY_OBJECT_ARRAY, distributedPlan.derivedPartitions());
        else {
            // TODO: IGNITE-11176 - Need to support cancellation
            IgniteInternalFuture<Long> fut = tx.updateAsync(cctx, ids, parts, qryDesc.schemaName(), qryDesc.sql(), qryParams.arguments(), flags, qryParams.pageSize(), timeout);
            UpdateResult res = new UpdateResult(fut.get(), X.EMPTY_OBJECT_ARRAY, distributedPlan.derivedPartitions());
            if (commit)
                toCommit.commit();
            return res;
        }
    } catch (ClusterTopologyServerNotFoundException e) {
        throw new CacheServerNotFoundException(e.getMessage(), e);
    } catch (IgniteCheckedException e) {
        IgniteSQLException sqlEx = X.cause(e, IgniteSQLException.class);
        if (sqlEx != null)
            throw sqlEx;
        Exception ex = IgniteUtils.convertExceptionNoWrap(e);
        if (ex instanceof IgniteException)
            throw (IgniteException) ex;
        U.error(log, "Error during update [localNodeId=" + ctx.localNodeId() + "]", ex);
        throw new IgniteSQLException("Failed to run update. " + ex.getMessage(), ex);
    } finally {
        if (commit)
            cctx.tm().resetContext();
    }
}
Also used : FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) EnlistOperation(org.apache.ignite.internal.processors.query.EnlistOperation) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) DmlUpdateResultsIterator(org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateResultsIterator) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) DmlDistributedPlanInfo(org.apache.ignite.internal.processors.query.h2.dml.DmlDistributedPlanInfo) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) BatchUpdateException(java.sql.BatchUpdateException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClusterReadOnlyException(org.apache.ignite.internal.processors.cache.distributed.dht.IgniteClusterReadOnlyException) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Aggregations

IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)6 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)4 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)4 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)4 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)4 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)3 GridSqlQuery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery)3 Column (org.h2.table.Column)3 SQLException (java.sql.SQLException)2 IgniteException (org.apache.ignite.IgniteException)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)2 DmlDistributedPlanInfo (org.apache.ignite.internal.processors.query.h2.dml.DmlDistributedPlanInfo)2 DmlUpdateResultsIterator (org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateResultsIterator)2 UpdatePlan (org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)2 GridSqlDelete (org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete)2