From 2a31dc15040f197bd6f632ac85c5de94d7ed4ad3 Mon Sep 17 00:00:00 2001 From: baerwang <52104949+baerwang@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:56:07 +0800 Subject: [PATCH] fix: show tables from database (#761) --- pkg/boot/discovery.go | 24 ++++------- pkg/runtime/ast/ast_test.go | 1 + pkg/runtime/optimize/dal/show_tables.go | 53 ++++++++++++++++++------- pkg/runtime/plan/dal/show_tables.go | 23 ++++++++++- 4 files changed, 69 insertions(+), 32 deletions(-) diff --git a/pkg/boot/discovery.go b/pkg/boot/discovery.go index 92a6e564..0ba794b6 100644 --- a/pkg/boot/discovery.go +++ b/pkg/boot/discovery.go @@ -77,7 +77,7 @@ 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 } @@ -85,10 +85,7 @@ func (fp *discovery) Init(ctx context.Context) error { 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 { @@ -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) { @@ -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 } @@ -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 } diff --git a/pkg/runtime/ast/ast_test.go b/pkg/runtime/ast/ast_test.go index 44bdbf91..1827f299 100644 --- a/pkg/runtime/ast/ast_test.go +++ b/pkg/runtime/ast/ast_test.go @@ -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", diff --git a/pkg/runtime/optimize/dal/show_tables.go b/pkg/runtime/optimize/dal/show_tables.go index 778a7f78..f08b136c 100644 --- a/pkg/runtime/optimize/dal/show_tables.go +++ b/pkg/runtime/optimize/dal/show_tables.go @@ -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 } diff --git a/pkg/runtime/plan/dal/show_tables.go b/pkg/runtime/plan/dal/show_tables.go index c7b0b187..89492ed5 100644 --- a/pkg/runtime/plan/dal/show_tables.go +++ b/pkg/runtime/plan/dal/show_tables.go @@ -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" @@ -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 } @@ -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) @@ -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 +}