use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.
the class HandlerTool method createItem.
/**
* create Item, the Item value referenced by field and changed by field changes
*
* @param sel
* @param fields
* @param type
* @return
*/
public static Item createItem(Item sel, List<Field> fields, int startIndex, boolean allPushDown, HandlerType type) {
Item ret;
if (sel.basicConstItem())
return sel;
if (sel instanceof ItemScalarSubQuery) {
ItemScalarSubQuery scalarSubQuery = (ItemScalarSubQuery) sel;
if (scalarSubQuery.getPlanNode().type() == PlanNode.PlanNodeType.NONAME) {
Item result = scalarSubQuery.getSelect();
result.setItemName(scalarSubQuery.getItemName());
result.setAlias(scalarSubQuery.getItemName());
return result;
} else {
return scalarSubQuery.getValue();
}
}
Item.ItemType i = sel.type();
if (i == Item.ItemType.FUNC_ITEM || i == Item.ItemType.COND_ITEM) {
ItemFunc func = (ItemFunc) sel;
if (func.getPushDownName() == null || func.getPushDownName().length() == 0) {
ret = createFunctionItem(func, fields, startIndex, allPushDown, type);
} else {
ret = createFieldItem(func, fields, startIndex);
}
} else if (i == Item.ItemType.SUM_FUNC_ITEM) {
ItemSum sumFunc = (ItemSum) sel;
if (type != HandlerType.GROUPBY) {
ret = createFieldItem(sumFunc, fields, startIndex);
} else if (sumFunc.getPushDownName() == null || sumFunc.getPushDownName().length() == 0) {
ret = createSumItem(sumFunc, fields, startIndex, allPushDown, type);
} else {
ret = createPushDownGroupBy(sumFunc, fields, startIndex);
}
} else {
ret = createFieldItem(sel, fields, startIndex);
}
ret.fixFields();
return ret;
}
use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery 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;
}
use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.
the class PlanUtil method rebuildSubQueryFuncItem.
private static Item rebuildSubQueryFuncItem(Item item) {
ItemFunc func = (ItemFunc) item;
Item itemTmp = item.cloneItem();
for (int index = 0; index < func.getArgCount(); index++) {
Item arg = item.arguments().get(index);
if (arg instanceof ItemScalarSubQuery) {
Item result = ((ItemScalarSubQuery) arg).getValue();
if (result == null || result.getResultItem() == null) {
itemTmp.arguments().set(index, new ItemNull());
} else {
itemTmp.arguments().set(index, result.getResultItem());
}
} else if (arg instanceof ItemInSubQuery) {
ItemInSubQuery inSubItem = (ItemInSubQuery) arg;
if (inSubItem.getValue().size() == 0) {
itemTmp.arguments().set(index, genBoolItem(inSubItem.isNeg()));
} else {
List<Item> newArgs = new ArrayList<>(inSubItem.getValue().size() + 1);
newArgs.add(inSubItem.getLeftOperand());
newArgs.addAll(inSubItem.getValue());
itemTmp.arguments().set(index, new ItemFuncIn(newArgs, inSubItem.isNeg()));
}
itemTmp.setItemName(null);
return itemTmp;
} else if (arg instanceof ItemFunc) {
itemTmp.arguments().set(index, rebuildSubQueryItem(arg));
}
}
itemTmp.setItemName(null);
return itemTmp;
}
use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.
the class MySQLItemVisitor method endVisit.
@Override
public void endVisit(SQLSelectStatement node) {
SQLSelectQuery sqlSelect = node.getSelect().getQuery();
item = new ItemScalarSubQuery(currentDb, sqlSelect, metaManager);
}
use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.
the class SubQueryPreProcessor method findComparisonsSubQueryToJoinNode.
/**
* http://dev.mysql.com/doc/refman/5.0/en/comparisons-using-subqueries.html
*/
private static PlanNode findComparisonsSubQueryToJoinNode(PlanNode qtn, BoolPtr childTransform) {
for (int i = 0; i < qtn.getChildren().size(); i++) {
PlanNode child = qtn.getChildren().get(i);
qtn.getChildren().set(i, findComparisonsSubQueryToJoinNode(child, childTransform));
}
for (Item itemSelect : qtn.getColumnsSelected()) {
if (itemSelect instanceof ItemScalarSubQuery) {
ItemScalarSubQuery scalarSubQuery = (ItemScalarSubQuery) itemSelect;
findComparisonsSubQueryToJoinNode(scalarSubQuery.getPlanNode(), childTransform);
}
}
// having contains sub query
buildSubQuery(qtn, new SubQueryFilter(), qtn.getHavingFilter(), false, childTransform);
SubQueryFilter find = new SubQueryFilter();
find.query = qtn;
find.filter = null;
Item where = qtn.getWhereFilter();
SubQueryFilter result = buildSubQuery(qtn, find, where, false, childTransform);
if (result != find) {
// that means where filter only contains sub query,just replace it
result.query.query(result.filter);
qtn.query(null);
// change result.filter and rebuild
result.query.setUpFields();
childTransform.set(true);
return result.query;
} else {
if (childTransform.get()) {
qtn.setUpFields();
}
return qtn;
}
}
Aggregations