Search in sources :

Example 6 with LongPtr

use of com.actiontech.dble.plan.common.ptr.LongPtr in project dble by actiontech.

the class ItemFuncMinMax method valReal.

@Override
public BigDecimal valReal() {
    double value = 0.0;
    if (compareAsDates) {
        LongPtr result = new LongPtr(0);
        cmpDatetimes(result);
        return new BigDecimal(MyTime.doubleFromDatetimePacked(datetimeItem.fieldType(), result.get()));
    }
    for (int i = 0; i < args.size(); i++) {
        if (i == 0)
            value = args.get(i).valReal().doubleValue();
        else {
            double tmp = args.get(i).valReal().doubleValue();
            if (!args.get(i).isNull() && (tmp < value ? cmpSign : -cmpSign) > 0)
                value = tmp;
        }
        if ((nullValue = args.get(i).isNull()))
            break;
    }
    return new BigDecimal(value);
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr) BigDecimal(java.math.BigDecimal)

Example 7 with LongPtr

use of com.actiontech.dble.plan.common.ptr.LongPtr in project dble by actiontech.

the class ItemFuncMinMax method valStr.

@Override
public String valStr() {
    if (compareAsDates) {
        if (isTemporal()) {
            /*
                 * In case of temporal data types, we always return string value
                 * according the format of the data type. For example, in case
                 * of LEAST(time_column, datetime_column) the result date type
                 * is DATETIME, so we return a 'YYYY-MM-DD hh:mm:ss' string even
                 * if time_column wins (conversion from TIME to DATETIME happens
                 * in this case).
                 */
            LongPtr result = new LongPtr(0);
            cmpDatetimes(result);
            if (nullValue)
                return null;
            MySQLTime ltime = new MySQLTime();
            MyTime.timeFromLonglongPacked(ltime, fieldType(), result.get());
            return MyTime.myTimeToStrL(ltime, decimals);
        } else {
            /*
                 * In case of VARCHAR result type we just return val_str() value
                 * of the winning item AS IS, without conversion.
                 */
            long minMaxIdx = cmpDatetimes(new LongPtr(0));
            if (nullValue)
                return null;
            String strRes = args.get((int) minMaxIdx).valStr();
            if (args.get((int) minMaxIdx).isNullValue()) {
                // check if the call to val_str() above returns a NULL value
                nullValue = true;
                return null;
            }
            return strRes;
        }
    }
    if (cmpType == ItemResult.INT_RESULT) {
        BigInteger nr = valInt();
        if (nullValue)
            return null;
        return nr.toString();
    } else if (cmpType == ItemResult.DECIMAL_RESULT) {
        BigDecimal bd = valDecimal();
        if (nullValue)
            return null;
        return bd.toString();
    } else if (cmpType == ItemResult.REAL_RESULT) {
        BigDecimal nr = valReal();
        if (nullValue)
            return null;
        /* purecov: inspected */
        return nr.toString();
    } else if (cmpType == ItemResult.STRING_RESULT) {
        String res = null;
        for (int i = 0; i < args.size(); i++) {
            if (i == 0)
                res = args.get(i).valStr();
            else {
                String res2 = args.get(i).valStr();
                if (res2 != null) {
                    int cmp = res.compareTo(res2);
                    if ((cmpSign < 0 ? cmp : -1 * cmp) < 0)
                        res = res2;
                }
            }
            if ((nullValue = args.get(i).isNull()))
                return null;
        }
        return res;
    } else {
        // This case should never be chosen
        return null;
    }
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr) MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 8 with LongPtr

use of com.actiontech.dble.plan.common.ptr.LongPtr in project dble by actiontech.

the class ItemFuncTimestampDiff method valInt.

@Override
public BigInteger valInt() {
    MySQLTime ltime1 = new MySQLTime();
    MySQLTime ltime2 = new MySQLTime();
    nullValue = false;
    int neg = 1;
    long months = 0;
    if (args.get(0).getDate(ltime1, MyTime.TIME_NO_ZERO_DATE) || args.get(1).getDate(ltime2, MyTime.TIME_NO_ZERO_DATE)) {
        nullValue = true;
        return BigInteger.ZERO;
    }
    LongPtr lpseconds = new LongPtr(0);
    LongPtr lpmicroseconds = new LongPtr(0);
    if (MyTime.calcTimeDiff(ltime2, ltime1, 1, lpseconds, lpmicroseconds))
        neg = -1;
    long seconds = lpseconds.get(), microseconds = lpmicroseconds.get();
    if (intType == MySqlIntervalUnit.YEAR || intType == MySqlIntervalUnit.QUARTER || intType == MySqlIntervalUnit.MONTH) {
        long yearBeg, yearEnd, monthBeg, monthEnd, dayBeg, dayEnd;
        long years = 0;
        long secondBeg, secondEnd, microsecondBeg, microsecondEnd;
        if (neg == -1) {
            yearBeg = ltime2.getYear();
            yearEnd = ltime1.getYear();
            monthBeg = ltime2.getMonth();
            monthEnd = ltime1.getMonth();
            dayBeg = ltime2.getDay();
            dayEnd = ltime1.getDay();
            secondBeg = ltime2.getHour() * 3600 + ltime2.getMinute() * 60 + ltime2.getSecond();
            secondEnd = ltime1.getHour() * 3600 + ltime1.getMinute() * 60 + ltime1.getSecond();
            microsecondBeg = ltime2.getSecondPart();
            microsecondEnd = ltime1.getSecondPart();
        } else {
            yearBeg = ltime1.getYear();
            yearEnd = ltime2.getYear();
            monthBeg = ltime1.getMonth();
            monthEnd = ltime2.getMonth();
            dayBeg = ltime1.getDay();
            dayEnd = ltime2.getDay();
            secondBeg = ltime1.getHour() * 3600 + ltime1.getMinute() * 60 + ltime1.getSecond();
            secondEnd = ltime2.getHour() * 3600 + ltime2.getMinute() * 60 + ltime2.getSecond();
            microsecondBeg = ltime1.getSecondPart();
            microsecondEnd = ltime2.getSecondPart();
        }
        /* calc years */
        years = yearEnd - yearBeg;
        if (monthEnd < monthBeg || (monthEnd == monthBeg && dayEnd < dayBeg))
            years -= 1;
        /* calc months */
        months = 12 * years;
        if (monthEnd < monthBeg || (monthEnd == monthBeg && dayEnd < dayBeg))
            months += 12 - (monthBeg - monthEnd);
        else
            months += (monthEnd - monthBeg);
        if (dayEnd < dayBeg)
            months -= 1;
        else if ((dayEnd == dayBeg) && ((secondEnd < secondBeg) || (secondEnd == secondBeg && microsecondEnd < microsecondBeg)))
            months -= 1;
    }
    switch(intType) {
        case YEAR:
            return BigInteger.valueOf(months / 12 * neg);
        case QUARTER:
            return BigInteger.valueOf(months / 3 * neg);
        case MONTH:
            return BigInteger.valueOf(months * neg);
        case WEEK:
            return BigInteger.valueOf(seconds / MyTime.SECONDS_IN_24H / 7L * neg);
        case DAY:
            return BigInteger.valueOf(seconds / MyTime.SECONDS_IN_24H * neg);
        case HOUR:
            return BigInteger.valueOf(seconds / 3600L * neg);
        case MINUTE:
            return BigInteger.valueOf(seconds / 60L * neg);
        case SECOND:
            return BigInteger.valueOf(seconds * neg);
        case MICROSECOND:
            /*
                 * In MySQL difference between any two valid datetime values in
                 * microseconds fits into longlong.
                 */
            return BigInteger.valueOf((seconds * 1000000L + microseconds) * neg);
        default:
            nullValue = true;
            return BigInteger.ZERO;
    }
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr) MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime)

Example 9 with LongPtr

use of com.actiontech.dble.plan.common.ptr.LongPtr in project dble by actiontech.

the class MyTime method makeDateTime.

/**
 * Create a formated date/time value in a string.
 *
 * @return
 */
public static boolean makeDateTime(DateTimeFormat format, MySQLTime lTime, MySQLTimestampType type, StringPtr strPtr) {
    int hoursI;
    int weekday;
    int ptr = 0, end;
    char[] formatChars = format.getFormat().toCharArray();
    StringBuilder str = new StringBuilder();
    MyLocale locale = MyLocales.MY_LOCALE_EN_US;
    if (lTime.isNeg())
        str.append('-');
    end = format.getFormat().length();
    for (; ptr != end; ptr++) {
        if (formatChars[ptr] != '%' || ptr + 1 == end)
            str.append(formatChars[ptr]);
        else {
            switch(formatChars[++ptr]) {
                case 'M':
                    if (lTime.getMonth() == 0) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(locale.getMonthNames().getTypeNames()[(int) (lTime.getMonth() - 1)]);
                    break;
                case 'b':
                    if (lTime.getMonth() == 0) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(locale.getAbMonthNames().getTypeNames()[(int) (lTime.getMonth() - 1)]);
                    break;
                case 'W':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME || (lTime.getMonth() == 0 && lTime.getYear() == 0)) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    weekday = MyTime.calcWeekday(MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()), false);
                    str.append(locale.getDayNames().getTypeNames()[weekday]);
                    break;
                case 'a':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME || (lTime.getMonth() == 0 && lTime.getYear() == 0)) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    weekday = MyTime.calcWeekday(MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()), false);
                    str.append(locale.getAbDayNames().getTypeNames()[weekday]);
                    break;
                case 'D':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(String.format("%01d", lTime.getDay()));
                    if (lTime.getDay() >= 10 && lTime.getDay() <= 19)
                        str.append("th");
                    else {
                        int tmp = (int) (lTime.getDay() % 10);
                        switch(tmp) {
                            case 1:
                                str.append("st");
                                break;
                            case 2:
                                str.append("nd");
                                break;
                            case 3:
                                str.append("rd");
                                break;
                            default:
                                str.append("th");
                                break;
                        }
                    }
                    break;
                case 'Y':
                    str.append(String.format("%04d", lTime.getYear()));
                    break;
                case 'y':
                    str.append(String.format("%02d", lTime.getYear()));
                    break;
                case 'm':
                    str.append(String.format("%02d", lTime.getMonth()));
                    break;
                case 'c':
                    str.append(String.format("%01d", lTime.getMonth()));
                    break;
                case 'd':
                    str.append(String.format("%02d", lTime.getDay()));
                    break;
                case 'e':
                    str.append(String.format("%01d", lTime.getDay()));
                    break;
                case 'f':
                    str.append(String.format("%06d", lTime.getSecondPart()));
                    break;
                case 'H':
                    str.append(String.format("%02d", lTime.getHour()));
                    break;
                case 'h':
                case 'I':
                    hoursI = (int) ((lTime.getHour() % 24 + 11) % 12 + 1);
                    str.append(String.format("%02d", hoursI));
                    break;
                case 'i':
                    /* minutes */
                    str.append(String.format("%02d", lTime.getMinute()));
                    break;
                case 'j':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(String.format("%03d", MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()) - MyTime.calcDaynr(lTime.getYear(), 1, 1) + 1));
                    break;
                case 'k':
                    str.append(String.format("%01d", lTime.getHour()));
                    break;
                case 'l':
                    hoursI = (int) ((lTime.getHour() % 24 + 11) % 12 + 1);
                    str.append(String.format("%01d", lTime.getHour()));
                    break;
                case 'p':
                    hoursI = (int) (lTime.getHour() % 24);
                    str.append(hoursI < 12 ? "AM" : "PM");
                    break;
                case 'r':
                    String tmpFmt = ((lTime.getHour() % 24) < 12) ? "%02d:%02d:%02d AM" : "%02d:%02d:%02d PM";
                    str.append(String.format(tmpFmt, (lTime.getHour() + 11) % 12 + 1, lTime.getMinute(), lTime.getSecond()));
                    break;
                case 'S':
                case 's':
                    str.append(String.format("%02d", lTime.getSecond()));
                    break;
                case 'T':
                    str.append(String.format("%02d:%02d:%02d", lTime.getHour(), lTime.getMinute(), lTime.getSecond()));
                    break;
                case 'U':
                case 'u':
                    {
                        LongPtr year = new LongPtr(0);
                        if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                            strPtr.set(str.toString());
                            return true;
                        }
                        str.append(String.format("%02d", MyTime.calcWeek(lTime, formatChars[ptr] == 'U' ? MyTime.WEEK_FIRST_WEEKDAY : MyTime.WEEK_MONDAY_FIRST, year)));
                    }
                    break;
                case 'v':
                case 'V':
                    {
                        LongPtr year = new LongPtr(0);
                        if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                            strPtr.set(str.toString());
                            return true;
                        }
                        str.append(String.format("%02d", MyTime.calcWeek(lTime, formatChars[ptr] == 'V' ? (MyTime.WEEK_YEAR | MyTime.WEEK_FIRST_WEEKDAY) : (MyTime.WEEK_YEAR | MyTime.WEEK_MONDAY_FIRST), year)));
                    }
                    break;
                case 'x':
                case 'X':
                    {
                        LongPtr year = new LongPtr(0);
                        if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                            strPtr.set(str.toString());
                            return true;
                        }
                        MyTime.calcWeek(lTime, formatChars[ptr] == 'X' ? MyTime.WEEK_YEAR | MyTime.WEEK_FIRST_WEEKDAY : MyTime.WEEK_YEAR | MyTime.WEEK_MONDAY_FIRST, year);
                        str.append(String.format("%04d", year.get()));
                    }
                    break;
                case 'w':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME || (lTime.getMonth() == 0 && lTime.getYear() == 0)) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    weekday = MyTime.calcWeekday(MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()), true);
                    str.append(String.format("%01d", weekday));
                    break;
                default:
                    str.append(format.getFormat().substring(ptr));
                    break;
            }
        }
    }
    strPtr.set(str.toString());
    return false;
}
Also used : MyLocale(com.actiontech.dble.plan.common.locale.MyLocale) LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr)

Example 10 with LongPtr

use of com.actiontech.dble.plan.common.ptr.LongPtr in project dble by actiontech.

the class MyTime method myDecimalToDatetimeWithWarn.

/**
 * Convert decimal value to datetime value with a warning.
 *
 * @param decimal The value to convert from.
 * @param flags   Conversion flags.
 * @return False on success, true on error.
 * @param[out] ltime The variable to convert to.
 */
public static boolean myDecimalToDatetimeWithWarn(BigDecimal decimal, MySQLTime ltime, long flags) {
    LongPtr warnings = new LongPtr(0);
    String sbd = decimal.toString();
    String[] sbds = sbd.split("\\.");
    long intPart = Long.parseLong(sbds[0]);
    long secondPart = 0;
    if (sbds.length == 2)
        secondPart = Long.parseLong(sbds[1]);
    if (numberToDatetime(intPart, ltime, flags, warnings) == -1) {
        ltime.setZeroTime(MySQLTimestampType.MYSQL_TIMESTAMP_ERROR);
        return true;
    } else if (ltime.getTimeType() == MySQLTimestampType.MYSQL_TIMESTAMP_DATE) {
        /**
         * Generate a warning in case of DATE with fractional part:
         * 20011231.1234 . '2001-12-31' unless the caller does not want the
         * warning: for example, CAST does.
         */
        if (secondPart != 0 && (flags & TIME_NO_DATE_FRAC_WARN) == 0) {
            warnings.set(warnings.get() | MYSQL_TIME_WARN_TRUNCATED);
        }
    } else if ((flags & TIME_NO_NSEC_ROUNDING) == 0) {
        ltime.setSecondPart(secondPart);
    }
    return false;
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr)

Aggregations

LongPtr (com.actiontech.dble.plan.common.ptr.LongPtr)23 MySQLTime (com.actiontech.dble.plan.common.time.MySQLTime)6 BigDecimal (java.math.BigDecimal)3 FieldTypes (com.actiontech.dble.plan.common.item.FieldTypes)2 MyLocale (com.actiontech.dble.plan.common.locale.MyLocale)1 BoolPtr (com.actiontech.dble.plan.common.ptr.BoolPtr)1 DoublePtr (com.actiontech.dble.plan.common.ptr.DoublePtr)1 BigInteger (java.math.BigInteger)1