Search in sources :

Example 6 with BoolPtr

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

the class MySQLPlanNodeVisitor method visit.

public boolean visit(SQLUnionQuery sqlSelectQuery) {
    MergeNode mergeNode = new MergeNode();
    SQLSelectQuery left = sqlSelectQuery.getLeft();
    MySQLPlanNodeVisitor mtvLeft = new MySQLPlanNodeVisitor(this.currentDb, this.charsetIndex, this.metaManager, this.isSubQuery);
    mtvLeft.visit(left);
    mergeNode.addChild(mtvLeft.getTableNode());
    BoolPtr containSchemaPtr = new BoolPtr(mtvLeft.isContainSchema());
    mergeNode = checkRightChild(mergeNode, sqlSelectQuery.getRight(), isUnion(sqlSelectQuery.getOperator()), containSchemaPtr);
    this.tableNode = mergeNode;
    this.containSchema = containSchemaPtr.get();
    SQLOrderBy orderBy = sqlSelectQuery.getOrderBy();
    if (orderBy != null) {
        handleOrderBy(orderBy);
    }
    return true;
}
Also used : BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr)

Example 7 with BoolPtr

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

the class MyTime method extractDateTime.

/**
 * Extract datetime value to MYSQL_TIME struct from string value according
 * to format string.
 *
 * @param format              date/time format specification
 * @param valStr              String to decode
 * @param lTime               Store result here
 * @param cachedTimestampType It uses to get an appropriate warning in the case when the
 *                            value is truncated.
 * @return 1 error
 * @note Possibility to parse strings matching to patterns equivalent to
 * compound specifiers is mainly intended for use from inside of this
 * function in order to understand %T and %r conversion specifiers, so
 * number of conversion specifiers that can be used in such
 * sub-patterns is limited. Also most of checks are skipped in this
 * case.
 * @note If one adds new format specifiers to this function he should also
 * consider adding them to Item_func_str_to_date::fix_from_format().
 */
public static boolean extractDateTime(DateTimeFormat format, String valStr, MySQLTime lTime, MySQLTimestampType cachedTimestampType, String dateTimeType) {
    int weekday = 0, yearday = 0, daypart = 0;
    int weekNumber = -1;
    BoolPtr error = new BoolPtr(false);
    int strictWeekNumberYear = -1;
    int fracPart;
    boolean usaTime = false;
    boolean sundayFirstNFirstWeekNonIso = false;
    boolean strictWeekNumber = false;
    boolean strictWeekNumberYearType = false;
    int val = 0;
    int valEnd = valStr.length();
    char[] valcs = valStr.toCharArray();
    int ptr = 0;
    int end = format.getFormat().length();
    char[] ptrcs = format.getFormat().toCharArray();
    for (; ptr != end && val != valEnd; ptr++) {
        if (ptrcs[ptr] == '%' && ptr + 1 != end) {
            int valLen;
            int tmp;
            error.set(false);
            valLen = valEnd - val;
            switch(ptrcs[++ptr]) {
                /* Year */
                case 'Y':
                    tmp = val + Math.min(4, valLen);
                    lTime.setYear(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    if (tmp - val <= 2)
                        lTime.setYear(MyTime.year2000Handling(lTime.getYear()));
                    val = tmp;
                    break;
                case 'y':
                    tmp = val + Math.min(2, valLen);
                    lTime.setYear(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    val = tmp;
                    lTime.setYear(MyTime.year2000Handling(lTime.getYear()));
                    break;
                /* Month */
                case 'm':
                case 'c':
                    tmp = val + Math.min(2, valLen);
                    lTime.setMonth(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    val = tmp;
                    break;
                case 'M':
                    lTime.setMonth(MySQLcom.checkWord(MONTH_NAMES, valcs, val, valEnd));
                    if (lTime.getMonth() <= 0) {
                        // logger.warn
                        return true;
                    }
                    break;
                case 'b':
                    lTime.setMonth(MySQLcom.checkWord(AB_MONTH_NAMES, valcs, val, valEnd));
                    if (lTime.getMonth() <= 0) {
                        // logger.warn
                        return true;
                    }
                    break;
                /* Day */
                case 'd':
                case 'e':
                    tmp = val + Math.min(2, valLen);
                    lTime.setDay(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    val = tmp;
                    break;
                case 'D':
                    tmp = val + Math.min(2, valLen);
                    lTime.setDay(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    /* Skip 'st, 'nd, 'th .. */
                    val = tmp + Math.min((int) (valEnd - tmp), 2);
                    break;
                /* Hour */
                case 'h':
                case 'I':
                case 'l':
                    usaTime = true;
                /* fall through */
                case 'k':
                case 'H':
                    tmp = val + Math.min(2, valLen);
                    lTime.setHour(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    val = tmp;
                    break;
                /* Minute */
                case 'i':
                    tmp = val + Math.min(2, valLen);
                    lTime.setMinute(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    val = tmp;
                    break;
                /* Second */
                case 's':
                case 'S':
                    tmp = val + Math.min(2, valLen);
                    lTime.setSecond(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    val = tmp;
                    break;
                /* Second part */
                case 'f':
                    tmp = valEnd;
                    if (tmp - val > 6)
                        tmp = val + 6;
                    lTime.setSecondPart(MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue());
                    fracPart = 6 - (int) (tmp - val);
                    if (fracPart > 0)
                        lTime.setSecondPart(lTime.getSecondPart() * MySQLcom.LOG_10_INT[fracPart]);
                    val = tmp;
                    break;
                /* AM / PM */
                case 'p':
                    if (valLen < 2 || !usaTime) {
                        // logger.warn
                        return true;
                    }
                    if (new String(valcs, val, 2).compareTo("PM") == 0)
                        daypart = 12;
                    else if (new String(valcs, val, 2).compareTo("AM") == 0) {
                        {
                            // logger.warn
                            return true;
                        }
                    }
                    val += 2;
                    break;
                /* Exotic things */
                case 'W':
                    if ((weekday = MySQLcom.checkWord(DAY_NAMES, valcs, val, valEnd)) <= 0) {
                        {
                            // logger.warn
                            return true;
                        }
                    }
                    break;
                case 'a':
                    if ((weekday = MySQLcom.checkWord(AB_DAY_NAMES, valcs, val, valEnd)) <= 0) {
                        {
                            // logger.warn
                            return true;
                        }
                    }
                    break;
                case 'w':
                    tmp = val + 1;
                    if ((weekday = MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue()) < 0 || weekday >= 7) {
                        {
                            // logger.warn
                            return true;
                        }
                    }
                    /* We should use the same 1 - 7 scale for %w as for %W */
                    if (weekday == 0)
                        weekday = 7;
                    val = tmp;
                    break;
                case 'j':
                    tmp = val + Math.min(valLen, 3);
                    yearday = MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue();
                    val = tmp;
                    break;
                /* Week numbers */
                case 'V':
                case 'U':
                case 'v':
                case 'u':
                    sundayFirstNFirstWeekNonIso = (ptrcs[ptr] == 'U' || ptrcs[ptr] == 'V');
                    strictWeekNumber = (ptrcs[ptr] == 'V' || ptrcs[ptr] == 'v');
                    tmp = val + Math.min(valLen, 2);
                    if ((weekNumber = MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue()) < 0 || (strictWeekNumber && weekNumber == 0) || weekNumber > 53) {
                        {
                            // logger.warn
                            return true;
                        }
                    }
                    val = tmp;
                    break;
                /* Year used with 'strict' %V and %v week numbers */
                case 'X':
                case 'x':
                    strictWeekNumberYearType = (ptrcs[ptr] == 'X');
                    tmp = val + Math.min(4, valLen);
                    strictWeekNumberYear = MySQLcom.myStrtoll10(valcs, val, tmp, error).intValue();
                    val = tmp;
                    break;
                /* Time in AM/PM notation */
                case 'r':
                    /*
     * We can't just set error here, as we don't want to
     * generate two warnings in case of errors
     */
                    if (extractDateTime(TIME_AMPM_FORMAT, new String(valcs, val, valEnd - val), lTime, cachedTimestampType, "time"))
                        return true;
                    break;
                /* Time in 24-hour notation */
                case 'T':
                    if (extractDateTime(TIME_24_HRS_FORMAT, new String(valcs, val, valEnd - val), lTime, cachedTimestampType, "time"))
                        return true;
                    break;
                /* Conversion specifiers that match classes of characters */
                case '.':
                    while (val < valEnd && Ctype.isPunct(valcs[val])) val++;
                    break;
                case '@':
                    while (val < valEnd && Ctype.myIsAlpha(valcs[val])) val++;
                    break;
                case '#':
                    while (val < valEnd && Ctype.isDigit(valcs[val])) val++;
                    break;
                default:
                    {
                        // logger.warn
                        return true;
                    }
            }
            if (error.get()) {
                // logger.warn
                return true;
            }
        } else if (!Ctype.spaceChar(ptrcs[ptr])) {
            if (valcs[val] != ptrcs[ptr]) {
                // logger.warn
                return true;
            }
            val++;
        }
    }
    if (usaTime) {
        if (lTime.getHour() > 12 || lTime.getHour() < 1) {
            // logger.warn
            return true;
        }
        lTime.setHour(lTime.getHour() % 12 + daypart);
    }
    if (yearday > 0) {
        long days;
        days = calcDaynr(lTime.getYear(), 1L, 1L) + yearday - 1;
        if (days <= 0 || days > MAX_DAY_NUMBER) {
            // logger.warn
            return true;
        }
        LongPtr yPtr = new LongPtr(lTime.getYear());
        LongPtr mPtr = new LongPtr(lTime.getMonth());
        LongPtr dPtr = new LongPtr(lTime.getDay());
        getDateFromDaynr(days, yPtr, mPtr, dPtr);
        lTime.setYear(yPtr.get());
        lTime.setMonth(mPtr.get());
        lTime.setDay(dPtr.get());
    }
    if (weekNumber >= 0 && weekday != 0) {
        int days;
        long weekdayB;
        /*
     * %V,%v require %X,%x resprectively, %U,%u should be used with %Y
     * and not %X or %x
     */
        if ((strictWeekNumber && (strictWeekNumberYear < 0 || strictWeekNumberYearType != sundayFirstNFirstWeekNonIso)) || (!strictWeekNumber && strictWeekNumberYear >= 0)) {
            // logger.warn
            return true;
        }
        /* Number of days since year 0 till 1st Jan of this year */
        days = (int) calcDaynr((strictWeekNumber ? strictWeekNumberYear : lTime.getYear()), 1, 1);
        /* Which day of week is 1st Jan of this year */
        weekdayB = calcWeekday(days, sundayFirstNFirstWeekNonIso);
        /*
     * Below we are going to sum: 1) number of days since year 0 till
     * 1st day of 1st week of this year 2) number of days between 1st
     * week and our week 3) and position of our day in the week
     */
        if (sundayFirstNFirstWeekNonIso) {
            days += ((weekdayB == 0) ? 0 : 7) - weekdayB + (weekNumber - 1) * 7 + weekday % 7;
        } else {
            days += ((weekdayB <= 3) ? 0 : 7) - weekdayB + (weekNumber - 1) * 7 + (weekday - 1);
        }
        if (days <= 0 || days > MAX_DAY_NUMBER) {
            // logger.warn
            return true;
        }
        LongPtr yPtr = new LongPtr(lTime.getYear());
        LongPtr mPtr = new LongPtr(lTime.getMonth());
        LongPtr dPtr = new LongPtr(lTime.getDay());
        getDateFromDaynr(days, yPtr, mPtr, dPtr);
        lTime.setYear(yPtr.get());
        lTime.setMonth(mPtr.get());
        lTime.setDay(dPtr.get());
    }
    if (lTime.getMonth() > 12 || lTime.getDay() > 31 || lTime.getHour() > 23 || lTime.getMinute() > 59 || lTime.getSecond() > 59) {
        // logger.warn
        return true;
    }
    if (val != valEnd) {
        do {
            if (!Ctype.spaceChar(valcs[val])) {
                // 
                break;
            }
        } while (++val != valEnd);
    }
    return false;
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr) BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr)

Example 8 with BoolPtr

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

the class ArgComparator method getDateFromConst.

/**
 * Check if str_arg is a constant and convert it to datetime packed value.
 * Note, const_value may stay untouched, so the caller is responsible to
 * initialize it.
 *
 * @param dateArg date argument, it's name is used for error reporting.
 * @param strArg  string argument to get datetime value from.
 * @return true on error, false on success, false if str_arg is not a const.
 * @param[out] const_value the converted value is stored here, if not NULL.
 */
static boolean getDateFromConst(Item dateArg, Item strArg, LongPtr constValue) {
    BoolPtr error = new BoolPtr(false);
    long value = 0;
    if (strArg.fieldType() == FieldTypes.MYSQL_TYPE_TIME) {
        // Convert from TIME to DATETIME
        value = strArg.valDateTemporal();
        if (strArg.isNullValue())
            return true;
    } else {
        // Convert from string to DATETIME
        String strVal = strArg.valStr();
        MySQLTimestampType ttype = (dateArg.fieldType() == FieldTypes.MYSQL_TYPE_DATE ? MySQLTimestampType.MYSQL_TIMESTAMP_DATE : MySQLTimestampType.MYSQL_TIMESTAMP_DATETIME);
        if (strArg.isNullValue()) {
            return true;
        }
        value = MySQLcom.getDateFromStr(strVal, ttype, error);
        if (error.get())
            return true;
    }
    if (constValue != null)
        constValue.set(value);
    return false;
}
Also used : BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr) MySQLTimestampType(com.actiontech.dble.plan.common.time.MySQLTimestampType)

Aggregations

BoolPtr (com.actiontech.dble.plan.common.ptr.BoolPtr)8 MySQLTimestampType (com.actiontech.dble.plan.common.time.MySQLTimestampType)2 FieldTypes (com.actiontech.dble.plan.common.item.FieldTypes)1 ItemFunc (com.actiontech.dble.plan.common.item.function.ItemFunc)1 ItemCondAnd (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd)1 ItemCondOr (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr)1 ItemExistsSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemExistsSubQuery)1 ItemInSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery)1 ItemScalarSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)1 LongPtr (com.actiontech.dble.plan.common.ptr.LongPtr)1 PlanNode (com.actiontech.dble.plan.node.PlanNode)1 QueryNode (com.actiontech.dble.plan.node.QueryNode)1 BigDecimal (java.math.BigDecimal)1