use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat_plus by coderczp.
the class DruidMycatRouteStrategy method routeDisTable.
private RouteResultset routeDisTable(SQLStatement statement, RouteResultset rrs) throws SQLSyntaxErrorException {
SQLTableSource tableSource = null;
if (statement instanceof SQLInsertStatement) {
SQLInsertStatement insertStatement = (SQLInsertStatement) statement;
tableSource = insertStatement.getTableSource();
for (RouteResultsetNode node : rrs.getNodes()) {
SQLExprTableSource from2 = getDisTable(tableSource, node);
insertStatement.setTableSource(from2);
node.setStatement(insertStatement.toString());
}
}
if (statement instanceof SQLDeleteStatement) {
SQLDeleteStatement deleteStatement = (SQLDeleteStatement) statement;
tableSource = deleteStatement.getTableSource();
for (RouteResultsetNode node : rrs.getNodes()) {
SQLExprTableSource from2 = getDisTable(tableSource, node);
deleteStatement.setTableSource(from2);
node.setStatement(deleteStatement.toString());
}
}
if (statement instanceof SQLUpdateStatement) {
SQLUpdateStatement updateStatement = (SQLUpdateStatement) statement;
tableSource = updateStatement.getTableSource();
for (RouteResultsetNode node : rrs.getNodes()) {
SQLExprTableSource from2 = getDisTable(tableSource, node);
updateStatement.setTableSource(from2);
node.setStatement(updateStatement.toString());
}
}
return rrs;
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat_plus by coderczp.
the class DruidMycatRouteStrategy method routeNormalSqlWithAST.
@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema, String stmt, RouteResultset rrs, String charset, LayerCachePool cachePool, int sqlType, ServerConnection sc) throws SQLNonTransientException {
/**
* 只有mysql时只支持mysql语法
*/
SQLStatementParser parser = null;
if (schema.isNeedSupportMultiDBType()) {
parser = new MycatStatementParser(stmt);
} else {
parser = new MySqlStatementParser(stmt);
}
MycatSchemaStatVisitor visitor = null;
SQLStatement statement;
/**
* 解析出现问题统一抛SQL语法错误
*/
try {
statement = parser.parseStatement();
visitor = new MycatSchemaStatVisitor();
} catch (Exception t) {
LOGGER.error("DruidMycatRouteStrategyError", t);
throw new SQLSyntaxErrorException(t);
}
/**
* 检验unsupported statement
*/
checkUnSupportedStatement(statement);
DruidParser druidParser = DruidParserFactory.create(schema, statement, visitor);
druidParser.parser(schema, rrs, statement, stmt, cachePool, visitor);
DruidShardingParseInfo ctx = druidParser.getCtx();
rrs.setTables(ctx.getTables());
if (visitor.isSubqueryRelationOr()) {
String err = "In subQuery,the or condition is not supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
/* 按照以下情况路由
1.2.1 可以直接路由.
1.2.2 两个表夸库join的sql.调用calat
1.2.3 需要先执行subquery 的sql.把subquery拆分出来.获取结果后,与outerquery
*/
// add huangyiming 分片规则不一样的且表中带查询条件的则走Catlet
List<String> tables = ctx.getTables();
SchemaConfig schemaConf = MycatServer.getInstance().getConfig().getSchemas().get(schema.getName());
int index = 0;
RuleConfig firstRule = null;
boolean directRoute = true;
Set<String> firstDataNodes = new HashSet<String>();
Map<String, TableConfig> tconfigs = schemaConf == null ? null : schemaConf.getTables();
Map<String, RuleConfig> rulemap = new HashMap<>();
if (tconfigs != null) {
for (String tableName : tables) {
TableConfig tc = tconfigs.get(tableName);
if (tc == null) {
// add 别名中取
Map<String, String> tableAliasMap = ctx.getTableAliasMap();
if (tableAliasMap != null && tableAliasMap.get(tableName) != null) {
tc = schemaConf.getTables().get(tableAliasMap.get(tableName));
}
}
if (index == 0) {
if (tc != null) {
firstRule = tc.getRule();
// 没有指定分片规则时,不做处理
if (firstRule == null) {
continue;
}
firstDataNodes.addAll(tc.getDataNodes());
rulemap.put(tc.getName(), firstRule);
}
} else {
if (tc != null) {
// ER关系表的时候是可能存在字表中没有tablerule的情况,所以加上判断
RuleConfig ruleCfg = tc.getRule();
if (ruleCfg == null) {
// 没有指定分片规则时,不做处理
continue;
}
Set<String> dataNodes = new HashSet<String>();
dataNodes.addAll(tc.getDataNodes());
rulemap.put(tc.getName(), ruleCfg);
// 如果匹配规则不相同或者分片的datanode不相同则需要走子查询处理
if (firstRule != null && ((ruleCfg != null && !ruleCfg.getRuleAlgorithm().equals(firstRule.getRuleAlgorithm())) || (!dataNodes.equals(firstDataNodes)))) {
directRoute = false;
break;
}
}
}
index++;
}
}
RouteResultset rrsResult = rrs;
if (directRoute) {
// 直接路由
if (!RouterUtil.isAllGlobalTable(ctx, schemaConf)) {
if (rulemap.size() > 1 && !checkRuleField(rulemap, visitor)) {
String err = "In case of slice table,there is no rule field in the relationship condition!";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
}
rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
} else {
int subQuerySize = visitor.getSubQuerys().size();
if (subQuerySize == 0 && ctx.getTables().size() == 2) {
// 两表关联,考虑使用catlet
if (!visitor.getRelationships().isEmpty()) {
rrs.setCacheAble(false);
rrs.setFinishedRoute(true);
rrsResult = catletRoute(schema, ctx.getSql(), charset, sc);
} else {
rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
}
} else if (subQuerySize == 1) {
// 只涉及一张表的子查询,使用 MiddlerResultHandler 获取中间结果后,改写原有 sql 继续执行 TODO 后期可能会考虑多个子查询的情况.
SQLSelect sqlselect = visitor.getSubQuerys().iterator().next();
if (!visitor.getRelationships().isEmpty()) {
// 当 inner query 和 outer query 有关联条件时,暂不支持
String err = "In case of slice table,sql have different rules,the relationship condition is not supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
} else {
SQLSelectQuery sqlSelectQuery = sqlselect.getQuery();
if (((MySqlSelectQueryBlock) sqlSelectQuery).getFrom() instanceof SQLExprTableSource) {
rrs.setCacheAble(false);
rrs.setFinishedRoute(true);
rrsResult = middlerResultRoute(schema, charset, sqlselect, sqlType, statement, sc);
}
}
} else if (subQuerySize >= 2) {
String err = "In case of slice table,sql has different rules,currently only one subQuery is supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
}
return rrsResult;
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat-Server by MyCATApache.
the class DruidSelectParser method changeSql.
/**
* 改写sql:需要加limit的加上
*/
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, LayerCachePool cachePool) throws SQLNonTransientException {
tryRoute(schema, rrs, cachePool);
rrs.copyLimitToNodes();
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
int limitStart = 0;
int limitSize = schema.getDefaultMaxLimit();
// clear group having
SQLSelectGroupByClause groupByClause = mysqlSelectQuery.getGroupBy();
// Modified by winbill, 20160614, do NOT include having clause when routing to multiple nodes
if (groupByClause != null && groupByClause.getHaving() != null && isRoutMultiNode(schema, rrs)) {
groupByClause.setHaving(null);
}
Map<String, Map<String, Set<ColumnRoutePair>>> allConditions = getAllConditions();
boolean isNeedAddLimit = isNeedAddLimit(schema, rrs, mysqlSelectQuery, allConditions);
if (isNeedAddLimit) {
Limit limit = new Limit();
limit.setRowCount(new SQLIntegerExpr(limitSize));
mysqlSelectQuery.setLimit(limit);
rrs.setLimitSize(limitSize);
String sql = getSql(rrs, stmt, isNeedAddLimit);
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitSize, true);
}
Limit limit = mysqlSelectQuery.getLimit();
if (limit != null && !isNeedAddLimit) {
SQLIntegerExpr offset = (SQLIntegerExpr) limit.getOffset();
SQLIntegerExpr count = (SQLIntegerExpr) limit.getRowCount();
if (offset != null) {
limitStart = offset.getNumber().intValue();
rrs.setLimitStart(limitStart);
}
if (count != null) {
limitSize = count.getNumber().intValue();
rrs.setLimitSize(limitSize);
}
if (isNeedChangeLimit(rrs)) {
Limit changedLimit = new Limit();
changedLimit.setRowCount(new SQLIntegerExpr(limitStart + limitSize));
if (offset != null) {
if (limitStart < 0) {
String msg = "You have an error in your SQL syntax; check the manual that " + "corresponds to your MySQL server version for the right syntax to use near '" + limitStart + "'";
throw new SQLNonTransientException(ErrorCode.ER_PARSE_ERROR + " - " + msg);
} else {
changedLimit.setOffset(new SQLIntegerExpr(0));
}
}
mysqlSelectQuery.setLimit(changedLimit);
String sql = getSql(rrs, stmt, isNeedAddLimit);
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitStart + limitSize, true);
// 设置改写后的sql
ctx.setSql(sql);
} else {
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), getCtx().getSql(), rrs.getLimitStart(), rrs.getLimitSize(), true);
// ctx.setSql(nativeSql);
}
}
if (rrs.isDistTable()) {
SQLTableSource from = mysqlSelectQuery.getFrom();
for (RouteResultsetNode node : rrs.getNodes()) {
SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
sqlIdentifierExpr.setParent(from);
sqlIdentifierExpr.setName(node.getSubTableName());
SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
from2.setAlias(from.getAlias());
mysqlSelectQuery.setFrom(from2);
node.setStatement(stmt.toString());
}
}
rrs.setCacheAble(isNeedCache(schema, rrs, mysqlSelectQuery, allConditions));
}
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat-Server by MyCATApache.
the class MycatSchemaStatVisitor method visit.
// DUAL
public boolean visit(MySqlDeleteStatement x) {
setAliasMap();
setMode(x, Mode.Delete);
accept(x.getFrom());
accept(x.getUsing());
x.getTableSource().accept(this);
if (x.getTableSource() instanceof SQLExprTableSource) {
SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr();
String ident = tableName.toString();
setCurrentTable(x, ident);
TableStat stat = this.getTableStat(ident, ident);
stat.incrementDeleteCount();
}
accept(x.getWhere());
accept(x.getOrderBy());
accept(x.getLimit());
return false;
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat-Server by MyCATApache.
the class DruidMycatRouteStrategy method routeNormalSqlWithAST.
@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema, String stmt, RouteResultset rrs, String charset, LayerCachePool cachePool, int sqlType, ServerConnection sc) throws SQLNonTransientException {
/**
* 只有mysql时只支持mysql语法
*/
SQLStatementParser parser = null;
if (schema.isNeedSupportMultiDBType()) {
parser = new MycatStatementParser(stmt);
} else {
parser = new MySqlStatementParser(stmt);
}
MycatSchemaStatVisitor visitor = null;
SQLStatement statement;
/**
* 解析出现问题统一抛SQL语法错误
*/
try {
statement = parser.parseStatement();
visitor = new MycatSchemaStatVisitor();
} catch (Exception t) {
LOGGER.error("DruidMycatRouteStrategyError", t);
throw new SQLSyntaxErrorException(t);
}
/**
* 检验unsupported statement
*/
checkUnSupportedStatement(statement);
DruidParser druidParser = DruidParserFactory.create(schema, statement, visitor);
druidParser.parser(schema, rrs, statement, stmt, cachePool, visitor);
DruidShardingParseInfo ctx = druidParser.getCtx();
rrs.setTables(ctx.getTables());
if (visitor.isSubqueryRelationOr()) {
String err = "In subQuery,the or condition is not supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
/* 按照以下情况路由
1.2.1 可以直接路由.
1.2.2 两个表夸库join的sql.调用calat
1.2.3 需要先执行subquery 的sql.把subquery拆分出来.获取结果后,与outerquery
*/
// add huangyiming 分片规则不一样的且表中带查询条件的则走Catlet
List<String> tables = ctx.getTables();
SchemaConfig schemaConf = MycatServer.getInstance().getConfig().getSchemas().get(schema.getName());
int index = 0;
RuleConfig firstRule = null;
boolean directRoute = true;
Set<String> firstDataNodes = new HashSet<String>();
Map<String, TableConfig> tconfigs = schemaConf == null ? null : schemaConf.getTables();
Map<String, RuleConfig> rulemap = new HashMap<>();
if (tconfigs != null) {
for (String tableName : tables) {
TableConfig tc = tconfigs.get(tableName);
if (tc == null) {
// add 别名中取
Map<String, String> tableAliasMap = ctx.getTableAliasMap();
if (tableAliasMap != null && tableAliasMap.get(tableName) != null) {
tc = schemaConf.getTables().get(tableAliasMap.get(tableName));
}
}
if (index == 0) {
if (tc != null) {
firstRule = tc.getRule();
// 没有指定分片规则时,不做处理
if (firstRule == null) {
continue;
}
firstDataNodes.addAll(tc.getDataNodes());
rulemap.put(tc.getName(), firstRule);
}
} else {
if (tc != null) {
// ER关系表的时候是可能存在字表中没有tablerule的情况,所以加上判断
RuleConfig ruleCfg = tc.getRule();
if (ruleCfg == null) {
// 没有指定分片规则时,不做处理
continue;
}
Set<String> dataNodes = new HashSet<String>();
dataNodes.addAll(tc.getDataNodes());
rulemap.put(tc.getName(), ruleCfg);
// 如果匹配规则不相同或者分片的datanode不相同则需要走子查询处理
if (firstRule != null && ((ruleCfg != null && !ruleCfg.getRuleAlgorithm().equals(firstRule.getRuleAlgorithm())) || (!dataNodes.equals(firstDataNodes)))) {
directRoute = false;
break;
}
}
}
index++;
}
}
RouteResultset rrsResult = rrs;
if (directRoute) {
// 直接路由
if (!RouterUtil.isAllGlobalTable(ctx, schemaConf)) {
if (rulemap.size() > 1 && !checkRuleField(rulemap, visitor)) {
String err = "In case of slice table,there is no rule field in the relationship condition!";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
}
rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
} else {
int subQuerySize = visitor.getSubQuerys().size();
if (subQuerySize == 0 && ctx.getTables().size() == 2) {
// 两表关联,考虑使用catlet
if (!visitor.getRelationships().isEmpty()) {
rrs.setCacheAble(false);
rrs.setFinishedRoute(true);
rrsResult = catletRoute(schema, ctx.getSql(), charset, sc);
} else {
rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
}
} else if (subQuerySize == 1) {
// 只涉及一张表的子查询,使用 MiddlerResultHandler 获取中间结果后,改写原有 sql 继续执行 TODO 后期可能会考虑多个子查询的情况.
SQLSelect sqlselect = visitor.getSubQuerys().iterator().next();
if (!visitor.getRelationships().isEmpty()) {
// 当 inner query 和 outer query 有关联条件时,暂不支持
String err = "In case of slice table,sql have different rules,the relationship condition is not supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
} else {
SQLSelectQuery sqlSelectQuery = sqlselect.getQuery();
if (((MySqlSelectQueryBlock) sqlSelectQuery).getFrom() instanceof SQLExprTableSource) {
rrs.setCacheAble(false);
rrs.setFinishedRoute(true);
rrsResult = middlerResultRoute(schema, charset, sqlselect, sqlType, statement, sc);
}
}
} else if (subQuerySize >= 2) {
String err = "In case of slice table,sql has different rules,currently only one subQuery is supported.";
LOGGER.error(err);
throw new SQLSyntaxErrorException(err);
}
}
return rrsResult;
}
Aggregations