Search in sources :

Example 1 with BoolPtr

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

the class GetDatetimeValue method get.

@Override
public long get(Item item, Item warnItem, BoolPtr isNull) {
    long value = 0;
    String str = null;
    if (item.isTemporal()) {
        value = item.valDateTemporal();
        isNull.set(item.isNullValue());
    } else {
        str = item.valStr();
        isNull.set(item.isNullValue());
    }
    if (isNull.get())
        return 0;
    if (str != null) {
        BoolPtr error = new BoolPtr(false);
        FieldTypes fType = warnItem.fieldType();
        MySQLTimestampType tType = fType == FieldTypes.MYSQL_TYPE_DATE ? MySQLTimestampType.MYSQL_TIMESTAMP_DATE : MySQLTimestampType.MYSQL_TIMESTAMP_DATETIME;
        value = MySQLcom.getDateFromStr(str, tType, error);
    }
    return value;
}
Also used : FieldTypes(com.actiontech.dble.plan.common.item.FieldTypes) BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr) MySQLTimestampType(com.actiontech.dble.plan.common.time.MySQLTimestampType)

Example 2 with BoolPtr

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

the class MyTime method getIntervalValue.

/**
 * Convert a string to a interval value.
 * <p>
 * To make code easy, allow interval objects without separators.
 */
public static boolean getIntervalValue(Item arg, MySqlIntervalUnit unit, StringPtr strValue, Interval interval) {
    long[] array = new long[5];
    long value = 0;
    if (unit == MySqlIntervalUnit.SECOND && arg.getDecimals() != 0) {
        BigDecimal decimalValue = arg.valDecimal();
        if (decimalValue == null)
            return false;
        boolean neg = decimalValue.compareTo(BigDecimal.ZERO) < 0;
        if (!neg) {
            interval.setNeg(false);
            interval.setSecond(decimalValue.longValue());
            interval.setSecondPart((long) ((decimalValue.doubleValue() - interval.getSecond()) * 1000000));
        } else {
            interval.setNeg(true);
            interval.setSecond(-decimalValue.longValue());
            interval.setSecondPart((long) ((-decimalValue.doubleValue() - interval.getSecond()) * 1000000));
        }
        return false;
    } else if (unit == MySqlIntervalUnit.YEAR || unit == MySqlIntervalUnit.QUARTER || unit == MySqlIntervalUnit.MONTH || unit == MySqlIntervalUnit.WEEK || unit == MySqlIntervalUnit.DAY || unit == MySqlIntervalUnit.HOUR || unit == MySqlIntervalUnit.MINUTE || unit == MySqlIntervalUnit.SECOND || unit == MySqlIntervalUnit.MICROSECOND) {
        value = arg.valInt().longValue();
        if (arg.isNullValue())
            return true;
        if (value < 0) {
            interval.setNeg(true);
            value = -value;
        }
    }
    BoolPtr negPtr = new BoolPtr(interval.isNeg());
    if (unit == MySqlIntervalUnit.YEAR) {
        interval.setYear(value);
    } else if (unit == MySqlIntervalUnit.QUARTER) {
        interval.setMonth((value * 3));
    } else if (unit == MySqlIntervalUnit.MONTH) {
        interval.setMonth(value);
    } else if (unit == MySqlIntervalUnit.WEEK) {
        interval.setDay((value * 7));
    } else if (unit == MySqlIntervalUnit.DAY) {
        interval.setDay(value);
    } else if (unit == MySqlIntervalUnit.HOUR) {
        interval.setHour(value);
    } else if (unit == MySqlIntervalUnit.MINUTE) {
        interval.setMinute(value);
    } else if (unit == MySqlIntervalUnit.SECOND) {
        interval.setSecond(value);
    } else if (unit == MySqlIntervalUnit.MICROSECOND) {
        interval.setSecondPart(value);
    } else if (unit == MySqlIntervalUnit.YEAR_MONTH) {
        if (getIntervalInfo(arg, strValue, negPtr, 2, array, false))
            return true;
        interval.setYear(array[0]);
        interval.setMonth(array[1]);
    } else if (unit == MySqlIntervalUnit.DAY_HOUR) {
        if (getIntervalInfo(arg, strValue, negPtr, 2, array, false))
            return true;
        interval.setDay(array[0]);
        interval.setHour(array[1]);
    } else if (unit == MySqlIntervalUnit.DAY_MINUTE) {
        if (getIntervalInfo(arg, strValue, negPtr, 3, array, false))
            return true;
        interval.setDay(array[0]);
        interval.setHour(array[1]);
        interval.setMinute(array[2]);
    } else if (unit == MySqlIntervalUnit.DAY_SECOND) {
        if (getIntervalInfo(arg, strValue, negPtr, 4, array, false))
            return true;
        interval.setDay(array[0]);
        interval.setHour(array[1]);
        interval.setMinute(array[2]);
        interval.setSecond(array[3]);
    } else if (unit == MySqlIntervalUnit.HOUR_MINUTE) {
        if (getIntervalInfo(arg, strValue, negPtr, 2, array, false))
            return true;
        interval.setHour(array[0]);
        interval.setMinute(array[1]);
    } else if (unit == MySqlIntervalUnit.HOUR_SECOND) {
        if (getIntervalInfo(arg, strValue, negPtr, 3, array, false))
            return true;
        interval.setHour(array[0]);
        interval.setMinute(array[1]);
        interval.setSecond(array[2]);
    } else if (unit == MySqlIntervalUnit.MINUTE_SECOND) {
        if (getIntervalInfo(arg, strValue, negPtr, 2, array, false))
            return true;
        interval.setMinute(array[0]);
        interval.setSecond(array[1]);
    } else if (unit == MySqlIntervalUnit.DAY_MICROSECOND) {
        if (getIntervalInfo(arg, strValue, negPtr, 5, array, true))
            return true;
        interval.setDay(array[0]);
        interval.setHour(array[1]);
        interval.setMinute(array[2]);
        interval.setSecond(array[3]);
        interval.setSecondPart(array[4]);
    } else if (unit == MySqlIntervalUnit.HOUR_MICROSECOND) {
        if (getIntervalInfo(arg, strValue, negPtr, 4, array, true))
            return true;
        interval.setHour(array[0]);
        interval.setMinute(array[1]);
        interval.setSecond(array[2]);
        interval.setSecondPart(array[3]);
    } else if (unit == MySqlIntervalUnit.MINUTE_MICROSECOND) {
        if (getIntervalInfo(arg, strValue, negPtr, 3, array, true))
            return true;
        interval.setMinute(array[0]);
        interval.setSecond(array[1]);
        interval.setSecondPart(array[2]);
    } else if (unit == MySqlIntervalUnit.SECOND_MICROSECOND) {
        if (getIntervalInfo(arg, strValue, negPtr, 2, array, true))
            return true;
        interval.setSecond(array[0]);
        interval.setSecondPart(array[1]);
    }
    interval.setNeg(negPtr.get());
    return false;
}
Also used : BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr) BigDecimal(java.math.BigDecimal)

Example 3 with BoolPtr

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

the class PlanUtil method rebuildSubQueryItem.

public static Item rebuildSubQueryItem(Item item) {
    if (!item.isWithSubQuery()) {
        return item;
    }
    BoolPtr reBuild = new BoolPtr(false);
    if (PlanUtil.isCmpFunc(item)) {
        Item res1 = rebuildBoolSubQuery(item, 0, reBuild);
        if (res1 != null) {
            return res1;
        }
        Item res2 = rebuildBoolSubQuery(item, 1, reBuild);
        if (res2 != null) {
            return res2;
        }
    } else if (item instanceof ItemInSubQuery) {
        ItemInSubQuery inSubItem = (ItemInSubQuery) item;
        if (inSubItem.getValue().size() == 0) {
            return genBoolItem(inSubItem.isNeg());
        } else {
            List<Item> args = new ArrayList<>(inSubItem.getValue().size() + 1);
            args.add(inSubItem.getLeftOperand());
            args.addAll(inSubItem.getValue());
            return new ItemFuncIn(args, inSubItem.isNeg());
        }
    } else if (item instanceof ItemExistsSubQuery) {
        ItemExistsSubQuery existsSubQuery = (ItemExistsSubQuery) item;
        Item result = existsSubQuery.getValue();
        if (result == null) {
            return genBoolItem(existsSubQuery.isNot());
        } else {
            return genBoolItem(!existsSubQuery.isNot());
        }
    } else if (item instanceof ItemCondAnd || item instanceof ItemCondOr) {
        for (int index = 0; index < item.getArgCount(); index++) {
            Item rebuildItem = rebuildSubQueryItem(item.arguments().get(index));
            item.arguments().set(index, rebuildItem);
            item.setItemName(null);
        }
    } else if (item instanceof ItemScalarSubQuery) {
        Item result = ((ItemScalarSubQuery) item).getValue();
        if (result == null || result.getResultItem() == null) {
            return new ItemFuncEqual(new ItemInt(1), new ItemInt(0));
        }
        return result.getResultItem();
    }
    if (!reBuild.get() && item instanceof ItemFunc) {
        return rebuildSubQueryFuncItem(item);
    }
    return item;
}
Also used : ItemCondAnd(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd) ItemExistsSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemExistsSubQuery) ItemCondOr(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr) ItemInSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery) ItemFunc(com.actiontech.dble.plan.common.item.function.ItemFunc) BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr) ItemScalarSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)

Example 4 with BoolPtr

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

the class SubQueryProcessor method optimize.

public static PlanNode optimize(PlanNode qtn) {
    BoolPtr merged = new BoolPtr(false);
    qtn = tryTransformerQuery(qtn, merged);
    if (merged.get())
        qtn.setUpFields();
    return qtn;
}
Also used : BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr)

Example 5 with BoolPtr

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

the class SubQueryProcessor method tryTransformerQuery.

/**
 * find query node in qtn ,change to other 3 type node
 */
private static PlanNode tryTransformerQuery(PlanNode qtn, BoolPtr boolptr) {
    boolean childMerged = false;
    for (int index = 0; index < qtn.getChildren().size(); index++) {
        PlanNode child = qtn.getChildren().get(index);
        BoolPtr boolPtr = new BoolPtr(false);
        PlanNode newChild = tryTransformerQuery(child, boolPtr);
        if (boolPtr.get())
            childMerged = true;
        qtn.getChildren().set(index, newChild);
    }
    if (childMerged)
        qtn.setUpFields();
    if (qtn.type() == PlanNodeType.QUERY) {
        qtn = transformerQuery((QueryNode) qtn, boolptr);
    }
    return qtn;
}
Also used : PlanNode(com.actiontech.dble.plan.node.PlanNode) QueryNode(com.actiontech.dble.plan.node.QueryNode) BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr)

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