Skip to content

Commit

Permalink
查询 group by 别名.字段问题
Browse files Browse the repository at this point in the history
处理就是rrs 的 GroupByCols对象里面的通配符去掉
  • Loading branch information
noah committed Feb 19, 2019
1 parent 51dc4dc commit 2d80929
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
import io.mycat.route.parser.druid.MycatStatementParser;
import io.mycat.route.parser.druid.RouteCalculateUnit;
import io.mycat.route.parser.util.WildcardUtil;
import io.mycat.route.util.RouterUtil;
import io.mycat.sqlengine.mpp.ColumnRoutePair;
import io.mycat.sqlengine.mpp.HavingCols;
Expand Down Expand Up @@ -245,6 +246,7 @@ protected Map<String, String> parseAggGroupCommon(SchemaConfig schema, SQLStatem
if(mysqlSelectQuery.getGroupBy() != null) {
List<SQLExpr> groupByItems = mysqlSelectQuery.getGroupBy().getItems();
String[] groupByCols = buildGroupByCols(groupByItems,aliaColumns);
WildcardUtil.wildcards(groupByCols);
rrs.setGroupByCols(groupByCols);
rrs.setHavings(buildGroupByHaving(mysqlSelectQuery.getGroupBy().getHaving(),aliaColumns));
rrs.setHasAggrColumn(true);
Expand Down Expand Up @@ -502,7 +504,7 @@ private void repairExpr(SQLTableSource source, RouteResultsetNode node) throws S

SQLIdentifierExpr expr = (SQLIdentifierExpr) ((SQLExprTableSource) right).getExpr();
alias = Strings.isBlank(alias) ? expr.getName() : alias;
String subTableName = subTableNames.get(subTableName(expr.getName().toUpperCase()));
String subTableName = subTableNames.get(WildcardUtil.wildcard(expr.getName().toUpperCase()));
if (Strings.isNotBlank(subTableName)) {
String tableName = expr.getName();
alias = Strings.isBlank(alias) ? tableName : alias;
Expand All @@ -518,7 +520,7 @@ private void repairExpr(SQLTableSource source, RouteResultsetNode node) throws S

SQLIdentifierExpr expr = (SQLIdentifierExpr) exprTableSource.getExpr();
alias = Strings.isBlank(alias) ? expr.getName() : alias;
String subTableName = subTableNames.get(subTableName(expr.getName().toUpperCase()));
String subTableName = subTableNames.get(WildcardUtil.wildcard(expr.getName().toUpperCase()));
if (Strings.isNotBlank(subTableName)) {
String tableName = expr.getName();
alias = Strings.isBlank(alias) ? tableName : alias;
Expand All @@ -529,11 +531,6 @@ private void repairExpr(SQLTableSource source, RouteResultsetNode node) throws S
}
}

private String subTableName(String tableName) {
return tableName.replaceAll("`", "");
}


/**
* 获取所有的条件:因为可能被or语句拆分成多个RouteCalculateUnit,条件分散了
* @return
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/mycat/route/parser/util/WildcardUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.mycat.route.parser.util;

public class WildcardUtil {

public static String wildcard(String name) {
if (name.startsWith("`")) {
name = name.replaceAll("`", "");
} else if (name.startsWith("\"")) {
name = name.replaceAll("\"", "");
} else if (name.startsWith("'")) {
name = name.replaceAll("'", "");
}
return name;
}

public static void wildcards(String[] names) {
for (int i = 0; i < names.length; i++) {
names[i] = wildcard(names[i]);
}
}
}
11 changes: 11 additions & 0 deletions src/test/java/io/mycat/route/DruidMysqlRouteStrategyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,17 @@ public void testAlias() throws Exception {
Assert.assertEquals(1, rrs.getNodes().length);
}

public void testGroupAlias() throws Exception {

SchemaConfig schema = schemaMap.get("TESTDB");
RouteResultset rrs = null;
//别名大小写路由
String sql = "select * from travelrecord A group by a.id = 1;";
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null,
cachePool);
Assert.assertEquals(1, rrs.getNodes().length);
}

private String formatSql(String sql) {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatement();
Expand Down

0 comments on commit 2d80929

Please sign in to comment.