Search in sources :

Example 26 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class PartitionExtractor method extractSingle.

/**
 * Extract single partition.
 *
 * @param leftCol Left column.
 * @param rightConst Right constant.
 * @param rightParam Right parameter.
 * @param tblModel Table model.
 * @return Partition or {@code null} if failed to extract.
 */
@Nullable
private PartitionSingleNode extractSingle(GridSqlColumn leftCol, GridSqlConst rightConst, GridSqlParameter rightParam, PartitionTableModel tblModel) throws IgniteCheckedException {
    assert leftCol != null;
    Column leftCol0 = leftCol.column();
    assert leftCol0.getTable() != null;
    assert leftCol0.getTable() instanceof GridH2Table;
    GridH2Table tbl = (GridH2Table) leftCol0.getTable();
    if (!tbl.isColumnForPartitionPruning(leftCol0))
        return null;
    PartitionTable tbl0 = tblModel.table(leftCol.tableAlias());
    // If table is in ignored set, then we cannot use it for partition extraction.
    if (tbl0 == null)
        return null;
    if (rightConst != null) {
        int part = partResolver.partition(rightConst.value().getObject(), leftCol0.getType(), tbl.cacheName());
        return new PartitionConstantNode(tbl0, part);
    } else if (rightParam != null) {
        int colType = leftCol0.getType();
        return new PartitionParameterNode(tbl0, partResolver, rightParam.index(), leftCol0.getType(), mappedType(colType));
    } else
        return null;
}
Also used : PartitionTable(org.apache.ignite.internal.sql.optimizer.affinity.PartitionTable) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) PartitionParameterNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionParameterNode) PartitionConstantNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionConstantNode) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class DmlAstUtils method selectForDelete.

/**
 * Generate SQL SELECT based on DELETE's WHERE, LIMIT, etc.
 *
 * @param del Delete statement.
 * @return SELECT statement.
 */
public static GridSqlSelect selectForDelete(GridSqlDelete del) {
    GridSqlSelect mapQry = new GridSqlSelect();
    mapQry.from(del.from());
    Set<GridSqlTable> tbls = new HashSet<>();
    collectAllGridTablesInTarget(del.from(), tbls);
    assert tbls.size() == 1 : "Failed to determine target table for DELETE";
    GridSqlTable tbl = tbls.iterator().next();
    GridH2Table gridTbl = tbl.dataTable();
    assert gridTbl != null : "Failed to determine target grid table for DELETE";
    Column h2KeyCol = gridTbl.getColumn(QueryUtils.KEY_COL);
    Column h2ValCol = gridTbl.getColumn(QueryUtils.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);
    GridSqlElement where = del.where();
    mapQry.where(where);
    mapQry.limit(del.limit());
    return mapQry;
}
Also used : 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) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) HashSet(java.util.HashSet)

Example 28 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class DmlAstUtils method getFastUpdateArgs.

/**
 * @param update UPDATE statement.
 * @return {@code null} if given statement directly updates {@code _val} column with a literal or param value
 * and filters by single non expression key (and, optionally,  by single non expression value).
 */
public static FastUpdate getFastUpdateArgs(GridSqlUpdate update) {
    IgnitePair<GridSqlElement> filter = findKeyValueEqualityCondition(update.where());
    if (filter == null)
        return null;
    if (update.cols().size() != 1)
        return null;
    Table tbl = update.cols().get(0).column().getTable();
    if (!(tbl instanceof GridH2Table))
        return null;
    GridH2RowDescriptor desc = ((GridH2Table) tbl).rowDescriptor();
    if (!desc.isValueColumn(update.cols().get(0).column().getColumnId()))
        return null;
    GridSqlElement set = update.set().get(update.cols().get(0).columnName());
    if (!(set instanceof GridSqlConst || set instanceof GridSqlParameter))
        return null;
    return FastUpdate.create(filter.getKey(), filter.getValue(), set);
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) Table(org.h2.table.Table) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)

Example 29 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class DmlAstUtils method selectForUpdate.

/**
 * Generate SQL SELECT based on UPDATE's WHERE, LIMIT, etc.
 *
 * @param update Update statement.
 * @return SELECT statement.
 */
public static GridSqlSelect selectForUpdate(GridSqlUpdate update) {
    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(QueryUtils.KEY_COL);
    Column h2ValCol = gridTbl.getColumn(QueryUtils.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();
    // On no MVCC mode we cannot use lazy mode when UPDATE query contains index with updated columns
    // and that index may be chosen to scan by WHERE condition
    // because in this case any rows update may be updated several times.
    // e.g. in the cases below we cannot use lazy mode:
    // 
    // 1. CREATE INDEX idx on test(val)
    // UPDATE test SET val = val + 1 WHERE val >= ?
    // 
    // 2. CREATE INDEX idx on test(val0, val1)
    // UPDATE test SET val1 = val1 + 1 WHERE val0 >= ?
    mapQry.canBeLazy(!isIndexWithUpdateColumnsMayBeUsed(gridTbl, update.cols().stream().map(GridSqlColumn::column).collect(Collectors.toSet()), extractColumns(gridTbl, where)));
    mapQry.where(where);
    mapQry.limit(update.limit());
    return mapQry;
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) 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) 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 30 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class DmlAstUtils method isEqualityCondition.

/**
 * @param op Operation.
 * @param key true - check for key equality condition,
 *            otherwise check for value equality condition
 * @return Whether this condition is of form {@code colName} = ?
 */
private static boolean isEqualityCondition(GridSqlOperation op, boolean key) {
    if (op.operationType() != GridSqlOperationType.EQUAL)
        return false;
    GridSqlElement left = op.child(0);
    GridSqlElement right = op.child(1);
    if (!(left instanceof GridSqlColumn))
        return false;
    GridSqlColumn column = (GridSqlColumn) left;
    if (!(column.column().getTable() instanceof GridH2Table))
        return false;
    GridH2RowDescriptor desc = ((GridH2Table) column.column().getTable()).rowDescriptor();
    return (key ? desc.isKeyColumn(column.column().getColumnId()) : desc.isValueColumn(column.column().getColumnId())) && (right instanceof GridSqlConst || right instanceof GridSqlParameter);
}
Also used : GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)

Aggregations

GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)66 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)26 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)18 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)18 Column (org.h2.table.Column)18 ArrayList (java.util.ArrayList)16 IgniteException (org.apache.ignite.IgniteException)15 HashSet (java.util.HashSet)13 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)12 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)11 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)11 SQLException (java.sql.SQLException)10 List (java.util.List)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)8 Map (java.util.Map)7 IgniteEx (org.apache.ignite.internal.IgniteEx)7 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)7 HashMap (java.util.HashMap)5