Search in sources :

Example 1 with HazelcastBetweenOperator

use of com.hazelcast.jet.sql.impl.validate.operators.predicate.HazelcastBetweenOperator in project hazelcast by hazelcast.

the class HazelcastSqlToRelConverter method convertBetween.

/**
 * Convert "val BETWEEN lower_bound AND upper_bound" expression to
 * 1. If ASYMMETRIC : "val >= lower_bound AND val =< upper_bound" expression. Default mode.
 * 2. If SYMMETRIC : "(val >= lower_bound AND val =< upper_bound) OR (val <= lower_bound AND val >= upper_bound)"
 * expression
 */
public RexNode convertBetween(SqlCall call, Blackboard blackboard) {
    SqlOperator currentOperator = call.getOperator();
    assert currentOperator instanceof HazelcastBetweenOperator;
    final RexBuilder rexBuilder = getRexBuilder();
    final HazelcastBetweenOperator betweenOp = (HazelcastBetweenOperator) currentOperator;
    final List<RexNode> list = convertExpressionList(rexBuilder, blackboard, call.getOperandList(), betweenOp.getOperandTypeChecker().getConsistency());
    final RexNode valueOperand = list.get(SqlBetweenOperator.VALUE_OPERAND);
    final RexNode lowerOperand = list.get(SqlBetweenOperator.LOWER_OPERAND);
    final RexNode upperOperand = list.get(SqlBetweenOperator.UPPER_OPERAND);
    RexNode ge1 = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, valueOperand, lowerOperand);
    RexNode le1 = rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, valueOperand, upperOperand);
    RexNode and1 = rexBuilder.makeCall(SqlStdOperatorTable.AND, ge1, le1);
    RexNode res;
    final SqlBetweenOperator.Flag symmetric = betweenOp.getFlag();
    switch(symmetric) {
        case ASYMMETRIC:
            res = and1;
            break;
        case SYMMETRIC:
            RexNode ge2 = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, valueOperand, upperOperand);
            RexNode le2 = rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, valueOperand, lowerOperand);
            RexNode and2 = rexBuilder.makeCall(SqlStdOperatorTable.AND, ge2, le2);
            res = rexBuilder.makeCall(SqlStdOperatorTable.OR, and1, and2);
            break;
        default:
            throw Util.unexpected(symmetric);
    }
    if (betweenOp.isNegated()) {
        res = rexBuilder.makeCall(SqlStdOperatorTable.NOT, res);
    }
    return res;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) HazelcastBetweenOperator(com.hazelcast.jet.sql.impl.validate.operators.predicate.HazelcastBetweenOperator) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

HazelcastBetweenOperator (com.hazelcast.jet.sql.impl.validate.operators.predicate.HazelcastBetweenOperator)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 SqlBetweenOperator (org.apache.calcite.sql.fun.SqlBetweenOperator)1