Search in sources :

Example 1 with GridSqlOperationType

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

the class PartitionExtractor method tryExtractBetween.

/**
 * Try to extract partitions from {@code op} assuming that it's between operation or simple range.
 *
 * @param op Sql operation.
 * @param tblModel Table model.
 * @return {@code PartitionSingleNode} if operation reduced to one partition,
 *   {@code PartitionGroupNode} if operation reduced to multiple partitions or null if operation is neither
 *   between nor simple range. Null also returns if it's not possible to extract partitions from given operation.
 * @throws IgniteCheckedException If failed.
 */
private PartitionNode tryExtractBetween(GridSqlOperation op, PartitionTableModel tblModel) throws IgniteCheckedException {
    // Between operation (or similar range) should contain exact two children.
    assert op.size() == 2;
    GridSqlAst left = op.child();
    GridSqlAst right = op.child(1);
    GridSqlOperationType leftOpType = retrieveOperationType(left);
    GridSqlOperationType rightOpType = retrieveOperationType(right);
    if ((GridSqlOperationType.BIGGER == rightOpType || GridSqlOperationType.BIGGER_EQUAL == rightOpType) && (GridSqlOperationType.SMALLER == leftOpType || GridSqlOperationType.SMALLER_EQUAL == leftOpType)) {
        GridSqlAst tmp = left;
        left = right;
        right = tmp;
    } else if (!((GridSqlOperationType.BIGGER == leftOpType || GridSqlOperationType.BIGGER_EQUAL == leftOpType) && (GridSqlOperationType.SMALLER == rightOpType || GridSqlOperationType.SMALLER_EQUAL == rightOpType)))
        return null;
    // Try parse left AST.
    GridSqlColumn leftCol;
    if (left instanceof GridSqlOperation && left.child() instanceof GridSqlColumn && (((GridSqlColumn) left.child()).column().getTable() instanceof GridH2Table))
        leftCol = left.child();
    else
        return null;
    // Try parse right AST.
    GridSqlColumn rightCol;
    if (right instanceof GridSqlOperation && right.child() instanceof GridSqlColumn)
        rightCol = right.child();
    else
        return null;
    GridH2Table tbl = (GridH2Table) leftCol.column().getTable();
    // Check that columns might be used for partition pruning.
    if (!tbl.isColumnForPartitionPruning(leftCol.column()))
        return null;
    // Check that both left and right AST use same column.
    if (!F.eq(leftCol.schema(), rightCol.schema()) || !F.eq(leftCol.columnName(), rightCol.columnName()) || !F.eq(leftCol.tableAlias(), rightCol.tableAlias()))
        return null;
    // Check columns type
    if (!(leftCol.column().getType() == Value.BYTE || leftCol.column().getType() == Value.SHORT || leftCol.column().getType() == Value.INT || leftCol.column().getType() == Value.LONG))
        return null;
    // Try parse left AST right value (value to the right of '>' or '>=').
    GridSqlConst leftConst;
    if (left.child(1) instanceof GridSqlConst)
        leftConst = left.child(1);
    else
        return null;
    // Try parse right AST right value (value to the right of '<' or '<=').
    GridSqlConst rightConst;
    if (right.child(1) instanceof GridSqlConst)
        rightConst = right.child(1);
    else
        return null;
    long leftLongVal;
    long rightLongVal;
    try {
        leftLongVal = leftConst.value().getLong();
        rightLongVal = rightConst.value().getLong();
    } catch (Exception e) {
        return null;
    }
    // Increment left long value if '>' is used.
    if (((GridSqlOperation) left).operationType() == GridSqlOperationType.BIGGER)
        leftLongVal++;
    // Decrement right long value if '<' is used.
    if (((GridSqlOperation) right).operationType() == GridSqlOperationType.SMALLER)
        rightLongVal--;
    Set<PartitionSingleNode> parts = new HashSet<>();
    PartitionTable tbl0 = tblModel.table(leftCol.tableAlias());
    // If table is in ignored set, then we cannot use it for partition extraction.
    if (tbl0 == null)
        return null;
    for (long i = leftLongVal; i <= rightLongVal; i++) {
        int part = partResolver.partition(i, leftCol.column().getType(), tbl0.cacheName());
        parts.add(new PartitionConstantNode(tbl0, part));
        if (parts.size() > maxPartsCntBetween)
            return null;
    }
    return parts.isEmpty() ? PartitionNoneNode.INSTANCE : parts.size() == 1 ? parts.iterator().next() : new PartitionGroupNode(parts);
}
Also used : PartitionTable(org.apache.ignite.internal.sql.optimizer.affinity.PartitionTable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) PartitionSingleNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionSingleNode) PartitionConstantNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionConstantNode) GridSqlOperationType(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) PartitionGroupNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionGroupNode) HashSet(java.util.HashSet)

Example 2 with GridSqlOperationType

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

the class GridSqlQueryParser method parseExpression0.

/**
 * @param expression Expression.
 * @param calcTypes Calculate types for all the expressions.
 * @return Parsed expression.
 */
private GridSqlElement parseExpression0(Expression expression, boolean calcTypes) {
    if (expression instanceof ExpressionColumn) {
        ExpressionColumn expCol = (ExpressionColumn) expression;
        return new GridSqlColumn(expCol.getColumn(), parseTableFilter(expCol.getTableFilter()), SCHEMA_NAME.get(expCol), expCol.getOriginalTableAliasName(), expCol.getColumnName());
    }
    if (expression instanceof Alias)
        return new GridSqlAlias(expression.getAlias(), parseExpression(expression.getNonAliasExpression(), calcTypes), true);
    if (expression instanceof ValueExpression)
        // == comparison is legit, see ValueExpression#getSQL()
        return expression == ValueExpression.getDefault() ? GridSqlKeyword.DEFAULT : new GridSqlConst(expression.getValue(null));
    if (expression instanceof Operation) {
        Operation operation = (Operation) expression;
        Operation.OpType type = OPERATION_TYPE.get(operation);
        if (type == Operation.OpType.NEGATE) {
            assert OPERATION_RIGHT.get(operation) == null;
            return new GridSqlOperation(GridSqlOperationType.NEGATE, parseExpression(OPERATION_LEFT.get(operation), calcTypes));
        }
        return new GridSqlOperation(mapOperationType(type), parseExpression(OPERATION_LEFT.get(operation), calcTypes), parseExpression(OPERATION_RIGHT.get(operation), calcTypes));
    }
    if (expression instanceof Comparison) {
        Comparison cmp = (Comparison) expression;
        GridSqlOperationType opType = COMPARISON_TYPES[COMPARISON_TYPE.get(cmp)];
        assert opType != null : COMPARISON_TYPE.get(cmp);
        Expression leftExp = COMPARISON_LEFT.get(cmp);
        GridSqlElement left = parseExpression(leftExp, calcTypes);
        if (opType.childrenCount() == 1)
            return new GridSqlOperation(opType, left);
        Expression rightExp = COMPARISON_RIGHT.get(cmp);
        GridSqlElement right = parseExpression(rightExp, calcTypes);
        return new GridSqlOperation(opType, left, right);
    }
    if (expression instanceof ConditionNot)
        return new GridSqlOperation(NOT, parseExpression(expression.getNotIfPossible(null), calcTypes));
    if (expression instanceof ConditionAndOr) {
        ConditionAndOr andOr = (ConditionAndOr) expression;
        int type = ANDOR_TYPE.get(andOr);
        assert type == ConditionAndOr.AND || type == ConditionAndOr.OR;
        return new GridSqlOperation(type == ConditionAndOr.AND ? AND : OR, parseExpression(ANDOR_LEFT.get(andOr), calcTypes), parseExpression(ANDOR_RIGHT.get(andOr), calcTypes));
    }
    if (expression instanceof Subquery) {
        Query qry = ((Subquery) expression).getQuery();
        return parseQueryExpression(qry);
    }
    if (expression instanceof ConditionIn) {
        GridSqlOperation res = new GridSqlOperation(IN);
        res.addChild(parseExpression(LEFT_CI.get((ConditionIn) expression), calcTypes));
        List<Expression> vals = VALUE_LIST_CI.get((ConditionIn) expression);
        for (Expression val : vals) res.addChild(parseExpression(val, calcTypes));
        return res;
    }
    if (expression instanceof ConditionInConstantSet) {
        GridSqlOperation res = new GridSqlOperation(IN);
        res.addChild(parseExpression(LEFT_CICS.get((ConditionInConstantSet) expression), calcTypes));
        List<Expression> vals = VALUE_LIST_CICS.get((ConditionInConstantSet) expression);
        for (Expression val : vals) res.addChild(parseExpression(val, calcTypes));
        return res;
    }
    if (expression instanceof ConditionInSelect) {
        GridSqlOperation res = new GridSqlOperation(IN);
        boolean all = ALL.get((ConditionInSelect) expression);
        int compareType = COMPARE_TYPE.get((ConditionInSelect) expression);
        assert0(!all, expression);
        assert0(compareType == Comparison.EQUAL, expression);
        res.addChild(parseExpression(LEFT_CIS.get((ConditionInSelect) expression), calcTypes));
        Query qry = QUERY_IN.get((ConditionInSelect) expression);
        res.addChild(parseQueryExpression(qry));
        return res;
    }
    if (expression instanceof CompareLike) {
        assert0(ESCAPE.get((CompareLike) expression) == null, expression);
        boolean regexp = REGEXP_CL.get((CompareLike) expression);
        return new GridSqlOperation(regexp ? REGEXP : LIKE, parseExpression(LEFT.get((CompareLike) expression), calcTypes), parseExpression(RIGHT.get((CompareLike) expression), calcTypes));
    }
    if (expression instanceof Function) {
        Function f = (Function) expression;
        GridSqlFunction res = new GridSqlFunction(null, f.getName());
        if (f.getArgs() != null) {
            if (f.getFunctionType() == Function.TABLE || f.getFunctionType() == Function.TABLE_DISTINCT) {
                Column[] cols = FUNC_TBL_COLS.get((TableFunction) f);
                Expression[] args = f.getArgs();
                assert cols.length == args.length;
                for (int i = 0; i < cols.length; i++) {
                    GridSqlElement arg = parseExpression(args[i], calcTypes);
                    GridSqlAlias alias = new GridSqlAlias(cols[i].getName(), arg, false);
                    alias.resultType(fromColumn(cols[i]));
                    res.addChild(alias);
                }
            } else {
                for (Expression arg : f.getArgs()) {
                    if (arg == null) {
                        if (f.getFunctionType() != Function.CASE)
                            throw new IllegalStateException("Function type with null arg: " + f.getFunctionType());
                        res.addChild(GridSqlPlaceholder.EMPTY);
                    } else
                        res.addChild(parseExpression(arg, calcTypes));
                }
            }
        }
        if (f.getFunctionType() == Function.CAST || f.getFunctionType() == Function.CONVERT) {
            checkTypeSupported(f.getType(), "[expSql=" + f.getSQL() + ']');
            res.resultType(fromExpression(f));
        }
        return res;
    }
    if (expression instanceof JavaFunction) {
        JavaFunction f = (JavaFunction) expression;
        FunctionAlias alias = FUNC_ALIAS.get(f);
        GridSqlFunction res = new GridSqlFunction(alias.getSchema().getName(), f.getName());
        if (f.getArgs() != null) {
            for (Expression arg : f.getArgs()) res.addChild(parseExpression(arg, calcTypes));
        }
        return res;
    }
    if (expression instanceof Parameter)
        return new GridSqlParameter(((Parameter) expression).getIndex());
    if (expression instanceof Aggregate) {
        Aggregate.AggregateType type = TYPE.get((Aggregate) expression);
        if (GridSqlAggregateFunction.isValidType(type)) {
            GridSqlAggregateFunction res = new GridSqlAggregateFunction(DISTINCT.get((Aggregate) expression), type);
            Expression on = ON.get((Aggregate) expression);
            if (on != null)
                res.addChild(parseExpression(on, calcTypes));
            ArrayList<SelectOrderBy> orders = GROUP_CONCAT_ORDER_LIST.get((Aggregate) expression);
            if (!F.isEmpty(orders))
                parseGroupConcatOrder(res, orders, calcTypes);
            Expression separator = GROUP_CONCAT_SEPARATOR.get((Aggregate) expression);
            if (separator != null)
                res.setGroupConcatSeparator(parseExpression(separator, calcTypes));
            return res;
        }
    }
    if (expression instanceof ExpressionList) {
        Expression[] exprs = EXPR_LIST.get((ExpressionList) expression);
        GridSqlArray res = new GridSqlArray(exprs.length);
        for (Expression expr : exprs) res.addChild(parseExpression(expr, calcTypes));
        return res;
    }
    if (expression instanceof ConditionExists) {
        Query qry = QUERY_EXISTS.get((ConditionExists) expression);
        GridSqlOperation res = new GridSqlOperation(EXISTS);
        res.addChild(parseQueryExpression(qry));
        return res;
    }
    throw new IgniteException("Unsupported expression: " + expression + " [type=" + expression.getClass().getSimpleName() + ']');
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) ConditionNot(org.h2.expression.ConditionNot) Query(org.h2.command.dml.Query) JavaFunction(org.h2.expression.JavaFunction) Operation(org.h2.expression.Operation) ConditionAndOr(org.h2.expression.ConditionAndOr) Subquery(org.h2.expression.Subquery) ExpressionColumn(org.h2.expression.ExpressionColumn) Function(org.h2.expression.Function) TableFunction(org.h2.expression.TableFunction) JavaFunction(org.h2.expression.JavaFunction) Comparison(org.h2.expression.Comparison) 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) IgniteException(org.apache.ignite.IgniteException) ExpressionList(org.h2.expression.ExpressionList) FunctionAlias(org.h2.engine.FunctionAlias) ConditionIn(org.h2.expression.ConditionIn) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) CompareLike(org.h2.expression.CompareLike) ConditionInConstantSet(org.h2.expression.ConditionInConstantSet) ConditionInSelect(org.h2.expression.ConditionInSelect) Expression(org.h2.expression.Expression) GridSqlType.fromExpression(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression) ValueExpression(org.h2.expression.ValueExpression) FunctionAlias(org.h2.engine.FunctionAlias) Alias(org.h2.expression.Alias) ValueExpression(org.h2.expression.ValueExpression) Parameter(org.h2.expression.Parameter) Aggregate(org.h2.expression.Aggregate) ConditionExists(org.h2.expression.ConditionExists)

Aggregations

HashSet (java.util.HashSet)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)1 GridSqlAst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst)1 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)1 GridSqlConst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst)1 GridSqlOperation (org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)1 GridSqlOperationType (org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType)1 GridSqlType.fromColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn)1 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)1 PartitionConstantNode (org.apache.ignite.internal.sql.optimizer.affinity.PartitionConstantNode)1 PartitionGroupNode (org.apache.ignite.internal.sql.optimizer.affinity.PartitionGroupNode)1 PartitionSingleNode (org.apache.ignite.internal.sql.optimizer.affinity.PartitionSingleNode)1 PartitionTable (org.apache.ignite.internal.sql.optimizer.affinity.PartitionTable)1 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)1 Query (org.h2.command.dml.Query)1 SelectOrderBy (org.h2.command.dml.SelectOrderBy)1 FunctionAlias (org.h2.engine.FunctionAlias)1