Java Code Examples for org.apache.calcite.rex.RexBuilder#makeExactLiteral()

The following examples show how to use org.apache.calcite.rex.RexBuilder#makeExactLiteral() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlSplittableAggFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>{@code COUNT(*)}, and {@code COUNT} applied to all NOT NULL arguments,
 * become {@code 1}; otherwise
 * {@code CASE WHEN arg0 IS NOT NULL THEN 1 ELSE 0 END}.
 */
public RexNode singleton(RexBuilder rexBuilder, RelDataType inputRowType,
    AggregateCall aggregateCall) {
  final List<RexNode> predicates = new ArrayList<>();
  for (Integer arg : aggregateCall.getArgList()) {
    final RelDataType type = inputRowType.getFieldList().get(arg).getType();
    if (type.isNullable()) {
      predicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(type, arg)));
    }
  }
  final RexNode predicate =
      RexUtil.composeConjunction(rexBuilder, predicates, true);
  if (predicate == null) {
    return rexBuilder.makeExactLiteral(BigDecimal.ONE);
  } else {
    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, predicate,
        rexBuilder.makeExactLiteral(BigDecimal.ONE),
        rexBuilder.makeExactLiteral(BigDecimal.ZERO));
  }
}
 
Example 2
Source File: RelBuilder.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates a literal (constant expression). */
public RexNode literal(Object value) {
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    if (value == null) {
        return rexBuilder.constantNull();
    } else if (value instanceof Boolean) {
        return rexBuilder.makeLiteral((Boolean) value);
    } else if (value instanceof BigDecimal) {
        return rexBuilder.makeExactLiteral((BigDecimal) value);
    } else if (value instanceof Float || value instanceof Double) {
        return rexBuilder.makeApproxLiteral(BigDecimal.valueOf(((Number) value).doubleValue()));
    } else if (value instanceof Number) {
        return rexBuilder.makeExactLiteral(BigDecimal.valueOf(((Number) value).longValue()));
    } else if (value instanceof String) {
        return rexBuilder.makeLiteral((String) value);
    } else {
        throw new IllegalArgumentException("cannot convert " + value + " (" + value.getClass() + ") to a constant");
    }
}
 
Example 3
Source File: AggregateReduceFunctionsRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RexNode divide(boolean biased, RexBuilder rexBuilder, RexNode sumXY,
    RexNode sumXSumY, RexNode countArg) {
  final RexNode avgSumSquaredArg =
       rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, sumXSumY, countArg);
  final RexNode diff =
      rexBuilder.makeCall(SqlStdOperatorTable.MINUS, sumXY, avgSumSquaredArg);
  final RexNode denominator;
  if (biased) {
    denominator = countArg;
  } else {
    final RexLiteral one = rexBuilder.makeExactLiteral(BigDecimal.ONE);
    final RexNode nul = rexBuilder.makeNullLiteral(countArg.getType());
    final RexNode countMinusOne =
        rexBuilder.makeCall(SqlStdOperatorTable.MINUS, countArg, one);
    final RexNode countEqOne =
        rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, countArg, one);
    denominator = rexBuilder.makeCall(SqlStdOperatorTable.CASE, countEqOne,
        nul, countMinusOne);
  }
  return rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, diff, denominator);
}
 
Example 4
Source File: SqlSplittableAggFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>{@code COUNT(*)}, and {@code COUNT} applied to all NOT NULL arguments,
 * become {@code 1}; otherwise
 * {@code CASE WHEN arg0 IS NOT NULL THEN 1 ELSE 0 END}.
 */
public RexNode singleton(RexBuilder rexBuilder, RelDataType inputRowType,
    AggregateCall aggregateCall) {
  final List<RexNode> predicates = new ArrayList<>();
  for (Integer arg : aggregateCall.getArgList()) {
    final RelDataType type = inputRowType.getFieldList().get(arg).getType();
    if (type.isNullable()) {
      predicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(type, arg)));
    }
  }
  final RexNode predicate =
      RexUtil.composeConjunction(rexBuilder, predicates, true);
  final RexNode rexOne = rexBuilder.makeExactLiteral(
      BigDecimal.ONE, aggregateCall.getType());
  if (predicate == null) {
    return rexOne;
  } else {
    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, predicate, rexOne,
        rexBuilder.makeExactLiteral(BigDecimal.ZERO, aggregateCall.getType()));
  }
}
 
Example 5
Source File: RelBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a literal (constant expression). */
public RexNode literal(Object value) {
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  if (value == null) {
    final RelDataType type = getTypeFactory().createSqlType(SqlTypeName.NULL);
    return rexBuilder.makeNullLiteral(type);
  } else if (value instanceof Boolean) {
    return rexBuilder.makeLiteral((Boolean) value);
  } else if (value instanceof BigDecimal) {
    return rexBuilder.makeExactLiteral((BigDecimal) value);
  } else if (value instanceof Float || value instanceof Double) {
    return rexBuilder.makeApproxLiteral(
        BigDecimal.valueOf(((Number) value).doubleValue()));
  } else if (value instanceof Number) {
    return rexBuilder.makeExactLiteral(
        BigDecimal.valueOf(((Number) value).longValue()));
  } else if (value instanceof String) {
    return rexBuilder.makeLiteral((String) value);
  } else if (value instanceof Enum) {
    return rexBuilder.makeLiteral(value,
        getTypeFactory().createSqlType(SqlTypeName.SYMBOL), false);
  } else {
    throw new IllegalArgumentException("cannot convert " + value
        + " (" + value.getClass() + ") to a constant");
  }
}
 
Example 6
Source File: EmpInitializerExpressionFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RexNode newColumnDefaultValue(RelOptTable table,
    int iColumn, InitializerContext context) {
  final RexBuilder rexBuilder = context.getRexBuilder();
  final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
  switch (iColumn) {
  case 0:
    return rexBuilder.makeExactLiteral(new BigDecimal(123),
        typeFactory.createSqlType(SqlTypeName.INTEGER));
  case 1:
    return rexBuilder.makeLiteral("Bob");
  case 5:
    return rexBuilder.makeExactLiteral(new BigDecimal(555),
        typeFactory.createSqlType(SqlTypeName.INTEGER));
  default:
    return super.newColumnDefaultValue(table, iColumn, context);
  }
}
 
Example 7
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void addRow(ImmutableList.Builder<ImmutableList<RexLiteral>> builder,
    RexBuilder rexBuilder, Object... values) {
  ImmutableList.Builder<RexLiteral> b = ImmutableList.builder();
  final RelDataType varcharType =
      rexBuilder.getTypeFactory().createSqlType(SqlTypeName.VARCHAR);
  for (Object value : values) {
    final RexLiteral literal;
    if (value == null) {
      literal = rexBuilder.makeNullLiteral(varcharType);
    } else if (value instanceof Integer) {
      literal = rexBuilder.makeExactLiteral(
          BigDecimal.valueOf((Integer) value));
    } else {
      literal = rexBuilder.makeLiteral((String) value);
    }
    b.add(literal);
  }
  builder.add(b.build());
}
 
Example 8
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: CountingFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode newColumnDefaultValue(RelOptTable table,
    int iColumn, InitializerContext context) {
  THREAD_CALL_COUNT.get().incrementAndGet();
  final RelDataTypeField field =
      table.getRowType().getFieldList().get(iColumn);
  if (defaultColumns.contains(field.getName())) {
    final RexBuilder rexBuilder = context.getRexBuilder();
    return rexBuilder.makeExactLiteral(BigDecimal.ONE);
  }
  return super.newColumnDefaultValue(table, iColumn, context);
}
 
Example 11
Source File: AggregateReduceFunctionsRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RexNode reduceRegrSzz(
    Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    List<RexNode> inputExprs,
    int xIndex,
    int yIndex,
    int nullFilterIndex) {
  // regr_sxx(x, y) ==>
  //    sum(y * y, x) - sum(y, x) * sum(y, x) / regr_count(x, y)
  //

  final RelOptCluster cluster = oldAggRel.getCluster();
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
  final RelDataType argXType = getFieldType(oldAggRel.getInput(), xIndex);
  final RelDataType argYType =
      xIndex == yIndex ? argXType : getFieldType(oldAggRel.getInput(), yIndex);
  final RelDataType nullFilterIndexType =
      nullFilterIndex == yIndex ? argYType : getFieldType(oldAggRel.getInput(), yIndex);

  final RelDataType oldCallType =
      typeFactory.createTypeWithNullability(oldCall.getType(),
          argXType.isNullable() || argYType.isNullable() || nullFilterIndexType.isNullable());

  final RexNode argX =
      rexBuilder.ensureType(oldCallType, inputExprs.get(xIndex), true);
  final RexNode argY =
      rexBuilder.ensureType(oldCallType, inputExprs.get(yIndex), true);
  final RexNode argNullFilter =
      rexBuilder.ensureType(oldCallType, inputExprs.get(nullFilterIndex), true);

  final RexNode argXArgY = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, argX, argY);
  final int argSquaredOrdinal = lookupOrAdd(inputExprs, argXArgY);

  final RexNode argXAndYNotNullFilter = rexBuilder.makeCall(SqlStdOperatorTable.AND,
      rexBuilder.makeCall(SqlStdOperatorTable.AND,
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, argX),
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, argY)),
      rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, argNullFilter));
  final int argXAndYNotNullFilterOrdinal = lookupOrAdd(inputExprs, argXAndYNotNullFilter);
  final RexNode sumXY = getSumAggregatedRexNodeWithBinding(
      oldAggRel, oldCall, newCalls, aggCallMapping, argXArgY.getType(),
      argSquaredOrdinal, argXAndYNotNullFilterOrdinal);
  final RexNode sumXYCast = rexBuilder.ensureType(oldCallType, sumXY, true);

  final RexNode sumX = getSumAggregatedRexNode(oldAggRel, oldCall,
      newCalls, aggCallMapping, rexBuilder, xIndex, argXAndYNotNullFilterOrdinal);
  final RexNode sumY = xIndex == yIndex
      ? sumX
      : getSumAggregatedRexNode(oldAggRel, oldCall, newCalls,
          aggCallMapping, rexBuilder, yIndex, argXAndYNotNullFilterOrdinal);

  final RexNode sumXSumY = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, sumX, sumY);

  final RexNode countArg = getRegrCountRexNode(oldAggRel, oldCall, newCalls, aggCallMapping,
      ImmutableIntList.of(xIndex), ImmutableList.of(argXType), argXAndYNotNullFilterOrdinal);

  RexLiteral zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
  RexNode nul = rexBuilder.constantNull();
  final RexNode avgSumXSumY = rexBuilder.makeCall(SqlStdOperatorTable.CASE,
      rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, countArg, zero), nul,
          rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, sumXSumY, countArg));
  final RexNode avgSumXSumYCast = rexBuilder.ensureType(oldCallType, avgSumXSumY, true);
  final RexNode result =
      rexBuilder.makeCall(SqlStdOperatorTable.MINUS, sumXYCast, avgSumXSumYCast);
  return rexBuilder.makeCast(oldCall.getType(), result);
}
 
Example 12
Source File: SqlNodeToRexConverterImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RexNode convertLiteral(
    SqlRexContext cx,
    SqlLiteral literal) {
  RexBuilder rexBuilder = cx.getRexBuilder();
  RelDataTypeFactory typeFactory = cx.getTypeFactory();
  SqlValidator validator = cx.getValidator();
  if (literal.getValue() == null) {
    // Since there is no eq. RexLiteral of SqlLiteral.Unknown we
    // treat it as a cast(null as boolean)
    RelDataType type;
    if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
      type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
      type = typeFactory.createTypeWithNullability(type, true);
    } else {
      type = validator.getValidatedNodeType(literal);
    }
    return rexBuilder.makeCast(
        type,
        rexBuilder.constantNull());
  }

  BitString bitString;
  SqlIntervalLiteral.IntervalValue intervalValue;
  long l;

  switch (literal.getTypeName()) {
  case DECIMAL:
    // exact number
    BigDecimal bd = literal.getValueAs(BigDecimal.class);
    return rexBuilder.makeExactLiteral(
        bd,
        literal.createSqlType(typeFactory));

  case DOUBLE:
    // approximate type
    // TODO:  preserve fixed-point precision and large integers
    return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));

  case CHAR:
    return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
  case BOOLEAN:
    return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
  case BINARY:
    bitString = literal.getValueAs(BitString.class);
    Preconditions.checkArgument((bitString.getBitCount() % 8) == 0,
        "incomplete octet");

    // An even number of hexits (e.g. X'ABCD') makes whole number
    // of bytes.
    ByteString byteString = new ByteString(bitString.getAsByteArray());
    return rexBuilder.makeBinaryLiteral(byteString);
  case SYMBOL:
    return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
  case TIMESTAMP:
    return rexBuilder.makeTimestampLiteral(
        literal.getValueAs(TimestampString.class),
        ((SqlTimestampLiteral) literal).getPrec());
  case TIME:
    return rexBuilder.makeTimeLiteral(
        literal.getValueAs(TimeString.class),
        ((SqlTimeLiteral) literal).getPrec());
  case DATE:
    return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));

  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    SqlIntervalQualifier sqlIntervalQualifier =
        literal.getValueAs(SqlIntervalLiteral.IntervalValue.class)
            .getIntervalQualifier();
    return rexBuilder.makeIntervalLiteral(
        literal.getValueAs(BigDecimal.class),
        sqlIntervalQualifier);
  default:
    throw Util.unexpected(literal.getTypeName());
  }
}
 
Example 13
Source File: AggregateReduceFunctionsRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
private RexNode reduceRegrSzz(
    Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    List<RexNode> inputExprs,
    int xIndex,
    int yIndex,
    int nullFilterIndex) {
  // regr_sxx(x, y) ==>
  //    sum(y * y, x) - sum(y, x) * sum(y, x) / regr_count(x, y)
  //

  final RelOptCluster cluster = oldAggRel.getCluster();
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
  final RelDataType argXType = getFieldType(oldAggRel.getInput(), xIndex);
  final RelDataType argYType =
      xIndex == yIndex ? argXType : getFieldType(oldAggRel.getInput(), yIndex);
  final RelDataType nullFilterIndexType =
      nullFilterIndex == yIndex ? argYType : getFieldType(oldAggRel.getInput(), yIndex);

  final RelDataType oldCallType =
      typeFactory.createTypeWithNullability(oldCall.getType(),
          argXType.isNullable() || argYType.isNullable() || nullFilterIndexType.isNullable());

  final RexNode argX =
      rexBuilder.ensureType(oldCallType, inputExprs.get(xIndex), true);
  final RexNode argY =
      rexBuilder.ensureType(oldCallType, inputExprs.get(yIndex), true);
  final RexNode argNullFilter =
      rexBuilder.ensureType(oldCallType, inputExprs.get(nullFilterIndex), true);

  final RexNode argXArgY = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, argX, argY);
  final int argSquaredOrdinal = lookupOrAdd(inputExprs, argXArgY);

  final RexNode argXAndYNotNullFilter = rexBuilder.makeCall(SqlStdOperatorTable.AND,
      rexBuilder.makeCall(SqlStdOperatorTable.AND,
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, argX),
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, argY)),
      rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, argNullFilter));
  final int argXAndYNotNullFilterOrdinal = lookupOrAdd(inputExprs, argXAndYNotNullFilter);
  final RexNode sumXY = getSumAggregatedRexNodeWithBinding(
      oldAggRel, oldCall, newCalls, aggCallMapping, argXArgY.getType(),
      argSquaredOrdinal, argXAndYNotNullFilterOrdinal);
  final RexNode sumXYCast = rexBuilder.ensureType(oldCallType, sumXY, true);

  final RexNode sumX = getSumAggregatedRexNode(oldAggRel, oldCall,
      newCalls, aggCallMapping, rexBuilder, xIndex, argXAndYNotNullFilterOrdinal);
  final RexNode sumY = xIndex == yIndex
      ? sumX
      : getSumAggregatedRexNode(oldAggRel, oldCall, newCalls,
          aggCallMapping, rexBuilder, yIndex, argXAndYNotNullFilterOrdinal);

  final RexNode sumXSumY = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, sumX, sumY);

  final RexNode countArg = getRegrCountRexNode(oldAggRel, oldCall, newCalls, aggCallMapping,
      ImmutableIntList.of(xIndex), ImmutableList.of(argXType), argXAndYNotNullFilterOrdinal);

  RexLiteral zero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
  RexNode nul = rexBuilder.makeNullLiteral(zero.getType());
  final RexNode avgSumXSumY = rexBuilder.makeCall(SqlStdOperatorTable.CASE,
      rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, countArg, zero), nul,
          rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, sumXSumY, countArg));
  final RexNode avgSumXSumYCast = rexBuilder.ensureType(oldCallType, avgSumXSumY, true);
  final RexNode result =
      rexBuilder.makeCall(SqlStdOperatorTable.MINUS, sumXYCast, avgSumXSumYCast);
  return rexBuilder.makeCast(oldCall.getType(), result);
}
 
Example 14
Source File: SqlNodeToRexConverterImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RexNode convertLiteral(
    SqlRexContext cx,
    SqlLiteral literal) {
  RexBuilder rexBuilder = cx.getRexBuilder();
  RelDataTypeFactory typeFactory = cx.getTypeFactory();
  SqlValidator validator = cx.getValidator();
  if (literal.getValue() == null) {
    // Since there is no eq. RexLiteral of SqlLiteral.Unknown we
    // treat it as a cast(null as boolean)
    RelDataType type;
    if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
      type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
      type = typeFactory.createTypeWithNullability(type, true);
    } else {
      type = validator.getValidatedNodeType(literal);
    }
    return rexBuilder.makeNullLiteral(type);
  }

  BitString bitString;
  SqlIntervalLiteral.IntervalValue intervalValue;
  long l;

  switch (literal.getTypeName()) {
  case DECIMAL:
    // exact number
    BigDecimal bd = literal.getValueAs(BigDecimal.class);
    return rexBuilder.makeExactLiteral(
        bd,
        literal.createSqlType(typeFactory));

  case DOUBLE:
    // approximate type
    // TODO:  preserve fixed-point precision and large integers
    return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));

  case CHAR:
    return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
  case BOOLEAN:
    return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
  case BINARY:
    bitString = literal.getValueAs(BitString.class);
    Preconditions.checkArgument((bitString.getBitCount() % 8) == 0,
        "incomplete octet");

    // An even number of hexits (e.g. X'ABCD') makes whole number
    // of bytes.
    ByteString byteString = new ByteString(bitString.getAsByteArray());
    return rexBuilder.makeBinaryLiteral(byteString);
  case SYMBOL:
    return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
  case TIMESTAMP:
    return rexBuilder.makeTimestampLiteral(
        literal.getValueAs(TimestampString.class),
        ((SqlTimestampLiteral) literal).getPrec());
  case TIME:
    return rexBuilder.makeTimeLiteral(
        literal.getValueAs(TimeString.class),
        ((SqlTimeLiteral) literal).getPrec());
  case DATE:
    return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));

  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    SqlIntervalQualifier sqlIntervalQualifier =
        literal.getValueAs(SqlIntervalLiteral.IntervalValue.class)
            .getIntervalQualifier();
    return rexBuilder.makeIntervalLiteral(
        literal.getValueAs(BigDecimal.class),
        sqlIntervalQualifier);
  default:
    throw Util.unexpected(literal.getTypeName());
  }
}