Search in sources :

Example 1 with NEGATE

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

the class GridSubqueryJoinOptimizer method subQueryCanBePulledOut.

/**
 * Whether sub-query can be pulled out or not.
 *
 * @param subQry Sub-query.
 * @return {@code true} if sub-query cound be pulled out, {@code false} otherwise.
 */
private static boolean subQueryCanBePulledOut(GridSqlSubquery subQry) {
    GridSqlQuery subQ = subQry.subquery();
    if (!isSimpleSelect(subQ))
        return false;
    assert subQ instanceof GridSqlSelect;
    GridSqlSelect subS = (GridSqlSelect) subQ;
    if (subS.where() == null)
        return false;
    Predicate<GridSqlAst> andOrEqPre = ELEMENT_IS_AND_OPERATION.or(ELEMENT_IS_EQ);
    if (!andOrEqPre.test(subS.where()))
        return false;
    ASTNodeFinder opEqFinder = new ASTNodeFinder(subS.where(), (parent, child) -> ELEMENT_IS_EQ.test(parent), andOrEqPre);
    List<GridSqlColumn> sqlCols = new ArrayList<>();
    ASTNodeFinder.Result res;
    while ((res = opEqFinder.findNext()) != null) {
        GridSqlOperation op = (GridSqlOperation) res.getEl();
        assert op.size() == 2;
        GridSqlColumn[] colArr = new GridSqlColumn[2];
        for (int i = 0; i < 2; i++) {
            if (op.child(i) instanceof GridSqlColumn)
                colArr[i] = op.child(i);
            else if (op.child(i) instanceof GridSqlOperation && ((GridSqlOperation) op.child(i)).operationType() == NEGATE && op.child() instanceof GridSqlColumn)
                colArr[i] = op.child(i).child();
        }
        if (colArr[0] == null || colArr[1] == null || // whether the column refers to exactly the same table
        colArr[0].expressionInFrom() != colArr[1].expressionInFrom()) {
            if (colArr[0] != null && colArr[0].expressionInFrom() == subS.from())
                sqlCols.add(colArr[0]);
            if (colArr[1] != null && colArr[1].expressionInFrom() == subS.from())
                sqlCols.add(colArr[1]);
        }
    }
    GridSqlAst subTbl = GridSqlAlias.unwrap(subS.from());
    return subTbl instanceof GridSqlTable && isUniqueIndexExists(sqlCols, (GridSqlTable) subTbl);
}
Also used : ArrayList(java.util.ArrayList) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridSqlQuery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)

Aggregations

ArrayList (java.util.ArrayList)1 GridSqlAst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst)1 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)1 GridSqlOperation (org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)1 GridSqlQuery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery)1 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)1 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)1