From 2c2ec8adc0c30695a33b642e0f7d430520b0147e Mon Sep 17 00:00:00 2001 From: maskwang Date: Sat, 29 Aug 2020 23:31:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=86=E8=A1=A8?= =?UTF-8?q?=E4=B8=AD=E5=88=AB=E5=90=8D=E5=AD=97=E6=AE=B5=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81group=20by=20,=20=E6=8A=A5=E5=AD=97=E6=AE=B5=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98=20#581?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/router/router.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/proxy/router/router.go b/proxy/router/router.go index 5be4df0c..79324639 100644 --- a/proxy/router/router.go +++ b/proxy/router/router.go @@ -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" ) @@ -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) { @@ -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 { @@ -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) } }