From 3907d4c693a8828372b789d65def8d06a9717f68 Mon Sep 17 00:00:00 2001 From: AilinKid <314806019@qq.com> Date: Fri, 26 Jul 2024 15:28:49 +0800 Subject: [PATCH 1/2] . Signed-off-by: AilinKid <314806019@qq.com> --- pkg/planner/cascades/implementation_rules.go | 2 +- pkg/planner/core/BUILD.bazel | 1 - pkg/planner/core/core_init.go | 1 + pkg/planner/core/find_best_task.go | 3 ++- pkg/planner/core/logical_plans.go | 2 +- .../core/operator/logicalop/BUILD.bazel | 4 ++++ .../{ => operator/logicalop}/logical_show.go | 22 ++++++++++++++----- pkg/planner/core/physical_plans.go | 3 ++- pkg/planner/core/planbuilder.go | 5 +++-- pkg/planner/core/stringer.go | 4 ++-- pkg/planner/pattern/pattern.go | 2 +- .../util/utilfuncp/func_pointer_misc.go | 4 ++++ 12 files changed, 38 insertions(+), 15 deletions(-) rename pkg/planner/core/{ => operator/logicalop}/logical_show.go (90%) diff --git a/pkg/planner/cascades/implementation_rules.go b/pkg/planner/cascades/implementation_rules.go index abd1fe288514d..b5af5e3c7d7ae 100644 --- a/pkg/planner/cascades/implementation_rules.go +++ b/pkg/planner/cascades/implementation_rules.go @@ -244,7 +244,7 @@ func (*ImplShow) Match(_ *memo.GroupExpr, prop *property.PhysicalProperty) (matc // OnImplement implements ImplementationRule OnImplement interface. func (*ImplShow) OnImplement(expr *memo.GroupExpr, _ *property.PhysicalProperty) ([]memo.Implementation, error) { logicProp := expr.Group.Prop - show := expr.ExprNode.(*plannercore.LogicalShow) + show := expr.ExprNode.(*logicalop.LogicalShow) // TODO(zz-jason): unifying LogicalShow and PhysicalShow to a single // struct. So that we don't need to create a new PhysicalShow object, which // can help us to reduce the gc pressure of golang runtime and improve the diff --git a/pkg/planner/core/BUILD.bazel b/pkg/planner/core/BUILD.bazel index ca587d22aa0e7..60d5e20b58b17 100644 --- a/pkg/planner/core/BUILD.bazel +++ b/pkg/planner/core/BUILD.bazel @@ -37,7 +37,6 @@ go_library( "logical_projection.go", "logical_selection.go", "logical_sequence.go", - "logical_show.go", "logical_show_ddl_jobs.go", "logical_sort.go", "logical_table_dual.go", diff --git a/pkg/planner/core/core_init.go b/pkg/planner/core/core_init.go index dbf2ed32594f1..fc356ca4b9157 100644 --- a/pkg/planner/core/core_init.go +++ b/pkg/planner/core/core_init.go @@ -35,6 +35,7 @@ func init() { utilfuncp.GetStreamAggs = getStreamAggs utilfuncp.GetHashAggs = getHashAggs utilfuncp.PruneByItems = pruneByItems + utilfuncp.FindBestTask4LogicalShow = findBestTask4LogicalShow utilfuncp.FindBestTask4LogicalCTETable = findBestTask4LogicalCTETable utilfuncp.FindBestTask4LogicalMemTable = findBestTask4LogicalMemTable utilfuncp.ExhaustPhysicalPlans4LogicalMaxOneRow = exhaustPhysicalPlans4LogicalMaxOneRow diff --git a/pkg/planner/core/find_best_task.go b/pkg/planner/core/find_best_task.go index ee98c4118bc81..bddd5ead599b1 100644 --- a/pkg/planner/core/find_best_task.go +++ b/pkg/planner/core/find_best_task.go @@ -109,7 +109,8 @@ func findBestTask4LogicalTableDual(p *LogicalTableDual, prop *property.PhysicalP return rt, 1, nil } -func findBestTask4LogicalShow(p *LogicalShow, prop *property.PhysicalProperty, planCounter *base.PlanCounterTp, _ *optimizetrace.PhysicalOptimizeOp) (base.Task, int64, error) { +func findBestTask4LogicalShow(lp base.LogicalPlan, prop *property.PhysicalProperty, planCounter *base.PlanCounterTp, _ *optimizetrace.PhysicalOptimizeOp) (base.Task, int64, error) { + p := lp.(*logicalop.LogicalShow) if !prop.IsSortItemEmpty() || planCounter.Empty() { return base.InvalidTask, 0, nil } diff --git a/pkg/planner/core/logical_plans.go b/pkg/planner/core/logical_plans.go index b4187483d085a..126da6c212213 100644 --- a/pkg/planner/core/logical_plans.go +++ b/pkg/planner/core/logical_plans.go @@ -43,7 +43,7 @@ var ( _ base.LogicalPlan = &LogicalExpand{} _ base.LogicalPlan = &LogicalUnionScan{} _ base.LogicalPlan = &logicalop.LogicalMemTable{} - _ base.LogicalPlan = &LogicalShow{} + _ base.LogicalPlan = &logicalop.LogicalShow{} _ base.LogicalPlan = &LogicalShowDDLJobs{} _ base.LogicalPlan = &LogicalCTE{} _ base.LogicalPlan = &logicalop.LogicalCTETable{} diff --git a/pkg/planner/core/operator/logicalop/BUILD.bazel b/pkg/planner/core/operator/logicalop/BUILD.bazel index b1ca05dd884bb..a59d124da173f 100644 --- a/pkg/planner/core/operator/logicalop/BUILD.bazel +++ b/pkg/planner/core/operator/logicalop/BUILD.bazel @@ -8,6 +8,7 @@ go_library( "logical_max_one_row.go", "logical_mem_table.go", "logical_schema_producer.go", + "logical_show.go", ], importpath = "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop", visibility = ["//visibility:public"], @@ -15,6 +16,8 @@ go_library( "//pkg/expression", "//pkg/infoschema", "//pkg/kv", + "//pkg/parser/ast", + "//pkg/parser/auth", "//pkg/parser/model", "//pkg/planner/core/base", "//pkg/planner/core/operator/baseimpl", @@ -28,6 +31,7 @@ go_library( "//pkg/types", "//pkg/util/dbterror/plannererrors", "//pkg/util/plancodec", + "//pkg/util/size", "//pkg/util/tracing", ], ) diff --git a/pkg/planner/core/logical_show.go b/pkg/planner/core/operator/logicalop/logical_show.go similarity index 90% rename from pkg/planner/core/logical_show.go rename to pkg/planner/core/operator/logicalop/logical_show.go index 20b4511baeba8..33df6e19308fe 100644 --- a/pkg/planner/core/logical_show.go +++ b/pkg/planner/core/operator/logicalop/logical_show.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package core +package logicalop import ( "unsafe" @@ -22,16 +22,16 @@ import ( "github.com/pingcap/tidb/pkg/parser/auth" "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/planner/core/base" - "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" "github.com/pingcap/tidb/pkg/planner/property" "github.com/pingcap/tidb/pkg/planner/util/optimizetrace" + "github.com/pingcap/tidb/pkg/planner/util/utilfuncp" "github.com/pingcap/tidb/pkg/util/plancodec" "github.com/pingcap/tidb/pkg/util/size" ) // LogicalShow represents a show plan. type LogicalShow struct { - logicalop.LogicalSchemaProducer + LogicalSchemaProducer ShowContents Extractor base.ShowPredicateExtractor @@ -76,7 +76,7 @@ func (s *ShowContents) MemoryUsage() (sum int64) { // Init initializes LogicalShow. func (p LogicalShow) Init(ctx base.PlanContext) *LogicalShow { - p.BaseLogicalPlan = logicalop.NewBaseLogicalPlan(ctx, plancodec.TypeShow, &p, 0) + p.BaseLogicalPlan = NewBaseLogicalPlan(ctx, plancodec.TypeShow, &p, 0) return &p } @@ -90,7 +90,7 @@ func (p LogicalShow) Init(ctx base.PlanContext) *LogicalShow { // FindBestTask implements the base.LogicalPlan.<3rd> interface. func (p *LogicalShow) FindBestTask(prop *property.PhysicalProperty, planCounter *base.PlanCounterTp, _ *optimizetrace.PhysicalOptimizeOp) (base.Task, int64, error) { - return findBestTask4LogicalShow(p, prop, planCounter, nil) + return utilfuncp.FindBestTask4LogicalShow(p, prop, planCounter, nil) } // BuildKeyInfo inherits BaseLogicalPlan.LogicalPlan.<4th> implementation. @@ -138,3 +138,15 @@ func (p *LogicalShow) DeriveStats(_ []*property.StatsInfo, selfSchema *expressio // ConvertOuterToInnerJoin inherits BaseLogicalPlan.LogicalPlan.<24th> implementation. // *************************** end implementation of logicalPlan interface *************************** + +// todo: merge getFakeStats with the one in logical_show_ddl_jobs.go +func getFakeStats(schema *expression.Schema) *property.StatsInfo { + profile := &property.StatsInfo{ + RowCount: 1, + ColNDVs: make(map[int64]float64, schema.Len()), + } + for _, col := range schema.Columns { + profile.ColNDVs[col.UniqueID] = 1 + } + return profile +} diff --git a/pkg/planner/core/physical_plans.go b/pkg/planner/core/physical_plans.go index 6227f352831d2..e637d5d469c96 100644 --- a/pkg/planner/core/physical_plans.go +++ b/pkg/planner/core/physical_plans.go @@ -16,6 +16,7 @@ package core import ( "fmt" + "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" "strconv" "strings" "unsafe" @@ -2547,7 +2548,7 @@ func CollectPlanStatsVersion(plan base.PhysicalPlan, statsInfos map[string]uint6 type PhysicalShow struct { physicalSchemaProducer - ShowContents + logicalop.ShowContents Extractor base.ShowPredicateExtractor } diff --git a/pkg/planner/core/planbuilder.go b/pkg/planner/core/planbuilder.go index 40849fd2f992b..1629ec73c90c2 100644 --- a/pkg/planner/core/planbuilder.go +++ b/pkg/planner/core/planbuilder.go @@ -18,6 +18,7 @@ import ( "context" "encoding/binary" "fmt" + "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" "math" "reflect" "strconv" @@ -3169,8 +3170,8 @@ func splitWhere(where ast.ExprNode) []ast.ExprNode { } func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (base.Plan, error) { - p := LogicalShow{ - ShowContents: ShowContents{ + p := logicalop.LogicalShow{ + ShowContents: logicalop.ShowContents{ Tp: show.Tp, CountWarningsOrErrors: show.CountWarningsOrErrors, DBName: show.DBName, diff --git a/pkg/planner/core/stringer.go b/pkg/planner/core/stringer.go index 81dfc5076145b..8a1bae3716b35 100644 --- a/pkg/planner/core/stringer.go +++ b/pkg/planner/core/stringer.go @@ -175,9 +175,9 @@ func toString(in base.Plan, strs []string, idxs []int) ([]string, []int) { str = "Lock" case *ShowDDL: str = "ShowDDL" - case *LogicalShow: + case *logicalop.LogicalShow: str = "Show" - if pl := in.(*LogicalShow); pl.Extractor != nil { + if pl := in.(*logicalop.LogicalShow); pl.Extractor != nil { str = str + "(" + pl.Extractor.ExplainInfo() + ")" } case *PhysicalShow: diff --git a/pkg/planner/pattern/pattern.go b/pkg/planner/pattern/pattern.go index 9892b09d6753b..fcc020d3badad 100644 --- a/pkg/planner/pattern/pattern.go +++ b/pkg/planner/pattern/pattern.go @@ -113,7 +113,7 @@ func GetOperand(p base.LogicalPlan) Operand { return OperandMemTableScan case *plannercore.LogicalIndexScan: return OperandIndexScan - case *plannercore.LogicalShow: + case *logicalop.LogicalShow: return OperandShow case *plannercore.LogicalWindow: return OperandWindow diff --git a/pkg/planner/util/utilfuncp/func_pointer_misc.go b/pkg/planner/util/utilfuncp/func_pointer_misc.go index f00397111d19a..d0fa09bca6f27 100644 --- a/pkg/planner/util/utilfuncp/func_pointer_misc.go +++ b/pkg/planner/util/utilfuncp/func_pointer_misc.go @@ -101,3 +101,7 @@ var FindBestTask4LogicalCTETable func(lp base.LogicalPlan, prop *property.Physic var FindBestTask4LogicalMemTable func(lp base.LogicalPlan, prop *property.PhysicalProperty, planCounter *base.PlanCounterTp, opt *optimizetrace.PhysicalOptimizeOp) (t base.Task, cntPlan int64, err error) + +// FindBestTask4LogicalShow will be called by LogicalShow in logicalOp pkg. +var FindBestTask4LogicalShow func(lp base.LogicalPlan, prop *property.PhysicalProperty, planCounter *base.PlanCounterTp, + _ *optimizetrace.PhysicalOptimizeOp) (base.Task, int64, error) From aa636ce2c4b74b8f39cc7bc6154fbd6e53a882d9 Mon Sep 17 00:00:00 2001 From: AilinKid <314806019@qq.com> Date: Fri, 26 Jul 2024 15:34:18 +0800 Subject: [PATCH 2/2] . Signed-off-by: AilinKid <314806019@qq.com> --- pkg/planner/core/physical_plans.go | 2 +- pkg/planner/core/planbuilder.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/planner/core/physical_plans.go b/pkg/planner/core/physical_plans.go index e637d5d469c96..9516604c812e0 100644 --- a/pkg/planner/core/physical_plans.go +++ b/pkg/planner/core/physical_plans.go @@ -16,7 +16,6 @@ package core import ( "fmt" - "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" "strconv" "strings" "unsafe" @@ -31,6 +30,7 @@ import ( "github.com/pingcap/tidb/pkg/planner/cardinality" "github.com/pingcap/tidb/pkg/planner/core/base" "github.com/pingcap/tidb/pkg/planner/core/cost" + "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" "github.com/pingcap/tidb/pkg/planner/property" "github.com/pingcap/tidb/pkg/planner/util" "github.com/pingcap/tidb/pkg/planner/util/coreusage" diff --git a/pkg/planner/core/planbuilder.go b/pkg/planner/core/planbuilder.go index 1629ec73c90c2..b4cf711adcace 100644 --- a/pkg/planner/core/planbuilder.go +++ b/pkg/planner/core/planbuilder.go @@ -18,7 +18,6 @@ import ( "context" "encoding/binary" "fmt" - "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" "math" "reflect" "strconv" @@ -42,6 +41,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/opcode" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/planner/core/base" + "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" "github.com/pingcap/tidb/pkg/planner/property" "github.com/pingcap/tidb/pkg/planner/util" "github.com/pingcap/tidb/pkg/privilege"