Skip to content

Commit

Permalink
make fmt
Browse files Browse the repository at this point in the history
Signed-off-by: AilinKid <[email protected]>
  • Loading branch information
AilinKid committed Apr 7, 2024
1 parent dbfe731 commit 6f091fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 17 additions & 1 deletion pkg/planner/util/coreusage/costMisc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,39 @@ const (
CostFlagTrace
)

// CostVer2 is a structure of cost basic of version2
type CostVer2 struct {
cost float64
trace *CostTrace
}

// GetCost returns the cost value of the costVer2
func (c *CostVer2) GetCost() float64 {
return c.cost
}

// GetTrace returns the trace of current costVer2
func (c *CostVer2) GetTrace() *CostTrace {
return c.trace
}

// CostTrace record the basic factor and formula in cost est.
type CostTrace struct {
factorCosts map[string]float64 // map[factorName]cost, used to calibrate the cost model
formula string // It used to trace the cost calculation.
}

// GetFormula return the formula of current costTrace.
func (c *CostTrace) GetFormula() string {
return c.formula
}

// GetFactorCosts return the factors of current costTrace.
func (c *CostTrace) GetFactorCosts() map[string]float64 {
return c.factorCosts
}

// NewZeroCostVer2 return a new zero costVer2.
func NewZeroCostVer2(trace bool) (ret CostVer2) {
if trace {
ret.trace = &CostTrace{make(map[string]float64), ""}
Expand All @@ -53,14 +60,17 @@ func hasCostFlag(costFlag, flag uint64) bool {
return (costFlag & flag) > 0
}

// TraceCost indicates whether to trace cost.
func TraceCost(option *PlanCostOption) bool {
if option != nil && hasCostFlag(option.CostFlag, CostFlagTrace) {
return true
}
return false
}

func NewCostVer2(option *PlanCostOption, factor CostVer2Factor, cost float64, lazyFormula func() string) (ret CostVer2) {
// NewCostVer2 is the constructor of CostVer2.
func NewCostVer2(option *PlanCostOption, factor CostVer2Factor, cost float64,
lazyFormula func() string) (ret CostVer2) {
ret.cost = cost
if TraceCost(option) {
ret.trace = &CostTrace{make(map[string]float64), ""}
Expand All @@ -70,15 +80,18 @@ func NewCostVer2(option *PlanCostOption, factor CostVer2Factor, cost float64, la
return ret
}

// CostVer2Factor is a record of internal cost factor.
type CostVer2Factor struct {
Name string
Value float64
}

// String return the current CostVer2Factor's format string.
func (f CostVer2Factor) String() string {
return fmt.Sprintf("%s(%v)", f.Name, f.Value)
}

// SumCostVer2 sum the cost up of all the passed args.
func SumCostVer2(costs ...CostVer2) (ret CostVer2) {
if len(costs) == 0 {
return
Expand All @@ -101,6 +114,7 @@ func SumCostVer2(costs ...CostVer2) (ret CostVer2) {
return ret
}

// DivCostVer2 is div utility func of CostVer2.
func DivCostVer2(cost CostVer2, denominator float64) (ret CostVer2) {
ret.cost = cost.cost / denominator
if cost.trace != nil {
Expand All @@ -113,6 +127,7 @@ func DivCostVer2(cost CostVer2, denominator float64) (ret CostVer2) {
return ret
}

// MulCostVer2 is mul utility func of CostVer2.
func MulCostVer2(cost CostVer2, scale float64) (ret CostVer2) {
ret.cost = cost.cost * scale
if cost.trace != nil {
Expand All @@ -125,4 +140,5 @@ func MulCostVer2(cost CostVer2, scale float64) (ret CostVer2) {
return ret
}

// ZeroCostVer2 is a pre-defined zero CostVer2.
var ZeroCostVer2 = NewZeroCostVer2(false)
9 changes: 7 additions & 2 deletions pkg/planner/util/coreusage/optTracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import "github.com/pingcap/tidb/pkg/util/tracing"
// logicalOptRule inside the accommodated pkg `util` should only be depended on by logical `rule` pkg.
//
// rule related -----> core/util
//**************************************** below logical optimize trace related ******************************************
//********************** below logical optimize trace related *************************

// LogicalOptimizeOp is logical optimizing option for tracing.
type LogicalOptimizeOp struct {
Expand Down Expand Up @@ -68,27 +68,31 @@ func (op *LogicalOptimizeOp) RecordFinalLogicalPlan(build func() *tracing.PlanTr
op.tracer.RecordFinalLogicalPlan(build())
}

//**************************************** below physical optimize trace related ******************************************
//********************** below physical optimize trace related *************************

// PhysicalOptimizeOp is logical optimizing option for tracing.
type PhysicalOptimizeOp struct {
// tracer is goring to track optimize steps during physical optimizing
tracer *tracing.PhysicalOptimizeTracer
}

// DefaultPhysicalOptimizeOption is default physical optimizing option.
func DefaultPhysicalOptimizeOption() *PhysicalOptimizeOp {
return &PhysicalOptimizeOp{}
}

// WithEnableOptimizeTracer is utility func to append the PhysicalOptimizeTracer into current PhysicalOptimizeOp.
func (op *PhysicalOptimizeOp) WithEnableOptimizeTracer(tracer *tracing.PhysicalOptimizeTracer) *PhysicalOptimizeOp {
op.tracer = tracer
return op
}

// AppendCandidate is utility func to append the CandidatePlanTrace into current PhysicalOptimizeOp.
func (op *PhysicalOptimizeOp) AppendCandidate(c *tracing.CandidatePlanTrace) {
op.tracer.AppendCandidate(c)
}

// GetTracer returns the current op's PhysicalOptimizeTracer.
func (op *PhysicalOptimizeOp) GetTracer() *tracing.PhysicalOptimizeTracer {
return op.tracer
}
Expand All @@ -104,6 +108,7 @@ type PlanCostOption struct {
tracer *PhysicalOptimizeOp
}

// GetTracer returns the current op's PhysicalOptimizeOp.
func (op *PlanCostOption) GetTracer() *PhysicalOptimizeOp {
return op.tracer
}
Expand Down

0 comments on commit 6f091fa

Please sign in to comment.