use of org.gridgain.internal.h2.value.ValueDate 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.gridgain.internal.h2.value.ValueDate in project h2database by h2database.
the class TestDate method testValueDate.
private void testValueDate() {
assertEquals("2000-01-01", LegacyDateTimeUtils.fromDate(null, null, Date.valueOf("2000-01-01")).getString());
assertEquals("0000-00-00", ValueDate.fromDateValue(0).getString());
assertEquals("9999-12-31", ValueDate.parse("9999-12-31").getString());
assertEquals("-9999-12-31", ValueDate.parse("-9999-12-31").getString());
ValueDate d1 = ValueDate.parse("2001-01-01");
assertEquals("2001-01-01", LegacyDateTimeUtils.toDate(null, null, d1).toString());
assertEquals("DATE '2001-01-01'", d1.getTraceSQL());
assertEquals("DATE '2001-01-01'", d1.toString());
assertEquals(Value.DATE, d1.getValueType());
long dv = d1.getDateValue();
assertEquals((int) ((dv >>> 32) ^ dv), d1.hashCode());
TypeInfo type = d1.getType();
assertEquals(d1.getString().length(), type.getDisplaySize());
assertEquals(ValueDate.PRECISION, type.getPrecision());
ValueDate d1b = ValueDate.parse("2001-01-01");
assertTrue(d1 == d1b);
Value.clearCache();
d1b = ValueDate.parse("2001-01-01");
assertFalse(d1 == d1b);
assertTrue(d1.equals(d1));
assertTrue(d1.equals(d1b));
assertTrue(d1b.equals(d1));
assertEquals(0, d1.compareTo(d1b, null, null));
assertEquals(0, d1b.compareTo(d1, null, null));
ValueDate d2 = ValueDate.parse("2002-02-02");
assertFalse(d1.equals(d2));
assertFalse(d2.equals(d1));
assertEquals(-1, d1.compareTo(d2, null, null));
assertEquals(1, d2.compareTo(d1, null, null));
}
use of org.gridgain.internal.h2.value.ValueDate in project h2database by h2database.
the class TestDate method testDateTimeUtils.
private void testDateTimeUtils() {
TimeZone old = TimeZone.getDefault();
/*
* java.util.TimeZone doesn't support LMT, so perform this test with
* fixed time zone offset
*/
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
DateTimeUtils.resetCalendar();
try {
ValueTimestamp ts1 = ValueTimestamp.parse("-999-08-07 13:14:15.16", null);
ValueTimestamp ts2 = ValueTimestamp.parse("19999-08-07 13:14:15.16", null);
ValueTime t1 = (ValueTime) ts1.convertTo(TypeInfo.TYPE_TIME);
ValueTime t2 = (ValueTime) ts2.convertTo(TypeInfo.TYPE_TIME);
ValueDate d1 = ts1.convertToDate(null);
ValueDate d2 = ts2.convertToDate(null);
assertEquals("-0999-08-07 13:14:15.16", ts1.getString());
assertEquals("-0999-08-07", d1.getString());
assertEquals("13:14:15.16", t1.getString());
assertEquals("19999-08-07 13:14:15.16", ts2.getString());
assertEquals("19999-08-07", d2.getString());
assertEquals("13:14:15.16", t2.getString());
TimeZone timeZone = TimeZone.getDefault();
ValueTimestamp ts1a = LegacyDateTimeUtils.fromTimestamp(null, timeZone, LegacyDateTimeUtils.toTimestamp(null, null, ts1));
ValueTimestamp ts2a = LegacyDateTimeUtils.fromTimestamp(null, timeZone, LegacyDateTimeUtils.toTimestamp(null, null, ts2));
assertEquals("-0999-08-07 13:14:15.16", ts1a.getString());
assertEquals("19999-08-07 13:14:15.16", ts2a.getString());
} finally {
TimeZone.setDefault(old);
DateTimeUtils.resetCalendar();
}
}
use of org.gridgain.internal.h2.value.ValueDate in project ignite by apache.
the class InlineIndexHelper method compareAsDateTime.
/**
* @param pageAddr Page address.
* @param off Offset.
* @param v Value to compare.
* @param type Highest value type.
* @return Compare result ({@code -2} means we can't compare).
*/
private int compareAsDateTime(long pageAddr, int off, Value v, int type) {
// only compatible types are supported now.
if (PageUtils.getByte(pageAddr, off) == type) {
switch(type) {
case Value.TIME:
long nanos1 = PageUtils.getLong(pageAddr, off + 1);
long nanos2 = ((ValueTime) v.convertTo(type)).getNanos();
return fixSort(Long.signum(nanos1 - nanos2), sortType());
case Value.DATE:
long date1 = PageUtils.getLong(pageAddr, off + 1);
long date2 = ((ValueDate) v.convertTo(type)).getDateValue();
return fixSort(Long.signum(date1 - date2), sortType());
case Value.TIMESTAMP:
ValueTimestamp v0 = (ValueTimestamp) v.convertTo(type);
date1 = PageUtils.getLong(pageAddr, off + 1);
date2 = v0.getDateValue();
int c = Long.signum(date1 - date2);
if (c == 0) {
nanos1 = PageUtils.getLong(pageAddr, off + 9);
nanos2 = v0.getTimeNanos();
c = Long.signum(nanos1 - nanos2);
}
return fixSort(c, sortType());
}
}
return Integer.MIN_VALUE;
}
use of org.gridgain.internal.h2.value.ValueDate in project h2database by h2database.
the class PgServerThread method writeDataColumn.
private void writeDataColumn(ResultSet rs, int column, int pgType, boolean text) throws Exception {
Value v = ((JdbcResultSet) rs).get(column);
if (v == ValueNull.INSTANCE) {
writeInt(-1);
return;
}
if (text) {
// plain text
switch(pgType) {
case PgServer.PG_TYPE_BOOL:
writeInt(1);
dataOut.writeByte(v.getBoolean() ? 't' : 'f');
break;
default:
byte[] data = v.getString().getBytes(getEncoding());
writeInt(data.length);
write(data);
}
} else {
// binary
switch(pgType) {
case PgServer.PG_TYPE_INT2:
writeInt(2);
writeShort(v.getShort());
break;
case PgServer.PG_TYPE_INT4:
writeInt(4);
writeInt(v.getInt());
break;
case PgServer.PG_TYPE_INT8:
writeInt(8);
dataOut.writeLong(v.getLong());
break;
case PgServer.PG_TYPE_FLOAT4:
writeInt(4);
dataOut.writeFloat(v.getFloat());
break;
case PgServer.PG_TYPE_FLOAT8:
writeInt(8);
dataOut.writeDouble(v.getDouble());
break;
case PgServer.PG_TYPE_BYTEA:
{
byte[] data = v.getBytesNoCopy();
writeInt(data.length);
write(data);
break;
}
case PgServer.PG_TYPE_DATE:
{
ValueDate d = (ValueDate) v.convertTo(Value.DATE);
writeInt(4);
writeInt((int) (toPostgreDays(d.getDateValue())));
break;
}
case PgServer.PG_TYPE_TIME:
{
ValueTime t = (ValueTime) v.convertTo(Value.TIME);
writeInt(8);
long m = t.getNanos();
if (INTEGER_DATE_TYPES) {
// long format
m /= 1_000;
} else {
// double format
m = Double.doubleToLongBits(m * 0.000_000_001);
}
dataOut.writeLong(m);
break;
}
case PgServer.PG_TYPE_TIMESTAMP_NO_TMZONE:
{
ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP);
writeInt(8);
long m = toPostgreDays(t.getDateValue()) * 86_400;
long nanos = t.getTimeNanos();
if (INTEGER_DATE_TYPES) {
// long format
m = m * 1_000_000 + nanos / 1_000;
} else {
// double format
m = Double.doubleToLongBits(m + nanos * 0.000_000_001);
}
dataOut.writeLong(m);
break;
}
default:
throw new IllegalStateException("output binary format is undefined");
}
}
}
Aggregations