Skip to content

Commit

Permalink
planner: move logical interface definition to base pkg (#52732)
Browse files Browse the repository at this point in the history
ref #51664, ref #52714
  • Loading branch information
AilinKid authored Apr 23, 2024
1 parent 3368ccd commit b1baf4d
Show file tree
Hide file tree
Showing 79 changed files with 981 additions and 835 deletions.
1 change: 1 addition & 0 deletions pkg/expression/integration_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ go_test(
"//pkg/parser/mysql",
"//pkg/parser/terror",
"//pkg/planner/core",
"//pkg/planner/core/base",
"//pkg/session",
"//pkg/sessionctx/variable",
"//pkg/store/mockstore",
Expand Down
3 changes: 2 additions & 1 deletion pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/parser/terror"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/session"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/store/mockstore"
Expand Down Expand Up @@ -408,7 +409,7 @@ func TestFilterExtractFromDNF(t *testing.T) {
require.NoError(t, err, "error %v, for resolve name, expr %s", err, tt.exprStr)
p, err := plannercore.BuildLogicalPlanForTest(ctx, sctx, stmts[0], ret.InfoSchema)
require.NoError(t, err, "error %v, for build plan, expr %s", err, tt.exprStr)
selection := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
selection := p.(base.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
conds := make([]expression.Expression, len(selection.Conditions))
for i, cond := range selection.Conditions {
conds[i] = expression.PushDownNot(sctx.GetExprCtx(), cond)
Expand Down
1 change: 1 addition & 0 deletions pkg/planner/cardinality/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ go_test(
"//pkg/parser/model",
"//pkg/parser/mysql",
"//pkg/planner/core",
"//pkg/planner/core/base",
"//pkg/session",
"//pkg/sessionctx",
"//pkg/sessionctx/stmtctx",
Expand Down
7 changes: 4 additions & 3 deletions pkg/planner/cardinality/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/planner/cardinality"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/session"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
Expand Down Expand Up @@ -103,7 +104,7 @@ func BenchmarkSelectivity(b *testing.B) {
b.Run("Selectivity", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := cardinality.Selectivity(sctx.GetPlanCtx(), &statsTbl.HistColl, p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection).Conditions, nil)
_, _, err := cardinality.Selectivity(sctx.GetPlanCtx(), &statsTbl.HistColl, p.(base.LogicalPlan).Children()[0].(*plannercore.LogicalSelection).Conditions, nil)
require.NoError(b, err)
}
b.ReportAllocs()
Expand Down Expand Up @@ -449,7 +450,7 @@ func TestSelectivity(t *testing.T) {
p, err := plannercore.BuildLogicalPlanForTest(ctx, sctx, stmts[0], ret.InfoSchema)
require.NoErrorf(t, err, "for building plan, expr %s", err, tt.exprs)

sel := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
sel := p.(base.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
ds := sel.Children()[0].(*plannercore.DataSource)

histColl := statsTbl.GenerateHistCollFromColumnInfo(ds.TableInfo(), ds.Schema().Columns)
Expand Down Expand Up @@ -507,7 +508,7 @@ func TestDNFCondSelectivity(t *testing.T) {
p, err := plannercore.BuildLogicalPlanForTest(ctx, sctx, stmts[0], ret.InfoSchema)
require.NoErrorf(t, err, "error %v, for building plan, sql %s", err, tt)

sel := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
sel := p.(base.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
ds := sel.Children()[0].(*plannercore.DataSource)

histColl := statsTbl.GenerateHistCollFromColumnInfo(ds.TableInfo(), ds.Schema().Columns)
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/cardinality/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/planner/cardinality"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/testkit/testdata"
Expand Down Expand Up @@ -208,7 +209,7 @@ func TestTraceDebugSelectivity(t *testing.T) {
p, err := plannercore.BuildLogicalPlanForTest(context.Background(), sctx, stmt, ret.InfoSchema)
require.NoError(t, err)

sel := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
sel := p.(base.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)
ds := sel.Children()[0].(*plannercore.DataSource)

dsSchemaCols = append(dsSchemaCols, ds.Schema().Columns)
Expand Down
1 change: 1 addition & 0 deletions pkg/planner/cascades/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
"//pkg/parser",
"//pkg/parser/model",
"//pkg/planner/core",
"//pkg/planner/core/base",
"//pkg/planner/memo",
"//pkg/planner/pattern",
"//pkg/planner/property",
Expand Down
7 changes: 3 additions & 4 deletions pkg/planner/cascades/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"math"

"github.com/pingcap/tidb/pkg/expression"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
Expand Down Expand Up @@ -60,7 +59,7 @@ func (opt *Optimizer) ResetImplementationRules(rules map[pattern.Operand][]Imple

// GetImplementationRules gets all the candidate implementation rules of the optimizer
// for the logical plan node.
func (opt *Optimizer) GetImplementationRules(node plannercore.LogicalPlan) []ImplementationRule {
func (opt *Optimizer) GetImplementationRules(node base.LogicalPlan) []ImplementationRule {
return opt.implementationRuleMap[pattern.GetOperand(node)]
}

Expand Down Expand Up @@ -99,7 +98,7 @@ func (opt *Optimizer) GetImplementationRules(node plannercore.LogicalPlan) []Imp
// for each expression in each group under the required physical property. A
// memo structure is used for a group to reduce the repeated search on the same
// required physical property.
func (opt *Optimizer) FindBestPlan(sctx base.PlanContext, logical plannercore.LogicalPlan) (p base.PhysicalPlan, cost float64, err error) {
func (opt *Optimizer) FindBestPlan(sctx base.PlanContext, logical base.LogicalPlan) (p base.PhysicalPlan, cost float64, err error) {
logical, err = opt.onPhasePreprocessing(sctx, logical)
if err != nil {
return nil, 0, err
Expand All @@ -117,7 +116,7 @@ func (opt *Optimizer) FindBestPlan(sctx base.PlanContext, logical plannercore.Lo
return p, cost, err
}

func (*Optimizer) onPhasePreprocessing(_ base.PlanContext, plan plannercore.LogicalPlan) (plannercore.LogicalPlan, error) {
func (*Optimizer) onPhasePreprocessing(_ base.PlanContext, plan base.LogicalPlan) (base.LogicalPlan, error) {
var err error
plan, err = plan.PruneColumns(plan.Schema().Columns, nil)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions pkg/planner/cascades/optimize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/model"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/planner/property"
Expand All @@ -46,7 +47,7 @@ func TestImplGroupZeroCost(t *testing.T) {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

rootGroup := memo.Convert2Group(logic)
Expand All @@ -73,7 +74,7 @@ func TestInitGroupSchema(t *testing.T) {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

g := memo.Convert2Group(logic)
Expand All @@ -98,7 +99,7 @@ func TestFillGroupStats(t *testing.T) {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

rootGroup := memo.Convert2Group(logic)
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestPreparePossibleProperties(t *testing.T) {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

logic, err = optimizer.onPhasePreprocessing(ctx.GetPlanCtx(), logic)
Expand Down Expand Up @@ -229,7 +230,7 @@ func TestAppliedRuleSet(t *testing.T) {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

group := memo.Convert2Group(logic)
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/cascades/stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/model"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/testkit/testdata"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestGroupStringer(t *testing.T) {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

logic, err = optimizer.onPhasePreprocessing(ctx, logic)
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/cascades/transformation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/planner/context"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/planner/util"
Expand Down Expand Up @@ -858,7 +859,7 @@ func (*pushDownJoin) predicatePushDown(
leftCond []expression.Expression,
rightCond []expression.Expression,
remainCond []expression.Expression,
dual plannercore.LogicalPlan,
dual base.LogicalPlan,
) {
var equalCond []*expression.ScalarFunction
var leftPushCond, rightPushCond, otherCond []expression.Expression
Expand Down
5 changes: 3 additions & 2 deletions pkg/planner/cascades/transformation_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/model"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/planner/memo"
"github.com/pingcap/tidb/pkg/planner/pattern"
"github.com/pingcap/tidb/pkg/testkit/testdata"
Expand All @@ -48,7 +49,7 @@ func testGroupToString(t *testing.T, input []string, output []struct {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

logic, err = optimizer.onPhasePreprocessing(ctx, logic)
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestAggPushDownGather(t *testing.T) {
plan, err := plannercore.BuildLogicalPlanForTest(context.Background(), ctx, stmt, is)
require.NoError(t, err)

logic, ok := plan.(plannercore.LogicalPlan)
logic, ok := plan.(base.LogicalPlan)
require.True(t, ok)

logic, err = optimizer.onPhasePreprocessing(ctx, logic)
Expand Down
33 changes: 12 additions & 21 deletions pkg/planner/core/access_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,14 @@ import (
type dataAccesser interface {

// AccessObject return plan's `table`, `partition` and `index`.
AccessObject() AccessObject
AccessObject() base.AccessObject

// OperatorInfo return other operator information to be explained.
OperatorInfo(normalized bool) string
}

type partitionAccesser interface {
accessObject(base.PlanContext) AccessObject
}

// AccessObject represents what is accessed by an operator.
// It corresponds to the "access object" column in an EXPLAIN statement result.
type AccessObject interface {
String() string
NormalizedString() string
// SetIntoPB transform itself into a protobuf message and set into the binary plan.
SetIntoPB(*tipb.ExplainOperator)
accessObject(base.PlanContext) base.AccessObject
}

// DynamicPartitionAccessObject represents the partitions accessed by the children of this operator.
Expand Down Expand Up @@ -242,7 +233,7 @@ func (o OtherAccessObject) SetIntoPB(pb *tipb.ExplainOperator) {
}

// AccessObject implements dataAccesser interface.
func (p *PhysicalIndexScan) AccessObject() AccessObject {
func (p *PhysicalIndexScan) AccessObject() base.AccessObject {
res := &ScanAccessObject{
Database: p.DBName.O,
}
Expand Down Expand Up @@ -275,7 +266,7 @@ func (p *PhysicalIndexScan) AccessObject() AccessObject {
}

// AccessObject implements dataAccesser interface.
func (p *PhysicalTableScan) AccessObject() AccessObject {
func (p *PhysicalTableScan) AccessObject() base.AccessObject {
res := &ScanAccessObject{
Database: p.DBName.O,
}
Expand All @@ -295,15 +286,15 @@ func (p *PhysicalTableScan) AccessObject() AccessObject {
}

// AccessObject implements dataAccesser interface.
func (p *PhysicalMemTable) AccessObject() AccessObject {
func (p *PhysicalMemTable) AccessObject() base.AccessObject {
return &ScanAccessObject{
Database: p.DBName.O,
Table: p.Table.Name.O,
}
}

// AccessObject implements dataAccesser interface.
func (p *PointGetPlan) AccessObject() AccessObject {
func (p *PointGetPlan) AccessObject() base.AccessObject {
res := &ScanAccessObject{
Database: p.dbName,
Table: p.TblInfo.Name.O,
Expand Down Expand Up @@ -336,7 +327,7 @@ func (p *PointGetPlan) AccessObject() AccessObject {
}

// AccessObject implements physicalScan interface.
func (p *BatchPointGetPlan) AccessObject() AccessObject {
func (p *BatchPointGetPlan) AccessObject() base.AccessObject {
res := &ScanAccessObject{
Database: p.dbName,
Table: p.TblInfo.Name.O,
Expand Down Expand Up @@ -413,7 +404,7 @@ func getDynamicAccessPartition(sctx base.PlanContext, tblInfo *model.TableInfo,
return res
}

func (p *PhysicalTableReader) accessObject(sctx base.PlanContext) AccessObject {
func (p *PhysicalTableReader) accessObject(sctx base.PlanContext) base.AccessObject {
if !sctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return DynamicPartitionAccessObjects(nil)
}
Expand Down Expand Up @@ -467,7 +458,7 @@ func (p *PhysicalTableReader) accessObject(sctx base.PlanContext) AccessObject {
return res
}

func (p *PhysicalIndexReader) accessObject(sctx base.PlanContext) AccessObject {
func (p *PhysicalIndexReader) accessObject(sctx base.PlanContext) base.AccessObject {
if !sctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return DynamicPartitionAccessObjects(nil)
}
Expand All @@ -483,7 +474,7 @@ func (p *PhysicalIndexReader) accessObject(sctx base.PlanContext) AccessObject {
return DynamicPartitionAccessObjects{res}
}

func (p *PhysicalIndexLookUpReader) accessObject(sctx base.PlanContext) AccessObject {
func (p *PhysicalIndexLookUpReader) accessObject(sctx base.PlanContext) base.AccessObject {
if !sctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return DynamicPartitionAccessObjects(nil)
}
Expand All @@ -499,7 +490,7 @@ func (p *PhysicalIndexLookUpReader) accessObject(sctx base.PlanContext) AccessOb
return DynamicPartitionAccessObjects{res}
}

func (p *PhysicalIndexMergeReader) accessObject(sctx base.PlanContext) AccessObject {
func (p *PhysicalIndexMergeReader) accessObject(sctx base.PlanContext) base.AccessObject {
if !sctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() {
return DynamicPartitionAccessObjects(nil)
}
Expand All @@ -516,7 +507,7 @@ func (p *PhysicalIndexMergeReader) accessObject(sctx base.PlanContext) AccessObj
}

// AccessObject implements physicalScan interface.
func (p *PhysicalCTE) AccessObject() AccessObject {
func (p *PhysicalCTE) AccessObject() base.AccessObject {
if p.cteName == p.cteAsName {
return OtherAccessObject(fmt.Sprintf("CTE:%s", p.cteName.L))
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/planner/core/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "base",
srcs = [
"misc_base.go",
"plan_base.go",
"task_base.go",
],
Expand All @@ -12,6 +13,7 @@ go_library(
"//pkg/expression",
"//pkg/kv",
"//pkg/planner/context",
"//pkg/planner/funcdep",
"//pkg/planner/property",
"//pkg/planner/util/coreusage",
"//pkg/types",
Expand Down
26 changes: 26 additions & 0 deletions pkg/planner/core/base/misc_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 PingCAP, 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 base

import "github.com/pingcap/tipb/go-tipb"

// AccessObject represents what is accessed by an operator.
// It corresponds to the "access object" column in an EXPLAIN statement result.
type AccessObject interface {
String() string
NormalizedString() string
// SetIntoPB transform itself into a protobuf message and set into the binary plan.
SetIntoPB(*tipb.ExplainOperator)
}
Loading

0 comments on commit b1baf4d

Please sign in to comment.