Search in sources :

Example 11 with GridSqlElement

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

the class GridSqlQueryParser method parseInsert.

/**
 * @param insert Insert.
 * @see <a href="http://h2database.com/html/grammar.html#insert">H2 insert spec</a>
 */
private GridSqlInsert parseInsert(Insert insert) {
    GridSqlInsert res = (GridSqlInsert) h2ObjToGridObj.get(insert);
    if (res != null)
        return res;
    res = new GridSqlInsert();
    h2ObjToGridObj.put(insert, res);
    Table srcTbl = INSERT_TABLE.get(insert);
    GridSqlElement tbl = parseTable(srcTbl);
    res.into(tbl).direct(INSERT_DIRECT.get(insert)).sorted(INSERT_SORTED.get(insert));
    Column[] srcCols = INSERT_COLUMNS.get(insert);
    GridSqlColumn[] cols = new GridSqlColumn[srcCols.length];
    for (int i = 0; i < srcCols.length; i++) {
        cols[i] = new GridSqlColumn(srcCols[i], tbl, null, null, srcCols[i].getName());
        cols[i].resultType(fromColumn(srcCols[i]));
    }
    res.columns(cols);
    List<Expression[]> srcRows = INSERT_ROWS.get(insert);
    if (!srcRows.isEmpty()) {
        List<GridSqlElement[]> rows = new ArrayList<>(srcRows.size());
        for (Expression[] srcRow : srcRows) {
            GridSqlElement[] row = new GridSqlElement[srcRow.length];
            for (int i = 0; i < srcRow.length; i++) row[i] = parseExpression(srcRow[i], false);
            rows.add(row);
        }
        res.rows(rows);
    } else {
        res.rows(Collections.<GridSqlElement[]>emptyList());
        res.query(parseQuery(INSERT_QUERY.get(insert)));
    }
    return res;
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) RangeTable(org.h2.table.RangeTable) MetaTable(org.h2.table.MetaTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) ArrayList(java.util.ArrayList) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) GridSqlType.fromColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) GridSqlType.fromExpression(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression) ValueExpression(org.h2.expression.ValueExpression)

Example 12 with GridSqlElement

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

the class GridSqlQuerySplitter method extractPartitionFromEquality.

/**
 * Analyses the equality operation and extracts the partition if possible
 *
 * @param op AST equality operation.
 * @param ctx Kernal Context.
 * @return partition info, or {@code null} if none identified
 */
private static CacheQueryPartitionInfo extractPartitionFromEquality(GridSqlOperation op, GridKernalContext ctx) throws IgniteCheckedException {
    assert op.operationType() == GridSqlOperationType.EQUAL;
    GridSqlElement left = op.child(0);
    GridSqlElement right = op.child(1);
    if (!(left instanceof GridSqlColumn))
        return null;
    if (!(right instanceof GridSqlConst) && !(right instanceof GridSqlParameter))
        return null;
    GridSqlColumn column = (GridSqlColumn) left;
    assert column.column().getTable() instanceof GridH2Table;
    GridH2Table tbl = (GridH2Table) column.column().getTable();
    GridH2RowDescriptor desc = tbl.rowDescriptor();
    IndexColumn affKeyCol = tbl.getAffinityKeyColumn();
    int colId = column.column().getColumnId();
    if ((affKeyCol == null || colId != affKeyCol.column.getColumnId()) && !desc.isKeyColumn(colId))
        return null;
    if (right instanceof GridSqlConst) {
        GridSqlConst constant = (GridSqlConst) right;
        return new CacheQueryPartitionInfo(ctx.affinity().partition(tbl.cacheName(), constant.value().getObject()), null, null, -1, -1);
    }
    GridSqlParameter param = (GridSqlParameter) right;
    return new CacheQueryPartitionInfo(-1, tbl.cacheName(), tbl.getName(), column.column().getType(), param.index());
}
Also used : GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) CacheQueryPartitionInfo(org.apache.ignite.internal.processors.cache.query.CacheQueryPartitionInfo) IndexColumn(org.h2.table.IndexColumn)

Example 13 with GridSqlElement

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

the class UpdatePlanBuilder method verifyUpdateColumns.

/**
 * Check that UPDATE statement affects no key columns.
 *
 * @param statement Statement.
 */
private static void verifyUpdateColumns(GridSqlStatement statement) {
    if (statement == null || !(statement instanceof GridSqlUpdate))
        return;
    GridSqlUpdate update = (GridSqlUpdate) statement;
    GridSqlElement updTarget = update.target();
    Set<GridSqlTable> tbls = new HashSet<>();
    DmlAstUtils.collectAllGridTablesInTarget(updTarget, tbls);
    if (tbls.size() != 1)
        throw new IgniteSQLException("Failed to determine target table for UPDATE", IgniteQueryErrorCode.TABLE_NOT_FOUND);
    GridSqlTable tbl = tbls.iterator().next();
    GridH2Table gridTbl = tbl.dataTable();
    if (updateAffectsKeyColumns(gridTbl, update.set().keySet()))
        throw new IgniteSQLException("SQL UPDATE can't modify key or its fields directly", IgniteQueryErrorCode.KEY_UPDATE);
}
Also used : GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) 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) HashSet(java.util.HashSet)

Example 14 with GridSqlElement

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

Example 15 with GridSqlElement

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

the class UpdatePlanBuilder method planForInsert.

/**
     * Prepare update plan for INSERT or MERGE.
     *
     * @param stmt INSERT or MERGE statement.
     * @return Update plan.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings("ConstantConditions")
private static UpdatePlan planForInsert(GridSqlStatement stmt) throws IgniteCheckedException {
    GridSqlQuery sel;
    GridSqlElement target;
    GridSqlColumn[] cols;
    boolean isTwoStepSubqry;
    int rowsNum;
    GridSqlTable tbl;
    GridH2RowDescriptor desc;
    if (stmt instanceof GridSqlInsert) {
        GridSqlInsert ins = (GridSqlInsert) stmt;
        target = ins.into();
        tbl = gridTableForElement(target);
        desc = tbl.dataTable().rowDescriptor();
        cols = ins.columns();
        sel = DmlAstUtils.selectForInsertOrMerge(cols, ins.rows(), ins.query(), desc);
        isTwoStepSubqry = (ins.query() != null);
        rowsNum = isTwoStepSubqry ? 0 : ins.rows().size();
    } else if (stmt instanceof GridSqlMerge) {
        GridSqlMerge merge = (GridSqlMerge) stmt;
        target = merge.into();
        tbl = gridTableForElement(target);
        desc = tbl.dataTable().rowDescriptor();
        // This check also protects us from attempts to update key or its fields directly -
        // when no key except cache key can be used, it will serve only for uniqueness checks,
        // not for updates, and hence will allow putting new pairs only.
        // We don't quote _key and _val column names on CREATE TABLE, so they are always uppercase here.
        GridSqlColumn[] keys = merge.keys();
        if (keys.length != 1 || !desc.isKeyColumn(tbl.dataTable().getColumn(keys[0].columnName()).getColumnId()))
            throw new CacheException("SQL MERGE does not support arbitrary keys");
        cols = merge.columns();
        sel = DmlAstUtils.selectForInsertOrMerge(cols, merge.rows(), merge.query(), desc);
        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 = (isTwoStepSubqry && (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];
    for (int i = 0; i < cols.length; i++) {
        GridSqlColumn col = cols[i];
        String colName = col.columnName();
        colNames[i] = colName;
        colTypes[i] = col.resultType().type();
        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;
    }
    KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
    KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
    if (stmt instanceof GridSqlMerge)
        return UpdatePlan.forMerge(tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, sel.getSQL(), !isTwoStepSubqry, rowsNum);
    else
        return UpdatePlan.forInsert(tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, sel.getSQL(), !isTwoStepSubqry, rowsNum);
}
Also used : GridSqlMerge(org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge) GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) CacheException(javax.cache.CacheException) 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) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlInsert(org.apache.ignite.internal.processors.query.h2.sql.GridSqlInsert)

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