use of org.h2.value.ValueInterval in project h2database by h2database.
the class DateTimeFunction method extractInterval.
private static int extractInterval(Value date, int field) {
ValueInterval interval = (ValueInterval) date;
IntervalQualifier qualifier = interval.getQualifier();
boolean negative = interval.isNegative();
long leading = interval.getLeading(), remaining = interval.getRemaining();
long v;
switch(field) {
case YEAR:
v = IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining);
break;
case MONTH:
v = IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining);
break;
case DAY:
case DAY_OF_YEAR:
v = IntervalUtils.daysFromInterval(qualifier, negative, leading, remaining);
break;
case HOUR:
v = IntervalUtils.hoursFromInterval(qualifier, negative, leading, remaining);
break;
case MINUTE:
v = IntervalUtils.minutesFromInterval(qualifier, negative, leading, remaining);
break;
case SECOND:
v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / NANOS_PER_SECOND;
break;
case MILLISECOND:
v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000_000 % 1_000;
break;
case MICROSECOND:
v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000 % 1_000_000;
break;
case NANOSECOND:
v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) % NANOS_PER_SECOND;
break;
default:
throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
}
return (int) v;
}
use of org.h2.value.ValueInterval in project h2database by h2database.
the class IntervalOperation method getDateTimeWithInterval.
private Value getDateTimeWithInterval(SessionLocal session, Value l, Value r, int lType, int rType) {
switch(lType) {
case Value.TIME:
if (DataType.isYearMonthIntervalType(rType)) {
throw DbException.getInternalError("type=" + rType);
}
return ValueTime.fromNanos(getTimeWithInterval(r, ((ValueTime) l).getNanos()));
case Value.TIME_TZ:
{
if (DataType.isYearMonthIntervalType(rType)) {
throw DbException.getInternalError("type=" + rType);
}
ValueTimeTimeZone t = (ValueTimeTimeZone) l;
return ValueTimeTimeZone.fromNanos(getTimeWithInterval(r, t.getNanos()), t.getTimeZoneOffsetSeconds());
}
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
if (DataType.isYearMonthIntervalType(rType)) {
long m = IntervalUtils.intervalToAbsolute((ValueInterval) r).longValue();
if (opType == IntervalOpType.DATETIME_MINUS_INTERVAL) {
m = -m;
}
return DateTimeFunction.dateadd(session, DateTimeFunction.MONTH, m, l);
} else {
BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
if (lType == Value.DATE) {
BigInteger a1 = BigInteger.valueOf(absoluteDayFromDateValue(((ValueDate) l).getDateValue()));
a2 = a2.divide(NANOS_PER_DAY_BI);
BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2);
return ValueDate.fromDateValue(dateValueFromAbsoluteDay(n.longValue()));
} else {
long[] a = dateAndTimeFromValue(l, session);
long absoluteDay = absoluteDayFromDateValue(a[0]);
long timeNanos = a[1];
BigInteger[] dr = a2.divideAndRemainder(NANOS_PER_DAY_BI);
if (opType == IntervalOpType.DATETIME_PLUS_INTERVAL) {
absoluteDay += dr[0].longValue();
timeNanos += dr[1].longValue();
} else {
absoluteDay -= dr[0].longValue();
timeNanos -= dr[1].longValue();
}
if (timeNanos >= NANOS_PER_DAY) {
timeNanos -= NANOS_PER_DAY;
absoluteDay++;
} else if (timeNanos < 0) {
timeNanos += NANOS_PER_DAY;
absoluteDay--;
}
return dateTimeToValue(l, dateValueFromAbsoluteDay(absoluteDay), timeNanos);
}
}
}
throw DbException.getInternalError("type=" + opType);
}
use of org.h2.value.ValueInterval in project h2database by h2database.
the class TimeZoneOperation method parseInterval.
/**
* Parses a daytime interval as time zone offset.
*
* @param interval the interval
* @return the time zone offset in seconds
*/
public static int parseInterval(Value interval) {
ValueInterval i = (ValueInterval) interval.convertTo(TypeInfo.TYPE_INTERVAL_HOUR_TO_SECOND);
long h = i.getLeading(), seconds = i.getRemaining();
if (h > 18 || h == 18 && seconds != 0 || seconds % DateTimeUtils.NANOS_PER_SECOND != 0) {
throw DbException.getInvalidValueException("time zone", i.getTraceSQL());
}
int newOffset = (int) (h * 3_600 + seconds / DateTimeUtils.NANOS_PER_SECOND);
if (i.isNegative()) {
newOffset = -newOffset;
}
return newOffset;
}
use of org.h2.value.ValueInterval in project apollo by salesforce.
the class StreamTransfer method writeValue.
/**
* Write a value.
*
* @param v the value
* @throws IOException on failure
*/
public void writeValue(Value v, DataOutputStream out) throws IOException {
int type = v.getValueType();
switch(type) {
case Value.NULL:
writeInt(NULL, out);
break;
case Value.BINARY:
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeInt(BINARY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
}
// $FALL-THROUGH$
case Value.VARBINARY:
writeInt(VARBINARY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.JAVA_OBJECT:
writeInt(JAVA_OBJECT, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.UUID:
{
writeInt(UUID, out);
ValueUuid uuid = (ValueUuid) v;
writeLong(uuid.getHigh(), out);
writeLong(uuid.getLow(), out);
break;
}
case Value.BOOLEAN:
writeInt(BOOLEAN, out);
writeBoolean(v.getBoolean(), out);
break;
case Value.TINYINT:
writeInt(TINYINT, out);
writeByte(v.getByte(), out);
break;
case Value.TIME:
writeInt(TIME, out);
writeLong(((ValueTime) v).getNanos(), out);
break;
case Value.TIME_TZ:
{
ValueTimeTimeZone t = (ValueTimeTimeZone) v;
if (version >= Constants.TCP_PROTOCOL_VERSION_19) {
writeInt(TIME_TZ, out);
writeLong(t.getNanos(), out);
writeInt(t.getTimeZoneOffsetSeconds(), out);
} else {
writeInt(TIME, out);
/*
* Don't call SessionRemote.currentTimestamp(), it may require own remote call
* and old server will not return custom time zone anyway.
*/
ValueTimestampTimeZone current = DateTimeUtils.currentTimestamp(DateTimeUtils.getTimeZone());
writeLong(DateTimeUtils.normalizeNanosOfDay(t.getNanos() + (t.getTimeZoneOffsetSeconds() - current.getTimeZoneOffsetSeconds()) * DateTimeUtils.NANOS_PER_DAY), out);
}
break;
}
case Value.DATE:
writeInt(DATE, out);
writeLong(((ValueDate) v).getDateValue(), out);
break;
case Value.TIMESTAMP:
{
writeInt(TIMESTAMP, out);
ValueTimestamp ts = (ValueTimestamp) v;
writeLong(ts.getDateValue(), out);
writeLong(ts.getTimeNanos(), out);
break;
}
case Value.TIMESTAMP_TZ:
{
writeInt(TIMESTAMP_TZ, out);
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
writeLong(ts.getDateValue(), out);
writeLong(ts.getTimeNanos(), out);
int timeZoneOffset = ts.getTimeZoneOffsetSeconds();
writeInt(//
version >= Constants.TCP_PROTOCOL_VERSION_19 ? timeZoneOffset : timeZoneOffset / 60, out);
break;
}
case Value.DECFLOAT:
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeInt(DECFLOAT, out);
writeString(v.getString(), out);
break;
}
// $FALL-THROUGH$
case Value.NUMERIC:
writeInt(NUMERIC, out);
writeString(v.getString(), out);
break;
case Value.DOUBLE:
writeInt(DOUBLE, out);
writeDouble(v.getDouble(), out);
break;
case Value.REAL:
writeInt(REAL, out);
writeFloat(v.getFloat(), out);
break;
case Value.INTEGER:
writeInt(INTEGER, out);
writeInt(v.getInt(), out);
break;
case Value.BIGINT:
writeInt(BIGINT, out);
writeLong(v.getLong(), out);
break;
case Value.SMALLINT:
writeInt(SMALLINT, out);
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeShort(v.getShort(), out);
} else {
writeInt(v.getShort(), out);
}
break;
case Value.VARCHAR:
writeInt(VARCHAR, out);
writeString(v.getString(), out);
break;
case Value.VARCHAR_IGNORECASE:
writeInt(VARCHAR_IGNORECASE, out);
writeString(v.getString(), out);
break;
case Value.CHAR:
writeInt(CHAR, out);
writeString(v.getString(), out);
break;
case Value.BLOB:
{
writeInt(BLOB, out);
ValueBlob lob = (ValueBlob) v;
LobData lobData = lob.getLobData();
long length = lob.octetLength();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
writeLong(-1, out);
writeInt(lobDataDatabase.getTableId(), out);
writeLong(lobDataDatabase.getLobId(), out);
writeBytes(calculateLobMac(lobDataDatabase.getLobId()), out);
writeLong(length, out);
break;
}
if (length < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length=" + length);
}
writeLong(length, out);
long written = IOUtils.copyAndCloseInput(lob.getInputStream(), out);
if (written != length) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length:" + length + " written:" + written);
}
writeInt(LOB_MAGIC, out);
break;
}
case Value.CLOB:
{
writeInt(CLOB, out);
ValueClob lob = (ValueClob) v;
LobData lobData = lob.getLobData();
long charLength = lob.charLength();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
writeLong(-1, out);
writeInt(lobDataDatabase.getTableId(), out);
writeLong(lobDataDatabase.getLobId(), out);
writeBytes(calculateLobMac(lobDataDatabase.getLobId()), out);
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeLong(lob.octetLength(), out);
}
writeLong(charLength, out);
break;
}
if (charLength < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length=" + charLength);
}
writeLong(charLength, out);
Reader reader = lob.getReader();
Data.copyString(reader, out);
writeInt(LOB_MAGIC, out);
break;
}
case Value.ARRAY:
{
writeInt(ARRAY, out);
ValueArray va = (ValueArray) v;
Value[] list = va.getList();
int len = list.length;
writeInt(len, out);
for (Value value : list) {
writeValue(value, out);
}
break;
}
case Value.ROW:
{
writeInt(version >= Constants.TCP_PROTOCOL_VERSION_18 ? ROW : ARRAY, out);
ValueRow va = (ValueRow) v;
Value[] list = va.getList();
int len = list.length;
writeInt(len, out);
for (Value value : list) {
writeValue(value, out);
}
break;
}
case Value.ENUM:
{
writeInt(ENUM, out);
writeInt(v.getInt(), out);
if (version < Constants.TCP_PROTOCOL_VERSION_20) {
writeString(v.getString(), out);
}
break;
}
case Value.GEOMETRY:
writeInt(GEOMETRY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL, out);
writeByte((byte) ordinal, out);
writeLong(interval.getLeading(), out);
} else {
writeInt(VARCHAR, out);
writeString(v.getString(), out);
}
break;
case Value.INTERVAL_SECOND:
case Value.INTERVAL_YEAR_TO_MONTH:
case Value.INTERVAL_DAY_TO_HOUR:
case Value.INTERVAL_DAY_TO_MINUTE:
case Value.INTERVAL_DAY_TO_SECOND:
case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL, out);
writeByte((byte) ordinal, out);
writeLong(interval.getLeading(), out);
writeLong(interval.getRemaining(), out);
} else {
writeInt(VARCHAR, out);
writeString(v.getString(), out);
}
break;
case Value.JSON:
{
writeInt(JSON, out);
writeBytes(v.getBytesNoCopy(), out);
break;
}
default:
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "type=" + type);
}
}
use of org.h2.value.ValueInterval in project SpringStudy by myounghaklee.
the class IntervalOperation method getValue.
@Override
public Value getValue(SessionLocal session) {
Value l = left.getValue(session);
Value r = right.getValue(session);
if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
return ValueNull.INSTANCE;
}
int lType = l.getValueType(), rType = r.getValueType();
switch(opType) {
case INTERVAL_PLUS_INTERVAL:
case INTERVAL_MINUS_INTERVAL:
{
BigInteger a1 = IntervalUtils.intervalToAbsolute((ValueInterval) l);
BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(Value.getHigherOrder(lType, rType) - Value.INTERVAL_YEAR), opType == IntervalOpType.INTERVAL_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2));
}
case INTERVAL_DIVIDE_INTERVAL:
return ValueNumeric.get(IntervalUtils.intervalToAbsolute((ValueInterval) l)).divide(ValueNumeric.get(IntervalUtils.intervalToAbsolute((ValueInterval) r)), type);
case DATETIME_PLUS_INTERVAL:
case DATETIME_MINUS_INTERVAL:
return getDateTimeWithInterval(session, l, r, lType, rType);
case INTERVAL_MULTIPLY_NUMERIC:
case INTERVAL_DIVIDE_NUMERIC:
{
BigDecimal a1 = new BigDecimal(IntervalUtils.intervalToAbsolute((ValueInterval) l));
BigDecimal a2 = r.getBigDecimal();
return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(lType - Value.INTERVAL_YEAR), (opType == IntervalOpType.INTERVAL_MULTIPLY_NUMERIC ? a1.multiply(a2) : a1.divide(a2)).toBigInteger());
}
case DATETIME_MINUS_DATETIME:
{
Value result;
if ((lType == Value.TIME || lType == Value.TIME_TZ) && (rType == Value.TIME || rType == Value.TIME_TZ)) {
long diff;
if (lType == Value.TIME && rType == Value.TIME) {
diff = ((ValueTime) l).getNanos() - ((ValueTime) r).getNanos();
} else {
ValueTimeTimeZone left = (ValueTimeTimeZone) l.convertTo(TypeInfo.TYPE_TIME_TZ, session), right = (ValueTimeTimeZone) r.convertTo(TypeInfo.TYPE_TIME_TZ, session);
diff = left.getNanos() - right.getNanos() + (right.getTimeZoneOffsetSeconds() - left.getTimeZoneOffsetSeconds()) * DateTimeUtils.NANOS_PER_SECOND;
}
boolean negative = diff < 0;
if (negative) {
diff = -diff;
}
result = ValueInterval.from(IntervalQualifier.HOUR_TO_SECOND, negative, diff / NANOS_PER_HOUR, diff % NANOS_PER_HOUR);
} else if (forcedType != null && DataType.isYearMonthIntervalType(forcedType.getValueType())) {
long[] dt1 = dateAndTimeFromValue(l, session), dt2 = dateAndTimeFromValue(r, session);
long dateValue1 = lType == Value.TIME || lType == Value.TIME_TZ ? session.currentTimestamp().getDateValue() : dt1[0];
long dateValue2 = rType == Value.TIME || rType == Value.TIME_TZ ? session.currentTimestamp().getDateValue() : dt2[0];
long leading = 12L * (DateTimeUtils.yearFromDateValue(dateValue1) - DateTimeUtils.yearFromDateValue(dateValue2)) + DateTimeUtils.monthFromDateValue(dateValue1) - DateTimeUtils.monthFromDateValue(dateValue2);
int d1 = DateTimeUtils.dayFromDateValue(dateValue1);
int d2 = DateTimeUtils.dayFromDateValue(dateValue2);
if (leading >= 0) {
if (d1 < d2 || d1 == d2 && dt1[1] < dt2[1]) {
leading--;
}
} else if (d1 > d2 || d1 == d2 && dt1[1] > dt2[1]) {
leading++;
}
boolean negative;
if (leading < 0) {
negative = true;
leading = -leading;
} else {
negative = false;
}
result = ValueInterval.from(IntervalQualifier.MONTH, negative, leading, 0L);
} else if (lType == Value.DATE && rType == Value.DATE) {
long diff = absoluteDayFromDateValue(((ValueDate) l).getDateValue()) - absoluteDayFromDateValue(((ValueDate) r).getDateValue());
boolean negative = diff < 0;
if (negative) {
diff = -diff;
}
result = ValueInterval.from(IntervalQualifier.DAY, negative, diff, 0L);
} else {
BigInteger diff = nanosFromValue(session, l).subtract(nanosFromValue(session, r));
if (lType == Value.TIMESTAMP_TZ || rType == Value.TIMESTAMP_TZ) {
l = l.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, session);
r = r.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, session);
diff = diff.add(BigInteger.valueOf((((ValueTimestampTimeZone) r).getTimeZoneOffsetSeconds() - ((ValueTimestampTimeZone) l).getTimeZoneOffsetSeconds()) * NANOS_PER_SECOND));
}
result = IntervalUtils.intervalFromAbsolute(IntervalQualifier.DAY_TO_SECOND, diff);
}
if (forcedType != null) {
result = result.castTo(forcedType, session);
}
return result;
}
}
throw DbException.getInternalError("type=" + opType);
}
Aggregations