use of org.h2.value.ValueInterval in project h2database by h2database.
the class TestDateTimeUtils method testParseIntervalImpl.
private void testParseIntervalImpl(IntervalQualifier qualifier, boolean negative, long leading, long remaining, String s, String full) {
ValueInterval expected = ValueInterval.from(qualifier, negative, leading, remaining);
assertEquals(expected, IntervalUtils.parseInterval(qualifier, negative, s));
StringBuilder b = new StringBuilder();
b.append("INTERVAL ").append('\'');
if (negative) {
b.append('-');
}
b.append(full).append("' ").append(qualifier);
assertEquals(b.toString(), expected.getString());
}
use of org.h2.value.ValueInterval in project h2database by h2database.
the class JSR310Utils method valueToPeriod.
/**
* Converts a value to a Period.
*
* This method should only be called from Java 8 or later version.
*
* @param value
* the value to convert
* @return the Period
*/
public static Period valueToPeriod(Value value) {
if (!(value instanceof ValueInterval)) {
value = value.convertTo(TypeInfo.TYPE_INTERVAL_YEAR_TO_MONTH);
}
if (!DataType.isYearMonthIntervalType(value.getValueType())) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, (Throwable) null, value.getString());
}
ValueInterval v = (ValueInterval) value;
IntervalQualifier qualifier = v.getQualifier();
boolean negative = v.isNegative();
long leading = v.getLeading();
long remaining = v.getRemaining();
int y = Value.convertToInt(IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining), null);
int m = Value.convertToInt(IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining), null);
return Period.of(y, m, 0);
}
use of org.h2.value.ValueInterval in project h2database by h2database.
the class JSR310Utils method periodToValue.
/**
* Converts a Period to a Value.
*
* @param period
* the Period to convert, not {@code null}
* @return the value
*/
public static ValueInterval periodToValue(Period period) {
int days = period.getDays();
if (days != 0) {
throw DbException.getInvalidValueException("Period.days", days);
}
int years = period.getYears();
int months = period.getMonths();
IntervalQualifier qualifier;
boolean negative = false;
long leading = 0L, remaining = 0L;
if (years == 0) {
if (months == 0L) {
// Use generic qualifier
qualifier = IntervalQualifier.YEAR_TO_MONTH;
} else {
qualifier = IntervalQualifier.MONTH;
leading = months;
if (leading < 0) {
leading = -leading;
negative = true;
}
}
} else {
if (months == 0L) {
qualifier = IntervalQualifier.YEAR;
leading = years;
if (leading < 0) {
leading = -leading;
negative = true;
}
} else {
qualifier = IntervalQualifier.YEAR_TO_MONTH;
leading = years * 12 + months;
if (leading < 0) {
leading = -leading;
negative = true;
}
remaining = leading % 12;
leading /= 12;
}
}
return ValueInterval.from(qualifier, negative, leading, remaining);
}
use of org.h2.value.ValueInterval in project h2database by h2database.
the class ValueDataType method write.
@Override
public void write(WriteBuffer buff, Value v) {
if (v == ValueNull.INSTANCE) {
buff.put((byte) 0);
return;
}
int type = v.getValueType();
switch(type) {
case Value.BOOLEAN:
buff.put(v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
break;
case Value.TINYINT:
buff.put(TINYINT).put(v.getByte());
break;
case Value.SMALLINT:
buff.put(SMALLINT).putShort(v.getShort());
break;
case Value.ENUM:
case Value.INTEGER:
{
int x = v.getInt();
if (x < 0) {
buff.put(INT_NEG).putVarInt(-x);
} else if (x < 16) {
buff.put((byte) (INT_0_15 + x));
} else {
buff.put(type == Value.INTEGER ? INTEGER : ENUM).putVarInt(x);
}
break;
}
case Value.BIGINT:
writeLong(buff, v.getLong());
break;
case Value.NUMERIC:
{
BigDecimal x = v.getBigDecimal();
if (BigDecimal.ZERO.equals(x)) {
buff.put(NUMERIC_0_1);
} else if (BigDecimal.ONE.equals(x)) {
buff.put((byte) (NUMERIC_0_1 + 1));
} else {
int scale = x.scale();
BigInteger b = x.unscaledValue();
int bits = b.bitLength();
if (bits <= 63) {
if (scale == 0) {
buff.put(NUMERIC_SMALL_0).putVarLong(b.longValue());
} else {
buff.put(NUMERIC_SMALL).putVarInt(scale).putVarLong(b.longValue());
}
} else {
byte[] bytes = b.toByteArray();
buff.put(NUMERIC).putVarInt(scale).putVarInt(bytes.length).put(bytes);
}
}
break;
}
case Value.DECFLOAT:
{
ValueDecfloat d = (ValueDecfloat) v;
buff.put((byte) DECFLOAT);
if (d.isFinite()) {
BigDecimal x = d.getBigDecimal();
byte[] bytes = x.unscaledValue().toByteArray();
buff.putVarInt(x.scale()).putVarInt(bytes.length).put(bytes);
} else {
int c;
if (d == ValueDecfloat.NEGATIVE_INFINITY) {
c = -3;
} else if (d == ValueDecfloat.POSITIVE_INFINITY) {
c = -2;
} else {
c = -1;
}
buff.putVarInt(0).putVarInt(c);
}
break;
}
case Value.TIME:
writeTimestampTime(buff.put(TIME), ((ValueTime) v).getNanos());
break;
case Value.TIME_TZ:
{
ValueTimeTimeZone t = (ValueTimeTimeZone) v;
long nanosOfDay = t.getNanos();
buff.put((byte) TIME_TZ).putVarInt((int) (nanosOfDay / DateTimeUtils.NANOS_PER_SECOND)).putVarInt((int) (nanosOfDay % DateTimeUtils.NANOS_PER_SECOND));
writeTimeZone(buff, t.getTimeZoneOffsetSeconds());
break;
}
case Value.DATE:
buff.put(DATE).putVarLong(((ValueDate) v).getDateValue());
break;
case Value.TIMESTAMP:
{
ValueTimestamp ts = (ValueTimestamp) v;
buff.put(TIMESTAMP).putVarLong(ts.getDateValue());
writeTimestampTime(buff, ts.getTimeNanos());
break;
}
case Value.TIMESTAMP_TZ:
{
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
buff.put((byte) TIMESTAMP_TZ).putVarLong(ts.getDateValue());
writeTimestampTime(buff, ts.getTimeNanos());
writeTimeZone(buff, ts.getTimeZoneOffsetSeconds());
break;
}
case Value.JAVA_OBJECT:
writeBinary(JAVA_OBJECT, buff, v);
break;
case Value.VARBINARY:
{
byte[] b = v.getBytesNoCopy();
int len = b.length;
if (len < 32) {
buff.put((byte) (VARBINARY_0_31 + len)).put(b);
} else {
buff.put(VARBINARY).putVarInt(len).put(b);
}
break;
}
case Value.BINARY:
writeBinary((byte) BINARY, buff, v);
break;
case Value.UUID:
{
ValueUuid uuid = (ValueUuid) v;
buff.put(UUID).putLong(uuid.getHigh()).putLong(uuid.getLow());
break;
}
case Value.VARCHAR:
{
String s = v.getString();
int len = s.length();
if (len < 32) {
buff.put((byte) (VARCHAR_0_31 + len)).putStringData(s, len);
} else {
writeString(buff.put(VARCHAR), s);
}
break;
}
case Value.VARCHAR_IGNORECASE:
writeString(buff.put(VARCHAR_IGNORECASE), v.getString());
break;
case Value.CHAR:
writeString(buff.put(CHAR), v.getString());
break;
case Value.DOUBLE:
{
double x = v.getDouble();
if (x == 1.0d) {
buff.put((byte) (DOUBLE_0_1 + 1));
} else {
long d = Double.doubleToLongBits(x);
if (d == ValueDouble.ZERO_BITS) {
buff.put(DOUBLE_0_1);
} else {
buff.put(DOUBLE).putVarLong(Long.reverse(d));
}
}
break;
}
case Value.REAL:
{
float x = v.getFloat();
if (x == 1.0f) {
buff.put((byte) (REAL_0_1 + 1));
} else {
int f = Float.floatToIntBits(x);
if (f == ValueReal.ZERO_BITS) {
buff.put(REAL_0_1);
} else {
buff.put(REAL).putVarInt(Integer.reverse(f));
}
}
break;
}
case Value.BLOB:
{
buff.put(BLOB);
ValueBlob lob = (ValueBlob) v;
LobData lobData = lob.getLobData();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength());
} else {
byte[] small = ((LobDataInMemory) lobData).getSmall();
buff.putVarInt(small.length).put(small);
}
break;
}
case Value.CLOB:
{
buff.put(CLOB);
ValueClob lob = (ValueClob) v;
LobData lobData = lob.getLobData();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength()).putVarLong(lob.charLength());
} else {
byte[] small = ((LobDataInMemory) lobData).getSmall();
buff.putVarInt(small.length).put(small).putVarLong(lob.charLength());
}
break;
}
case Value.ARRAY:
case Value.ROW:
{
Value[] list = ((ValueCollectionBase) v).getList();
buff.put(type == Value.ARRAY ? ARRAY : ROW).putVarInt(list.length);
for (Value x : list) {
write(buff, x);
}
break;
}
case Value.GEOMETRY:
writeBinary(GEOMETRY, buff, v);
break;
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
{
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading());
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:
{
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading()).putVarLong(interval.getRemaining());
break;
}
case Value.JSON:
writeBinary((byte) JSON, buff, v);
break;
default:
throw DbException.getInternalError("type=" + v.getValueType());
}
}
use of org.h2.value.ValueInterval in project h2database by h2database.
the class Percentile method interpolate.
private static Value interpolate(Value v0, Value v1, BigDecimal factor, int dataType, SessionLocal session, CompareMode compareMode) {
if (v0.compareTo(v1, session, compareMode) == 0) {
return v0;
}
switch(dataType) {
case Value.TINYINT:
case Value.SMALLINT:
case Value.INTEGER:
return ValueNumeric.get(interpolateDecimal(BigDecimal.valueOf(v0.getInt()), BigDecimal.valueOf(v1.getInt()), factor));
case Value.BIGINT:
return ValueNumeric.get(interpolateDecimal(BigDecimal.valueOf(v0.getLong()), BigDecimal.valueOf(v1.getLong()), factor));
case Value.NUMERIC:
case Value.DECFLOAT:
return ValueNumeric.get(interpolateDecimal(v0.getBigDecimal(), v1.getBigDecimal(), factor));
case Value.REAL:
case Value.DOUBLE:
return ValueNumeric.get(interpolateDecimal(BigDecimal.valueOf(v0.getDouble()), BigDecimal.valueOf(v1.getDouble()), factor));
case Value.TIME:
{
ValueTime t0 = (ValueTime) v0, t1 = (ValueTime) v1;
BigDecimal n0 = BigDecimal.valueOf(t0.getNanos());
BigDecimal n1 = BigDecimal.valueOf(t1.getNanos());
return ValueTime.fromNanos(interpolateDecimal(n0, n1, factor).longValue());
}
case Value.TIME_TZ:
{
ValueTimeTimeZone t0 = (ValueTimeTimeZone) v0, t1 = (ValueTimeTimeZone) v1;
BigDecimal n0 = BigDecimal.valueOf(t0.getNanos());
BigDecimal n1 = BigDecimal.valueOf(t1.getNanos());
BigDecimal offset = BigDecimal.valueOf(t0.getTimeZoneOffsetSeconds()).multiply(BigDecimal.ONE.subtract(factor)).add(BigDecimal.valueOf(t1.getTimeZoneOffsetSeconds()).multiply(factor));
int intOffset = offset.intValue();
BigDecimal intOffsetBD = BigDecimal.valueOf(intOffset);
BigDecimal bd = interpolateDecimal(n0, n1, factor);
if (offset.compareTo(intOffsetBD) != 0) {
bd = bd.add(offset.subtract(intOffsetBD).multiply(BigDecimal.valueOf(DateTimeUtils.NANOS_PER_SECOND)));
}
long timeNanos = bd.longValue();
if (timeNanos < 0L) {
timeNanos += DateTimeUtils.NANOS_PER_SECOND;
intOffset++;
} else if (timeNanos >= DateTimeUtils.NANOS_PER_DAY) {
timeNanos -= DateTimeUtils.NANOS_PER_SECOND;
intOffset--;
}
return ValueTimeTimeZone.fromNanos(timeNanos, intOffset);
}
case Value.DATE:
{
ValueDate d0 = (ValueDate) v0, d1 = (ValueDate) v1;
BigDecimal a0 = BigDecimal.valueOf(DateTimeUtils.absoluteDayFromDateValue(d0.getDateValue()));
BigDecimal a1 = BigDecimal.valueOf(DateTimeUtils.absoluteDayFromDateValue(d1.getDateValue()));
return ValueDate.fromDateValue(DateTimeUtils.dateValueFromAbsoluteDay(interpolateDecimal(a0, a1, factor).longValue()));
}
case Value.TIMESTAMP:
{
ValueTimestamp ts0 = (ValueTimestamp) v0, ts1 = (ValueTimestamp) v1;
BigDecimal a0 = timestampToDecimal(ts0.getDateValue(), ts0.getTimeNanos());
BigDecimal a1 = timestampToDecimal(ts1.getDateValue(), ts1.getTimeNanos());
BigInteger[] dr = interpolateDecimal(a0, a1, factor).toBigInteger().divideAndRemainder(IntervalUtils.NANOS_PER_DAY_BI);
long absoluteDay = dr[0].longValue();
long timeNanos = dr[1].longValue();
if (timeNanos < 0) {
timeNanos += DateTimeUtils.NANOS_PER_DAY;
absoluteDay--;
}
return ValueTimestamp.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay), timeNanos);
}
case Value.TIMESTAMP_TZ:
{
ValueTimestampTimeZone ts0 = (ValueTimestampTimeZone) v0, ts1 = (ValueTimestampTimeZone) v1;
BigDecimal a0 = timestampToDecimal(ts0.getDateValue(), ts0.getTimeNanos());
BigDecimal a1 = timestampToDecimal(ts1.getDateValue(), ts1.getTimeNanos());
BigDecimal offset = BigDecimal.valueOf(ts0.getTimeZoneOffsetSeconds()).multiply(BigDecimal.ONE.subtract(factor)).add(BigDecimal.valueOf(ts1.getTimeZoneOffsetSeconds()).multiply(factor));
int intOffset = offset.intValue();
BigDecimal intOffsetBD = BigDecimal.valueOf(intOffset);
BigDecimal bd = interpolateDecimal(a0, a1, factor);
if (offset.compareTo(intOffsetBD) != 0) {
bd = bd.add(offset.subtract(intOffsetBD).multiply(BigDecimal.valueOf(DateTimeUtils.NANOS_PER_SECOND)));
}
BigInteger[] dr = bd.toBigInteger().divideAndRemainder(IntervalUtils.NANOS_PER_DAY_BI);
long absoluteDay = dr[0].longValue();
long timeNanos = dr[1].longValue();
if (timeNanos < 0) {
timeNanos += DateTimeUtils.NANOS_PER_DAY;
absoluteDay--;
}
return ValueTimestampTimeZone.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay), timeNanos, intOffset);
}
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
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:
return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(dataType - Value.INTERVAL_YEAR), interpolateDecimal(new BigDecimal(IntervalUtils.intervalToAbsolute((ValueInterval) v0)), new BigDecimal(IntervalUtils.intervalToAbsolute((ValueInterval) v1)), factor).toBigInteger());
default:
// Use the same rules as PERCENTILE_DISC
return (factor.compareTo(HALF) > 0 ? v1 : v0);
}
}
Aggregations