Skip to content

Commit

Permalink
feat: support show columns and show index in sharding mode (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
dk-lockdown authored Sep 16, 2022
1 parent dca3b50 commit 2c20554
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
49 changes: 49 additions & 0 deletions pkg/optimize/optimize_show_table_meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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/third_party/parser/ast"
)

func (o Optimizer) optimizeShowTableMeta(ctx context.Context, stmt *ast.ShowStmt, args []interface{}) (proto.Plan, error) {
if stmt.Tp != ast.ShowColumns && stmt.Tp != ast.ShowIndex {
return nil, errors.New("statement must be show columns stmt or show index stmt")
}

executor := o.executors[0]
tableName := stmt.Table.Name.O
if topology, exists := o.topologies[tableName]; exists {
stmt.Table.Name.O = topology.DBs[executor.GroupName()][0]
return &plan.ShowTableMetaPlan{
Stmt: stmt,
Args: args,
Executor: executor,
}, nil
}
return &plan.ShowTableMetaPlan{
Stmt: stmt,
Args: args,
Executor: executor,
}, nil
}
4 changes: 2 additions & 2 deletions pkg/optimize/optimize_show_table_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func (o Optimizer) optimizeShowTableStatus(ctx context.Context, stmt *ast.ShowSt
pattern := stmt.Pattern.Pattern.(*driver.ValueExpr)
tableName := pattern.GetDatumString()
if topology, exists = o.topologies[tableName]; !exists {
return &plan.ShowTableStatusPlan{
return &plan.ShowTableMetaPlan{
Stmt: stmt,
Args: args,
Executor: o.executors[0],
}, nil
} else {
table := topology.DBs[o.executors[0].GroupName()][0]
pattern.SetValue(table)
return &plan.ShowTableStatusPlan{
return &plan.ShowTableMetaPlan{
Stmt: stmt,
Args: args,
Executor: o.executors[0],
Expand Down
2 changes: 2 additions & 0 deletions pkg/optimize/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (o Optimizer) Optimize(ctx context.Context, stmt ast.StmtNode, args ...inte
switch t.Tp {
case ast.ShowTableStatus:
return o.optimizeShowTableStatus(ctx, t, args)
case ast.ShowColumns, ast.ShowIndex:
return o.optimizeShowTableMeta(ctx, t, args)
}
}
sqlText := proto.SqlText(ctx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/plan/show_table_status.go → pkg/plan/show_table_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (
"github.com/cectc/dbpack/third_party/parser/format"
)

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

func (p *ShowTableStatusPlan) Execute(ctx context.Context, _ ...*ast.TableOptimizerHint) (proto.Result, uint16, error) {
func (p *ShowTableMetaPlan) Execute(ctx context.Context, _ ...*ast.TableOptimizerHint) (proto.Result, uint16, error) {
var (
sb strings.Builder
sql string
Expand Down
31 changes: 31 additions & 0 deletions test/shd/sharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,37 @@ func (suite *_ShardingSuite) TestShowTableStatus() {
}
}

func (suite *_ShardingSuite) TestShowTableMeta() {
rows, err := suite.db.Query("SHOW COLUMNS FROM city")
if suite.NoErrorf(err, "show columns error: %v", err) {
var (
field, sqlType, null, key, defaultValue, extra interface{}
)
suite.T().Log("table city columns:")
for rows.Next() {
err := rows.Scan(&field, &sqlType, &null, &key, &defaultValue, &extra)
suite.NoError(err)
suite.T().Logf("%s %s %s %s %s %s", field, sqlType, null, key, defaultValue, extra)
}
}
rows, err = suite.db.Query("SHOW INDEX FROM city")
if suite.NoErrorf(err, "show index error: %v", err) {
var (
table, non_unique, key_name, seq_in_index, column_name, collation, cardinality, sub_part, packed, null,
index_type, comment, index_comment, visible, expression interface{}
)
suite.T().Log("table city index:")
for rows.Next() {
err := rows.Scan(&table, &non_unique, &key_name, &seq_in_index, &column_name, &collation, &cardinality,
&sub_part, &packed, &null, &index_type, &comment, &index_comment, &visible, &expression)
suite.NoError(err)
suite.T().Logf("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", table, non_unique,
key_name, seq_in_index, column_name, collation, cardinality, sub_part, packed, null, index_type,
comment, index_comment, visible, expression)
}
}
}

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

0 comments on commit 2c20554

Please sign in to comment.