Search in sources :

Example 21 with ValueDate

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

the class DateTimeFunctions method extract.

/**
 * Extracts specified field from the specified date-time value.
 *
 * @param part
 *            the date part
 * @param value
 *            the date-time value
 * @return extracted field
 */
public static Value extract(String part, Value value) {
    Value result;
    int field = getDatePart(part);
    if (field != EPOCH) {
        result = ValueInt.get(getIntDatePart(value, field));
    } else {
        // Case where we retrieve the EPOCH time.
        // First we retrieve the dateValue and his time in nanoseconds.
        long[] a = DateTimeUtils.dateAndTimeFromValue(value);
        long dateValue = a[0];
        long timeNanos = a[1];
        // We compute the time in nanoseconds and the total number of days.
        BigDecimal timeNanosBigDecimal = new BigDecimal(timeNanos);
        BigDecimal numberOfDays = new BigDecimal(DateTimeUtils.absoluteDayFromDateValue(dateValue));
        BigDecimal nanosSeconds = new BigDecimal(1_000_000_000);
        BigDecimal secondsPerDay = new BigDecimal(DateTimeUtils.SECONDS_PER_DAY);
        // Case where the value is of type time e.g. '10:00:00'
        if (value instanceof ValueTime) {
            // In order to retrieve the EPOCH time we only have to convert the time
            // in nanoseconds (previously retrieved) in seconds.
            result = ValueDecimal.get(timeNanosBigDecimal.divide(nanosSeconds));
        } else if (value instanceof ValueDate) {
            // Case where the value is of type date '2000:01:01', we have to retrieve the
            // total number of days and multiply it by the number of seconds in a day.
            result = ValueDecimal.get(numberOfDays.multiply(secondsPerDay));
        } else if (value instanceof ValueTimestampTimeZone) {
            // Case where the value is a of type ValueTimestampTimeZone
            // ('2000:01:01 10:00:00+05').
            // We retrieve the time zone offset in minutes
            ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
            BigDecimal timeZoneOffsetSeconds = new BigDecimal(v.getTimeZoneOffsetMins() * 60);
            // Sum the time in nanoseconds and the total number of days in seconds
            // and adding the timeZone offset in seconds.
            result = ValueDecimal.get(timeNanosBigDecimal.divide(nanosSeconds).add(numberOfDays.multiply(secondsPerDay)).subtract(timeZoneOffsetSeconds));
        } else {
            // By default, we have the date and the time ('2000:01:01 10:00:00') if no type
            // is given.
            // We just have to sum the time in nanoseconds and the total number of days in
            // seconds.
            result = ValueDecimal.get(timeNanosBigDecimal.divide(nanosSeconds).add(numberOfDays.multiply(secondsPerDay)));
        }
    }
    return result;
}
Also used : ValueTime(org.h2.value.ValueTime) Value(org.h2.value.Value) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate) BigDecimal(java.math.BigDecimal)

Example 22 with ValueDate

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

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.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate)

Example 23 with ValueDate

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

the class TestTimeStampWithTimeZone method testConversionsImpl.

private void testConversionsImpl(String timeStr, boolean testReverse, CastDataProvider provider) {
    ValueTimestamp ts = ValueTimestamp.parse(timeStr, null);
    ValueDate d = ts.convertToDate(provider);
    ValueTime t = (ValueTime) ts.convertTo(TypeInfo.TYPE_TIME, provider);
    ValueTimestampTimeZone tstz = ValueTimestampTimeZone.parse(timeStr, null);
    assertEquals(ts, tstz.convertTo(TypeInfo.TYPE_TIMESTAMP, provider));
    assertEquals(d, tstz.convertToDate(provider));
    assertEquals(t, tstz.convertTo(TypeInfo.TYPE_TIME, provider));
    assertEquals(LegacyDateTimeUtils.toTimestamp(provider, null, ts), LegacyDateTimeUtils.toTimestamp(provider, null, tstz));
    if (testReverse) {
        assertEquals(0, tstz.compareTo(ts.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), null, null));
        assertEquals(d.convertTo(TypeInfo.TYPE_TIMESTAMP, provider).convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), d.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider));
        assertEquals(t.convertTo(TypeInfo.TYPE_TIMESTAMP, provider).convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), t.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider));
    }
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate)

Example 24 with ValueDate

use of org.gridgain.internal.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 25 with ValueDate

use of org.gridgain.internal.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)

Aggregations

ValueDate (org.h2.value.ValueDate)23 ValueTime (org.h2.value.ValueTime)18 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)16 ValueTimestamp (org.h2.value.ValueTimestamp)15 BigDecimal (java.math.BigDecimal)10 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)10 BigInteger (java.math.BigInteger)9 ValueDate (org.gridgain.internal.h2.value.ValueDate)9 ValueTime (org.gridgain.internal.h2.value.ValueTime)9 Value (org.gridgain.internal.h2.value.Value)8 ValueTimestamp (org.gridgain.internal.h2.value.ValueTimestamp)8 ValueTimestampTimeZone (org.gridgain.internal.h2.value.ValueTimestampTimeZone)8 ValueInterval (org.gridgain.internal.h2.value.ValueInterval)6 TypeInfo (org.gridgain.internal.h2.value.TypeInfo)4 ValueInterval (org.h2.value.ValueInterval)4 ResultInterface (org.gridgain.internal.h2.result.ResultInterface)3 ValueLobDb (org.gridgain.internal.h2.value.ValueLobDb)3 ValueString (org.gridgain.internal.h2.value.ValueString)3 Value (org.h2.value.Value)3 Date (java.sql.Date)2