Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复分表中别名字段不支持group by , 报字段不存在问题 #581 #582

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion proxy/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/flike/kingshard/config"
"github.com/flike/kingshard/core/errors"
"github.com/flike/kingshard/core/golog"
"github.com/flike/kingshard/core/hack"
"github.com/flike/kingshard/sqlparser"
)

Expand Down Expand Up @@ -537,7 +538,10 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
node.Distinct,
)

var prefix string
var (
prefix string
asColNames map[string]string
)
//rewrite select expr
for _, expr := range node.SelectExprs {
switch v := expr.(type) {
Expand Down Expand Up @@ -568,6 +572,11 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
}
//if expr has as
if v.As != nil {
if asColNames == nil {
asColNames = make(map[string]string)
}
asColNames[hack.String(v.As)] = hack.String(colName.Name)

buf.Fprintf(" as %s", v.As)
}
} else {
Expand All @@ -582,6 +591,14 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
if len(node.GroupBy) != 0 {
prefix = ","
for _, n := range node.GroupBy {
// use real column name replace as name
if colName, ok := n.(*sqlparser.ColName); ok {
if v, ok := asColNames[hack.String(colName.Name)]; ok {
buf.Fprintf("%s%s", prefix, v)
continue
}
}

buf.Fprintf("%s%v", prefix, n)
}
}
Expand Down