From 60b8709c36ca4b523f396486c3c8d6d4f15fa3a2 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Thu, 2 Jun 2022 18:13:36 +0800 Subject: [PATCH 1/8] compatible show tables/databases stmt with mysql --- cmd/explaintest/r/show.result | 13 +++++++++++++ cmd/explaintest/t/show.test | 5 +++++ planner/core/planbuilder.go | 36 ++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 cmd/explaintest/r/show.result create mode 100644 cmd/explaintest/t/show.test diff --git a/cmd/explaintest/r/show.result b/cmd/explaintest/r/show.result new file mode 100644 index 0000000000000..3332d2d8d6bc3 --- /dev/null +++ b/cmd/explaintest/r/show.result @@ -0,0 +1,13 @@ +show tables; +Tables_in_test +show databases; +Database +INFORMATION_SCHEMA +METRICS_SCHEMA +PERFORMANCE_SCHEMA +mysql +test +show tables like '%xx'; +Tables_in_test (%xx) +show databases like '%xx'; +Database (%xx) diff --git a/cmd/explaintest/t/show.test b/cmd/explaintest/t/show.test new file mode 100644 index 0000000000000..29fb33d69929f --- /dev/null +++ b/cmd/explaintest/t/show.test @@ -0,0 +1,5 @@ +# test show output field name +show tables; +show databases; +show tables like '%xx'; +show databases like '%xx'; diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 7a11f48fa5812..a714eb0d84265 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -2937,12 +2937,8 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, if (show.Tp == ast.ShowTables || show.Tp == ast.ShowTableStatus) && p.DBName == "" { return nil, ErrNoDB } - extractor := newShowBaseExtractor(*show) - if extractor.Extract() { - p.Extractor = extractor - // Avoid building Selection. - show.Pattern = nil - } + p.Extractor = newShowBaseExtractor(*show) + p.Extractor.Extract() case ast.ShowCreateTable, ast.ShowCreateSequence, ast.ShowPlacementForTable, ast.ShowPlacementForPartition: var err error if table, err := b.is.TableByName(show.Table.Schema, show.Table.Name); err == nil { @@ -3012,7 +3008,8 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, var err error var np LogicalPlan np = p - if show.Pattern != nil { + // If we have ShowPredicateExtractor, we do not buildSelection with Pattern + if show.Pattern != nil && p.Extractor == nil { show.Pattern.Expr = &ast.ColumnNameExpr{ Name: &ast.ColumnName{Name: p.OutputNames()[0].ColName}, } @@ -4620,12 +4617,22 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp case ast.ShowConfig: names = []string{"Type", "Instance", "Name", "Value"} case ast.ShowDatabases: - names = []string{"Database"} + fieldDB := "Database" + patternName := extractPatternLikeName(s.Pattern) + if patternName != "" { + fieldDB = fmt.Sprintf("%s (%s)", fieldDB, patternName) + } + names = []string{fieldDB} case ast.ShowOpenTables: names = []string{"Database", "Table", "In_use", "Name_locked"} ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLong, mysql.TypeLong} case ast.ShowTables: - names = []string{fmt.Sprintf("Tables_in_%s", s.DBName)} + fieldTable := fmt.Sprintf("Tables_in_%s", s.DBName) + patternName := extractPatternLikeName(s.Pattern) + if patternName != "" { + fieldTable = fmt.Sprintf("%s (%s)", fieldTable, patternName) + } + names = []string{fieldTable} if s.Full { names = append(names, "Table_type") } @@ -4842,3 +4849,14 @@ func (b *PlanBuilder) buildCompactTable(node *ast.CompactTableStmt) (Plan, error } return p, nil } + +func extractPatternLikeName(patternLike *ast.PatternLikeExpr) string { + if patternLike == nil { + return "" + } + switch patternLike.Pattern.(type) { + case *driver.ValueExpr: + return patternLike.Pattern.(*driver.ValueExpr).GetString() + } + return "" +} From 8454e21420ebf15532fd2f1ca46cbf5de28e3682 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Thu, 2 Jun 2022 18:54:50 +0800 Subject: [PATCH 2/8] fix test --- cmd/explaintest/r/show.result | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/cmd/explaintest/r/show.result b/cmd/explaintest/r/show.result index 3332d2d8d6bc3..b2a1dea3a2daa 100644 --- a/cmd/explaintest/r/show.result +++ b/cmd/explaintest/r/show.result @@ -1,12 +1,84 @@ show tables; Tables_in_test +PK_S_MULTI_30_tmp +access_path_selection +city +customer +date_dim +dd +dt +e +employees +lo +org_department +org_employee_position +org_position +person +pp +precise_types +rr +s +sgc +sgc1 +sgc2 +sgc3 +st +store_sales +t +t0 +t004 +t01 +t02 +t1 +t11111 +t2 +t3 +t4 +ta +tb +tbl +tbl1 +tbl_0 +tbl_001 +tbl_002 +tbl_003 +tbl_004 +tbl_005 +tbl_006 +tbl_007 +tbl_008 +tbl_009 +tbl_1 +tbl_2 +tbl_3 +tbl_4 +test01 +test02 +th +tpk +tpk1 +tu +v +v1 +v2 +web_sales +一 show databases; Database INFORMATION_SCHEMA METRICS_SCHEMA PERFORMANCE_SCHEMA +agg_predicate_pushdown +cd_test_utf8 +collation_check_use_collation +collation_misc +collation_point_get +imdbload mysql test +test1 +with_cluster_index +wout_cluster_index show tables like '%xx'; Tables_in_test (%xx) show databases like '%xx'; From ce18ca52d39f8861520253b7a213704643b37054 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Thu, 2 Jun 2022 19:29:30 +0800 Subject: [PATCH 3/8] fix test --- cmd/explaintest/r/show.result | 81 ----------------------------------- cmd/explaintest/t/show.test | 2 - planner/core/stringer_test.go | 4 +- 3 files changed, 2 insertions(+), 85 deletions(-) diff --git a/cmd/explaintest/r/show.result b/cmd/explaintest/r/show.result index b2a1dea3a2daa..6dfbf77c4af44 100644 --- a/cmd/explaintest/r/show.result +++ b/cmd/explaintest/r/show.result @@ -1,84 +1,3 @@ -show tables; -Tables_in_test -PK_S_MULTI_30_tmp -access_path_selection -city -customer -date_dim -dd -dt -e -employees -lo -org_department -org_employee_position -org_position -person -pp -precise_types -rr -s -sgc -sgc1 -sgc2 -sgc3 -st -store_sales -t -t0 -t004 -t01 -t02 -t1 -t11111 -t2 -t3 -t4 -ta -tb -tbl -tbl1 -tbl_0 -tbl_001 -tbl_002 -tbl_003 -tbl_004 -tbl_005 -tbl_006 -tbl_007 -tbl_008 -tbl_009 -tbl_1 -tbl_2 -tbl_3 -tbl_4 -test01 -test02 -th -tpk -tpk1 -tu -v -v1 -v2 -web_sales -一 -show databases; -Database -INFORMATION_SCHEMA -METRICS_SCHEMA -PERFORMANCE_SCHEMA -agg_predicate_pushdown -cd_test_utf8 -collation_check_use_collation -collation_misc -collation_point_get -imdbload -mysql -test -test1 -with_cluster_index -wout_cluster_index show tables like '%xx'; Tables_in_test (%xx) show databases like '%xx'; diff --git a/cmd/explaintest/t/show.test b/cmd/explaintest/t/show.test index 29fb33d69929f..b90131d18f861 100644 --- a/cmd/explaintest/t/show.test +++ b/cmd/explaintest/t/show.test @@ -1,5 +1,3 @@ # test show output field name -show tables; -show databases; show tables like '%xx'; show databases like '%xx'; diff --git a/planner/core/stringer_test.go b/planner/core/stringer_test.go index 50f15ae9e9c37..6f509064cf260 100644 --- a/planner/core/stringer_test.go +++ b/planner/core/stringer_test.go @@ -46,11 +46,11 @@ func TestPlanStringer(t *testing.T) { }, { sql: "show columns from t where field = 'a'", - plan: "Show->Sel([eq(Column#13, a)])->Projection", + plan: "Show()->Sel([eq(Column#13, a)])->Projection", }, { sql: "desc t", - plan: "Show", + plan: "Show()", }, { sql: "desc t a", From e5e99c1d4cdf0c08b50920a1af727d1f63577d54 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Thu, 2 Jun 2022 19:31:50 +0800 Subject: [PATCH 4/8] clean code --- planner/core/planbuilder.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index a714eb0d84265..150be7f5a57dd 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -4618,8 +4618,7 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp names = []string{"Type", "Instance", "Name", "Value"} case ast.ShowDatabases: fieldDB := "Database" - patternName := extractPatternLikeName(s.Pattern) - if patternName != "" { + if patternName := extractPatternLikeName(s.Pattern); patternName != "" { fieldDB = fmt.Sprintf("%s (%s)", fieldDB, patternName) } names = []string{fieldDB} @@ -4628,8 +4627,7 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLong, mysql.TypeLong} case ast.ShowTables: fieldTable := fmt.Sprintf("Tables_in_%s", s.DBName) - patternName := extractPatternLikeName(s.Pattern) - if patternName != "" { + if patternName := extractPatternLikeName(s.Pattern); patternName != "" { fieldTable = fmt.Sprintf("%s (%s)", fieldTable, patternName) } names = []string{fieldTable} From 2f060fa9712d29a1d0159136ac6bca76195f9ab7 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Thu, 2 Jun 2022 20:36:23 +0800 Subject: [PATCH 5/8] fix test --- planner/core/planbuilder.go | 5 +++-- planner/core/stringer_test.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 150be7f5a57dd..7c426b0334382 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -2937,8 +2937,9 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, if (show.Tp == ast.ShowTables || show.Tp == ast.ShowTableStatus) && p.DBName == "" { return nil, ErrNoDB } - p.Extractor = newShowBaseExtractor(*show) - p.Extractor.Extract() + if extractor := newShowBaseExtractor(*show); extractor.Extract() { + p.Extractor = extractor + } case ast.ShowCreateTable, ast.ShowCreateSequence, ast.ShowPlacementForTable, ast.ShowPlacementForPartition: var err error if table, err := b.is.TableByName(show.Table.Schema, show.Table.Name); err == nil { diff --git a/planner/core/stringer_test.go b/planner/core/stringer_test.go index 6f509064cf260..50f15ae9e9c37 100644 --- a/planner/core/stringer_test.go +++ b/planner/core/stringer_test.go @@ -46,11 +46,11 @@ func TestPlanStringer(t *testing.T) { }, { sql: "show columns from t where field = 'a'", - plan: "Show()->Sel([eq(Column#13, a)])->Projection", + plan: "Show->Sel([eq(Column#13, a)])->Projection", }, { sql: "desc t", - plan: "Show()", + plan: "Show", }, { sql: "desc t a", From b2283c177d146026603171a3992a87fe02670417 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Mon, 6 Jun 2022 18:29:53 +0800 Subject: [PATCH 6/8] Update planner/core/planbuilder.go Co-authored-by: tangenta --- planner/core/planbuilder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 7c426b0334382..b568f614d7836 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -4853,9 +4853,9 @@ func extractPatternLikeName(patternLike *ast.PatternLikeExpr) string { if patternLike == nil { return "" } - switch patternLike.Pattern.(type) { + switch v := patternLike.Pattern.(type) { case *driver.ValueExpr: - return patternLike.Pattern.(*driver.ValueExpr).GetString() + return v.GetString() } return "" } From 9778fcf9963d3724c91077f195cfd7c1ee4d9793 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Fri, 24 Jun 2022 13:46:34 +0800 Subject: [PATCH 7/8] add extra flag to determine to buildPattern or not --- planner/core/logical_plans.go | 3 ++- planner/core/planbuilder.go | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/planner/core/logical_plans.go b/planner/core/logical_plans.go index 48b4da2e96cd6..93528c93716e2 100644 --- a/planner/core/logical_plans.go +++ b/planner/core/logical_plans.go @@ -1838,7 +1838,8 @@ type LogicalShow struct { logicalSchemaProducer ShowContents - Extractor ShowPredicateExtractor + Extractor ShowPredicateExtractor + buildPattern bool // It depends on ShowPredicateExtractor now } // LogicalShowDDLJobs is for showing DDL job list. diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index b568f614d7836..267e0ee973d2d 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -2928,6 +2928,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, GlobalScope: show.GlobalScope, Extended: show.Extended, }, + buildPattern: true, }.Init(b.ctx) isView := false isSequence := false @@ -2939,6 +2940,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, } if extractor := newShowBaseExtractor(*show); extractor.Extract() { p.Extractor = extractor + p.buildPattern = false } case ast.ShowCreateTable, ast.ShowCreateSequence, ast.ShowPlacementForTable, ast.ShowPlacementForPartition: var err error @@ -3010,7 +3012,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, var np LogicalPlan np = p // If we have ShowPredicateExtractor, we do not buildSelection with Pattern - if show.Pattern != nil && p.Extractor == nil { + if show.Pattern != nil && p.buildPattern { show.Pattern.Expr = &ast.ColumnNameExpr{ Name: &ast.ColumnName{Name: p.OutputNames()[0].ColName}, } From 1a25e102d189a97a10e107a9a32ae26fc04edb08 Mon Sep 17 00:00:00 2001 From: likzn <1020193211@qq.com> Date: Fri, 24 Jun 2022 13:59:52 +0800 Subject: [PATCH 8/8] refactor --- planner/core/logical_plans.go | 3 +-- planner/core/planbuilder.go | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/planner/core/logical_plans.go b/planner/core/logical_plans.go index 93528c93716e2..48b4da2e96cd6 100644 --- a/planner/core/logical_plans.go +++ b/planner/core/logical_plans.go @@ -1838,8 +1838,7 @@ type LogicalShow struct { logicalSchemaProducer ShowContents - Extractor ShowPredicateExtractor - buildPattern bool // It depends on ShowPredicateExtractor now + Extractor ShowPredicateExtractor } // LogicalShowDDLJobs is for showing DDL job list. diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 267e0ee973d2d..cc77ebbab918a 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -2928,10 +2928,11 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, GlobalScope: show.GlobalScope, Extended: show.Extended, }, - buildPattern: true, }.Init(b.ctx) isView := false isSequence := false + // It depends on ShowPredicateExtractor now + buildPattern := true switch show.Tp { case ast.ShowDatabases, ast.ShowVariables, ast.ShowTables, ast.ShowColumns, ast.ShowTableStatus, ast.ShowCollation: @@ -2940,7 +2941,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, } if extractor := newShowBaseExtractor(*show); extractor.Extract() { p.Extractor = extractor - p.buildPattern = false + buildPattern = false } case ast.ShowCreateTable, ast.ShowCreateSequence, ast.ShowPlacementForTable, ast.ShowPlacementForPartition: var err error @@ -3012,7 +3013,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan, var np LogicalPlan np = p // If we have ShowPredicateExtractor, we do not buildSelection with Pattern - if show.Pattern != nil && p.buildPattern { + if show.Pattern != nil && buildPattern { show.Pattern.Expr = &ast.ColumnNameExpr{ Name: &ast.ColumnName{Name: p.OutputNames()[0].ColName}, }