Skip to content

Commit

Permalink
feature: support show table status (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
dk-lockdown committed Sep 7, 2022
1 parent 6611c17 commit f896fc3
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 5 deletions.
15 changes: 11 additions & 4 deletions pkg/executor/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (executor *ShardingExecutor) ExecuteFieldList(ctx context.Context, table, w
}

func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql string) (result proto.Result, warn uint16, err error) {
proto.WithVariable(ctx, constant.TransactionTimeout, executor.config.TransactionTimeout)
spanCtx, span := tracing.GetTraceSpan(ctx, tracing.SHDComQuery)
defer span.End()

Expand Down Expand Up @@ -220,7 +221,15 @@ func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql stri
InsertId: 0,
}, 0, nil
case *ast.ShowStmt:
return executor.executors[0].Query(spanCtx, sql)
switch stmt.Tp {
case ast.ShowEngines, ast.ShowDatabases, ast.ShowCreateDatabase:
return executor.executors[0].Query(spanCtx, sql)
}
plan, err = executor.optimizer.Optimize(spanCtx, queryStmt)
if err != nil {
return nil, 0, err
}
return plan.Execute(spanCtx)
case *ast.BeginStmt:
tx := group.NewComplexTx(executor.optimizer)
executor.localTransactionMap.Store(connectionID, tx)
Expand Down Expand Up @@ -267,7 +276,6 @@ func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql stri
if err != nil {
return nil, 0, err
}
proto.WithVariable(spanCtx, constant.TransactionTimeout, executor.config.TransactionTimeout)
return plan.Execute(spanCtx)
default:
txi, ok := executor.localTransactionMap.Load(connectionID)
Expand All @@ -279,13 +287,13 @@ func (executor *ShardingExecutor) ExecutorComQuery(ctx context.Context, sql stri
if err != nil {
return nil, 0, err
}
proto.WithVariable(spanCtx, constant.TransactionTimeout, executor.config.TransactionTimeout)
return plan.Execute(spanCtx)
}
}

func (executor *ShardingExecutor) ExecutorComStmtExecute(
ctx context.Context, stmt *proto.Stmt) (result proto.Result, warns uint16, err error) {
proto.WithVariable(ctx, constant.TransactionTimeout, executor.config.TransactionTimeout)
spanCtx, span := tracing.GetTraceSpan(ctx, tracing.SHDComStmtExecute)
defer span.End()

Expand Down Expand Up @@ -324,7 +332,6 @@ func (executor *ShardingExecutor) ExecutorComStmtExecute(
if err != nil {
return nil, 0, err
}
proto.WithVariable(spanCtx, constant.TransactionTimeout, executor.config.TransactionTimeout)
return plan.Execute(spanCtx)
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/optimize/optimize_show_table_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2022 CECTC, Inc.
*
* Licensed 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 optimize

import (
"context"

"github.com/pkg/errors"

"github.com/cectc/dbpack/pkg/plan"
"github.com/cectc/dbpack/pkg/proto"
"github.com/cectc/dbpack/pkg/topo"
"github.com/cectc/dbpack/third_party/parser/ast"
driver "github.com/cectc/dbpack/third_party/types/parser_driver"
)

func (o Optimizer) optimizeShowTableStatus(ctx context.Context, stmt *ast.ShowStmt, args []interface{}) (proto.Plan, error) {
var (
topology *topo.Topology
exists bool
)

if stmt.Tp != ast.ShowTableStatus {
return nil, errors.New("statement must be show table status stmt")
}

pattern := stmt.Pattern.Pattern.(*driver.ValueExpr)
tableName := pattern.GetDatumString()
if topology, exists = o.topologies[tableName]; !exists {
return &plan.ShowTableStatusPlan{
Stmt: stmt,
Args: args,
Executor: o.executors[0],
}, nil
} else {
table := topology.DBs[o.executors[0].GroupName()][0]
pattern.SetValue(table)
return &plan.ShowTableStatusPlan{
Stmt: stmt,
Args: args,
Executor: o.executors[0],
}, nil
}
}
5 changes: 5 additions & 0 deletions pkg/optimize/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (o Optimizer) Optimize(ctx context.Context, stmt ast.StmtNode, args ...inte
return o.optimizeDelete(ctx, t, args)
case *ast.UpdateStmt:
return o.optimizeUpdate(ctx, t, args)
case *ast.ShowStmt:
switch t.Tp {
case ast.ShowTableStatus:
return o.optimizeShowTableStatus(ctx, t, args)
}
}
sqlText := proto.SqlText(ctx)
return nil, errors.Errorf("unsupported statement type, sql: %s", sqlText)
Expand Down
2 changes: 1 addition & 1 deletion pkg/plan/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *QueryDirectlyPlan) Execute(ctx context.Context, hints ...*ast.TableOpti
sql string
err error
)
restoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags|format.RestoreStringWithoutDefaultCharset, &sb)
restoreCtx := format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)
if err = p.Stmt.Restore(restoreCtx); err != nil {
return nil, 0, errors.WithStack(err)
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/plan/show_table_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 CECTC, Inc.
*
* Licensed 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 plan

import (
"context"
"strings"

"github.com/pkg/errors"

"github.com/cectc/dbpack/pkg/constant"
"github.com/cectc/dbpack/pkg/proto"
"github.com/cectc/dbpack/third_party/parser/ast"
"github.com/cectc/dbpack/third_party/parser/format"
)

type ShowTableStatusPlan struct {
Stmt *ast.ShowStmt
Args []interface{}
Executor proto.DBGroupExecutor
}

func (p *ShowTableStatusPlan) Execute(ctx context.Context, _ ...*ast.TableOptimizerHint) (proto.Result, uint16, error) {
var (
sb strings.Builder
sql string
err error
)
restoreCtx := format.NewRestoreCtx(constant.DBPackRestoreFormat, &sb)
if err = p.Stmt.Restore(restoreCtx); err != nil {
return nil, 0, errors.WithStack(err)
}
sql = sb.String()
commandType := proto.CommandType(ctx)
switch commandType {
case constant.ComQuery:
return p.Executor.Query(ctx, sql)
case constant.ComStmtExecute:
return p.Executor.PrepareQuery(ctx, sql, p.Args...)
default:
return nil, 0, nil
}
}
37 changes: 37 additions & 0 deletions test/shd/sharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,43 @@ func (suite *_ShardingSuite) TestShowCreateDatabase() {
}
}

func (suite *_ShardingSuite) TestShowTableStatus() {
rows, err := suite.db.Query("SHOW TABLE STATUS LIKE 'city'")
if suite.NoErrorf(err, "show table status error: %v", err) {
var (
name, engine, version, rowFormat, rowNum, avgRowLength, dataLength, maxDataLength, indexLength, dataFree,
autoIncrement, createTime, updateTime, checkTime, collation, checkSum, createOption, comment interface{}
)
for rows.Next() {
err := rows.Scan(&name, &engine, &version, &rowFormat, &rowNum, &avgRowLength, &dataLength, &maxDataLength,
&indexLength, &dataFree, &autoIncrement, &createTime, &updateTime, &checkTime, &collation, &checkSum,
&createOption, &comment)
suite.NoError(err)
suite.T().Log("table city:")
suite.T().Logf("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
name, engine, version, rowFormat, rowNum, avgRowLength, dataLength, maxDataLength, indexLength, dataFree,
autoIncrement, createTime, updateTime, checkTime, collation, checkSum, createOption, comment)
}
}
rows, err = suite.db.Query("SHOW TABLE STATUS LIKE 'countrylanguage'")
if suite.NoErrorf(err, "show table status error: %v", err) {
var (
name, engine, version, rowFormat, rowNum, avgRowLength, dataLength, maxDataLength, indexLength, dataFree,
autoIncrement, createTime, updateTime, checkTime, collation, checkSum, createOption, comment interface{}
)
for rows.Next() {
err := rows.Scan(&name, &engine, &version, &rowFormat, &rowNum, &avgRowLength, &dataLength, &maxDataLength,
&indexLength, &dataFree, &autoIncrement, &createTime, &updateTime, &checkTime, &collation, &checkSum,
&createOption, &comment)
suite.NoError(err)
suite.T().Log("table countrylanguage:")
suite.T().Logf("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
name, engine, version, rowFormat, rowNum, avgRowLength, dataLength, maxDataLength, indexLength, dataFree,
autoIncrement, createTime, updateTime, checkTime, collation, checkSum, createOption, comment)
}
}
}

func (suite *_ShardingSuite) TestDeleteCity() {
result, err := suite.db.Exec(deleteCity, 10, 20)
suite.Assert().Nil(err)
Expand Down

0 comments on commit f896fc3

Please sign in to comment.