Search in sources :

Example 11 with GridCacheTwoStepQuery

use of org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery in project ignite by apache.

the class UpdatePlanBuilder method checkPlanCanBeDistributed.

/**
 * Checks whether the given update plan can be distributed and returns additional info.
 *
 * @param idx Indexing.
 * @param conn Connection.
 * @param fieldsQry Initial update query.
 * @param loc Local query flag.
 * @param selectQry Derived select query.
 * @param cacheName Cache name.
 * @return distributed update plan info, or {@code null} if cannot be distributed.
 * @throws IgniteCheckedException if failed.
 */
private static DmlDistributedPlanInfo checkPlanCanBeDistributed(IgniteH2Indexing idx, Connection conn, SqlFieldsQuery fieldsQry, boolean loc, String selectQry, String cacheName) throws IgniteCheckedException {
    if (loc || !isSkipReducerOnUpdateQuery(fieldsQry) || DmlUtils.isBatched(fieldsQry))
        return null;
    assert conn != null;
    try {
        // Get a new prepared statement for derived select query.
        try (PreparedStatement stmt = conn.prepareStatement(selectQry)) {
            idx.bindParameters(stmt, F.asList(fieldsQry.getArgs()));
            GridCacheTwoStepQuery qry = GridSqlQuerySplitter.split(conn, GridSqlQueryParser.prepared(stmt), fieldsQry.getArgs(), fieldsQry.isCollocated(), fieldsQry.isDistributedJoins(), fieldsQry.isEnforceJoinOrder(), idx);
            boolean distributed = qry.skipMergeTable() && qry.mapQueries().size() == 1 && !qry.mapQueries().get(0).hasSubQueries();
            return distributed ? new DmlDistributedPlanInfo(qry.isReplicatedOnly(), idx.collectCacheIds(CU.cacheId(cacheName), qry)) : null;
        }
    } catch (SQLException e) {
        throw new IgniteCheckedException(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) PreparedStatement(java.sql.PreparedStatement)

Example 12 with GridCacheTwoStepQuery

use of org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery in project ignite by apache.

the class GridSqlQuerySplitter method split.

/**
 * @param conn Connection.
 * @param prepared Prepared.
 * @param params Parameters.
 * @param collocatedGrpBy Whether the query has collocated GROUP BY keys.
 * @param distributedJoins If distributed joins enabled.
 * @param enforceJoinOrder Enforce join order.
 * @param h2 Indexing.
 * @return Two step query.
 * @throws SQLException If failed.
 * @throws IgniteCheckedException If failed.
 */
public static GridCacheTwoStepQuery split(Connection conn, Prepared prepared, Object[] params, boolean collocatedGrpBy, boolean distributedJoins, boolean enforceJoinOrder, IgniteH2Indexing h2) throws SQLException, IgniteCheckedException {
    if (params == null)
        params = GridCacheSqlQuery.EMPTY_PARAMS;
    // Here we will just do initial query parsing. Do not use optimized
    // subqueries because we do not have unique FROM aliases yet.
    GridSqlQuery qry = parse(prepared, false);
    String originalSql = qry.getSQL();
    // debug("ORIGINAL", originalSql);
    final boolean explain = qry.explain();
    qry.explain(false);
    GridSqlQuerySplitter splitter = new GridSqlQuerySplitter(params, collocatedGrpBy, h2.kernalContext());
    // Normalization will generate unique aliases for all the table filters in FROM.
    // Also it will collect all tables and schemas from the query.
    splitter.normalizeQuery(qry);
    // debug("NORMALIZED", qry.getSQL());
    // Here we will have correct normalized AST with optimized join order.
    // The distributedJoins parameter is ignored because it is not relevant for
    // the REDUCE query optimization.
    qry = parse(optimize(h2, conn, qry.getSQL(), params, false, enforceJoinOrder), true);
    // Do the actual query split. We will update the original query AST, need to be careful.
    splitter.splitQuery(qry);
    // We must have at least one map query.
    assert !F.isEmpty(splitter.mapSqlQrys) : "map";
    // We must have a reduce query.
    assert splitter.rdcSqlQry != null : "rdc";
    // distributed joins at all.
    if (distributedJoins) {
        boolean allCollocated = true;
        for (GridCacheSqlQuery mapSqlQry : splitter.mapSqlQrys) {
            Prepared prepared0 = optimize(h2, conn, mapSqlQry.query(), mapSqlQry.parameters(params), true, enforceJoinOrder);
            allCollocated &= isCollocated((Query) prepared0);
            mapSqlQry.query(parse(prepared0, true).getSQL());
        }
        // We do not need distributed joins if all MAP queries are collocated.
        if (allCollocated)
            distributedJoins = false;
    }
    // Setup resulting two step query and return it.
    GridCacheTwoStepQuery twoStepQry = new GridCacheTwoStepQuery(originalSql, splitter.tbls);
    twoStepQry.reduceQuery(splitter.rdcSqlQry);
    for (GridCacheSqlQuery mapSqlQry : splitter.mapSqlQrys) twoStepQry.addMapQuery(mapSqlQry);
    twoStepQry.skipMergeTable(splitter.rdcQrySimple);
    twoStepQry.explain(explain);
    twoStepQry.distributedJoins(distributedJoins);
    // all map queries must have non-empty derivedPartitions to use this feature.
    twoStepQry.derivedPartitions(mergePartitionsFromMultipleQueries(twoStepQry.mapQueries()));
    return twoStepQry;
}
Also used : GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery) Query(org.h2.command.dml.Query) Prepared(org.h2.command.Prepared) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)

Aggregations

GridCacheTwoStepQuery (org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)5 Prepared (org.h2.command.Prepared)5 PreparedStatement (java.sql.PreparedStatement)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 LinkedHashSet (java.util.LinkedHashSet)3 GridQueryFieldMetadata (org.apache.ignite.internal.processors.query.GridQueryFieldMetadata)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 IgniteSystemProperties.getInteger (org.apache.ignite.IgniteSystemProperties.getInteger)2 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)2 GridCacheSqlQuery (org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)2