Search in sources :

Example 1 with SqlBetweenOperator

use of org.apache.calcite.sql.fun.SqlBetweenOperator in project calcite by apache.

the class StandardConvertletTable method convertBetween.

/**
 * Converts a BETWEEN expression.
 *
 * <p>Called automatically via reflection.
 */
public RexNode convertBetween(SqlRexContext cx, SqlBetweenOperator op, SqlCall call) {
    final List<RexNode> list = convertExpressionList(cx, call.getOperandList(), op.getOperandTypeChecker().getConsistency());
    final RexNode x = list.get(SqlBetweenOperator.VALUE_OPERAND);
    final RexNode y = list.get(SqlBetweenOperator.LOWER_OPERAND);
    final RexNode z = list.get(SqlBetweenOperator.UPPER_OPERAND);
    final RexBuilder rexBuilder = cx.getRexBuilder();
    RexNode ge1 = ge(rexBuilder, x, y);
    RexNode le1 = le(rexBuilder, x, z);
    RexNode and1 = and(rexBuilder, ge1, le1);
    RexNode res;
    final SqlBetweenOperator.Flag symmetric = op.flag;
    switch(symmetric) {
        case ASYMMETRIC:
            res = and1;
            break;
        case SYMMETRIC:
            RexNode ge2 = ge(rexBuilder, x, z);
            RexNode le2 = le(rexBuilder, x, y);
            RexNode and2 = and(rexBuilder, ge2, le2);
            res = or(rexBuilder, and1, and2);
            break;
        default:
            throw Util.unexpected(symmetric);
    }
    final SqlBetweenOperator betweenOp = (SqlBetweenOperator) call.getOperator();
    if (betweenOp.isNegated()) {
        res = rexBuilder.makeCall(SqlStdOperatorTable.NOT, res);
    }
    return res;
}
Also used : SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with SqlBetweenOperator

use of org.apache.calcite.sql.fun.SqlBetweenOperator in project drill by apache.

the class DrillOperatorTable method populateWrappedCalciteOperators.

private void populateWrappedCalciteOperators() {
    for (SqlOperator calciteOperator : inner.getOperatorList()) {
        final SqlOperator wrapper;
        if (calciteOperator instanceof SqlAggFunction) {
            wrapper = new DrillCalciteSqlAggFunctionWrapper((SqlAggFunction) calciteOperator, getFunctionListWithInference(calciteOperator.getName()));
        } else if (calciteOperator instanceof SqlFunction) {
            wrapper = new DrillCalciteSqlFunctionWrapper((SqlFunction) calciteOperator, getFunctionListWithInference(calciteOperator.getName()));
        } else if (calciteOperator instanceof SqlBetweenOperator) {
            // During the procedure of converting to RexNode,
            // StandardConvertletTable.convertBetween expects the SqlOperator to be a subclass of SqlBetweenOperator
            final SqlBetweenOperator sqlBetweenOperator = (SqlBetweenOperator) calciteOperator;
            wrapper = new DrillCalciteSqlBetweenOperatorWrapper(sqlBetweenOperator);
        } else {
            final String drillOpName;
            // Otherwise, Calcite will mix them up with binary operator subtract (-) or add (+)
            if (calciteOperator == SqlStdOperatorTable.UNARY_MINUS || calciteOperator == SqlStdOperatorTable.UNARY_PLUS) {
                drillOpName = calciteOperator.getName();
            } else {
                drillOpName = FunctionCallFactory.convertToDrillFunctionName(calciteOperator.getName());
            }
            final List<DrillFuncHolder> drillFuncHolders = getFunctionListWithInference(drillOpName);
            if (drillFuncHolders.isEmpty()) {
                continue;
            }
            wrapper = new DrillCalciteSqlOperatorWrapper(calciteOperator, drillOpName, drillFuncHolders);
        }
        calciteToWrapper.put(calciteOperator, wrapper);
    }
}
Also used : DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) SqlFunction(org.apache.calcite.sql.SqlFunction)

Aggregations

SqlBetweenOperator (org.apache.calcite.sql.fun.SqlBetweenOperator)2 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)1 SqlFunction (org.apache.calcite.sql.SqlFunction)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 DrillFuncHolder (org.apache.drill.exec.expr.fn.DrillFuncHolder)1