diff --git a/src/main/java/com/actiontech/dble/backend/mysql/nio/handler/builder/sqlvisitor/MysqlVisitor.java b/src/main/java/com/actiontech/dble/backend/mysql/nio/handler/builder/sqlvisitor/MysqlVisitor.java index 6ac81ec6b1..58f4df5a02 100644 --- a/src/main/java/com/actiontech/dble/backend/mysql/nio/handler/builder/sqlvisitor/MysqlVisitor.java +++ b/src/main/java/com/actiontech/dble/backend/mysql/nio/handler/builder/sqlvisitor/MysqlVisitor.java @@ -53,16 +53,16 @@ public ReplaceableStringBuilder getSql() { public abstract void visit(); - /** - * @param tableNode - */ + protected void buildTableName(TableNode tableNode, StringBuilder sb) { sb.append(" `").append(tableNode.getPureName()).append("`"); String alias = tableNode.getAlias(); - if (alias != null) { - sb.append(" `").append(alias).append("`"); - } else if (tableNode.getSubAlias() != null) { - sb.append(" `").append(tableNode.getSubAlias()).append("`"); + if (tableNode.getSubAlias() != null) { + if (alias != null) { + sb.append(" `").append(alias).append("`"); + } else { + sb.append(" `").append(tableNode.getSubAlias()).append("`"); + } } List hintList = tableNode.getHintList(); if (hintList != null && !hintList.isEmpty()) { diff --git a/src/main/java/com/actiontech/dble/plan/optimizer/SubQueryProcessor.java b/src/main/java/com/actiontech/dble/plan/optimizer/SubQueryProcessor.java index d6a3a76598..01b6bcd601 100644 --- a/src/main/java/com/actiontech/dble/plan/optimizer/SubQueryProcessor.java +++ b/src/main/java/com/actiontech/dble/plan/optimizer/SubQueryProcessor.java @@ -40,16 +40,14 @@ public static PlanNode optimize(PlanNode qtn) { /** * find query node in qtn ,change to other 3 type node * - * @param qtn - * @return */ 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 cbptr = new BoolPtr(false); - PlanNode newChild = tryTransformerQuery(child, cbptr); - if (cbptr.get()) + BoolPtr boolPtr = new BoolPtr(false); + PlanNode newChild = tryTransformerQuery(child, boolPtr); + if (boolPtr.get()) childMerged = true; qtn.getChildren().set(index, newChild); } @@ -64,13 +62,11 @@ private static PlanNode tryTransformerQuery(PlanNode qtn, BoolPtr boolptr) { /** * transformerQuery * - * @param query - * @return */ private static PlanNode transformerQuery(QueryNode query, BoolPtr boolptr) { boolean canBeMerged = ViewUtil.canBeMerged(query.getChild()); if (canBeMerged) { - // merge viewnode's property to view's child + // merge view node's property to view's child PlanNode newNode = mergeNode(query, query.getChild()); boolptr.set(true); return newNode; @@ -83,18 +79,15 @@ private static PlanNode transformerQuery(QueryNode query, BoolPtr boolptr) { * merge parent's property to child,and return new child, * of course ,the child is canBeMerged * - * @param parent - * @param child - * @return */ private static PlanNode mergeNode(PlanNode parent, PlanNode child) { - final List newSels = mergeSelect(parent, child); + final List newSelects = mergeSelect(parent); mergeWhere(parent, child); mergeGroupBy(parent, child); mergeHaving(parent, child); mergeOrderBy(parent, child); mergeLimit(parent, child); - child.setColumnsSelected(newSels); + child.setColumnsSelected(newSelects); if (!StringUtils.isEmpty(parent.getAlias())) { child.setAlias(parent.getAlias()); } else if (!StringUtils.isEmpty(parent.getSubAlias())) { @@ -110,14 +103,12 @@ private static PlanNode mergeNode(PlanNode parent, PlanNode child) { * sql:select idd + 1 from v_t1 ==> select (id+1) + 1 from t1 tt1 order by * id+1 * - * @return child should contains new select's infos - * @notice */ - private static List mergeSelect(PlanNode parent, PlanNode child) { - List pSels = parent.getColumnsSelected(); - List cNewSels = new ArrayList<>(); - for (Item pSel : pSels) { + private static List mergeSelect(PlanNode parent) { + List pSelects = parent.getColumnsSelected(); + List cNewSelects = new ArrayList<>(); + for (Item pSel : pSelects) { Item pSel0 = PlanUtil.pushDownItem(parent, pSel, true); String selName = pSel.getAlias(); if (StringUtils.isEmpty(selName)) { @@ -128,9 +119,9 @@ private static List mergeSelect(PlanNode parent, PlanNode child) { selName = Item.FNAF + selName; } pSel0.setAlias(selName); - cNewSels.add(pSel0); + cNewSelects.add(pSel0); } - return cNewSels; + return cNewSelects; } private static void mergeWhere(PlanNode parent, PlanNode child) { diff --git a/src/main/java/com/actiontech/dble/plan/visitor/MySQLPlanNodeVisitor.java b/src/main/java/com/actiontech/dble/plan/visitor/MySQLPlanNodeVisitor.java index bb17b7b32d..423274d16d 100644 --- a/src/main/java/com/actiontech/dble/plan/visitor/MySQLPlanNodeVisitor.java +++ b/src/main/java/com/actiontech/dble/plan/visitor/MySQLPlanNodeVisitor.java @@ -237,6 +237,9 @@ public boolean visit(SQLSubqueryTableSource subQueryTables) { this.tableNode.setSubQuery(true); if (subQueryTables.getAlias() != null) { tableNode.alias(subQueryTables.getAlias()); + if (tableNode.getSubAlias() == null) { + tableNode.setSubAlias(tableNode.getAlias()); + } } return true; }