Java Code Examples for org.apache.calcite.avatica.util.DateTimeUtils#unixDateExtract()

The following examples show how to use org.apache.calcite.avatica.util.DateTimeUtils#unixDateExtract() . 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: RexInterpreter.java    From Quicksql with MIT License 6 votes vote down vote up
private Comparable extract(RexCall call, List<Comparable> values) {
  final Comparable v = values.get(1);
  if (v == N) {
    return N;
  }
  final TimeUnitRange timeUnitRange = (TimeUnitRange) values.get(0);
  final int v2;
  if (v instanceof Long) {
    // TIMESTAMP
    v2 = (int) (((Long) v) / TimeUnit.DAY.multiplier.longValue());
  } else {
    // DATE
    v2 = (Integer) v;
  }
  return DateTimeUtils.unixDateExtract(timeUnitRange, v2);
}
 
Example 2
Source File: SqlFunctionUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
public static int addMonths(int date, int m) {
	int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
	int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
	int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
	m0 += m;
	int deltaYear = (int) DateTimeUtils.floorDiv(m0, 12);
	y0 += deltaYear;
	m0 = (int) DateTimeUtils.floorMod(m0, 12);
	if (m0 == 0) {
		y0 -= 1;
		m0 += 12;
	}
	int last = lastDay(y0, m0);
	if (d0 > last) {
		d0 = last;
	}
	return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
}
 
Example 3
Source File: SqlFunctions.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Adds a given number of months to a date, represented as the number of
 * days since the epoch. */
public static int addMonths(int date, int m) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
  m0 += m;
  int deltaYear = (int) DateTimeUtils.floorDiv(m0, 12);
  y0 += deltaYear;
  m0 = (int) DateTimeUtils.floorMod(m0, 12);
  if (m0 == 0) {
    y0 -= 1;
    m0 += 12;
  }

  int last = lastDay(y0, m0);
  if (d0 > last) {
    d0 = last;
  }
  return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
}
 
Example 4
Source File: RexInterpreter.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Comparable extract(RexCall call, List<Comparable> values) {
  final Comparable v = values.get(1);
  if (v == N) {
    return N;
  }
  final TimeUnitRange timeUnitRange = (TimeUnitRange) values.get(0);
  final int v2;
  if (v instanceof Long) {
    // TIMESTAMP
    v2 = (int) (((Long) v) / TimeUnit.DAY.multiplier.longValue());
  } else {
    // DATE
    v2 = (Integer) v;
  }
  return DateTimeUtils.unixDateExtract(timeUnitRange, v2);
}
 
Example 5
Source File: SqlDateTimeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static long convertExtract(TimeUnitRange range, long ts, LogicalType type, TimeZone tz) {
	TimeUnit startUnit = range.startUnit;
	long offset = tz.getOffset(ts);
	long utcTs = ts + offset;

	switch (startUnit) {
		case MILLENNIUM:
		case CENTURY:
		case YEAR:
		case QUARTER:
		case MONTH:
		case DAY:
		case DOW:
		case DOY:
		case WEEK:
			if (type instanceof TimestampType) {
				long d = divide(utcTs, TimeUnit.DAY.multiplier);
				return DateTimeUtils.unixDateExtract(range, d);
			} else if (type instanceof DateType) {
				return divide(utcTs, TimeUnit.DAY.multiplier);
			} else {
				// TODO support it
				throw new TableException(type + " is unsupported now.");
			}
		case DECADE:
			// TODO support it
			throw new TableException("DECADE is unsupported now.");
		case EPOCH:
			// TODO support it
			throw new TableException("EPOCH is unsupported now.");
		default:
			// fall through
	}

	long res = mod(utcTs, getFactory(startUnit));
	res = divide(res, startUnit.multiplier);
	return res;

}
 
Example 6
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param date days since epoch
 * @return days of the last day of the month since epoch
 */
public static int lastDay(int date) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 7
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param timestamp milliseconds from epoch
 * @return milliseconds of the last day of the month since epoch
 */
public static int lastDay(long timestamp) {
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 8
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * Converts a date (days since epoch) to a {@link LocalDate}.
 *
 * @param date days since epoch
 * @return localDate
 */
private static LocalDate dateToLocalDate(int date) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
  return LocalDate.of(y0, m0, d0);
}
 
Example 9
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** Adds a given number of months to a date, represented as the number of
 * days since the epoch. */
public static int addMonths(int date, int m) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
  int y = m / 12;
  y0 += y;
  m0 += m - y * 12;
  int last = lastDay(y0, m0);
  if (d0 > last) {
    d0 = last;
  }
  return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
}
 
Example 10
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param date days since epoch
 * @return days of the last day of the month since epoch
 */
public static int lastDay(int date) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 11
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param timestamp milliseconds from epoch
 * @return milliseconds of the last day of the month since epoch
 */
public static int lastDay(long timestamp) {
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 12
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a date (days since epoch) to a {@link LocalDate}.
 *
 * @param date days since epoch
 * @return localDate
 */
private static LocalDate dateToLocalDate(int date) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
  return LocalDate.of(y0, m0, d0);
}