Java Code Examples for org.apache.calcite.util.Util#calendar()

The following examples show how to use org.apache.calcite.util.Util#calendar() . 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: SparkHandlerImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a SparkHandlerImpl. */
private SparkHandlerImpl() {
  if (!CLASS_DIR.isDirectory() && !CLASS_DIR.mkdirs()) {
    System.err.println("Unable to create temporary folder " + CLASS_DIR);
  }
  classServer = new HttpServer(CLASS_DIR);

  // Start the classServer and store its URI in a spark system property
  // (which will be passed to executors so that they can connect to it)
  classServer.start();
  System.setProperty("spark.repl.class.uri", classServer.uri());

  // Generate a starting point for class names that is unlikely to clash with
  // previous classes. A better solution would be to clear the class directory
  // on startup.
  final Calendar calendar = Util.calendar();
  classId = new AtomicInteger(
      calendar.get(Calendar.HOUR_OF_DAY) * 10000
      + calendar.get(Calendar.MINUTE) * 100
      + calendar.get(Calendar.SECOND));
}
 
Example 2
Source File: DruidDateRangeRulesTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1738">[CALCITE-1738]
 * Push CAST of literals to Druid</a>. */
@Test void testFilterWithCast() {
  final Fixture2 f = new Fixture2();
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.JANUARY, 1);
  final TimestampString from = TimestampString.fromCalendarFields(c);
  c.clear();
  c.set(2011, Calendar.JANUARY, 1);
  final TimestampString to = TimestampString.fromCalendarFields(c);

  // d >= 2010-01-01 AND d < 2011-01-01
  checkDateRangeNoSimplify(f,
      f.and(
          f.ge(f.d, f.cast(f.timestampDataType, f.timestampLiteral(from))),
          f.lt(f.d, f.cast(f.timestampDataType, f.timestampLiteral(to)))),
      is("[2010-01-01T00:00:00.000Z/2011-01-01T00:00:00.000Z]"));
}
 
Example 3
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Tests {@link RexBuilder#makeDateLiteral(DateString)}. */
@Test void testDateLiteral() {
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  RelDataType dateType = typeFactory.createSqlType(SqlTypeName.DATE);
  final RexBuilder builder = new RexBuilder(typeFactory);

  // Old way: provide a Calendar
  final Calendar calendar = Util.calendar();
  calendar.set(1969, Calendar.JULY, 21); // one small step
  calendar.set(Calendar.MILLISECOND, 0);
  checkDate(builder.makeLiteral(calendar, dateType, false));

  // Old way #2: Provide in Integer
  checkDate(builder.makeLiteral(MOON_DAY, dateType, false));

  // The new way
  final DateString d = new DateString(1969, 7, 21);
  checkDate(builder.makeLiteral(d, dateType, false));
}
 
Example 4
Source File: DateRangeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Calendar timestampValue(RexLiteral timeLiteral) {
  switch (timeLiteral.getTypeName()) {
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
    return Util.calendar(
        SqlFunctions.timestampWithLocalTimeZoneToTimestamp(
            timeLiteral.getValueAs(Long.class), tz));
  case TIMESTAMP:
    return Util.calendar(timeLiteral.getValueAs(Long.class));
  case DATE:
    // Cast date to timestamp with local time zone
    final DateString d = timeLiteral.getValueAs(DateString.class);
    return Util.calendar(d.getMillisSinceEpoch());
  default:
    throw Util.unexpected(timeLiteral.getTypeName());
  }
}
 
Example 5
Source File: DateRangeRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
private Calendar timestampValue(RexLiteral timeLiteral) {
    switch (timeLiteral.getTypeName()) {
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
        return Util.calendar(
                SqlFunctions.timestampWithLocalTimeZoneToTimestamp(timeLiteral.getValueAs(Long.class), tz));
    case TIMESTAMP:
        return Util.calendar(timeLiteral.getValueAs(Long.class));
    case DATE:
        // Cast date to timestamp with local time zone
        final DateString d = timeLiteral.getValueAs(DateString.class);
        return Util.calendar(d.getMillisSinceEpoch());
    default:
        throw Util.unexpected(timeLiteral.getTypeName());
    }
}
 
Example 6
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFloorExtractBothRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  Fixture2 f = new Fixture2();
  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f,
      f.and(f.eq(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
          f.eq(f.exMonthTs, f.literal(5))),
      is("AND(AND(>=($9, 2010-01-01 00:00:00), <($9, 2011-01-01 00:00:00)),"
          + " AND(>=($9, 2010-05-01 00:00:00), <($9, 2010-06-01 00:00:00)))"));

  // No lower range for floor
  checkDateRange(f,
      f.and(f.le(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
          f.eq(f.exMonthTs, f.literal(5))),
      is("AND(<($9, 2011-01-01 00:00:00), =(EXTRACT(FLAG(MONTH), $9), 5))"));

  // No lower range for floor
  checkDateRange(f,
      f.and(f.gt(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
          f.eq(f.exMonthTs, f.literal(5))),
      is("AND(>=($9, 2011-01-01 00:00:00), =(EXTRACT(FLAG(MONTH), $9), 5))"));

  // No upper range for individual floor rexNodes, but combined results in bounded interval
  checkDateRange(f,
      f.and(f.le(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
          f.eq(f.exMonthTs, f.literal(5)),
          f.ge(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c)))),
      is("AND(<($9, 2011-01-01 00:00:00), AND(>=($9, 2010-05-01 00:00:00),"
          + " <($9, 2010-06-01 00:00:00)), >=($9, 2010-01-01 00:00:00))"));

}
 
Example 7
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFloorRewriteWithTimezone() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 1, 11, 30, 0);
  final Fixture2 f = new Fixture2();
  checkDateRange(f,
      f.eq(f.floorHour,
          f.timestampLocalTzLiteral(TimestampString.fromCalendarFields(c))),
      "IST",
      is("AND(>=($9, 2010-02-01 17:00:00), <($9, 2010-02-01 18:00:00))"),
      CoreMatchers.any(String.class));

  c.clear();
  c.set(2010, Calendar.FEBRUARY, 1, 11, 00, 0);
  checkDateRange(f,
      f.eq(f.floorHour,
          f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      "IST",
      is("AND(>=($9, 2010-02-01 11:00:00), <($9, 2010-02-01 12:00:00))"),
      CoreMatchers.any(String.class));

  c.clear();
  c.set(2010, Calendar.FEBRUARY, 1, 00, 00, 0);
  checkDateRange(f,
      f.eq(f.floorHour, f.dateLiteral(DateString.fromCalendarFields(c))),
      "IST",
      is("AND(>=($9, 2010-02-01 00:00:00), <($9, 2010-02-01 01:00:00))"),
      CoreMatchers.any(String.class));
}
 
Example 8
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCeilGeRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.ge(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">($9, 2010-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.ge(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">($9, 2009-01-01 00:00:00)"));
}
 
Example 9
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCeilGtRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.gt(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">($9, 2010-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.gt(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">($9, 2010-01-01 00:00:00)"));
}
 
Example 10
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCeilLeRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.le(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<=($9, 2010-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.le(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<=($9, 2010-01-01 00:00:00)"));
}
 
Example 11
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCeilLtRewrite() {
  final Calendar c = Util.calendar();

  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.lt(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<=($9, 2010-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.lt(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<=($9, 2009-01-01 00:00:00)"));
}
 
Example 12
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFloorGeRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.ge(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">=($9, 2011-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.ge(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">=($9, 2010-01-01 00:00:00)"));
}
 
Example 13
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFloorGtRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.gt(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">=($9, 2011-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.gt(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is(">=($9, 2011-01-01 00:00:00)"));
}
 
Example 14
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFloorLeRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.le(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<($9, 2011-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.le(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<($9, 2011-01-01 00:00:00)"));
}
 
Example 15
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFloorLtRewrite() {
  final Calendar c = Util.calendar();

  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  checkDateRange(f, f.lt(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<($9, 2011-01-01 00:00:00)"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.lt(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("<($9, 2010-01-01 00:00:00)"));
}
 
Example 16
Source File: DruidDateRangeRulesTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RexNode timestampLiteral(int year, int month, int day) {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(year, month, day);
  final TimestampString ts = TimestampString.fromCalendarFields(c);
  return timestampLiteral(ts);
}
 
Example 17
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testCeilEqRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  // Always False
  checkDateRange(f, f.eq(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("false"));
  checkDateRange(f, f.eq(f.timestampLiteral(TimestampString.fromCalendarFields(c)), f.ceilYear),
      is("false"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.eq(f.ceilYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2009-01-01 00:00:00), <=($9, 2010-01-01 00:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 1, 0, 0, 0);
  checkDateRange(f, f.eq(f.ceilMonth, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-01-01 00:00:00), <=($9, 2010-02-01 00:00:00))"));

  c.set(2010, Calendar.DECEMBER, 1, 0, 0, 0);
  checkDateRange(f, f.eq(f.ceilMonth, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-11-01 00:00:00), <=($9, 2010-12-01 00:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 0, 0, 0);
  checkDateRange(f, f.eq(f.ceilDay, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-02-03 00:00:00), <=($9, 2010-02-04 00:00:00))"));

  c.set(2010, Calendar.DECEMBER, 31, 0, 0, 0);
  checkDateRange(f, f.eq(f.ceilDay, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-12-30 00:00:00), <=($9, 2010-12-31 00:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 4, 0, 0);
  checkDateRange(f, f.eq(f.ceilHour, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-02-04 03:00:00), <=($9, 2010-02-04 04:00:00))"));

  c.set(2010, Calendar.DECEMBER, 31, 23, 0, 0);
  checkDateRange(f, f.eq(f.ceilHour, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-12-31 22:00:00), <=($9, 2010-12-31 23:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 2, 32, 0);
  checkDateRange(f,
      f.eq(f.ceilMinute, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-02-04 02:31:00), <=($9, 2010-02-04 02:32:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 2, 59, 0);
  checkDateRange(f,
      f.eq(f.ceilMinute, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>($9, 2010-02-04 02:58:00), <=($9, 2010-02-04 02:59:00))"));
}
 
Example 18
Source File: DateRangeRulesTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testFloorEqRewrite() {
  final Calendar c = Util.calendar();
  c.clear();
  c.set(2010, Calendar.FEBRUARY, 10, 11, 12, 05);
  final Fixture2 f = new Fixture2();
  // Always False
  checkDateRange(f, f.eq(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("false"));
  checkDateRange(f, f.eq(f.timestampLiteral(TimestampString.fromCalendarFields(c)), f.floorYear),
      is("false"));

  c.clear();
  c.set(2010, Calendar.JANUARY, 1, 0, 0, 0);
  checkDateRange(f, f.eq(f.floorYear, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-01-01 00:00:00), <($9, 2011-01-01 00:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 1, 0, 0, 0);
  checkDateRange(f, f.eq(f.floorMonth, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-02-01 00:00:00), <($9, 2010-03-01 00:00:00))"));

  c.set(2010, Calendar.DECEMBER, 1, 0, 0, 0);
  checkDateRange(f, f.eq(f.floorMonth, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-12-01 00:00:00), <($9, 2011-01-01 00:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 0, 0, 0);
  checkDateRange(f, f.eq(f.floorDay, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-02-04 00:00:00), <($9, 2010-02-05 00:00:00))"));

  c.set(2010, Calendar.DECEMBER, 31, 0, 0, 0);
  checkDateRange(f, f.eq(f.floorDay, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-12-31 00:00:00), <($9, 2011-01-01 00:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 4, 0, 0);
  checkDateRange(f, f.eq(f.floorHour, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-02-04 04:00:00), <($9, 2010-02-04 05:00:00))"));

  c.set(2010, Calendar.DECEMBER, 31, 23, 0, 0);
  checkDateRange(f, f.eq(f.floorHour, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-12-31 23:00:00), <($9, 2011-01-01 00:00:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 2, 32, 0);
  checkDateRange(f,
      f.eq(f.floorMinute, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-02-04 02:32:00), <($9, 2010-02-04 02:33:00))"));

  c.set(2010, Calendar.FEBRUARY, 4, 2, 59, 0);
  checkDateRange(f,
      f.eq(f.floorMinute, f.timestampLiteral(TimestampString.fromCalendarFields(c))),
      is("AND(>=($9, 2010-02-04 02:59:00), <($9, 2010-02-04 03:00:00))"));
}
 
Example 19
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Tests {@link RexBuilder#makeTimestampLiteral(TimestampString, int)}. */
@Test void testTimestampLiteral() {
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType timestampType =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
  final RelDataType timestampType3 =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 3);
  final RelDataType timestampType9 =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 9);
  final RelDataType timestampType18 =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 18);
  final RexBuilder builder = new RexBuilder(typeFactory);

  // Old way: provide a Calendar
  final Calendar calendar = Util.calendar();
  calendar.set(1969, Calendar.JULY, 21, 2, 56, 15); // one small step
  calendar.set(Calendar.MILLISECOND, 0);
  checkTimestamp(builder.makeLiteral(calendar, timestampType, false));

  // Old way #2: Provide a Long
  checkTimestamp(builder.makeLiteral(MOON, timestampType, false));

  // The new way
  final TimestampString ts = new TimestampString(1969, 7, 21, 2, 56, 15);
  checkTimestamp(builder.makeLiteral(ts, timestampType, false));

  // Now with milliseconds
  final TimestampString ts2 = ts.withMillis(56);
  assertThat(ts2.toString(), is("1969-07-21 02:56:15.056"));
  final RexNode literal2 = builder.makeLiteral(ts2, timestampType3, false);
  assertThat(((RexLiteral) literal2).getValueAs(TimestampString.class)
          .toString(), is("1969-07-21 02:56:15.056"));

  // Now with nanoseconds
  final TimestampString ts3 = ts.withNanos(56);
  final RexNode literal3 = builder.makeLiteral(ts3, timestampType9, false);
  assertThat(((RexLiteral) literal3).getValueAs(TimestampString.class)
          .toString(), is("1969-07-21 02:56:15"));
  final TimestampString ts3b = ts.withNanos(2345678);
  final RexNode literal3b = builder.makeLiteral(ts3b, timestampType9, false);
  assertThat(((RexLiteral) literal3b).getValueAs(TimestampString.class)
          .toString(), is("1969-07-21 02:56:15.002"));

  // Now with a very long fraction
  final TimestampString ts4 = ts.withFraction("102030405060708090102");
  final RexNode literal4 = builder.makeLiteral(ts4, timestampType18, false);
  assertThat(((RexLiteral) literal4).getValueAs(TimestampString.class)
          .toString(), is("1969-07-21 02:56:15.102"));

  // toString
  assertThat(ts2.round(1).toString(), is("1969-07-21 02:56:15"));
  assertThat(ts2.round(2).toString(), is("1969-07-21 02:56:15.05"));
  assertThat(ts2.round(3).toString(), is("1969-07-21 02:56:15.056"));
  assertThat(ts2.round(4).toString(), is("1969-07-21 02:56:15.056"));

  assertThat(ts2.toString(6), is("1969-07-21 02:56:15.056000"));
  assertThat(ts2.toString(1), is("1969-07-21 02:56:15.0"));
  assertThat(ts2.toString(0), is("1969-07-21 02:56:15"));

  assertThat(ts2.round(0).toString(), is("1969-07-21 02:56:15"));
  assertThat(ts2.round(0).toString(0), is("1969-07-21 02:56:15"));
  assertThat(ts2.round(0).toString(1), is("1969-07-21 02:56:15.0"));
  assertThat(ts2.round(0).toString(2), is("1969-07-21 02:56:15.00"));

  assertThat(TimestampString.fromMillisSinceEpoch(1456513560123L).toString(),
      is("2016-02-26 19:06:00.123"));
}
 
Example 20
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Tests {@link RexBuilder#makeTimeLiteral(TimeString, int)}. */
@Test void testTimeLiteral() {
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  RelDataType timeType = typeFactory.createSqlType(SqlTypeName.TIME);
  final RelDataType timeType3 =
      typeFactory.createSqlType(SqlTypeName.TIME, 3);
  final RelDataType timeType9 =
      typeFactory.createSqlType(SqlTypeName.TIME, 9);
  final RelDataType timeType18 =
      typeFactory.createSqlType(SqlTypeName.TIME, 18);
  final RexBuilder builder = new RexBuilder(typeFactory);

  // Old way: provide a Calendar
  final Calendar calendar = Util.calendar();
  calendar.set(1969, Calendar.JULY, 21, 2, 56, 15); // one small step
  calendar.set(Calendar.MILLISECOND, 0);
  checkTime(builder.makeLiteral(calendar, timeType, false));

  // Old way #2: Provide a Long
  checkTime(builder.makeLiteral(MOON_TIME, timeType, false));

  // The new way
  final TimeString t = new TimeString(2, 56, 15);
  assertThat(t.getMillisOfDay(), is(10575000));
  checkTime(builder.makeLiteral(t, timeType, false));

  // Now with milliseconds
  final TimeString t2 = t.withMillis(56);
  assertThat(t2.getMillisOfDay(), is(10575056));
  assertThat(t2.toString(), is("02:56:15.056"));
  final RexNode literal2 = builder.makeLiteral(t2, timeType3, false);
  assertThat(((RexLiteral) literal2).getValueAs(TimeString.class)
      .toString(), is("02:56:15.056"));

  // Now with nanoseconds
  final TimeString t3 = t.withNanos(2345678);
  assertThat(t3.getMillisOfDay(), is(10575002));
  final RexNode literal3 = builder.makeLiteral(t3, timeType9, false);
  assertThat(((RexLiteral) literal3).getValueAs(TimeString.class)
      .toString(), is("02:56:15.002"));

  // Now with a very long fraction
  final TimeString t4 = t.withFraction("102030405060708090102");
  assertThat(t4.getMillisOfDay(), is(10575102));
  final RexNode literal4 = builder.makeLiteral(t4, timeType18, false);
  assertThat(((RexLiteral) literal4).getValueAs(TimeString.class)
      .toString(), is("02:56:15.102"));

  // toString
  assertThat(t2.round(1).toString(), is("02:56:15"));
  assertThat(t2.round(2).toString(), is("02:56:15.05"));
  assertThat(t2.round(3).toString(), is("02:56:15.056"));
  assertThat(t2.round(4).toString(), is("02:56:15.056"));

  assertThat(t2.toString(6), is("02:56:15.056000"));
  assertThat(t2.toString(1), is("02:56:15.0"));
  assertThat(t2.toString(0), is("02:56:15"));

  assertThat(t2.round(0).toString(), is("02:56:15"));
  assertThat(t2.round(0).toString(0), is("02:56:15"));
  assertThat(t2.round(0).toString(1), is("02:56:15.0"));
  assertThat(t2.round(0).toString(2), is("02:56:15.00"));

  assertThat(TimeString.fromMillisOfDay(53560123).toString(),
      is("14:52:40.123"));
}