Skip to content

Commit 29b6dfc

Browse files
committed
opt: rename "CostFlags" to "Penalties"
`memo.CostFlags` has been renamed to `memo.Penalities` which better reflects its meaning. Release note: None
1 parent 71a7dd2 commit 29b6dfc

File tree

5 files changed

+36
-36
lines changed

5 files changed

+36
-36
lines changed

pkg/sql/opt/memo/cost.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import "math"
1111
// particular operator tree.
1212
// TODO: Need more details about what one "unit" of cost means.
1313
type Cost struct {
14-
C float64
15-
Flags CostFlags
14+
C float64
15+
Penalties
1616

1717
// aux is auxiliary information within a cost that does not affect how the
1818
// cost is compared to other costs with Less.
@@ -27,14 +27,14 @@ type Cost struct {
2727
// group members during testing, by setting their cost so high that any other
2828
// member will have a lower cost.
2929
var MaxCost = Cost{
30-
C: math.Inf(+1),
31-
Flags: HugeCostPenalty | FullScanPenalty | UnboundedCardinality,
30+
C: math.Inf(+1),
31+
Penalties: HugeCostPenalty | FullScanPenalty | UnboundedCardinality,
3232
}
3333

3434
// Less returns true if this cost is lower than the given cost.
3535
func (c Cost) Less(other Cost) bool {
36-
if c.Flags != other.Flags {
37-
return c.Flags < other.Flags
36+
if c.Penalties != other.Penalties {
37+
return c.Penalties < other.Penalties
3838
}
3939
// Two plans with the same cost can have slightly different floating point
4040
// results (e.g. same subcosts being added up in a different order). So we
@@ -52,7 +52,7 @@ func (c Cost) Less(other Cost) bool {
5252
// Add adds the other cost to this cost.
5353
func (c *Cost) Add(other Cost) {
5454
c.C += other.C
55-
c.Flags |= other.Flags
55+
c.Penalties |= other.Penalties
5656
if c.aux.fullScanCount > math.MaxUint8-other.aux.fullScanCount {
5757
// Avoid overflow.
5858
c.aux.fullScanCount = math.MaxUint8
@@ -75,20 +75,20 @@ func (c *Cost) IncrFullScanCount() {
7575
c.aux.fullScanCount++
7676
}
7777

78-
// CostFlags is an ordered bitmask where each bit indicates a cost penalty. The
78+
// Penalties is an ordered bitmask where each bit indicates a cost penalty. The
7979
// penalties are ordered by precedence, with the highest precedence penalty
80-
// using the highest-order bit. This allows CostFlags to be easily compared with
81-
// built-in comparison operators (>, <, =, etc.). For example, CostFlags with
82-
// HugeCostPenalty will always be greater than CostFlags without.
83-
type CostFlags uint8
80+
// using the highest-order bit. This allows Penalties to be easily compared with
81+
// built-in comparison operators (>, <, =, etc.). For example, Penalties with
82+
// HugeCostPenalty will always be greater than Penalties without.
83+
type Penalties uint8
8484

8585
const (
8686
// HugeCostPenalty is true if a plan should be avoided at all costs. This is
8787
// used when the optimizer is forced to use a particular plan, and will
8888
// error if it cannot be used. It takes precedence over other penalties,
8989
// since it indicates that a plan is being forced with a hint, and will
9090
// error if we cannot comply with the hint.
91-
HugeCostPenalty CostFlags = 1 << (7 - iota)
91+
HugeCostPenalty Penalties = 1 << (7 - iota)
9292

9393
// FullScanPenalty is true if the cost of a full table or index scan is
9494
// penalized, indicating that a full scan should only be used if no other
@@ -101,5 +101,5 @@ const (
101101
UnboundedCardinality
102102

103103
// NoPenalties represents no penalties.
104-
NoPenalties CostFlags = 0
104+
NoPenalties Penalties = 0
105105
)

pkg/sql/opt/memo/cost_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ func TestCostLess(t *testing.T) {
2626
{Cost{C: 1}, Cost{C: 1.00000001}, true},
2727
{Cost{C: 1000}, Cost{C: 1000.00000000001}, false},
2828
{Cost{C: 1000}, Cost{C: 1000.00001}, true},
29-
{Cost{C: 1.0, Flags: FullScanPenalty}, Cost{C: 1.0}, false},
30-
{Cost{C: 1.0}, Cost{C: 1.0, Flags: HugeCostPenalty}, true},
31-
{Cost{C: 1.0, Flags: FullScanPenalty | HugeCostPenalty}, Cost{C: 1.0}, false},
32-
{Cost{C: 1.0, Flags: FullScanPenalty}, Cost{C: 1.0, Flags: HugeCostPenalty}, true},
29+
{Cost{C: 1.0, Penalties: FullScanPenalty}, Cost{C: 1.0}, false},
30+
{Cost{C: 1.0}, Cost{C: 1.0, Penalties: HugeCostPenalty}, true},
31+
{Cost{C: 1.0, Penalties: FullScanPenalty | HugeCostPenalty}, Cost{C: 1.0}, false},
32+
{Cost{C: 1.0, Penalties: FullScanPenalty}, Cost{C: 1.0, Penalties: HugeCostPenalty}, true},
3333
{MaxCost, Cost{C: 1.0}, false},
3434
{Cost{C: 0.0}, MaxCost, true},
3535
{MaxCost, MaxCost, false},
36-
{MaxCost, Cost{C: 1.0, Flags: FullScanPenalty}, false},
37-
{Cost{C: 1.0, Flags: HugeCostPenalty}, MaxCost, true},
38-
{Cost{C: 2.0}, Cost{C: 1.0, Flags: UnboundedCardinality}, true},
39-
{Cost{C: 1.0, Flags: UnboundedCardinality}, Cost{C: 2.0}, false},
36+
{MaxCost, Cost{C: 1.0, Penalties: FullScanPenalty}, false},
37+
{Cost{C: 1.0, Penalties: HugeCostPenalty}, MaxCost, true},
38+
{Cost{C: 2.0}, Cost{C: 1.0, Penalties: UnboundedCardinality}, true},
39+
{Cost{C: 1.0, Penalties: UnboundedCardinality}, Cost{C: 2.0}, false},
4040
// Auxiliary information should not affect the comparison.
4141
{Cost{C: 1.0, aux: testAux{0}}, Cost{C: 1.0, aux: testAux{1}}, false},
4242
}
@@ -55,9 +55,9 @@ func TestCostAdd(t *testing.T) {
5555
{Cost{C: 0.0}, Cost{C: 0.0}, Cost{C: 0.0}},
5656
{Cost{C: -1.0}, Cost{C: 1.0}, Cost{C: 0.0}},
5757
{Cost{C: 1.5}, Cost{C: 2.5}, Cost{C: 4.0}},
58-
{Cost{C: 1.0, Flags: FullScanPenalty}, Cost{C: 2.0}, Cost{C: 3.0, Flags: FullScanPenalty}},
59-
{Cost{C: 1.0}, Cost{C: 2.0, Flags: HugeCostPenalty}, Cost{C: 3.0, Flags: HugeCostPenalty}},
60-
{Cost{C: 1.0, Flags: UnboundedCardinality}, Cost{C: 2.0, Flags: HugeCostPenalty}, Cost{C: 3.0, Flags: HugeCostPenalty | UnboundedCardinality}},
58+
{Cost{C: 1.0, Penalties: FullScanPenalty}, Cost{C: 2.0}, Cost{C: 3.0, Penalties: FullScanPenalty}},
59+
{Cost{C: 1.0}, Cost{C: 2.0, Penalties: HugeCostPenalty}, Cost{C: 3.0, Penalties: HugeCostPenalty}},
60+
{Cost{C: 1.0, Penalties: UnboundedCardinality}, Cost{C: 2.0, Penalties: HugeCostPenalty}, Cost{C: 3.0, Penalties: HugeCostPenalty | UnboundedCardinality}},
6161
{Cost{C: 1.0, aux: testAux{1}}, Cost{C: 1.0, aux: testAux{2}}, Cost{C: 2.0, aux: testAux{3}}},
6262
{Cost{C: 1.0, aux: testAux{200}}, Cost{C: 1.0, aux: testAux{100}}, Cost{C: 2.0, aux: testAux{255}}},
6363
}

pkg/sql/opt/memo/expr_format.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -919,16 +919,16 @@ func (f *ExprFmtCtx) formatRelational(e RelExpr, tp treeprinter.Node) {
919919
if cost.C != 0 {
920920
tp.Childf("cost: %.9g", cost.C)
921921
}
922-
if cost.Flags != NoPenalties {
922+
if cost.Penalties != NoPenalties {
923923
var b strings.Builder
924924
b.WriteString("cost-flags:")
925-
if cost.Flags&FullScanPenalty != 0 {
925+
if cost.Penalties&FullScanPenalty != 0 {
926926
b.WriteString(" full-scan-penalty")
927927
}
928-
if cost.Flags&HugeCostPenalty != 0 {
928+
if cost.Penalties&HugeCostPenalty != 0 {
929929
b.WriteString(" huge-cost-penalty")
930930
}
931-
if cost.Flags&UnboundedCardinality != 0 {
931+
if cost.Penalties&UnboundedCardinality != 0 {
932932
b.WriteString(" unbounded-cardinality")
933933
}
934934
tp.Child(b.String())

pkg/sql/opt/xform/coster.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ var (
190190
// that "violate" a hint like forcing a specific index or join algorithm.
191191
// If the final expression has this cost or larger, it means that there was no
192192
// plan that could satisfy the hints.
193-
hugeCost = memo.Cost{C: 1e100, Flags: memo.HugeCostPenalty}
193+
hugeCost = memo.Cost{C: 1e100, Penalties: memo.HugeCostPenalty}
194194

195195
// SmallDistributeCost is the per-operation cost overhead for scans which may
196196
// access remote regions, but the scanned table is unpartitioned with no lease
@@ -217,8 +217,8 @@ var (
217217
// region instead of relying on costing, which may not guarantee
218218
// the correct plan is found?
219219
LargeDistributeCostWithHomeRegion = memo.Cost{
220-
C: LargeDistributeCost.C / 2,
221-
Flags: memo.HugeCostPenalty,
220+
C: LargeDistributeCost.C / 2,
221+
Penalties: memo.HugeCostPenalty,
222222
}
223223
)
224224

@@ -645,7 +645,7 @@ func (c *coster) ComputeCost(candidate memo.RelExpr, required *physical.Required
645645
if candidate.Relational().Cardinality.IsUnbounded() {
646646
cost.C += cpuCostFactor
647647
if c.evalCtx.SessionData().OptimizerPreferBoundedCardinality {
648-
cost.Flags |= memo.UnboundedCardinality
648+
cost.Penalties |= memo.UnboundedCardinality
649649
}
650650
}
651651

@@ -909,7 +909,7 @@ func (c *coster) computeScanCost(scan *memo.ScanExpr, required *physical.Require
909909
cost.IncrFullScanCount()
910910
if scan.Flags.AvoidFullScan {
911911
// Apply a penalty for a full scan if needed.
912-
cost.Flags |= memo.FullScanPenalty
912+
cost.Penalties |= memo.FullScanPenalty
913913
}
914914
}
915915

pkg/sql/prep/statement.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ func (p *planCosts) NumCustom() int {
187187
// average cost of the custom plans.
188188
func (p *planCosts) IsGenericOptimal() bool {
189189
// Check cost flags and full scan counts.
190-
if gc := p.generic.FullScanCount(); gc > 0 || p.generic.Flags != memo.NoPenalties {
190+
if gc := p.generic.FullScanCount(); gc > 0 || p.generic.Penalties != memo.NoPenalties {
191191
for i := 0; i < p.custom.length; i++ {
192-
if p.custom.costs[i].Flags < p.generic.Flags || gc > p.custom.costs[i].FullScanCount() {
192+
if p.custom.costs[i].Penalties < p.generic.Penalties || gc > p.custom.costs[i].FullScanCount() {
193193
return false
194194
}
195195
}

0 commit comments

Comments
 (0)