Skip to content

Commit

Permalink
Merge #60012
Browse files Browse the repository at this point in the history
60012: opt: move fraction function from statistics code to selectivity r=angelazxu a=angelazxu

The fraction function in the statistics builder code was only being used to
construct Selectivity, used inside the MakeSelectivity method.

This change moves the logic from the fraction function into the code for
Selectivity. It is an additional clean-up to #59413, to get rid of direct
numerical operations on Selectivity.

Fixes: #53860

Release note: None

Co-authored-by: Angela Xu <[email protected]>
  • Loading branch information
craig[bot] and angelazxu committed Feb 8, 2021
2 parents ee39d38 + 1b88044 commit 0e839ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
20 changes: 6 additions & 14 deletions pkg/sql/opt/memo/statistics_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2726,14 +2726,6 @@ func max(a, b float64) float64 {
return b
}

// fraction returns a/b if a is less than b. Otherwise returns 1.
func fraction(a, b float64) float64 {
if a < b {
return a / b
}
return 1
}

//////////////////////////////////////////////////
// Helper functions for selectivity calculation //
//////////////////////////////////////////////////
Expand Down Expand Up @@ -3624,8 +3616,8 @@ func (sb *statisticsBuilder) selectivityFromDistinctCount(
}

// Calculate the selectivity of the predicate.
nonNullSelectivity := props.MakeSelectivity(fraction(newDistinct, oldDistinct))
nullSelectivity := props.MakeSelectivity(fraction(colStat.NullCount, inputColStat.NullCount))
nonNullSelectivity := props.MakeSelectivityFromFraction(newDistinct, oldDistinct)
nullSelectivity := props.MakeSelectivityFromFraction(colStat.NullCount, inputColStat.NullCount)
return sb.predicateSelectivity(
nonNullSelectivity, nullSelectivity, inputColStat.NullCount, inputRowCount,
)
Expand Down Expand Up @@ -3658,8 +3650,8 @@ func (sb *statisticsBuilder) selectivityFromHistograms(
oldCount := oldHist.ValuesCount()

// Calculate the selectivity of the predicate.
nonNullSelectivity := props.MakeSelectivity(fraction(newCount, oldCount))
nullSelectivity := props.MakeSelectivity(fraction(colStat.NullCount, inputColStat.NullCount))
nonNullSelectivity := props.MakeSelectivityFromFraction(newCount, oldCount)
nullSelectivity := props.MakeSelectivityFromFraction(colStat.NullCount, inputColStat.NullCount)
selectivity.Multiply(sb.predicateSelectivity(
nonNullSelectivity, nullSelectivity, inputColStat.NullCount, inputStats.RowCount,
))
Expand Down Expand Up @@ -3749,7 +3741,7 @@ func (sb *statisticsBuilder) selectivityFromEquivalency(

// The selectivity of an equality condition var1=var2 is
// 1/max(distinct(var1), distinct(var2)).
return props.MakeSelectivity(fraction(1, maxDistinctCount))
return props.MakeSelectivityFromFraction(1, maxDistinctCount)
}

// selectivityFromEquivalenciesSemiJoin determines the selectivity of equality
Expand Down Expand Up @@ -3800,7 +3792,7 @@ func (sb *statisticsBuilder) selectivityFromEquivalencySemiJoin(
maxDistinctCountLeft = s.RowCount
}

return props.MakeSelectivity(fraction(minDistinctCountRight, maxDistinctCountLeft))
return props.MakeSelectivityFromFraction(minDistinctCountRight, maxDistinctCountLeft)
}

func (sb *statisticsBuilder) selectivityFromInvertedJoinCondition(
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/opt/props/selectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ func MakeSelectivity(sel float64) Selectivity {
return Selectivity{selectivityInRange(sel)}
}

// MakeSelectivityFromFraction calculates selectivity as a fraction of a and b
// if a is less than b and returns OneSelectivity otherwise..
func MakeSelectivityFromFraction(a, b float64) Selectivity {
if a < b {
return MakeSelectivity(a / b)
}
return OneSelectivity
}

// AsFloat returns the private selectivity field, allowing it to be accessed
// outside of this package.
func (s *Selectivity) AsFloat() float64 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/opt/props/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func TestSelectivity(t *testing.T) {
}
inf := math.MaxFloat64

// MakeSelectivityFromFraction variations.
test(MakeSelectivityFromFraction(1, 2), s(0.5))
test(MakeSelectivityFromFraction(1, 1), OneSelectivity)
test(MakeSelectivityFromFraction(1.5, 1), OneSelectivity)
test(MakeSelectivityFromFraction(1, 0), OneSelectivity)
test(MakeSelectivityFromFraction(0, 0), OneSelectivity)

// MinSelectivity variations.
test(MinSelectivity(s(0.4), s(0.5)), s(0.4))
test(MinSelectivity(s(0.5), s(0.4)), s(0.4))
Expand Down

0 comments on commit 0e839ab

Please sign in to comment.