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

The following examples show how to use org.apache.calcite.avatica.util.TimeUnit#MILLISECOND . 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: RexLiteral.java    From Quicksql with MIT License 6 votes vote down vote up
private String intervalString(BigDecimal v) {
  final List<TimeUnit> timeUnits = getTimeUnits(type.getSqlTypeName());
  final StringBuilder b = new StringBuilder();
  for (TimeUnit timeUnit : timeUnits) {
    final BigDecimal[] result = v.divideAndRemainder(timeUnit.multiplier);
    if (b.length() > 0) {
      b.append(timeUnit.separator);
    }
    final int width = b.length() == 0 ? -1 : width(timeUnit); // don't pad 1st
    pad(b, result[0].toString(), width);
    v = result[1];
  }
  if (Util.last(timeUnits) == TimeUnit.MILLISECOND) {
    while (b.toString().matches(".*\\.[0-9]*0")) {
      if (b.toString().endsWith(".0")) {
        b.setLength(b.length() - 2); // remove ".0"
      } else {
        b.setLength(b.length() - 1); // remove "0"
      }
    }
  }
  return b.toString();
}
 
Example 2
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 3
Source File: RexLiteral.java    From calcite with Apache License 2.0 6 votes vote down vote up
private String intervalString(BigDecimal v) {
  final List<TimeUnit> timeUnits = getTimeUnits(type.getSqlTypeName());
  final StringBuilder b = new StringBuilder();
  for (TimeUnit timeUnit : timeUnits) {
    final BigDecimal[] result = v.divideAndRemainder(timeUnit.multiplier);
    if (b.length() > 0) {
      b.append(timeUnit.separator);
    }
    final int width = b.length() == 0 ? -1 : width(timeUnit); // don't pad 1st
    pad(b, result[0].toString(), width);
    v = result[1];
  }
  if (Util.last(timeUnits) == TimeUnit.MILLISECOND) {
    while (b.toString().matches(".*\\.[0-9]*0")) {
      if (b.toString().endsWith(".0")) {
        b.setLength(b.length() - 2); // remove ".0"
      } else {
        b.setLength(b.length() - 1); // remove "0"
      }
    }
  }
  return b.toString();
}
 
Example 4
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 5
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 6
Source File: RexImpTable.java    From Quicksql with MIT License 4 votes vote down vote up
public Expression implement(RexToLixTranslator translator, RexCall call,
    List<Expression> translatedOperands) {
  final RexNode operand0 = call.getOperands().get(0);
  Expression trop0 = translatedOperands.get(0);
  final SqlTypeName typeName1 =
      call.getOperands().get(1).getType().getSqlTypeName();
  Expression trop1 = translatedOperands.get(1);
  final SqlTypeName typeName = call.getType().getSqlTypeName();
  switch (operand0.getType().getSqlTypeName()) {
  case DATE:
    switch (typeName) {
    case TIMESTAMP:
      trop0 = Expressions.convert_(
          Expressions.multiply(trop0,
              Expressions.constant(DateTimeUtils.MILLIS_PER_DAY)),
          long.class);
      break;
    default:
      switch (typeName1) {
      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:
        trop1 = Expressions.convert_(
            Expressions.divide(trop1,
                Expressions.constant(DateTimeUtils.MILLIS_PER_DAY)),
            int.class);
      }
    }
    break;
  case TIME:
    trop1 = Expressions.convert_(trop1, int.class);
    break;
  }
  switch (typeName1) {
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
    switch (call.getKind()) {
    case MINUS:
      trop1 = Expressions.negate(trop1);
    }
    switch (typeName) {
    case TIME:
      return Expressions.convert_(trop0, long.class);
    default:
      final BuiltInMethod method =
          operand0.getType().getSqlTypeName() == SqlTypeName.TIMESTAMP
              ? BuiltInMethod.ADD_MONTHS
              : BuiltInMethod.ADD_MONTHS_INT;
      return Expressions.call(method.method, trop0, trop1);
    }

  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:
    switch (call.getKind()) {
    case MINUS:
      return normalize(typeName, Expressions.subtract(trop0, trop1));
    default:
      return normalize(typeName, Expressions.add(trop0, trop1));
    }

  default:
    switch (call.getKind()) {
    case MINUS:
      switch (typeName) {
      case INTERVAL_YEAR:
      case INTERVAL_YEAR_MONTH:
      case INTERVAL_MONTH:
        return Expressions.call(BuiltInMethod.SUBTRACT_MONTHS.method,
            trop0, trop1);
      }
      TimeUnit fromUnit =
          typeName1 == SqlTypeName.DATE ? TimeUnit.DAY : TimeUnit.MILLISECOND;
      TimeUnit toUnit = TimeUnit.MILLISECOND;
      return multiplyDivide(
          Expressions.convert_(Expressions.subtract(trop0, trop1),
              (Class) long.class),
          fromUnit.multiplier, toUnit.multiplier);
    default:
      throw new AssertionError(call);
    }
  }
}
 
Example 7
Source File: RexImpTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override Expression implementSafe(final RexToLixTranslator translator,
    final RexCall call, final List<Expression> argValueList) {
  final RexNode operand0 = call.getOperands().get(0);
  Expression trop0 = argValueList.get(0);
  final SqlTypeName typeName1 =
      call.getOperands().get(1).getType().getSqlTypeName();
  Expression trop1 = argValueList.get(1);
  final SqlTypeName typeName = call.getType().getSqlTypeName();
  switch (operand0.getType().getSqlTypeName()) {
  case DATE:
    switch (typeName) {
    case TIMESTAMP:
      trop0 = Expressions.convert_(
          Expressions.multiply(trop0,
              Expressions.constant(DateTimeUtils.MILLIS_PER_DAY)),
          long.class);
      break;
    default:
      switch (typeName1) {
      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:
        trop1 = Expressions.convert_(
            Expressions.divide(trop1,
                Expressions.constant(DateTimeUtils.MILLIS_PER_DAY)),
            int.class);
      }
    }
    break;
  case TIME:
    trop1 = Expressions.convert_(trop1, int.class);
    break;
  }
  switch (typeName1) {
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
    switch (call.getKind()) {
    case MINUS:
      trop1 = Expressions.negate(trop1);
    }
    switch (typeName) {
    case TIME:
      return Expressions.convert_(trop0, long.class);
    default:
      final BuiltInMethod method =
          operand0.getType().getSqlTypeName() == SqlTypeName.TIMESTAMP
              ? BuiltInMethod.ADD_MONTHS
              : BuiltInMethod.ADD_MONTHS_INT;
      return Expressions.call(method.method, trop0, trop1);
    }

  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:
    switch (call.getKind()) {
    case MINUS:
      return normalize(typeName, Expressions.subtract(trop0, trop1));
    default:
      return normalize(typeName, Expressions.add(trop0, trop1));
    }

  default:
    switch (call.getKind()) {
    case MINUS:
      switch (typeName) {
      case INTERVAL_YEAR:
      case INTERVAL_YEAR_MONTH:
      case INTERVAL_MONTH:
        return Expressions.call(BuiltInMethod.SUBTRACT_MONTHS.method,
            trop0, trop1);
      }
      TimeUnit fromUnit =
          typeName1 == SqlTypeName.DATE ? TimeUnit.DAY : TimeUnit.MILLISECOND;
      TimeUnit toUnit = TimeUnit.MILLISECOND;
      return multiplyDivide(
          Expressions.convert_(Expressions.subtract(trop0, trop1),
              (Class) long.class),
          fromUnit.multiplier, toUnit.multiplier);
    default:
      throw new AssertionError(call);
    }
  }
}