Search in sources :

Example 11 with ValueInterval

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

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
 * @param mode
 *            the database mode
 * @return extracted field
 */
public static Value extract(String part, Value value, Mode mode) {
    Value result;
    int field = getDatePart(part);
    if (field != EPOCH) {
        result = ValueInt.get(getIntDatePart(value, field, mode));
    } else {
        // Case where we retrieve the EPOCH time.
        if (value instanceof ValueInterval) {
            ValueInterval interval = (ValueInterval) value;
            BigDecimal bd;
            if (interval.getQualifier().isYearMonth()) {
                interval = (ValueInterval) interval.convertTo(Value.INTERVAL_YEAR_TO_MONTH);
                long leading = interval.getLeading();
                long remaining = interval.getRemaining();
                bd = BigDecimal.valueOf(leading).multiply(BigDecimal.valueOf(31557600)).add(BigDecimal.valueOf(remaining * 2592000));
                if (interval.isNegative()) {
                    bd = bd.negate();
                }
            } else {
                bd = new BigDecimal(IntervalUtils.intervalToAbsolute(interval)).divide(BigDecimal.valueOf(NANOS_PER_SECOND));
            }
            return ValueDecimal.get(bd);
        }
        // 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(NANOS_PER_SECOND);
        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.gridgain.internal.h2.value.ValueTime) Value(org.gridgain.internal.h2.value.Value) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueDate(org.gridgain.internal.h2.value.ValueDate) ValueInterval(org.gridgain.internal.h2.value.ValueInterval) BigDecimal(java.math.BigDecimal)

Example 12 with ValueInterval

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

the class TimeZoneOperation method parseInterval.

/**
 * Parses a daytime interval as time zone offset.
 *
 * @param interval the interval
 * @return the time zone offset in seconds
 */
public static int parseInterval(Value interval) {
    ValueInterval i = (ValueInterval) interval.convertTo(TypeInfo.TYPE_INTERVAL_HOUR_TO_SECOND);
    long h = i.getLeading(), seconds = i.getRemaining();
    if (h > 18 || h == 18 && seconds != 0 || seconds % DateTimeUtils.NANOS_PER_SECOND != 0) {
        throw DbException.getInvalidValueException("time zone", i.getTraceSQL());
    }
    int newOffset = (int) (h * 3_600 + seconds / DateTimeUtils.NANOS_PER_SECOND);
    if (i.isNegative()) {
        newOffset = -newOffset;
    }
    return newOffset;
}
Also used : ValueInterval(org.h2.value.ValueInterval)

Example 13 with ValueInterval

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

Example 14 with ValueInterval

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

the class DateTimeFunction method extractInterval.

private static int extractInterval(Value date, int field) {
    ValueInterval interval = (ValueInterval) date;
    IntervalQualifier qualifier = interval.getQualifier();
    boolean negative = interval.isNegative();
    long leading = interval.getLeading(), remaining = interval.getRemaining();
    long v;
    switch(field) {
        case YEAR:
            v = IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining);
            break;
        case MONTH:
            v = IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining);
            break;
        case DAY:
        case DAY_OF_YEAR:
            v = IntervalUtils.daysFromInterval(qualifier, negative, leading, remaining);
            break;
        case HOUR:
            v = IntervalUtils.hoursFromInterval(qualifier, negative, leading, remaining);
            break;
        case MINUTE:
            v = IntervalUtils.minutesFromInterval(qualifier, negative, leading, remaining);
            break;
        case SECOND:
            v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / NANOS_PER_SECOND;
            break;
        case MILLISECOND:
            v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000_000 % 1_000;
            break;
        case MICROSECOND:
            v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000 % 1_000_000;
            break;
        case NANOSECOND:
            v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) % NANOS_PER_SECOND;
            break;
        default:
            throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
    }
    return (int) v;
}
Also used : ValueInterval(org.h2.value.ValueInterval) IntervalQualifier(org.h2.api.IntervalQualifier)

Example 15 with ValueInterval

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

the class Parser method readInterval.

private Expression readInterval() {
    boolean negative = readIf(MINUS_SIGN);
    if (!negative) {
        readIf(PLUS_SIGN);
    }
    if (currentTokenType != LITERAL || token.value(session).getValueType() != Value.VARCHAR) {
        addExpected("string");
        throw getSyntaxError();
    }
    String s = token.value(session).getString();
    read();
    TypeInfo typeInfo = readIntervalQualifier();
    try {
        ValueInterval interval = IntervalUtils.parseInterval(IntervalQualifier.valueOf(typeInfo.getValueType() - Value.INTERVAL_YEAR), negative, s);
        if (typeInfo.getDeclaredPrecision() != -1L || typeInfo.getDeclaredScale() != -1) {
            return TypedValueExpression.get(interval.castTo(typeInfo, session), typeInfo);
        }
        return ValueExpression.get(interval);
    } catch (Exception e) {
        throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s);
    }
}
Also used : ValueInterval(org.h2.value.ValueInterval) TypeInfo(org.h2.value.TypeInfo) DbException(org.h2.message.DbException)

Aggregations

ValueInterval (org.h2.value.ValueInterval)18 BigInteger (java.math.BigInteger)14 BigDecimal (java.math.BigDecimal)13 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)11 ValueInterval (org.gridgain.internal.h2.value.ValueInterval)10 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)7 ValueTime (org.gridgain.internal.h2.value.ValueTime)6 IntervalQualifier (org.h2.api.IntervalQualifier)6 ValueDate (org.h2.value.ValueDate)6 ValueTime (org.h2.value.ValueTime)6 Value (org.gridgain.internal.h2.value.Value)5 ValueTimestampTimeZone (org.gridgain.internal.h2.value.ValueTimestampTimeZone)5 Value (org.h2.value.Value)5 ValueTimestamp (org.h2.value.ValueTimestamp)4 ResultInterface (org.gridgain.internal.h2.result.ResultInterface)3 TypeInfo (org.gridgain.internal.h2.value.TypeInfo)3 ValueDate (org.gridgain.internal.h2.value.ValueDate)3 ValueLobDb (org.gridgain.internal.h2.value.ValueLobDb)3 ValueString (org.gridgain.internal.h2.value.ValueString)3 ValueTimestamp (org.gridgain.internal.h2.value.ValueTimestamp)3