Skip to content

Commit

Permalink
Merge pull request #211 from XiaoMi/dev
Browse files Browse the repository at this point in the history
修复分库分表 select xxx from tb as t force index(u_key) where t.id=x 语法错误
  • Loading branch information
xiyangxixian authored Apr 6, 2022
2 parents 00036fb + 5a5fcb4 commit 22a72b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion proxy/plan/decorator_table_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type TableNameDecorator struct {
origin *ast.TableName
rule router.Rule
result *RouteResult
Alias string
}

// NeedCreateTableNameDecoratorWithoutAlias check if TableName without alias needs decorate
Expand Down Expand Up @@ -111,7 +112,10 @@ func (t *TableNameDecorator) Restore(ctx *format.RestoreCtx) error {
} else {
ctx.WriteName(fmt.Sprintf("%s_%04d", t.origin.Name.String(), tableIndex))
}

if t.Alias != "" {
ctx.WriteKeyWord(" AS ")
ctx.WriteName(t.Alias)
}
for _, value := range t.origin.IndexHints {
ctx.WritePlain(" ")
if err := value.Restore(ctx); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions proxy/plan/plan_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ func HandleSelectStmt(p *SelectPlan, stmt *ast.SelectStmt) error {
return fmt.Errorf("handle Hint error: %v", err)
}


//如果是多节点执行,特殊处理groupby orderby having limit
//如果是多节点执行,特殊处理groupby orderby having limit
if !p.isExecOnSingleNode() {
// group by的处理必须在table处理之后
if err := handleGroupBy(p, stmt); err != nil {
Expand All @@ -208,8 +207,6 @@ func HandleSelectStmt(p *SelectPlan, stmt *ast.SelectStmt) error {
p.columnCount = len(stmt.Fields.Fields)
}



if err := handleHaving(p, stmt); err != nil {
return fmt.Errorf("handle Having error: %v", err)
}
Expand Down Expand Up @@ -496,10 +493,13 @@ func rewriteTableNameInTableSource(p *TableAliasStmtInfo, tableSource *ast.Table

// 这是一个分片表或关联表, 创建一个TableName的装饰器, 并替换原有节点
d, err := CreateTableNameDecorator(tableName, rule, p.GetRouteResult())
d.Alias = tableSource.AsName.String()
if err != nil {
return fmt.Errorf("create TableNameDecorator error: %v", err)
}
tableSource.Source = d
tableSource.AsName.L = ""
tableSource.AsName.O = ""
return nil
}

Expand Down
5 changes: 4 additions & 1 deletion proxy/plan/plan_select_subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *SubqueryColumnNameRewriteVisitor) Leave(n ast.Node) (node ast.Node, ok
if rule == nil || rule.GetType() == router.GlobalTableRuleType {
return n, true
}

decorator := CreateColumnNameExprDecorator(field, rule, false, s.info.GetRouteResult())
return decorator, true
}
Expand Down Expand Up @@ -178,9 +178,12 @@ func rewriteSubqueryTableNameInTableSource(p *TableAliasStmtInfo, tableSource *a

// 这是一个分片表或关联表, 创建一个TableName的装饰器, 并替换原有节点
d, err := CreateTableNameDecorator(tableName, rule, p.GetRouteResult())
d.Alias = tableSource.AsName.String()
if err != nil {
return fmt.Errorf("create TableNameDecorator error: %v", err)
}
tableSource.Source = d
tableSource.AsName.L = ""
tableSource.AsName.O = ""
return nil
}

0 comments on commit 22a72b9

Please sign in to comment.