Search in sources :

Example 21 with Expression

use of org.h2.expression.Expression in project ignite by apache.

the class GridSqlQueryParser method parseMerge.

/**
     * @param merge Merge.
     * @see <a href="http://h2database.com/html/grammar.html#merge">H2 merge spec</a>
     */
private GridSqlMerge parseMerge(Merge merge) {
    GridSqlMerge res = (GridSqlMerge) h2ObjToGridObj.get(merge);
    if (res != null)
        return res;
    res = new GridSqlMerge();
    h2ObjToGridObj.put(merge, res);
    Table srcTbl = MERGE_TABLE.get(merge);
    GridSqlElement tbl = parseTable(srcTbl);
    res.into(tbl);
    Column[] srcCols = MERGE_COLUMNS.get(merge);
    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);
    Column[] srcKeys = MERGE_KEYS.get(merge);
    GridSqlColumn[] keys = new GridSqlColumn[srcKeys.length];
    for (int i = 0; i < srcKeys.length; i++) keys[i] = new GridSqlColumn(srcKeys[i], tbl, null, null, srcKeys[i].getName());
    res.keys(keys);
    List<Expression[]> srcRows = MERGE_ROWS.get(merge);
    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(MERGE_QUERY.get(merge)));
    }
    return res;
}
Also used : RangeTable(org.h2.table.RangeTable) 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) 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 22 with Expression

use of org.h2.expression.Expression 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 : Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) DataType(org.h2.value.DataType) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 23 with Expression

use of org.h2.expression.Expression in project ignite by apache.

the class GridSqlSelect method getSQL.

/** {@inheritDoc} */
@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder(explain() ? "EXPLAIN SELECT" : "SELECT");
    if (distinct)
        buff.append(" DISTINCT");
    for (GridSqlAst expression : columns(true)) {
        buff.appendExceptFirst(",");
        buff.append('\n');
        buff.append(expression.getSQL());
    }
    if (from != null)
        buff.append("\nFROM ").append(from.getSQL());
    if (where != null)
        buff.append("\nWHERE ").append(StringUtils.unEnclose(where.getSQL()));
    if (grpCols != null) {
        buff.append("\nGROUP BY ");
        buff.resetCount();
        for (int grpCol : grpCols) {
            buff.appendExceptFirst(", ");
            addAlias(buff, cols.get(grpCol));
        }
    }
    if (havingCol >= 0) {
        buff.append("\nHAVING ");
        addAlias(buff, cols.get(havingCol));
    }
    getSortLimitSQL(buff);
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 24 with Expression

use of org.h2.expression.Expression in project ignite by apache.

the class GridH2CollocationModel method joinedWithCollocated.

/**
     * @param f Filter.
     * @return Affinity join type.
     */
private Affinity joinedWithCollocated(int f) {
    TableFilter tf = childFilters[f];
    GridH2Table tbl = (GridH2Table) tf.getTable();
    if (validate) {
        if (tbl.rowDescriptor().context().customAffinityMapper())
            throw customAffinityError(tbl.cacheName());
        if (F.isEmpty(tf.getIndexConditions())) {
            throw new CacheException("Failed to prepare distributed join query: " + "join condition does not use index [joinedCache=" + tbl.cacheName() + ", plan=" + tf.getSelect().getPlanSQL() + ']');
        }
    }
    IndexColumn affCol = tbl.getAffinityKeyColumn();
    boolean affKeyCondFound = false;
    if (affCol != null) {
        ArrayList<IndexCondition> idxConditions = tf.getIndexConditions();
        int affColId = affCol.column.getColumnId();
        for (int i = 0; i < idxConditions.size(); i++) {
            IndexCondition c = idxConditions.get(i);
            int colId = c.getColumn().getColumnId();
            int cmpType = c.getCompareType();
            if ((cmpType == Comparison.EQUAL || cmpType == Comparison.EQUAL_NULL_SAFE) && (colId == affColId || tbl.rowDescriptor().isKeyColumn(colId)) && c.isEvaluatable()) {
                affKeyCondFound = true;
                Expression exp = c.getExpression();
                exp = exp.getNonAliasExpression();
                if (exp instanceof ExpressionColumn) {
                    ExpressionColumn expCol = (ExpressionColumn) exp;
                    // This is one of our previous joins.
                    TableFilter prevJoin = expCol.getTableFilter();
                    if (prevJoin != null) {
                        GridH2CollocationModel cm = child(indexOf(prevJoin), true);
                        // different affinity columns from different tables.
                        if (cm != null && !cm.view) {
                            Type t = cm.type(true);
                            if (t.isPartitioned() && t.isCollocated() && isAffinityColumn(prevJoin, expCol, validate))
                                return Affinity.COLLOCATED_JOIN;
                        }
                    }
                }
            }
        }
    }
    return affKeyCondFound ? Affinity.HAS_AFFINITY_CONDITION : Affinity.NONE;
}
Also used : TableFilter(org.h2.table.TableFilter) CacheException(javax.cache.CacheException) Expression(org.h2.expression.Expression) IndexCondition(org.h2.index.IndexCondition) IndexColumn(org.h2.table.IndexColumn) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 25 with Expression

use of org.h2.expression.Expression in project ignite by apache.

the class GridH2CollocationModel method isAffinityColumn.

/**
     * @param f Table filter.
     * @param expCol Expression column.
     * @param validate Query validation flag.
     * @return {@code true} It it is an affinity column.
     */
private static boolean isAffinityColumn(TableFilter f, ExpressionColumn expCol, boolean validate) {
    Column col = expCol.getColumn();
    if (col == null)
        return false;
    Table t = col.getTable();
    if (t.isView()) {
        Query qry;
        if (f.getIndex() != null)
            qry = getSubQuery(f);
        else
            qry = GridSqlQueryParser.VIEW_QUERY.get((TableView) t);
        return isAffinityColumn(qry, expCol, validate);
    }
    if (t instanceof GridH2Table) {
        if (validate && ((GridH2Table) t).rowDescriptor().context().customAffinityMapper())
            throw customAffinityError(((GridH2Table) t).cacheName());
        IndexColumn affCol = ((GridH2Table) t).getAffinityKeyColumn();
        return affCol != null && col.getColumnId() == affCol.column.getColumnId();
    }
    return false;
}
Also used : Table(org.h2.table.Table) Query(org.h2.command.dml.Query) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) IndexColumn(org.h2.table.IndexColumn)

Aggregations

Expression (expression.Expression)13 Node (expression.Node)10 Number (expression.Number)9 Expression (org.h2.expression.Expression)9 Operator (expression.Operator)8 ExpressionColumn (org.h2.expression.ExpressionColumn)7 ValueExpression (org.h2.expression.ValueExpression)7 Column (org.h2.table.Column)6 IndexColumn (org.h2.table.IndexColumn)6 NodeException (expression.NodeException)5 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)5 ArrayList (java.util.ArrayList)4 Vector (java.util.Vector)4 GridSqlType.fromColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 Table (org.h2.table.Table)4 Identifier (expression.Identifier)3 StringAttribute (doc.attributes.StringAttribute)2 IgniteException (org.apache.ignite.IgniteException)2 CreateTable (org.h2.command.ddl.CreateTable)2