Skip to content

Commit

Permalink
fix: show tables from database (#761)
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang authored Sep 16, 2023
1 parent 1d95f8c commit 2a31dc1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
24 changes: 8 additions & 16 deletions pkg/boot/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,15 @@ func (fp *discovery) Init(ctx context.Context) error {
uconfig.IsEnableLocalMathCompu(cfg.Spec.EnableLocalMathComputation)
fp.options = cfg

if err := config.Init(*fp.options.Config, fp.options.Spec.APIVersion); err != nil {
if err = config.Init(*fp.options.Config, fp.options.Spec.APIVersion); err != nil {
return err
}

fp.tenantOp, err = config.NewTenantOperator(config.GetStoreOperate())
if err != nil {
return err
}
if err := fp.initAllConfigCenter(); err != nil {
return err
}
return nil
return fp.initAllConfigCenter()
}

func (fp *discovery) InitTenant(tenant string) error {
Expand Down Expand Up @@ -121,11 +118,7 @@ func (fp *discovery) initAllConfigCenter() error {
}

func (fp *discovery) GetDataSourceCluster(ctx context.Context, tenant, cluster string) (*config.DataSourceCluster, error) {
dataSourceCluster, err := fp.loadCluster(tenant, cluster)
if err != nil {
return nil, err
}
return dataSourceCluster, nil
return fp.loadCluster(tenant, cluster)
}

func (fp *discovery) GetGroup(ctx context.Context, tenant, cluster, group string) (*config.Group, error) {
Expand Down Expand Up @@ -197,10 +190,9 @@ func (fp *discovery) InitTrace(ctx context.Context) error {
}

func (fp *discovery) InitSupervisor(ctx context.Context) error {
if fp.options.Supervisor == nil {
return nil
if fp.options.Supervisor != nil {
security.DefaultTenantManager().SetSupervisor(fp.options.Supervisor)
}
security.DefaultTenantManager().SetSupervisor(fp.options.Supervisor)
return nil
}

Expand Down Expand Up @@ -270,11 +262,11 @@ func (fp *discovery) ListTables(ctx context.Context, tenant, cluster string) ([]
return nil, err
}

rule := cfg.ShardingRule
shardingRule := cfg.ShardingRule
tables := make([]string, 0, 4)

for i := range rule.Tables {
db, tb, err := misc.ParseTable(rule.Tables[i].Name)
for i := range shardingRule.Tables {
db, tb, err := misc.ParseTable(shardingRule.Tables[i].Name)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func TestParseMore(t *testing.T) {
"show indexes from student where Column_name='a'",
"show full columns from student",
"show full columns from student like 'PRI'",
"show tables from employees",

"explain select * from student where uid = 1",
"explain delete from student where uid = 1",
Expand Down
53 changes: 39 additions & 14 deletions pkg/runtime/optimize/dal/show_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,57 @@ import (
import (
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/runtime/ast"
rcontext "github.com/arana-db/arana/pkg/runtime/context"
"github.com/arana-db/arana/pkg/runtime/namespace"
"github.com/arana-db/arana/pkg/runtime/optimize"
"github.com/arana-db/arana/pkg/runtime/plan/dal"
"github.com/arana-db/arana/pkg/security"
)

func init() {
optimize.Register(ast.SQLTypeShowTables, optimizeShowTables)
}

func optimizeShowTables(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
func optimizeShowTables(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
stmt := o.Stmt.(*ast.ShowTables)
var invertedIndex map[string]string
for logicalTable, v := range o.Rule.VTables() {
t := v.Topology()
t.Each(func(x, y int) bool {
if _, phyTable, ok := t.Render(x, y); ok {
if invertedIndex == nil {
invertedIndex = make(map[string]string)

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

var tables []string
if table, ok := stmt.BaseShowWithSingleColumn.BaseShow.Like(); ok {
var (
tenant = rcontext.Tenant(ctx)
clusters = security.DefaultTenantManager().GetClusters(tenant)
)
for _, cluster := range clusters {
if table == cluster {
groups := namespace.Load(tenant, cluster).DBGroups()
for i := 0; i < len(groups); i++ {
tables = append(tables, groups[i])
}
invertedIndex[phyTable] = logicalTable
break
}
return true
})
}
}

ret := dal.NewShowTablesPlan(stmt)
ret.BindArgs(o.Args)
ret.SetInvertedShards(invertedIndex)
if len(tables) != 0 {
ret.SetTables(tables)
} else {
var invertedIndex map[string]string
for logicalTable, v := range o.Rule.VTables() {
t := v.Topology()
t.Each(func(x, y int) bool {
if _, phyTable, ok := t.Render(x, y); ok {
if invertedIndex == nil {
invertedIndex = make(map[string]string)
}
invertedIndex[phyTable] = logicalTable
}
return true
})
}
ret.SetInvertedShards(invertedIndex)
}
return ret, nil
}
23 changes: 21 additions & 2 deletions pkg/runtime/plan/dal/show_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/arana-db/arana/pkg/dataset"
"github.com/arana-db/arana/pkg/mysql"
"github.com/arana-db/arana/pkg/mysql/rows"
"github.com/arana-db/arana/pkg/mysql/thead"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/resultx"
"github.com/arana-db/arana/pkg/runtime/ast"
Expand All @@ -45,6 +46,7 @@ type ShowTablesPlan struct {
plan.BasePlan
Database string
Stmt *ast.ShowTables
tables []string
invertedShards map[string]string // phy table name -> logical table name
}

Expand All @@ -60,14 +62,27 @@ func (st *ShowTablesPlan) Type() proto.PlanType {
}

func (st *ShowTablesPlan) ExecIn(ctx context.Context, conn proto.VConn) (proto.Result, error) {
ctx, span := plan.Tracer.Start(ctx, "ShowTablesPlan.ExecIn")
defer span.End()

if len(st.tables) != 0 {
var (
columns = thead.Database.ToFields()
ds = &dataset.VirtualDataset{Columns: columns}
)

for i := 0; i < len(st.tables); i++ {
ds.Rows = append(ds.Rows, rows.NewTextVirtualRow(columns, []proto.Value{proto.NewValueString(st.tables[i])}))
}
return resultx.New(resultx.WithDataset(ds)), nil
}

var (
sb strings.Builder
indexes []int
res proto.Result
err error
)
ctx, span := plan.Tracer.Start(ctx, "ShowTablesPlan.ExecIn")
defer span.End()

if err = st.Stmt.Restore(ast.RestoreDefault, &sb, &indexes); err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -147,3 +162,7 @@ func (st *ShowTablesPlan) SetDatabase(db string) {
func (st *ShowTablesPlan) SetInvertedShards(m map[string]string) {
st.invertedShards = m
}

func (st *ShowTablesPlan) SetTables(tables []string) {
st.tables = tables
}

0 comments on commit 2a31dc1

Please sign in to comment.