use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.
the class StandardConvertletTable method convertFloorCeil.
protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) {
final boolean floor = call.getKind() == SqlKind.FLOOR;
// Rewrite floor, ceil of interval
if (call.operandCount() == 1 && call.operand(0) instanceof SqlIntervalLiteral) {
final SqlIntervalLiteral literal = call.operand(0);
SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue();
BigDecimal val = interval.getIntervalQualifier().getStartUnit().multiplier;
RexNode rexInterval = cx.convertExpression(literal);
final RexBuilder rexBuilder = cx.getRexBuilder();
RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0));
RexNode cond = ge(rexBuilder, rexInterval, zero);
RexNode pad = rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE));
RexNode cast = rexBuilder.makeReinterpretCast(rexInterval.getType(), pad, rexBuilder.makeLiteral(false));
RexNode sum = floor ? minus(rexBuilder, rexInterval, cast) : plus(rexBuilder, rexInterval, cast);
RexNode kase = floor ? case_(rexBuilder, rexInterval, cond, sum) : case_(rexBuilder, sum, cond, rexInterval);
RexNode factor = rexBuilder.makeExactLiteral(val);
RexNode div = divideInt(rexBuilder, kase, factor);
return multiply(rexBuilder, div, factor);
}
// normal floor, ceil function
return convertFunction(cx, (SqlFunction) call.getOperator(), call);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.
the class StandardConvertletTable method convertCall.
/**
* Converts a {@link SqlCall} to a {@link RexCall} with a perhaps different
* operator.
*/
private RexNode convertCall(SqlRexContext cx, SqlCall call, SqlOperator op) {
final List<SqlNode> operands = call.getOperandList();
final RexBuilder rexBuilder = cx.getRexBuilder();
final SqlOperandTypeChecker.Consistency consistency = op.getOperandTypeChecker() == null ? SqlOperandTypeChecker.Consistency.NONE : op.getOperandTypeChecker().getConsistency();
final List<RexNode> exprs = convertExpressionList(cx, operands, consistency);
RelDataType type = rexBuilder.deriveReturnType(op, exprs);
return rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.
the class StandardConvertletTable method convertDatetimeMinus.
public RexNode convertDatetimeMinus(SqlRexContext cx, SqlDatetimeSubtractionOperator op, SqlCall call) {
// Rewrite datetime minus
final RexBuilder rexBuilder = cx.getRexBuilder();
final List<SqlNode> operands = call.getOperandList();
final List<RexNode> exprs = convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
RelDataType int8Type = cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
final RexNode[] casts = new RexNode[2];
casts[0] = rexBuilder.makeCast(cx.getTypeFactory().createTypeWithNullability(int8Type, exprs.get(0).getType().isNullable()), exprs.get(0));
casts[1] = rexBuilder.makeCast(cx.getTypeFactory().createTypeWithNullability(int8Type, exprs.get(1).getType().isNullable()), exprs.get(1));
final RelDataType resType = cx.getValidator().getValidatedNodeType(call);
return rexBuilder.makeCall(resType, op, exprs.subList(0, 2));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder 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;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.
the class StandardConvertletTable method makeConstructorCall.
private static RexNode makeConstructorCall(SqlRexContext cx, SqlFunction constructor, List<RexNode> exprs) {
final RexBuilder rexBuilder = cx.getRexBuilder();
RelDataType type = rexBuilder.deriveReturnType(constructor, exprs);
int n = type.getFieldCount();
ImmutableList.Builder<RexNode> initializationExprs = ImmutableList.builder();
final InitializerContext initializerContext = new InitializerContext() {
public RexBuilder getRexBuilder() {
return rexBuilder;
}
public RexNode convertExpression(SqlNode e) {
throw new UnsupportedOperationException();
}
};
for (int i = 0; i < n; ++i) {
initializationExprs.add(cx.getInitializerExpressionFactory().newAttributeInitializer(type, constructor, i, exprs, initializerContext));
}
List<RexNode> defaultCasts = RexUtil.generateCastExpressions(rexBuilder, type, initializationExprs.build());
return rexBuilder.makeNewInvocation(type, defaultCasts);
}
Aggregations