Search in sources :

Example 31 with GridSqlElement

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

the class GridSubqueryJoinOptimizer method buildConditionBush.

/**
 * Creates tree from provided elements which will be connected with an AND operator.
 *
 * @param ops Ops.
 * @return Root of the resulting tree.
 */
private static GridSqlElement buildConditionBush(List<GridSqlElement> ops) {
    assert !F.isEmpty(ops);
    if (ops.size() == 1)
        return ops.get(0);
    int m = (ops.size() + 1) / 2;
    GridSqlElement left = buildConditionBush(ops.subList(0, m));
    GridSqlElement right = buildConditionBush(ops.subList(m, ops.size()));
    return new GridSqlOperation(AND, left, right);
}
Also used : GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)

Example 32 with GridSqlElement

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

the class GridSubqueryJoinOptimizer method isSimpleSelect.

/**
 * Whether Select query is simple or not.
 * <p>
 * We call query simple if it is select query (not union) and it has neither having nor grouping,
 * has no distinct clause, has no aggregations, has no limits, no sorting, no offset clause.
 * Also it is not SELECT FOR UPDATE.
 *
 * @param subQry Sub query.
 * @return {@code true} if it is simple query.
 */
private static boolean isSimpleSelect(GridSqlQuery subQry) {
    if (subQry instanceof GridSqlUnion)
        return false;
    GridSqlSelect select = (GridSqlSelect) subQry;
    boolean simple = F.isEmpty(select.sort()) && select.offset() == null && select.limit() == null && !select.isForUpdate() && !select.distinct() && select.havingColumn() < 0 && F.isEmpty(select.groupColumns());
    if (!simple)
        return false;
    for (GridSqlAst col : select.columns(true)) {
        if (!(col instanceof GridSqlElement))
            continue;
        // we have to traverse the tree because there may be such expressions
        // like ((MAX(col) - MIN(col)) / COUNT(col)
        ASTNodeFinder aggFinder = new ASTNodeFinder(col, (p, c) -> p instanceof GridSqlAggregateFunction);
        if (aggFinder.findNext() != null)
            return false;
        // In case of query like "SELECT * FROM (SELECT i||j FROM t) u;", where subquery contains pure operation
        // without an alias, we cannot determine which generated alias in the parent query the original expression
        // belongs to. So the best we can do is skip the case.
        ASTNodeFinder operationFinder = new ASTNodeFinder(col, (p, c) -> p instanceof GridSqlOperation, ast -> false);
        if (operationFinder.findNext() != null)
            return false;
    }
    return true;
}
Also used : GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlAggregateFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAggregateFunction) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Example 33 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 34 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 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)

Example 35 with GridSqlElement

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

the class DmlAstUtils method findParams.

/**
 * @param qry Select.
 * @param params Parameters.
 * @param target Extracted parameters.
 * @param paramIdxs Parameter indexes.
 * @return Extracted parameters list.
 */
@SuppressWarnings("unused")
private static List<Object> findParams(GridSqlQuery qry, Object[] params, ArrayList<Object> target, IntArray paramIdxs) {
    if (qry instanceof GridSqlSelect)
        return findParams((GridSqlSelect) qry, params, target, paramIdxs);
    GridSqlUnion union = (GridSqlUnion) qry;
    findParams(union.left(), params, target, paramIdxs);
    findParams(union.right(), params, target, paramIdxs);
    findParams((GridSqlElement) qry.limit(), params, target, paramIdxs);
    findParams((GridSqlElement) qry.offset(), params, target, paramIdxs);
    return target;
}
Also used : GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Aggregations

GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)24 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)17 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)16 Column (org.h2.table.Column)16 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)14 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)14 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)11 HashSet (java.util.HashSet)9 ArrayList (java.util.ArrayList)7 GridSqlConst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst)6 GridSqlOperation (org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)6 Expression (org.h2.expression.Expression)6 GridSqlParameter (org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)5 GridSqlQuery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery)5 GridSqlSubquery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery)5 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)5 GridSqlUnion (org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion)5 ValueExpression (org.h2.expression.ValueExpression)5 IndexColumn (org.h2.table.IndexColumn)5