Search in sources :

Example 16 with GridSqlElement

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

the class UpdatePlanBuilder method planForUpdate.

/**
     * Prepare update plan for UPDATE or DELETE.
     *
     * @param stmt UPDATE or DELETE statement.
     * @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, @Nullable Integer errKeysPos) throws IgniteCheckedException {
    GridSqlElement target;
    FastUpdateArguments 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 = gridTableForElement(target);
    GridH2Table gridTbl = tbl.dataTable();
    GridH2RowDescriptor desc = gridTbl.rowDescriptor();
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + gridTbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    if (fastUpdate != null)
        return UpdatePlan.forFastUpdate(mode, gridTbl, fastUpdate);
    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 column = updatedCols.get(i).column();
                if (desc.isValueColumn(column.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 newValSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
            sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt, errKeysPos);
            return UpdatePlan.forUpdate(gridTbl, colNames, colTypes, newValSupplier, valColIdx, sel.getSQL());
        } else {
            sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt, errKeysPos);
            return UpdatePlan.forDelete(gridTbl, sel.getSQL());
        }
    }
}
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 17 with GridSqlElement

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

the class UpdatePlanBuilder method gridTableForElement.

/**
     * @param target Expression to extract the table from.
     * @return Back end table for this element.
     */
private static GridSqlTable gridTableForElement(GridSqlElement target) {
    Set<GridSqlTable> tbls = new HashSet<>();
    DmlAstUtils.collectAllGridTablesInTarget(target, tbls);
    if (tbls.size() != 1)
        throw new IgniteSQLException("Failed to determine target table", IgniteQueryErrorCode.TABLE_NOT_FOUND);
    return tbls.iterator().next();
}
Also used : GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) HashSet(java.util.HashSet)

Example 18 with GridSqlElement

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

the class DmlAstUtils method selectForUpdate.

/**
 * Generate SQL SELECT based on UPDATE's WHERE, LIMIT, etc.
 *
 * @param update Update statement.
 * @param keysParamIdx Index of new param for the array of keys.
 * @return SELECT statement.
 */
public static GridSqlSelect selectForUpdate(GridSqlUpdate update, @Nullable Integer keysParamIdx) {
    GridSqlSelect mapQry = new GridSqlSelect();
    mapQry.from(update.target());
    Set<GridSqlTable> tbls = new HashSet<>();
    collectAllGridTablesInTarget(update.target(), tbls);
    assert tbls.size() == 1 : "Failed to determine target table for UPDATE";
    GridSqlTable tbl = tbls.iterator().next();
    GridH2Table gridTbl = tbl.dataTable();
    assert gridTbl != null : "Failed to determine target grid table for UPDATE";
    Column h2KeyCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.KEY_COL);
    Column h2ValCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.VAL_COL);
    GridSqlColumn keyCol = new GridSqlColumn(h2KeyCol, tbl, h2KeyCol.getName());
    keyCol.resultType(GridSqlType.fromColumn(h2KeyCol));
    GridSqlColumn valCol = new GridSqlColumn(h2ValCol, tbl, h2ValCol.getName());
    valCol.resultType(GridSqlType.fromColumn(h2ValCol));
    mapQry.addColumn(keyCol, true);
    mapQry.addColumn(valCol, true);
    for (GridSqlColumn c : update.cols()) {
        String newColName = Parser.quoteIdentifier("_upd_" + c.columnName());
        // We have to use aliases to cover cases when the user
        // wants to update _val field directly (if it's a literal)
        GridSqlAlias alias = new GridSqlAlias(newColName, elementOrDefault(update.set().get(c.columnName()), c), true);
        alias.resultType(c.resultType());
        mapQry.addColumn(alias, true);
    }
    GridSqlElement where = update.where();
    if (keysParamIdx != null)
        where = injectKeysFilterParam(where, keyCol, keysParamIdx);
    mapQry.where(where);
    mapQry.limit(update.limit());
    return mapQry;
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) 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) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) ValueString(org.h2.value.ValueString) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) HashSet(java.util.HashSet)

Example 19 with GridSqlElement

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

the class DmlAstUtils method gridTableForElement.

/**
 * @param target Expression to extract the table from.
 * @return Back end table for this element.
 */
public static GridSqlTable gridTableForElement(GridSqlElement target) {
    Set<GridSqlTable> tbls = new HashSet<>();
    collectAllGridTablesInTarget(target, tbls);
    if (tbls.size() != 1)
        throw new IgniteSQLException("Failed to determine target table", IgniteQueryErrorCode.TABLE_NOT_FOUND);
    return tbls.iterator().next();
}
Also used : GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) HashSet(java.util.HashSet)

Example 20 with GridSqlElement

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

the class DmlAstUtils method findKeyValueEqualityCondition.

/**
 * @param where Element to test.
 * @return Whether given element corresponds to {@code WHERE _key = ?}, and key is a literal expressed
 * in query or a query param.
 */
@SuppressWarnings("RedundantCast")
private static IgnitePair<GridSqlElement> findKeyValueEqualityCondition(GridSqlElement where) {
    if (where == null || !(where instanceof GridSqlOperation))
        return null;
    GridSqlOperation whereOp = (GridSqlOperation) where;
    // Does this WHERE limit only by _key?
    if (isKeyEqualityCondition(whereOp))
        return new IgnitePair<>((GridSqlElement) whereOp.child(1), null);
    // Or maybe it limits both by _key and _val?
    if (whereOp.operationType() != GridSqlOperationType.AND)
        return null;
    GridSqlElement left = whereOp.child(0);
    GridSqlElement right = whereOp.child(1);
    if (!(left instanceof GridSqlOperation && right instanceof GridSqlOperation))
        return null;
    GridSqlOperation leftOp = (GridSqlOperation) left;
    GridSqlOperation rightOp = (GridSqlOperation) right;
    if (isKeyEqualityCondition(leftOp)) {
        // _key = ? and _val = ?
        if (!isValueEqualityCondition(rightOp))
            return null;
        return new IgnitePair<>((GridSqlElement) leftOp.child(1), (GridSqlElement) rightOp.child(1));
    } else if (isKeyEqualityCondition(rightOp)) {
        // _val = ? and _key = ?
        if (!isValueEqualityCondition(leftOp))
            return null;
        return new IgnitePair<>((GridSqlElement) rightOp.child(1), (GridSqlElement) leftOp.child(1));
    } else
        // Neither
        return null;
}
Also used : IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)

Aggregations

GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)14 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)14 Column (org.h2.table.Column)11 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)10 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)10 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)10 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)9 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)9 HashSet (java.util.HashSet)7 Expression (org.h2.expression.Expression)6 ArrayList (java.util.ArrayList)5 GridSqlParameter (org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)5 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)5 ValueExpression (org.h2.expression.ValueExpression)5 IndexColumn (org.h2.table.IndexColumn)5 GridSqlConst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst)4 GridSqlType.fromColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)4 ExpressionColumn (org.h2.expression.ExpressionColumn)4