Search in sources :

Example 41 with ValueDate

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

the class DateTimeUtils method dateAndTimeFromValue.

/**
 * Extracts date value and nanos of day from the specified value.
 *
 * @param value
 *            value to extract fields from
 * @return array with date value and nanos of day
 */
public static long[] dateAndTimeFromValue(Value value) {
    long dateValue = EPOCH_DATE_VALUE;
    long timeNanos = 0;
    if (value instanceof ValueTimestamp) {
        ValueTimestamp v = (ValueTimestamp) value;
        dateValue = v.getDateValue();
        timeNanos = v.getTimeNanos();
    } else if (value instanceof ValueDate) {
        dateValue = ((ValueDate) value).getDateValue();
    } else if (value instanceof ValueTime) {
        timeNanos = ((ValueTime) value).getNanos();
    } else if (value instanceof ValueTimestampTimeZone) {
        ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
        dateValue = v.getDateValue();
        timeNanos = v.getTimeNanos();
    } else {
        ValueTimestamp v = (ValueTimestamp) value.convertTo(Value.TIMESTAMP);
        dateValue = v.getDateValue();
        timeNanos = v.getTimeNanos();
    }
    return new long[] { dateValue, timeNanos };
}
Also used : ValueTime(org.gridgain.internal.h2.value.ValueTime) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueDate(org.gridgain.internal.h2.value.ValueDate)

Example 42 with ValueDate

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

the class TestTimeStampWithTimeZone method testConversionsImpl.

private void testConversionsImpl(String timeStr, boolean testReverse) {
    ValueTimestamp ts = ValueTimestamp.parse(timeStr);
    ValueDate d = (ValueDate) ts.convertTo(Value.DATE);
    ValueTime t = (ValueTime) ts.convertTo(Value.TIME);
    ValueTimestampTimeZone tstz = ValueTimestampTimeZone.parse(timeStr);
    assertEquals(ts, tstz.convertTo(Value.TIMESTAMP));
    assertEquals(d, tstz.convertTo(Value.DATE));
    assertEquals(t, tstz.convertTo(Value.TIME));
    assertEquals(ts.getTimestamp(), tstz.getTimestamp());
    if (testReverse) {
        assertEquals(0, tstz.compareTo(ts.convertTo(Value.TIMESTAMP_TZ), null, null));
        assertEquals(d.convertTo(Value.TIMESTAMP).convertTo(Value.TIMESTAMP_TZ), d.convertTo(Value.TIMESTAMP_TZ));
        assertEquals(t.convertTo(Value.TIMESTAMP).convertTo(Value.TIMESTAMP_TZ), t.convertTo(Value.TIMESTAMP_TZ));
    }
}
Also used : ValueTime(org.gridgain.internal.h2.value.ValueTime) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueDate(org.gridgain.internal.h2.value.ValueDate)

Example 43 with ValueDate

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

the class IntervalOperation method getDateTimeWithInterval.

private Value getDateTimeWithInterval(Value l, Value r, int lType, int rType) {
    switch(lType) {
        case Value.TIME:
            {
                if (DataType.isYearMonthIntervalType(rType)) {
                    throw DbException.throwInternalError("type=" + rType);
                }
                BigInteger a1 = BigInteger.valueOf(((ValueTime) l).getNanos());
                BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
                BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2);
                if (n.signum() < 0 || n.compareTo(NANOS_PER_DAY_BI) >= 0) {
                    throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, n.toString());
                }
                return ValueTime.fromNanos(n.longValue());
            }
        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 DateTimeFunctions.dateadd("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);
                    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, false);
                }
            }
    }
    throw DbException.throwInternalError("type=" + opType);
}
Also used : ValueTime(org.gridgain.internal.h2.value.ValueTime) BigInteger(java.math.BigInteger) ValueDate(org.gridgain.internal.h2.value.ValueDate) ValueInterval(org.gridgain.internal.h2.value.ValueInterval)

Example 44 with ValueDate

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

the class Function method truncate.

private static Value truncate(Session session, Value v0, Value v1) {
    Value result;
    int t = v0.getValueType();
    switch(t) {
        case Value.TIMESTAMP:
            result = ValueTimestamp.fromDateValueAndNanos(((ValueTimestamp) v0).getDateValue(), 0);
            break;
        case Value.DATE:
            result = ValueTimestamp.fromDateValueAndNanos(((ValueDate) v0).getDateValue(), 0);
            break;
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v0;
                result = ValueTimestampTimeZone.fromDateValueAndNanos(ts.getDateValue(), 0, ts.getTimeZoneOffsetMins());
                break;
            }
        case Value.STRING:
            result = ValueTimestamp.fromDateValueAndNanos(ValueTimestamp.parse(v0.getString(), session.getDatabase().getMode()).getDateValue(), 0);
            break;
        default:
            int scale = v1 == null ? 0 : v1.getInt();
            if (t == Value.DOUBLE || t == Value.FLOAT) {
                double d = v0.getDouble();
                if (scale == 0) {
                    d = d < 0 ? Math.ceil(d) : Math.floor(d);
                } else {
                    double f = Math.pow(10, scale);
                    d *= f;
                    d = (d < 0 ? Math.ceil(d) : Math.floor(d)) / f;
                }
                result = t == Value.DOUBLE ? ValueDouble.get(d) : ValueFloat.get((float) d);
            } else {
                result = ValueDecimal.get(v0.getBigDecimal().setScale(scale, RoundingMode.DOWN));
            }
            break;
    }
    return result;
}
Also used : ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) Value(org.gridgain.internal.h2.value.Value) SequenceValue(org.gridgain.internal.h2.expression.SequenceValue) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueDate(org.gridgain.internal.h2.value.ValueDate)

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