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

The following examples show how to use org.apache.calcite.avatica.util.TimeUnit#MONTH . 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: RexBuilder.java    From Quicksql with MIT License 5 votes vote down vote up
/** Returns the lowest granularity unit for the given unit.
 * YEAR and MONTH intervals are stored as months;
 * HOUR, MINUTE, SECOND intervals are stored as milliseconds. */
protected static TimeUnit baseUnit(SqlTypeName unit) {
  if (unit.isYearMonth()) {
    return TimeUnit.MONTH;
  } else {
    return TimeUnit.MILLISECOND;
  }
}
 
Example 3
Source File: RexBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the lowest granularity unit for the given unit.
 * YEAR and MONTH intervals are stored as months;
 * HOUR, MINUTE, SECOND intervals are stored as milliseconds. */
protected static TimeUnit baseUnit(SqlTypeName unit) {
  if (unit.isYearMonth()) {
    return TimeUnit.MONTH;
  } else {
    return TimeUnit.MILLISECOND;
  }
}
 
Example 4
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 5
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 6
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);
}