Search in sources :

Example 11 with GridSqlQuery

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

the class GridSubqueryJoinOptimizer method pullOutSubQueries.

/**
 * Pulls out subquery from parent query where possible.
 *
 * @param parent Parent query where to find and pull out subqueries.
 */
public static void pullOutSubQueries(GridSqlQuery parent) {
    if (!optimizationEnabled())
        return;
    if (parent instanceof GridSqlUnion) {
        GridSqlUnion union = (GridSqlUnion) parent;
        pullOutSubQueries(union.left());
        pullOutSubQueries(union.right());
        return;
    }
    assert parent instanceof GridSqlSelect : "\"parent\" should be instance of GridSqlSelect class";
    GridSqlSelect select = (GridSqlSelect) parent;
    pullOutSubQryFromSelectExpr(select);
    pullOutSubQryFromInClause(select);
    pullOutSubQryFromExistsClause(select);
    pullOutSubQryFromTableList(select);
}
Also used : GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Example 12 with GridSqlQuery

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

the class GridSubqueryJoinOptimizer method isSimpleSelect.

/**
 * Whether Select query is simple or not.
 * <p>
 * We call query simple if it is select query (not union) and it has neither having nor grouping,
 * has no distinct clause, has no aggregations, has no limits, no sorting, no offset clause.
 * Also it is not SELECT FOR UPDATE.
 *
 * @param subQry Sub query.
 * @return {@code true} if it is simple query.
 */
private static boolean isSimpleSelect(GridSqlQuery subQry) {
    if (subQry instanceof GridSqlUnion)
        return false;
    GridSqlSelect select = (GridSqlSelect) subQry;
    boolean simple = F.isEmpty(select.sort()) && select.offset() == null && select.limit() == null && !select.isForUpdate() && !select.distinct() && select.havingColumn() < 0 && F.isEmpty(select.groupColumns());
    if (!simple)
        return false;
    for (GridSqlAst col : select.columns(true)) {
        if (!(col instanceof GridSqlElement))
            continue;
        // we have to traverse the tree because there may be such expressions
        // like ((MAX(col) - MIN(col)) / COUNT(col)
        ASTNodeFinder aggFinder = new ASTNodeFinder(col, (p, c) -> p instanceof GridSqlAggregateFunction);
        if (aggFinder.findNext() != null)
            return false;
        // In case of query like "SELECT * FROM (SELECT i||j FROM t) u;", where subquery contains pure operation
        // without an alias, we cannot determine which generated alias in the parent query the original expression
        // belongs to. So the best we can do is skip the case.
        ASTNodeFinder operationFinder = new ASTNodeFinder(col, (p, c) -> p instanceof GridSqlOperation, ast -> false);
        if (operationFinder.findNext() != null)
            return false;
    }
    return true;
}
Also used : GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlAggregateFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAggregateFunction) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Example 13 with GridSqlQuery

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery 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 mvccEnabled Mvcc flag.
 * @param planKey Plan key.
 * @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, boolean mvccEnabled, QueryDescriptor planKey, String selectQry, String cacheName, IgniteLogger log) throws IgniteCheckedException {
    if ((!mvccEnabled && !planKey.skipReducerOnUpdate()) || planKey.batched())
        return null;
    try (H2PooledConnection conn = idx.connections().connection(planKey.schemaName())) {
        H2Utils.setupConnection(conn, QueryContext.parseContext(idx.backupFilter(null, null), planKey.local()), planKey.distributedJoins(), planKey.enforceJoinOrder());
        // Get a new prepared statement for derived select query.
        try (PreparedStatement stmt = conn.prepareStatement(selectQry, H2StatementCache.queryFlags(planKey))) {
            Prepared prep = GridSqlQueryParser.prepared(stmt);
            GridSqlQuery selectStmt = (GridSqlQuery) new GridSqlQueryParser(false, log).parse(prep);
            GridCacheTwoStepQuery qry = GridSqlQuerySplitter.split(conn, selectStmt, selectQry, planKey.collocated(), planKey.distributedJoins(), planKey.enforceJoinOrder(), false, idx, prep.getParameters().size(), log);
            boolean distributed = // No split for local
            !qry.isLocalSplit() && // Over real caches
            qry.hasCacheIds() && // No merge table
            qry.skipMergeTable() && qry.mapQueries().size() == 1 && // One w/o subqueries
            !qry.mapQueries().get(0).hasSubQueries();
            if (distributed) {
                List<Integer> cacheIds = H2Utils.collectCacheIds(idx, CU.cacheId(cacheName), qry.tables());
                H2Utils.checkQuery(idx, cacheIds, qry.tables());
                return new DmlDistributedPlanInfo(qry.isReplicatedOnly(), cacheIds, qry.derivedPartitions());
            } else
                return null;
        }
    } catch (SQLException e) {
        throw new IgniteCheckedException(e);
    }
}
Also used : GridSqlQuery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery) H2PooledConnection(org.apache.ignite.internal.processors.query.h2.H2PooledConnection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlQueryParser(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQueryParser) Prepared(org.h2.command.Prepared) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) PreparedStatement(java.sql.PreparedStatement)

Example 14 with GridSqlQuery

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

Aggregations

GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)12 GridSqlQuery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery)9 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)7 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)6 GridSqlUnion (org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion)6 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)5 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)4 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 GridCacheTwoStepQuery (org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery)3 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)3 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 GridSqlAst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst)3 GridSqlInsert (org.apache.ignite.internal.processors.query.h2.sql.GridSqlInsert)3 GridSqlMerge (org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge)3 GridSqlQueryParser (org.apache.ignite.internal.processors.query.h2.sql.GridSqlQueryParser)3 Prepared (org.h2.command.Prepared)3 List (java.util.List)2