Java Code Examples for org.apache.calcite.avatica.util.TimeUnit#SECOND

The following examples show how to use org.apache.calcite.avatica.util.TimeUnit#SECOND . 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: ExpressionConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
private static TimeUnit timePointUnitToTimeUnit(TimePointUnit unit) {
	switch (unit) {
		case YEAR:
			return TimeUnit.YEAR;
		case MONTH:
			return TimeUnit.MONTH;
		case DAY:
			return TimeUnit.DAY;
		case HOUR:
			return TimeUnit.HOUR;
		case MINUTE:
			return TimeUnit.MINUTE;
		case SECOND:
			return TimeUnit.SECOND;
		case QUARTER:
			return TimeUnit.QUARTER;
		case WEEK:
			return TimeUnit.WEEK;
		case MILLISECOND:
			return TimeUnit.MILLISECOND;
		case MICROSECOND:
			return TimeUnit.MICROSECOND;
		default:
			throw new UnsupportedOperationException("TimePointUnit is: " + unit);
	}
}
 
Example 2
Source File: MysqlSqlDialect.java    From Quicksql with MIT License 5 votes vote down vote up
@Override public void unparseSqlIntervalQualifier(SqlWriter writer,
    SqlIntervalQualifier qualifier, RelDataTypeSystem typeSystem) {

  //  Unit Value         | Expected Format
  // --------------------+-------------------------------------------
  //  MICROSECOND        | MICROSECONDS
  //  SECOND             | SECONDS
  //  MINUTE             | MINUTES
  //  HOUR               | HOURS
  //  DAY                | DAYS
  //  WEEK               | WEEKS
  //  MONTH              | MONTHS
  //  QUARTER            | QUARTERS
  //  YEAR               | YEARS
  //  MINUTE_SECOND      | 'MINUTES:SECONDS'
  //  HOUR_MINUTE        | 'HOURS:MINUTES'
  //  DAY_HOUR           | 'DAYS HOURS'
  //  YEAR_MONTH         | 'YEARS-MONTHS'
  //  MINUTE_MICROSECOND | 'MINUTES:SECONDS.MICROSECONDS'
  //  HOUR_MICROSECOND   | 'HOURS:MINUTES:SECONDS.MICROSECONDS'
  //  SECOND_MICROSECOND | 'SECONDS.MICROSECONDS'
  //  DAY_MINUTE         | 'DAYS HOURS:MINUTES'
  //  DAY_MICROSECOND    | 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
  //  DAY_SECOND         | 'DAYS HOURS:MINUTES:SECONDS'
  //  HOUR_SECOND        | 'HOURS:MINUTES:SECONDS'

  if (!qualifier.useDefaultFractionalSecondPrecision()) {
    throw new AssertionError("Fractional second precision is not supported now ");
  }

  final String start = validate(qualifier.timeUnitRange.startUnit).name();
  if (qualifier.timeUnitRange.startUnit == TimeUnit.SECOND
      || qualifier.timeUnitRange.endUnit == null) {
    writer.keyword(start);
  } else {
    writer.keyword(start + "_" + qualifier.timeUnitRange.endUnit.name());
  }
}
 
Example 3
Source File: RexLiteral.java    From Quicksql with MIT License 5 votes vote down vote up
/** Returns a list of the time units covered by an interval type such
 * as HOUR TO SECOND. Adds MILLISECOND if the end is SECOND, to deal with
 * fractional seconds. */
private static List<TimeUnit> getTimeUnits(SqlTypeName typeName) {
  final TimeUnit start = typeName.getStartUnit();
  final TimeUnit end = typeName.getEndUnit();
  final ImmutableList<TimeUnit> list =
      TIME_UNITS.subList(start.ordinal(), end.ordinal() + 1);
  if (end == TimeUnit.SECOND) {
    return CompositeList.of(list, ImmutableList.of(TimeUnit.MILLISECOND));
  }
  return list;
}
 
Example 4
Source File: MysqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void unparseSqlIntervalQualifier(SqlWriter writer,
    SqlIntervalQualifier qualifier, RelDataTypeSystem typeSystem) {

  //  Unit Value         | Expected Format
  // --------------------+-------------------------------------------
  //  MICROSECOND        | MICROSECONDS
  //  SECOND             | SECONDS
  //  MINUTE             | MINUTES
  //  HOUR               | HOURS
  //  DAY                | DAYS
  //  WEEK               | WEEKS
  //  MONTH              | MONTHS
  //  QUARTER            | QUARTERS
  //  YEAR               | YEARS
  //  MINUTE_SECOND      | 'MINUTES:SECONDS'
  //  HOUR_MINUTE        | 'HOURS:MINUTES'
  //  DAY_HOUR           | 'DAYS HOURS'
  //  YEAR_MONTH         | 'YEARS-MONTHS'
  //  MINUTE_MICROSECOND | 'MINUTES:SECONDS.MICROSECONDS'
  //  HOUR_MICROSECOND   | 'HOURS:MINUTES:SECONDS.MICROSECONDS'
  //  SECOND_MICROSECOND | 'SECONDS.MICROSECONDS'
  //  DAY_MINUTE         | 'DAYS HOURS:MINUTES'
  //  DAY_MICROSECOND    | 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
  //  DAY_SECOND         | 'DAYS HOURS:MINUTES:SECONDS'
  //  HOUR_SECOND        | 'HOURS:MINUTES:SECONDS'

  if (!qualifier.useDefaultFractionalSecondPrecision()) {
    throw new AssertionError("Fractional second precision is not supported now ");
  }

  final String start = validate(qualifier.timeUnitRange.startUnit).name();
  if (qualifier.timeUnitRange.startUnit == TimeUnit.SECOND
      || qualifier.timeUnitRange.endUnit == null) {
    writer.keyword(start);
  } else {
    writer.keyword(start + "_" + qualifier.timeUnitRange.endUnit.name());
  }
}
 
Example 5
Source File: RexLiteral.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a list of the time units covered by an interval type such
 * as HOUR TO SECOND. Adds MILLISECOND if the end is SECOND, to deal with
 * fractional seconds. */
private static List<TimeUnit> getTimeUnits(SqlTypeName typeName) {
  final TimeUnit start = typeName.getStartUnit();
  final TimeUnit end = typeName.getEndUnit();
  final ImmutableList<TimeUnit> list =
      TIME_UNITS.subList(start.ordinal(), end.ordinal() + 1);
  if (end == TimeUnit.SECOND) {
    return CompositeList.of(list, ImmutableList.of(TimeUnit.MILLISECOND));
  }
  return list;
}
 
Example 6
Source File: StandardConvertletTable.java    From Quicksql with MIT License 4 votes vote down vote up
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  // TIMESTAMPDIFF(unit, t1, t2)
  //    => (t2 - t1) UNIT
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlLiteral unitLiteral = call.operand(0);
  TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
  BigDecimal multiplier = BigDecimal.ONE;
  BigDecimal divider = BigDecimal.ONE;
  SqlTypeName sqlTypeName = unit == TimeUnit.NANOSECOND
      ? SqlTypeName.BIGINT
      : SqlTypeName.INTEGER;
  switch (unit) {
  case MICROSECOND:
  case MILLISECOND:
  case NANOSECOND:
  case WEEK:
    multiplier = BigDecimal.valueOf(DateTimeUtils.MILLIS_PER_SECOND);
    divider = unit.multiplier;
    unit = TimeUnit.SECOND;
    break;
  case QUARTER:
    divider = unit.multiplier;
    unit = TimeUnit.MONTH;
    break;
  }
  final SqlIntervalQualifier qualifier =
      new SqlIntervalQualifier(unit, null, SqlParserPos.ZERO);
  final RexNode op2 = cx.convertExpression(call.operand(2));
  final RexNode op1 = cx.convertExpression(call.operand(1));
  final RelDataType intervalType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlIntervalType(qualifier),
          op1.getType().isNullable() || op2.getType().isNullable());
  final RexCall rexCall = (RexCall) rexBuilder.makeCall(
      intervalType, SqlStdOperatorTable.MINUS_DATE,
      ImmutableList.of(op2, op1));
  final RelDataType intType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlType(sqlTypeName),
          SqlTypeUtil.containsNullable(rexCall.getType()));
  RexNode e = rexBuilder.makeCast(intType, rexCall);
  return rexBuilder.multiplyDivide(e, multiplier, divider);
}
 
Example 7
Source File: MycatCalciteMySqlNodeVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public static TimeUnit[] getTimeUnit(SQLIntervalUnit unit) {
    TimeUnit[] timeUnits = new TimeUnit[2];
    switch (unit) {
        // case MICROSECOND:
        // timeUnits[0] = TimeUnit.MICROSECOND;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case SECOND:
            timeUnits[0] = TimeUnit.SECOND;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        case MINUTE:
            timeUnits[0] = TimeUnit.MINUTE;
            timeUnits[1] = TimeUnit.MINUTE;
            break;
        case HOUR:
            timeUnits[0] = TimeUnit.HOUR;
            timeUnits[1] = TimeUnit.HOUR;
            break;
        case DAY:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.DAY;
            break;
        case WEEK:
            timeUnits[0] = TimeUnit.WEEK;
            timeUnits[1] = TimeUnit.WEEK;
            break;
        case MONTH:
            timeUnits[0] = TimeUnit.MONTH;
            timeUnits[1] = TimeUnit.MONTH;
            break;
        case QUARTER:
            timeUnits[0] = TimeUnit.QUARTER;
            timeUnits[1] = TimeUnit.QUARTER;
            break;
        case YEAR:
            timeUnits[0] = TimeUnit.YEAR;
            timeUnits[1] = TimeUnit.YEAR;
            break;
        // case MINUTE_MICROSECOND:
        // timeUnits[0] = TimeUnit.MINUTE;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case MINUTE_SECOND:
            timeUnits[0] = TimeUnit.MINUTE;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        // case HOUR_MICROSECOND:
        // timeUnits[0] = TimeUnit.HOUR;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case HOUR_SECOND:
            timeUnits[0] = TimeUnit.HOUR;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        case HOUR_MINUTE:
            timeUnits[0] = TimeUnit.HOUR;
            timeUnits[1] = TimeUnit.MINUTE;
            break;
        // case DAY_MICROSECOND:
        // timeUnits[0] = TimeUnit.DAY;
        // timeUnits[1] = TimeUnit.MICROSECOND;
        // break;
        case DAY_SECOND:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.SECOND;
            break;
        case DAY_MINUTE:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.MINUTE;
            break;
        case DAY_HOUR:
            timeUnits[0] = TimeUnit.DAY;
            timeUnits[1] = TimeUnit.HOUR;
            break;
        case YEAR_MONTH:
            timeUnits[0] = TimeUnit.YEAR;
            timeUnits[1] = TimeUnit.MONTH;
            break;
        default:
            throw new ParserException("Unsupported time unit");
    }
    return timeUnits;
}
 
Example 8
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  // TIMESTAMPDIFF(unit, t1, t2)
  //    => (t2 - t1) UNIT
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlLiteral unitLiteral = call.operand(0);
  TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
  BigDecimal multiplier = BigDecimal.ONE;
  BigDecimal divider = BigDecimal.ONE;
  SqlTypeName sqlTypeName = unit == TimeUnit.NANOSECOND
      ? SqlTypeName.BIGINT
      : SqlTypeName.INTEGER;
  switch (unit) {
  case MICROSECOND:
  case MILLISECOND:
  case NANOSECOND:
  case WEEK:
    multiplier = BigDecimal.valueOf(DateTimeUtils.MILLIS_PER_SECOND);
    divider = unit.multiplier;
    unit = TimeUnit.SECOND;
    break;
  case QUARTER:
    divider = unit.multiplier;
    unit = TimeUnit.MONTH;
    break;
  }
  final SqlIntervalQualifier qualifier =
      new SqlIntervalQualifier(unit, null, SqlParserPos.ZERO);
  final RexNode op2 = cx.convertExpression(call.operand(2));
  final RexNode op1 = cx.convertExpression(call.operand(1));
  final RelDataType intervalType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlIntervalType(qualifier),
          op1.getType().isNullable() || op2.getType().isNullable());
  final RexCall rexCall = (RexCall) rexBuilder.makeCall(
      intervalType, SqlStdOperatorTable.MINUS_DATE,
      ImmutableList.of(op2, op1));
  final RelDataType intType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlType(sqlTypeName),
          SqlTypeUtil.containsNullable(rexCall.getType()));
  RexNode e = rexBuilder.makeCast(intType, rexCall);
  return rexBuilder.multiplyDivide(e, multiplier, divider);
}