Skip to content

Commit

Permalink
feat: support show status (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Jul 25, 2022
1 parent 82b5341 commit 30c4f09
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/executor/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (executor *RedirectExecutor) ExecutorComQuery(ctx *proto.Context) (proto.Re
case *ast.ShowStmt:
allowSchemaless := func(stmt *ast.ShowStmt) bool {
switch stmt.Tp {
case ast.ShowDatabases, ast.ShowVariables, ast.ShowTopology:
case ast.ShowDatabases, ast.ShowVariables, ast.ShowTopology, ast.ShowStatus:
return true
default:
return false
Expand Down
6 changes: 6 additions & 0 deletions pkg/runtime/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,12 @@ func (cc *convCtx) convShowStmt(node *ast.ShowStmt) Statement {
ret.like.String = like
}
return ret
case ast.ShowStatus:
ret := &ShowStatus{
baseShow: toBaseShow(),
global: node.GlobalScope,
}
return ret
default:
panic(fmt.Sprintf("unimplement: show type %v!", node.Tp))
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/runtime/ast/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
SQLTypeUnion // UNION
SQLTypeDropTrigger // DROP TRIGGER
SQLTypeCreateIndex // CREATE INDEX
SQLTypeShowStatus // SHOW STATUS
)

type RestoreFlag uint32
Expand Down Expand Up @@ -80,6 +81,7 @@ var _sqlTypeNames = [...]string{
SQLTypeUnion: "UNION",
SQLTypeDropTrigger: "DROP TRIGGER",
SQLTypeCreateIndex: "CREATE INDEX",
SQLTypeShowStatus: "SHOW STATUS",
}

// SQLType represents the type of SQL.
Expand Down
31 changes: 31 additions & 0 deletions pkg/runtime/ast/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,34 @@ func (s *ShowVariables) CntParams() int {
func (s *ShowVariables) Mode() SQLType {
return SQLTypeShowVariables
}

type ShowStatus struct {
*baseShow
flag showColumnsFlag
global bool
}

func (s *ShowStatus) Validate() error {
return nil
}

func (s *ShowStatus) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
sb.WriteString("SHOW ")

if s.global {
sb.WriteString(" GLOBAL ")
} else {
sb.WriteString(" SESSION ")
}
sb.WriteString(" STATUS ")

if err := s.baseShow.Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}

return nil
}

func (s *ShowStatus) Mode() SQLType {
return SQLTypeShowStatus
}
43 changes: 43 additions & 0 deletions pkg/runtime/optimize/dal/show_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package dal

import (
"context"
)

import (
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/runtime/ast"
"github.com/arana-db/arana/pkg/runtime/optimize"
"github.com/arana-db/arana/pkg/runtime/plan/dal"
)

func init() {
optimize.Register(ast.SQLTypeShowStatus, optimizeShowStatus)
}

func optimizeShowStatus(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
stmt := o.Stmt.(*ast.ShowStatus)

ret := dal.NewShowStatusPlan(stmt)
ret.BindArgs(o.Args)

return ret, nil
}
70 changes: 70 additions & 0 deletions pkg/runtime/plan/dal/show_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package dal

import (
"context"
"strings"
)

import (
"github.com/pkg/errors"
)

import (
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/runtime/ast"
"github.com/arana-db/arana/pkg/runtime/plan"
)

var _ proto.Plan = (*ShowOpenTablesPlan)(nil)

type ShowStatusPlan struct {
plan.BasePlan
Database string
stmt *ast.ShowStatus
}

func NewShowStatusPlan(stmt *ast.ShowStatus) *ShowStatusPlan {
return &ShowStatusPlan{stmt: stmt}
}

func (s *ShowStatusPlan) Type() proto.PlanType {
return proto.PlanTypeQuery
}

func (s *ShowStatusPlan) ExecIn(ctx context.Context, vConn proto.VConn) (proto.Result, error) {
var (
sb strings.Builder
args []int
)
ctx, span := plan.Tracer.Start(ctx, "ShowStatusPlan.ExecIn")
defer span.End()

if err := s.stmt.Restore(ast.RestoreDefault, &sb, &args); err != nil {
return nil, errors.Wrap(err, "failed to execute SHOW STATUS statement")
}

ret, err := vConn.Query(ctx, "", sb.String(), s.ToArgs(args)...)
if err != nil {
return nil, err
}

return ret, nil
}
33 changes: 33 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,36 @@ func (s *IntegrationSuite) TestHints() {
}

}

func (s *IntegrationSuite) TestShowStatus() {
var (
db = s.DB()
t = s.T()
)

type tt struct {
sql string
expectF func(t *testing.T, data [][]string) bool
}

for _, it := range []tt{
{"SHOW STATUS", func(t *testing.T, data [][]string) bool {
t.Logf("%+v", data)
return len(data) > 0
}},
{"SHOW STATUS LIKE 'Key%';", func(t *testing.T, data [][]string) bool {
t.Logf("%+v", data)
return len(data) >= 5
}},
} {
t.Run(it.sql, func(t *testing.T) {
// show table status
rows, err := db.Query(it.sql)
assert.NoError(t, err, "should query status successfully")
defer rows.Close()
data, _ := utils.PrintTable(rows)
assert.True(t, it.expectF(t, data))
})
}

}

0 comments on commit 30c4f09

Please sign in to comment.