Search in sources :

Example 31 with ValueDate

use of org.h2.value.ValueDate 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());
    }
}
Also used : LobData(org.h2.value.lob.LobData) ValueUuid(org.h2.value.ValueUuid) LobDataDatabase(org.h2.value.lob.LobDataDatabase) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueBlob(org.h2.value.ValueBlob) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) DataUtils.readString(org.h2.mvstore.DataUtils.readString) ValueClob(org.h2.value.ValueClob) ValueInterval(org.h2.value.ValueInterval) ValueBigint(org.h2.value.ValueBigint) ValueTinyint(org.h2.value.ValueTinyint) ValueSmallint(org.h2.value.ValueSmallint) BigDecimal(java.math.BigDecimal) ValueDecfloat(org.h2.value.ValueDecfloat) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) BigInteger(java.math.BigInteger)

Example 32 with ValueDate

use of org.h2.value.ValueDate 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);
    }
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) BigDecimal(java.math.BigDecimal)

Example 33 with ValueDate

use of org.h2.value.ValueDate in project h2database by h2database.

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);
}
Also used : DateTimeUtils.dateTimeToValue(org.h2.util.DateTimeUtils.dateTimeToValue) DateTimeUtils.absoluteDayFromDateValue(org.h2.util.DateTimeUtils.absoluteDayFromDateValue) Value(org.h2.value.Value) DateTimeUtils.dateAndTimeFromValue(org.h2.util.DateTimeUtils.dateAndTimeFromValue) BigInteger(java.math.BigInteger) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueInterval(org.h2.value.ValueInterval) BigDecimal(java.math.BigDecimal)

Example 34 with ValueDate

use of org.h2.value.ValueDate in project h2database by h2database.

the class DateTimeFunction method extractEpoch.

private static ValueNumeric extractEpoch(SessionLocal session, Value value) {
    ValueNumeric result;
    if (value instanceof ValueInterval) {
        ValueInterval interval = (ValueInterval) value;
        if (interval.getQualifier().isYearMonth()) {
            interval = (ValueInterval) interval.convertTo(TypeInfo.TYPE_INTERVAL_YEAR_TO_MONTH);
            long leading = interval.getLeading();
            long remaining = interval.getRemaining();
            BigInteger bi = BigInteger.valueOf(leading).multiply(BigInteger.valueOf(31557600)).add(BigInteger.valueOf(remaining * 2592000));
            if (interval.isNegative()) {
                bi = bi.negate();
            }
            return ValueNumeric.get(bi);
        } else {
            return ValueNumeric.get(new BigDecimal(IntervalUtils.intervalToAbsolute(interval)).divide(BD_NANOS_PER_SECOND));
        }
    }
    long[] a = DateTimeUtils.dateAndTimeFromValue(value, session);
    long dateValue = a[0];
    long timeNanos = a[1];
    if (value instanceof ValueTime) {
        result = ValueNumeric.get(BigDecimal.valueOf(timeNanos).divide(BD_NANOS_PER_SECOND));
    } else if (value instanceof ValueDate) {
        result = ValueNumeric.get(// 
        BigInteger.valueOf(DateTimeUtils.absoluteDayFromDateValue(dateValue)).multiply(BI_SECONDS_PER_DAY));
    } else {
        BigDecimal bd = BigDecimal.valueOf(timeNanos).divide(BD_NANOS_PER_SECOND).add(// 
        BigDecimal.valueOf(DateTimeUtils.absoluteDayFromDateValue(dateValue)).multiply(BD_SECONDS_PER_DAY));
        if (value instanceof ValueTimestampTimeZone) {
            result = ValueNumeric.get(bd.subtract(BigDecimal.valueOf(((ValueTimestampTimeZone) value).getTimeZoneOffsetSeconds())));
        } else if (value instanceof ValueTimeTimeZone) {
            result = ValueNumeric.get(bd.subtract(BigDecimal.valueOf(((ValueTimeTimeZone) value).getTimeZoneOffsetSeconds())));
        } else {
            result = ValueNumeric.get(bd);
        }
    }
    return result;
}
Also used : ValueTime(org.h2.value.ValueTime) ValueNumeric(org.h2.value.ValueNumeric) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) BigInteger(java.math.BigInteger) ValueDate(org.h2.value.ValueDate) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueInterval(org.h2.value.ValueInterval) BigDecimal(java.math.BigDecimal)

Example 35 with ValueDate

use of org.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");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueBigint(org.h2.value.ValueBigint) ValueSmallint(org.h2.value.ValueSmallint) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) ValueDate(org.h2.value.ValueDate) ValueArray(org.h2.value.ValueArray)

Aggregations

ValueDate (org.h2.value.ValueDate)23 ValueTimestamp (org.h2.value.ValueTimestamp)23 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)23 ValueTime (org.h2.value.ValueTime)22 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)18 Value (org.h2.value.Value)15 BigDecimal (java.math.BigDecimal)13 BigInteger (java.math.BigInteger)11 ValueDate (org.gridgain.internal.h2.value.ValueDate)9 ValueInterval (org.h2.value.ValueInterval)9 ValueTime (org.gridgain.internal.h2.value.ValueTime)6 ValueTimestamp (org.gridgain.internal.h2.value.ValueTimestamp)5 ValueBigint (org.h2.value.ValueBigint)5 ValueTimestampTimeZone (org.gridgain.internal.h2.value.ValueTimestampTimeZone)4 ValueSmallint (org.h2.value.ValueSmallint)4 ValueUuid (org.h2.value.ValueUuid)4 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Value (org.gridgain.internal.h2.value.Value)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3