Search in sources :

Example 66 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class IgniteH2Indexing method executeSelectLocal.

/**
 * Queries individual fields (generally used by JDBC drivers).
 *
 * @param qryId Query id.
 * @param qryDesc Query descriptor.
 * @param qryParams Query parameters.
 * @param select Select.
 * @param filter Cache name and key filter.
 * @param mvccTracker Query tracker.
 * @param cancel Query cancel.
 * @param inTx Flag whether the query is executed in transaction.
 * @param timeout Timeout.
 * @return Query result.
 * @throws IgniteCheckedException If failed.
 */
private GridQueryFieldsResult executeSelectLocal(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultSelect select, final IndexingQueryFilter filter, MvccQueryTracker mvccTracker, GridQueryCancel cancel, boolean inTx, int timeout) throws IgniteCheckedException {
    assert !select.mvccEnabled() || mvccTracker != null;
    String qry;
    if (select.forUpdate())
        qry = inTx ? select.forUpdateQueryTx() : select.forUpdateQueryOutTx();
    else
        qry = qryDesc.sql();
    boolean mvccEnabled = mvccTracker != null;
    try {
        assert select != null;
        if (ctx.security().enabled())
            checkSecurity(select.cacheIds());
        MvccSnapshot mvccSnapshot = null;
        if (mvccEnabled)
            mvccSnapshot = mvccTracker.snapshot();
        final QueryContext qctx = new QueryContext(0, filter, null, mvccSnapshot, null, true);
        return new GridQueryFieldsResultAdapter(select.meta(), null) {

            @Override
            public GridCloseableIterator<List<?>> iterator() throws IgniteCheckedException {
                H2PooledConnection conn = connections().connection(qryDesc.schemaName());
                try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_ITER_OPEN, MTC.span()))) {
                    H2Utils.setupConnection(conn, qctx, qryDesc.distributedJoins(), qryDesc.enforceJoinOrder(), qryParams.lazy());
                    PreparedStatement stmt = conn.prepareStatement(qry, H2StatementCache.queryFlags(qryDesc));
                    // Convert parameters into BinaryObjects.
                    Marshaller m = ctx.config().getMarshaller();
                    byte[] paramsBytes = U.marshal(m, qryParams.arguments());
                    final ClassLoader ldr = U.resolveClassLoader(ctx.config());
                    Object[] params;
                    if (m instanceof BinaryMarshaller) {
                        params = BinaryUtils.rawArrayFromBinary(((BinaryMarshaller) m).binaryMarshaller().unmarshal(paramsBytes, ldr));
                    } else
                        params = U.unmarshal(m, paramsBytes, ldr);
                    H2Utils.bindParameters(stmt, F.asList(params));
                    H2QueryInfo qryInfo = new H2QueryInfo(H2QueryInfo.QueryType.LOCAL, stmt, qry, ctx.localNodeId(), qryId);
                    ResultSet rs = executeSqlQueryWithTimer(stmt, conn, qry, timeout, cancel, qryParams.dataPageScanEnabled(), qryInfo);
                    return new H2FieldsIterator(rs, mvccTracker, conn, qryParams.pageSize(), log, IgniteH2Indexing.this, qryInfo, ctx.tracing());
                } catch (IgniteCheckedException | RuntimeException | Error e) {
                    conn.close();
                    try {
                        if (mvccTracker != null)
                            mvccTracker.onDone();
                    } catch (Exception e0) {
                        e.addSuppressed(e0);
                    }
                    throw e;
                }
            }
        };
    } catch (Exception e) {
        GridNearTxLocal tx = null;
        if (mvccEnabled && (tx != null || (tx = tx(ctx)) != null))
            tx.setRollbackOnly();
        throw e;
    }
}
Also used : MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) Marshaller(org.apache.ignite.marshaller.Marshaller) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) GridQueryFieldsResultAdapter(org.apache.ignite.internal.processors.query.GridQueryFieldsResultAdapter) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) PreparedStatement(java.sql.PreparedStatement) H2Utils.generateFieldsQueryString(org.apache.ignite.internal.processors.query.h2.H2Utils.generateFieldsQueryString) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) 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) ResultSet(java.sql.ResultSet) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 67 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN 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)

Example 68 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class IgniteH2Indexing method streamQuery0.

/**
 * Perform given statement against given data streamer. Only rows based INSERT is supported.
 *
 * @param qry Query.
 * @param schemaName Schema name.
 * @param streamer Streamer to feed data to.
 * @param dml DML statement.
 * @param args Statement arguments.
 * @return Number of rows in given INSERT statement.
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings({ "unchecked" })
private long streamQuery0(String qry, String schemaName, IgniteDataStreamer streamer, QueryParserResultDml dml, final Object[] args, String qryInitiatorId) throws IgniteCheckedException {
    long qryId = runningQryMgr.register(QueryUtils.INCLUDE_SENSITIVE ? qry : sqlWithoutConst(dml.statement()), GridCacheQueryType.SQL_FIELDS, schemaName, true, null, qryInitiatorId);
    Exception failReason = null;
    try {
        UpdatePlan plan = dml.plan();
        Iterator<List<?>> iter = new GridQueryCacheObjectsIterator(updateQueryRows(qryId, schemaName, plan, args), objectContext(), true);
        if (!iter.hasNext())
            return 0;
        IgniteBiTuple<?, ?> t = plan.processRow(iter.next());
        if (!iter.hasNext()) {
            streamer.addData(t.getKey(), t.getValue());
            return 1;
        } else {
            Map<Object, Object> rows = new LinkedHashMap<>(plan.rowCount());
            rows.put(t.getKey(), t.getValue());
            while (iter.hasNext()) {
                List<?> row = iter.next();
                t = plan.processRow(row);
                rows.put(t.getKey(), t.getValue());
            }
            streamer.addData(rows);
            return rows.size();
        }
    } catch (IgniteException | IgniteCheckedException e) {
        failReason = e;
        throw e;
    } finally {
        runningQryMgr.unregister(qryId, failReason);
    }
}
Also used : GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) 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) LinkedHashMap(java.util.LinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 69 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class PartitionExtractor method extractFromIn.

/**
 * Extract partition information from IN.
 *
 * @param op Operation.
 * @param tblModel Table model.
 * @return Partition.
 */
private PartitionNode extractFromIn(GridSqlOperation op, PartitionTableModel tblModel) throws IgniteCheckedException {
    // Operation should contain at least two children: left (column) and right (const or column).
    if (op.size() < 2)
        return PartitionAllNode.INSTANCE;
    // Left operand should be column.
    GridSqlAst left = op.child();
    GridSqlColumn leftCol = unwrapColumn(left);
    if (leftCol == null)
        return PartitionAllNode.INSTANCE;
    // Can work only with Ignite tables.
    if (!(leftCol.column().getTable() instanceof GridH2Table))
        return PartitionAllNode.INSTANCE;
    Set<PartitionSingleNode> parts = new HashSet<>();
    for (int i = 1; i < op.size(); i++) {
        GridSqlAst right = op.child(i);
        GridSqlConst rightConst;
        GridSqlParameter rightParam;
        if (right instanceof GridSqlConst) {
            rightConst = (GridSqlConst) right;
            rightParam = null;
        } else if (right instanceof GridSqlParameter) {
            rightConst = null;
            rightParam = (GridSqlParameter) right;
        } else
            // set globally. Hence, returning null.
            return PartitionAllNode.INSTANCE;
        // Extract.
        PartitionSingleNode part = extractSingle(leftCol, rightConst, rightParam, tblModel);
        // Same thing as above: single unknown partition in disjunction defeats optimization.
        if (part == null)
            return PartitionAllNode.INSTANCE;
        parts.add(part);
    }
    return parts.size() == 1 ? parts.iterator().next() : new PartitionGroupNode(parts);
}
Also used : GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) PartitionSingleNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionSingleNode) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter) PartitionGroupNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionGroupNode) HashSet(java.util.HashSet)

Example 70 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class GridSubqueryJoinOptimizer method pullOutSubQryFromTableList.

/**
 * Pull out sub-select from table list.
 * <p>
 * Example:
 * <pre>
 *   Before:
 *     SELECT d.name
 *       FROM emp e,
 *            (select * from dep) d
 *      WHERE e.dep_id = d.id
 *        AND e.sal > 2000
 *
 *   After:
 *     SELECT d.name
 *       FROM emp e
 *       JOIN dep d
 *      WHERE sal > 2000 AND d.id = e.dep_id
 * </pre>
 *
 * @param parent Parent select.
 * @param target Target sql element. Can be {@code null}.
 * @param childInd Column index.
 */
private static boolean pullOutSubQryFromTableList(GridSqlSelect parent, @Nullable GridSqlAst target, int childInd) {
    if (target != null && !ELEMENT_IS_JOIN.test(target))
        return false;
    GridSqlAlias wrappedSubQry = target != null ? target.child(childInd) : (GridSqlAlias) parent.from();
    GridSqlSubquery subQry = GridSqlAlias.unwrap(wrappedSubQry);
    if (!isSimpleSelect(subQry.subquery()))
        return false;
    GridSqlSelect subSel = subQry.subquery();
    if (!(subSel.from() instanceof GridSqlAlias) && !(subSel.from() instanceof GridSqlTable))
        // we can't deal with joins and others right now
        return false;
    GridSqlAlias subTbl = new GridSqlAlias(wrappedSubQry.alias(), GridSqlAlias.unwrap(subSel.from()));
    if (target == null)
        // it's true only when the subquery is only table in the table list
        // so we can safely replace entire FROM expression of the parent
        parent.from(subTbl);
    else
        target.child(childInd, subTbl);
    GridSqlAst where = subSel.where();
    if (where != null) {
        if (target != null) {
            GridSqlJoin join = (GridSqlJoin) target;
            join.child(GridSqlJoin.ON_CHILD, new GridSqlOperation(AND, join.on(), where));
        } else
            parent.where(parent.where() == null ? where : new GridSqlOperation(AND, parent.where(), where));
    }
    remapColumns(parent, subSel, // reference equality used intentionally here
    col -> wrappedSubQry == col.expressionInFrom(), subTbl);
    return true;
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlSubquery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlJoin(org.apache.ignite.internal.processors.query.h2.sql.GridSqlJoin) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

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