Search in sources :

Example 6 with GridSqlElement

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

the class DmlAstUtils method elementOrDefault.

/**
 * Do what we can to compute default value for this column (mimics H2 behavior).
 * @see Table#getDefaultValue
 * @see Column#validateConvertUpdateSequence
 * @param el SQL element.
 * @param col Column.
 * @return {@link GridSqlConst#NULL}, if {@code el} is null, or {@code el} if
 * it's not {@link GridSqlKeyword#DEFAULT}, or computed default value.
 */
private static GridSqlElement elementOrDefault(GridSqlElement el, GridSqlColumn col) {
    if (el == null)
        return GridSqlConst.NULL;
    if (el != GridSqlKeyword.DEFAULT)
        return el;
    Column h2Col = col.column();
    Expression dfltExpr = h2Col.getDefaultExpression();
    Value dfltVal;
    try {
        dfltVal = dfltExpr != null ? dfltExpr.getValue(null) : null;
    } catch (Exception ignored) {
        throw new IgniteSQLException("Failed to evaluate default value for a column " + col.columnName());
    }
    if (dfltVal != null)
        return new GridSqlConst(dfltVal);
    int type = h2Col.getType();
    DataType dt = DataType.getDataType(type);
    if (dt.decimal)
        dfltVal = ValueInt.get(0).convertTo(type);
    else if (dt.type == Value.TIMESTAMP)
        dfltVal = ValueTimestamp.fromMillis(U.currentTimeMillis());
    else if (dt.type == Value.TIME)
        dfltVal = ValueTime.fromNanos(0);
    else if (dt.type == Value.DATE)
        dfltVal = ValueDate.fromMillis(U.currentTimeMillis());
    else
        dfltVal = ValueString.get("").convertTo(type);
    return new GridSqlConst(dfltVal);
}
Also used : GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) DataType(org.h2.value.DataType) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 7 with GridSqlElement

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

the class DmlAstUtils method selectForDelete.

/**
 * Generate SQL SELECT based on DELETE's WHERE, LIMIT, etc.
 *
 * @param del Delete statement.
 * @param keysParamIdx Index for .
 * @return SELECT statement.
 */
public static GridSqlSelect selectForDelete(GridSqlDelete del, @Nullable Integer keysParamIdx) {
    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(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);
    GridSqlElement where = del.where();
    if (keysParamIdx != null)
        where = injectKeysFilterParam(where, keyCol, keysParamIdx);
    mapQry.where(where);
    mapQry.limit(del.limit());
    return mapQry;
}
Also used : 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) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) HashSet(java.util.HashSet)

Example 8 with GridSqlElement

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

the class DmlAstUtils method injectKeysFilterParam.

/**
 * Append additional condition to WHERE for it to select only specific keys.
 *
 * @param where Initial condition.
 * @param keyCol Column to base the new condition on.
 * @return New condition.
 */
private static GridSqlElement injectKeysFilterParam(GridSqlElement where, GridSqlColumn keyCol, int paramIdx) {
    // Yes, we need a subquery for "WHERE _key IN ?" to work with param being an array without dirty query rewriting.
    GridSqlSelect sel = new GridSqlSelect();
    GridSqlFunction from = new GridSqlFunction(GridSqlFunctionType.TABLE);
    sel.from(from);
    GridSqlColumn col = new GridSqlColumn(null, from, null, "TABLE", "_IGNITE_ERR_KEYS");
    sel.addColumn(col, true);
    GridSqlAlias alias = new GridSqlAlias("_IGNITE_ERR_KEYS", new GridSqlParameter(paramIdx));
    alias.resultType(keyCol.resultType());
    from.addChild(alias);
    GridSqlElement e = new GridSqlOperation(GridSqlOperationType.IN, keyCol, new GridSqlSubquery(sel));
    if (where == null)
        return e;
    else
        return new GridSqlOperation(GridSqlOperationType.AND, where, e);
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlSubquery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Example 9 with GridSqlElement

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement 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) Table(org.h2.table.Table) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) 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 10 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) 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)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