Java Code Examples for org.apache.calcite.sql.SqlIntervalLiteral#getValue()

The following examples show how to use org.apache.calcite.sql.SqlIntervalLiteral#getValue() . 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: Db2SqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparseSqlIntervalLiteral(SqlWriter writer,
    SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  // A duration is a positive or negative number representing an interval of time.
  // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS.
  // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS.
  // If one operand is a timestamp, the other operand can be any of teh duration.

  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
}
 
Example 2
Source File: BigQuerySqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** BigQuery interval syntax: INTERVAL int64 time_unit. */
@Override public void unparseSqlIntervalLiteral(
        SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  SqlIntervalLiteral.IntervalValue interval =
          (SqlIntervalLiteral.IntervalValue) literal.getValue();
  writer.keyword("INTERVAL");
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  Long intervalValueInLong;
  try {
    intervalValueInLong = Long.parseLong(literal.getValue().toString());
  } catch (NumberFormatException e) {
    throw new RuntimeException("Only INT64 is supported as the interval value for BigQuery.");
  }
  writer.literal(intervalValueInLong.toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
          RelDataTypeSystem.DEFAULT);
}
 
Example 3
Source File: Db2SqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseSqlIntervalLiteral(SqlWriter writer,
    SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  // A duration is a positive or negative number representing an interval of time.
  // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS.
  // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS.
  // If one operand is a timestamp, the other operand can be any of teh duration.

  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
}
 
Example 4
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void checkIntervalConv(String sql, String expected) {
  SqlValidator validator = getValidator();
  final SqlCall n = (SqlCall) parseAndValidate(validator, sql);

  SqlNode node = null;
  for (int i = 0; i < n.operandCount(); i++) {
    node = stripAs(n.operand(i));
    if (node instanceof SqlCall) {
      node = ((SqlCall) node).operand(0);
      break;
    }
  }

  assertNotNull(node);
  SqlIntervalLiteral intervalLiteral = (SqlIntervalLiteral) node;
  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) intervalLiteral.getValue();
  long l =
      interval.getIntervalQualifier().isYearMonth()
          ? SqlParserUtil.intervalToMonths(interval)
          : SqlParserUtil.intervalToMillis(interval);
  String actual = l + "";
  assertEquals(expected, actual);
}
 
Example 5
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 6
Source File: MssqlSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void unparseSqlIntervalLiteralMssql(
    SqlWriter writer, SqlIntervalLiteral literal, int sign) {
  final SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
  writer.sep(",", true);
  if (interval.getSign() * sign == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
}
 
Example 7
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 8
Source File: MssqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void unparseSqlIntervalLiteralMssql(
    SqlWriter writer, SqlIntervalLiteral literal, int sign) {
  final SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
  writer.sep(",", true);
  if (interval.getSign() * sign == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
}