Java Code Examples for org.apache.calcite.avatica.util.DateTimeUtils#MILLIS_PER_DAY

The following examples show how to use org.apache.calcite.avatica.util.DateTimeUtils#MILLIS_PER_DAY . 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: ElasticsearchEnumerators.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 2
Source File: ElasticsearchEnumerators.java    From Quicksql with MIT License 6 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 3
Source File: MongoEnumerator.java    From Quicksql with MIT License 6 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 4
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** SQL {@code CURRENT_DATE} function. */
@NonDeterministic
public static int currentDate(DataContext root) {
  final long timestamp = currentTimestamp(root);
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  final int time = (int) (timestamp % DateTimeUtils.MILLIS_PER_DAY);
  if (time < 0) {
    --date;
  }
  return date;
}
 
Example 5
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** SQL {@code CURRENT_DATE} function. */
@NonDeterministic
public static int currentDate(DataContext root) {
  final long timestamp = currentTimestamp(root);
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  final int time = (int) (timestamp % DateTimeUtils.MILLIS_PER_DAY);
  if (time < 0) {
    --date;
  }
  return date;
}
 
Example 6
Source File: GeodeUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz == null) {
    return o.toString();
  }
  if (Map.class.isAssignableFrom(clazz)
          && o instanceof PdxInstance) {
    // This is in case of nested Objects!
    return Util.toString(
            ((PdxInstance) o).getFieldNames(), "PDX[", ",", "]");
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 7
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** SQL {@code CURRENT_TIME} function. */
@NonDeterministic
public static int currentTime(DataContext root) {
  int time = (int) (currentTimestamp(root) % DateTimeUtils.MILLIS_PER_DAY);
  if (time < 0) {
    time += DateTimeUtils.MILLIS_PER_DAY;
  }
  return time;
}
 
Example 8
Source File: SqlFunctionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/** TODO: remove addMonths and subtractMonths if CALCITE-3881 fixed.
 *   https://issues.apache.org/jira/browse/CALCITE-3881
 **/
public static long addMonths(long timestamp, int m) {
	final long millis =
		DateTimeUtils.floorMod(timestamp, DateTimeUtils.MILLIS_PER_DAY);
	timestamp -= millis;
	final long x =
		addMonths((int) (timestamp / DateTimeUtils.MILLIS_PER_DAY), m);
	return x * DateTimeUtils.MILLIS_PER_DAY + millis;
}
 
Example 9
Source File: JdbcUtils.java    From Quicksql with MIT License 5 votes vote down vote up
private static Time shift(Time v) {
  if (v == null) {
    return null;
  }
  long time = v.getTime();
  int offset = TimeZone.getDefault().getOffset(time);
  return new Time((time + offset) % DateTimeUtils.MILLIS_PER_DAY);
}
 
Example 10
Source File: SqlFunctionsTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Converts a date (days since epoch) and milliseconds (since midnight)
 * into a timestamp (milliseconds since epoch). */
private long d2ts(int date, int millis) {
  return date * DateTimeUtils.MILLIS_PER_DAY + millis;
}
 
Example 11
Source File: SqlFunctions.java    From Quicksql with MIT License 4 votes vote down vote up
/** Converts the internal representation of a SQL DATE (int) to the Java
 * type used for UDF parameters ({@link java.sql.Date}). */
public static java.sql.Date internalToDate(int v) {
  final long t = v * DateTimeUtils.MILLIS_PER_DAY;
  return new java.sql.Date(t - LOCAL_TZ.getOffset(t));
}
 
Example 12
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** SQL {@code LOCAL_TIME} function. */
@NonDeterministic
public static int localTime(DataContext root) {
  return (int) (localTimestamp(root) % DateTimeUtils.MILLIS_PER_DAY);
}
 
Example 13
Source File: SqlFunctions.java    From Quicksql with MIT License 4 votes vote down vote up
/** SQL {@code LOCAL_TIME} function. */
@NonDeterministic
public static int localTime(DataContext root) {
  return (int) (localTimestamp(root) % DateTimeUtils.MILLIS_PER_DAY);
}
 
Example 14
Source File: DateString.java    From Quicksql with MIT License 4 votes vote down vote up
/** Returns the number of milliseconds since the epoch. Always a multiple of
 * 86,400,000 (the number of milliseconds in a day). */
public long getMillisSinceEpoch() {
  return getDaysSinceEpoch() * DateTimeUtils.MILLIS_PER_DAY;
}
 
Example 15
Source File: SqlFunctionsTest.java    From Quicksql with MIT License 4 votes vote down vote up
/** Converts a date (days since epoch) and milliseconds (since midnight)
 * into a timestamp (milliseconds since epoch). */
private long d2ts(int date, int millis) {
  return date * DateTimeUtils.MILLIS_PER_DAY + millis;
}
 
Example 16
Source File: QuicksqlServerResultSet.java    From Quicksql with MIT License 4 votes vote down vote up
private static Object getValue(ResultSet resultSet, int type, int j,
    Calendar calendar) throws SQLException {
  switch (type) {
  case Types.BIGINT:
    final long aLong = resultSet.getLong(j + 1);
    return aLong == 0 && resultSet.wasNull() ? null : aLong;
  case Types.INTEGER:
    final int anInt = resultSet.getInt(j + 1);
    return anInt == 0 && resultSet.wasNull() ? null : anInt;
  case Types.SMALLINT:
    final short aShort = resultSet.getShort(j + 1);
    return aShort == 0 && resultSet.wasNull() ? null : aShort;
  case Types.TINYINT:
    final byte aByte = resultSet.getByte(j + 1);
    return aByte == 0 && resultSet.wasNull() ? null : aByte;
  case Types.DOUBLE:
  case Types.FLOAT:
    final double aDouble = resultSet.getDouble(j + 1);
    return aDouble == 0D && resultSet.wasNull() ? null : aDouble;
  case Types.REAL:
    final float aFloat = resultSet.getFloat(j + 1);
    return aFloat == 0D && resultSet.wasNull() ? null : aFloat;
  case Types.DATE:
    final Date aDate = resultSet.getDate(j + 1, calendar);
    return aDate == null
        ? null
        : (int) (aDate.getTime() / DateTimeUtils.MILLIS_PER_DAY);
  case Types.TIME:
    final Time aTime = resultSet.getTime(j + 1, calendar);
    return aTime == null
        ? null
        : (int) (aTime.getTime() % DateTimeUtils.MILLIS_PER_DAY);
  case Types.TIMESTAMP:
    final Timestamp aTimestamp = resultSet.getTimestamp(j + 1, calendar);
    return aTimestamp == null ? null : aTimestamp.getTime();
  case Types.ARRAY:
    final Array array = resultSet.getArray(j + 1);
    if (null == array) {
      return null;
    }
    try {
      // Recursively extracts an Array using its ResultSet-representation
      return extractUsingResultSet(array, calendar);
    } catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
      // Not every database might implement Array.getResultSet(). This call
      // assumes a non-nested array (depends on the db if that's a valid assumption)
      return extractUsingArray(array, calendar);
    }
  case Types.STRUCT:
    Struct struct = resultSet.getObject(j + 1, Struct.class);
    Object[] attrs = struct.getAttributes();
    List<Object> list = new ArrayList<>(attrs.length);
    for (Object o : attrs) {
      list.add(o);
    }
    return list;
  default:
    return resultSet.getObject(j + 1);
  }
}
 
Example 17
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static int toInt(java.util.Date v, TimeZone timeZone) {
  return (int) (toLong(v, timeZone)  / DateTimeUtils.MILLIS_PER_DAY);
}
 
Example 18
Source File: JdbcResultSet.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private static Object getValue(ResultSet resultSet, int type, int j,
    Calendar calendar) throws SQLException {
  switch (type) {
  case Types.BIGINT:
    final long aLong = resultSet.getLong(j + 1);
    return aLong == 0 && resultSet.wasNull() ? null : aLong;
  case Types.INTEGER:
    final int anInt = resultSet.getInt(j + 1);
    return anInt == 0 && resultSet.wasNull() ? null : anInt;
  case Types.SMALLINT:
    final short aShort = resultSet.getShort(j + 1);
    return aShort == 0 && resultSet.wasNull() ? null : aShort;
  case Types.TINYINT:
    final byte aByte = resultSet.getByte(j + 1);
    return aByte == 0 && resultSet.wasNull() ? null : aByte;
  case Types.DOUBLE:
  case Types.FLOAT:
    final double aDouble = resultSet.getDouble(j + 1);
    return aDouble == 0D && resultSet.wasNull() ? null : aDouble;
  case Types.REAL:
    final float aFloat = resultSet.getFloat(j + 1);
    return aFloat == 0D && resultSet.wasNull() ? null : aFloat;
  case Types.DATE:
    final Date aDate = resultSet.getDate(j + 1, calendar);
    return aDate == null
        ? null
        : (int) (aDate.getTime() / DateTimeUtils.MILLIS_PER_DAY);
  case Types.TIME:
    final Time aTime = resultSet.getTime(j + 1, calendar);
    return aTime == null
        ? null
        : (int) (aTime.getTime() % DateTimeUtils.MILLIS_PER_DAY);
  case Types.TIMESTAMP:
    final Timestamp aTimestamp = resultSet.getTimestamp(j + 1, calendar);
    return aTimestamp == null ? null : aTimestamp.getTime();
  case Types.ARRAY:
    final Array array = resultSet.getArray(j + 1);
    if (null == array) {
      return null;
    }
    try {
      // Recursively extracts an Array using its ResultSet-representation
      return extractUsingResultSet(array, calendar);
    } catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
      // Not every database might implement Array.getResultSet(). This call
      // assumes a non-nested array (depends on the db if that's a valid assumption)
      return extractUsingArray(array, calendar);
    }
  case Types.STRUCT:
    Struct struct = resultSet.getObject(j + 1, Struct.class);
    Object[] attrs = struct.getAttributes();
    List<Object> list = new ArrayList<>(attrs.length);
    for (Object o : attrs) {
      list.add(o);
    }
    return list;
  default:
    return resultSet.getObject(j + 1);
  }
}
 
Example 19
Source File: SqlFunctions.java    From Quicksql with MIT License 2 votes vote down vote up
/**
 * Converts a timestamp (milliseconds since epoch) to a {@link LocalDate}.
 *
 * @param timestamp milliseconds from epoch
 * @return localDate
 */
private static LocalDate timeStampToLocalDate(long timestamp) {
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  return dateToLocalDate(date);
}
 
Example 20
Source File: SqlFunctions.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Converts a timestamp (milliseconds since epoch) to a {@link LocalDate}.
 *
 * @param timestamp milliseconds from epoch
 * @return localDate
 */
private static LocalDate timeStampToLocalDate(long timestamp) {
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  return dateToLocalDate(date);
}