use of org.gridgain.internal.h2.value.ValueDate in project SpringStudy by myounghaklee.
the class PgServerThread method writeDataColumn.
private void writeDataColumn(Value v, int pgType, boolean text) throws IOException {
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;
case PgServer.PG_TYPE_BYTEA:
{
byte[] bytes = v.getBytesNoCopy();
int length = bytes.length;
int cnt = length;
for (int i = 0; i < length; i++) {
byte b = bytes[i];
if (b < 32 || b > 126) {
cnt += 3;
} else if (b == 92) {
cnt++;
}
}
byte[] data = new byte[cnt];
for (int i = 0, j = 0; i < length; i++) {
byte b = bytes[i];
if (b < 32 || b > 126) {
data[j++] = '\\';
data[j++] = (byte) (((b >>> 6) & 3) + '0');
data[j++] = (byte) (((b >>> 3) & 7) + '0');
data[j++] = (byte) ((b & 7) + '0');
} else if (b == 92) {
data[j++] = '\\';
data[j++] = '\\';
} else {
data[j++] = b;
}
}
writeInt(data.length);
write(data);
break;
}
case PgServer.PG_TYPE_INT2_ARRAY:
case PgServer.PG_TYPE_INT4_ARRAY:
case PgServer.PG_TYPE_VARCHAR_ARRAY:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write('{');
Value[] values = ((ValueArray) v).getList();
Charset encoding = getEncoding();
for (int i = 0; i < values.length; i++) {
if (i > 0) {
baos.write(',');
}
String s = values[i].getString();
if (SHOULD_QUOTE.matcher(s).matches()) {
List<String> ss = new ArrayList<>();
for (String s0 : s.split("\\\\")) {
ss.add(s0.replace("\"", "\\\""));
}
s = "\"" + String.join("\\\\", ss) + "\"";
}
baos.write(s.getBytes(encoding));
}
baos.write('}');
writeInt(baos.size());
write(baos);
break;
default:
byte[] data = v.getString().getBytes(getEncoding());
writeInt(data.length);
write(data);
}
} else {
// binary
switch(pgType) {
case PgServer.PG_TYPE_BOOL:
writeInt(1);
dataOut.writeByte(v.getBoolean() ? 1 : 0);
break;
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_NUMERIC:
writeNumericBinary(v.getBigDecimal());
break;
case PgServer.PG_TYPE_BYTEA:
{
byte[] data = v.getBytesNoCopy();
writeInt(data.length);
write(data);
break;
}
case PgServer.PG_TYPE_DATE:
writeInt(4);
writeInt((int) (toPostgreDays(((ValueDate) v).getDateValue())));
break;
case PgServer.PG_TYPE_TIME:
writeTimeBinary(((ValueTime) v).getNanos(), 8);
break;
case PgServer.PG_TYPE_TIMETZ:
{
ValueTimeTimeZone t = (ValueTimeTimeZone) v;
long m = t.getNanos();
writeTimeBinary(m, 12);
dataOut.writeInt(-t.getTimeZoneOffsetSeconds());
break;
}
case PgServer.PG_TYPE_TIMESTAMP:
{
ValueTimestamp t = (ValueTimestamp) v;
long m = toPostgreDays(t.getDateValue()) * 86_400;
long nanos = t.getTimeNanos();
writeTimestampBinary(m, nanos);
break;
}
case PgServer.PG_TYPE_TIMESTAMPTZ:
{
ValueTimestampTimeZone t = (ValueTimestampTimeZone) v;
long m = toPostgreDays(t.getDateValue()) * 86_400;
long nanos = t.getTimeNanos() - t.getTimeZoneOffsetSeconds() * 1_000_000_000L;
if (nanos < 0L) {
m--;
nanos += DateTimeUtils.NANOS_PER_DAY;
}
writeTimestampBinary(m, nanos);
break;
}
default:
throw new IllegalStateException("output binary format is undefined");
}
}
}
use of org.gridgain.internal.h2.value.ValueDate in project SpringStudy by myounghaklee.
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 SpringStudy by myounghaklee.
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);
}
}
use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.
the class TestDate method testDateTimeUtils.
private void testDateTimeUtils() {
ValueTimestamp ts1 = ValueTimestamp.parse("-999-08-07 13:14:15.16");
ValueTimestamp ts2 = ValueTimestamp.parse("19999-08-07 13:14:15.16");
ValueTime t1 = (ValueTime) ts1.convertTo(Value.TIME);
ValueTime t2 = (ValueTime) ts2.convertTo(Value.TIME);
ValueDate d1 = (ValueDate) ts1.convertTo(Value.DATE);
ValueDate d2 = (ValueDate) ts2.convertTo(Value.DATE);
assertEquals("-999-08-07 13:14:15.16", ts1.getString());
assertEquals("-999-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());
ValueTimestamp ts1a = DateTimeUtils.convertTimestamp(ts1.getTimestamp(), DateTimeUtils.createGregorianCalendar());
ValueTimestamp ts2a = DateTimeUtils.convertTimestamp(ts2.getTimestamp(), DateTimeUtils.createGregorianCalendar());
assertEquals("-999-08-07 13:14:15.16", ts1a.getString());
assertEquals("19999-08-07 13:14:15.16", ts2a.getString());
// test for bug on Java 1.8.0_60 in "Europe/Moscow" timezone.
// Doesn't affect most other timezones
long millis = 1407437460000L;
long ms = DateTimeUtils.getTimeUTCWithoutDst(millis);
ms += DateTimeUtils.getTimeZoneOffset(ms);
long result1 = DateTimeUtils.nanosFromLocalMillis(ms);
long result2 = DateTimeUtils.nanosFromLocalMillis(ms);
assertEquals(result1, result2);
}
use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.
the class DateTimeUtils method convertDate.
/**
* Convert the date to the specified time zone.
*
* @param value the date (might be ValueNull)
* @param calendar the calendar
* @return the date using the correct time zone
*/
public static Date convertDate(Value value, Calendar calendar) {
if (value == ValueNull.INSTANCE) {
return null;
}
ValueDate d = (ValueDate) value.convertTo(Value.DATE);
Calendar cal = (Calendar) calendar.clone();
cal.clear();
cal.setLenient(true);
long dateValue = d.getDateValue();
long ms = convertToMillis(cal, yearFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue), 0, 0, 0, 0);
return new Date(ms);
}
Aggregations