use of com.actiontech.dble.plan.Order in project dble by actiontech.
the class SubQueryPreProcessor method transformInSubQuery.
private static SubQueryFilter transformInSubQuery(SubQueryFilter qtn, ItemInSubQuery filter, BoolPtr childTransform) {
Item leftColumn = filter.getLeftOperand();
PlanNode query = filter.getPlanNode();
query = findComparisonsSubQueryToJoinNode(query, childTransform);
QueryNode changeQuery = new QueryNode(query);
String alias = AUTOALIAS + query.getPureName();
changeQuery.setAlias(alias);
if (query.getColumnsSelected().size() != 1)
throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "only support subquery of one column");
query.setSubQuery(true).setDistinct(true);
final List<Item> newSelects = qtn.query.getColumnsSelected();
SubQueryFilter result = new SubQueryFilter();
Item rightColumn = query.getColumnsSelected().get(0);
qtn.query.setColumnsSelected(new ArrayList<Item>());
String rightJoinName = rightColumn.getAlias();
if (StringUtils.isEmpty(rightJoinName)) {
if (rightColumn instanceof ItemField) {
rightJoinName = rightColumn.getItemName();
} else {
rightColumn.setAlias(AUTONAME);
rightJoinName = AUTONAME;
}
}
ItemField rightJoinColumn = new ItemField(null, alias, rightJoinName);
// rename the left column's table name
result.query = new JoinNode(qtn.query, changeQuery);
// leave origin sql to new join node
result.query.setSql(qtn.query.getSql());
qtn.query.setSql(null);
result.query.select(newSelects);
qtn.query.setSubQuery(false);
if (!qtn.query.getOrderBys().isEmpty()) {
List<Order> orderBys = new ArrayList<>();
orderBys.addAll(qtn.query.getOrderBys());
result.query.setOrderBys(orderBys);
qtn.query.getOrderBys().clear();
}
if (!qtn.query.getGroupBys().isEmpty()) {
List<Order> groupBys = new ArrayList<>();
groupBys.addAll(qtn.query.getGroupBys());
result.query.setGroupBys(groupBys);
qtn.query.getGroupBys().clear();
result.query.having(qtn.query.getHavingFilter());
qtn.query.having(null);
}
if (qtn.query.getLimitFrom() != -1) {
result.query.setLimitFrom(qtn.query.getLimitFrom());
qtn.query.setLimitFrom(-1);
}
if (qtn.query.getLimitTo() != -1) {
result.query.setLimitTo(qtn.query.getLimitTo());
qtn.query.setLimitTo(-1);
}
if (filter.isNeg()) {
((JoinNode) result.query).setLeftOuterJoin().setNotIn(true);
ItemFuncEqual joinFilter = FilterUtils.equal(leftColumn, rightJoinColumn);
((JoinNode) result.query).addJoinFilter(joinFilter);
result.filter = null;
} else {
Item joinFilter = FilterUtils.equal(leftColumn, rightJoinColumn);
result.query.query(joinFilter);
result.filter = joinFilter;
}
result.query.setUpFields();
return result;
}
use of com.actiontech.dble.plan.Order in project dble by actiontech.
the class PlanUtil method orderContains.
/**
* when order1contains order2,return true, order2's column start from order1's first column
*
* @param orders1 orders1
* @param orders2 orders2
* @return orderContains
*/
public static boolean orderContains(List<Order> orders1, List<Order> orders2) {
if (orders1.size() < orders2.size())
return false;
else {
for (int index = 0; index < orders2.size(); index++) {
Order order2 = orders2.get(index);
Order order1 = orders1.get(index);
if (!order2.equals(order1)) {
return false;
}
}
return true;
}
}
use of com.actiontech.dble.plan.Order in project dble by actiontech.
the class ToStringUtil method orderListString.
public static String orderListString(List<Order> orderList) {
if (orderList == null)
return "null";
if (orderList.isEmpty())
return " ";
boolean isFirst = true;
StringBuilder sb = new StringBuilder();
for (Order order : orderList) {
if (isFirst) {
isFirst = false;
} else {
sb.append(", ");
}
sb.append(orderString(order));
}
return sb.toString();
}
use of com.actiontech.dble.plan.Order in project dble by actiontech.
the class MySQLItemVisitor method endVisit.
@Override
public void endVisit(SQLAggregateExpr x) {
List<Item> args = visitExprList(x.getArguments());
String funcName = x.getMethodName().toUpperCase();
SQLAggregateOption option = x.getOption();
boolean isDistinct = option != null;
switch(funcName) {
case "MAX":
item = new ItemSumMax(args, false, null);
break;
case "MIN":
item = new ItemSumMin(args, false, null);
break;
case "SUM":
item = new ItemSumSum(args, isDistinct, false, null);
break;
case "AVG":
item = new ItemSumAvg(args, isDistinct, false, null);
break;
case "GROUP_CONCAT":
SQLOrderBy orderExpr = (SQLOrderBy) x.getAttribute(ItemFuncKeyWord.ORDER_BY);
List<Order> orderList = null;
if (orderExpr != null) {
orderList = new ArrayList<>();
for (SQLSelectOrderByItem orderItem : orderExpr.getItems()) {
Order order = new Order(getItem(orderItem.getExpr()), orderItem.getType());
orderList.add(order);
}
}
SQLCharExpr charExpr = (SQLCharExpr) x.getAttribute(ItemFuncKeyWord.SEPARATOR);
String separator = ",";
if (charExpr != null) {
separator = charExpr.getText();
}
item = new ItemFuncGroupConcat(args, isDistinct, orderList, separator, false, null);
break;
case "COUNT":
item = new ItemSumCount(args, isDistinct, false, null);
break;
case "STDDEV":
item = new ItemSumStd(args, 0, false, null);
break;
default:
throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not supported " + funcName);
}
}
use of com.actiontech.dble.plan.Order in project dble by actiontech.
the class PlanNode method setUpRefers.
// column refered start
public void setUpRefers(boolean isPushDownNode) {
sumFuncs.clear();
referContext.setPushDownNode(isPushDownNode);
// select
for (Item sel : columnsSelected) {
setUpItemRefer(sel);
}
if (type() == PlanNodeType.JOIN) {
JoinNode jn = (JoinNode) this;
if (!isPushDownNode) {
for (Item bf : jn.getJoinFilter()) setUpItemRefer(bf);
setUpItemRefer(jn.getOtherJoinOnFilter());
}
}
// where, pushdown node does 't need where
if (!isPushDownNode) {
setUpItemRefer(whereFilter);
}
// group by
for (Order groupBy : groups) {
setUpItemRefer(groupBy.getItem());
}
// having
setUpItemRefer(havingFilter);
// order by
for (Order orderBy : orderBys) {
setUpItemRefer(orderBy.getItem());
}
// make list
for (List<Item> selSet : columnsReferredCache.asMap().values()) {
columnsReferList.addAll(selSet);
}
}
Aggregations